Corrected rethrow_if_nested

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@104943 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/exception b/include/exception
index 600b548..51e8313 100644
--- a/include/exception
+++ b/include/exception
@@ -211,19 +211,19 @@
 inline
 void
 rethrow_if_nested(const _E& __e, typename enable_if<
-                                   !is_same<_E, nested_exception>::value &&
-                                   is_convertible<_E*, nested_exception*>::value
+                                   is_polymorphic<_E>::value
                                                    >::type* = 0)
 {
-    static_cast<const nested_exception&>(__e).rethrow_nested();
+    const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
+    if (__nep)
+        __nep->rethrow_nested();
 }
 
 template <class _E>
 inline
 void
 rethrow_if_nested(const _E& __e, typename enable_if<
-                                   is_same<_E, nested_exception>::value ||
-                                   !is_convertible<_E*, nested_exception*>::value
+                                   !is_polymorphic<_E>::value
                                                    >::type* = 0)
 {
 }