Commit de0684bf by Marek Polacek

c++: Poor diagnostic for dynamic_cast in constexpr context [PR93414]

I neglected to add a proper diagnostic for the reference dynamic_cast
case when the operand of a dynamic_cast doesn't refer to a public base
of Derived, resulting in suboptimal error message

   error: call to non-'constexpr' function 'void* __cxa_bad_cast()'

2020-01-25  Marek Polacek  <polacek@redhat.com>

	PR c++/93414 - poor diagnostic for dynamic_cast in constexpr context.
	* constexpr.c (cxx_eval_dynamic_cast_fn): Add a reference
	dynamic_cast diagnostic.

	* g++.dg/cpp2a/constexpr-dynamic18.C: New test.
parent 10be08aa
2020-01-25 Marek Polacek <polacek@redhat.com>
PR c++/93414 - poor diagnostic for dynamic_cast in constexpr context.
* constexpr.c (cxx_eval_dynamic_cast_fn): Add a reference
dynamic_cast diagnostic.
2020-01-24 Jason Merrill <jason@redhat.com>
PR c++/93400 - ICE with constrained friend.
......
......@@ -1888,7 +1888,20 @@ cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
if (tree t = (TREE_CODE (obj) == COMPONENT_REF
? TREE_OPERAND (obj, 1) : obj))
if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
return integer_zero_node;
{
if (reference_p)
{
if (!ctx->quiet)
{
error_at (loc, "reference %<dynamic_cast%> failed");
inform (loc, "dynamic type %qT of its operand does "
"not have a base class of type %qT",
objtype, type);
}
*non_constant_p = true;
}
return integer_zero_node;
}
/* [class.cdtor] When a dynamic_cast is used in a constructor ...
or in a destructor ... if the operand of the dynamic_cast refers
......
2020-01-25 Marek Polacek <polacek@redhat.com>
PR c++/93414 - poor diagnostic for dynamic_cast in constexpr context.
* g++.dg/cpp2a/constexpr-dynamic18.C: New test.
2020-01-25 Feng Xue <fxue@os.amperecomputing.com>
PR ipa/93166
......
// PR c++/93414 - poor diagnostic for dynamic_cast in constexpr context.
// { dg-do compile { target c++2a } }
// Here 'b' doesn't point/refer to a public base of Derived.
struct Base {
constexpr virtual ~Base(){}
};
struct Derived: Base {
constexpr ~Derived(){}
};
constexpr const Derived& cast(const Base& b) {
return dynamic_cast<const Derived&>(b); // { dg-error "reference .dynamic_cast. failed" }
// { dg-message "dynamic type .const Base. of its operand does not have a base class of type .Derived." "" { target *-*-* } .-1 }
}
auto test() {
static constexpr Base b;
constexpr auto res = cast(b);
return res;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment