On 7/24/26 1:51 PM, Jakub Jelinek wrote:
On Fri, Jul 24, 2026 at 01:05:38PM -0400, Jason Merrill wrote:
if it fails, and tree_strip_nop_conversions (op) is an ADDR_EXPR,
use a loop to skip over all DECL_FIELD_IS_BASE (because those shouldn't
be then matching) and just use it to find out what the dynamic
type is?
Yes, though I note that clang's diagnostic doesn't properly describe the
case where there's an base of the desired type somewhere in the complete
object but not derived from the subobject we started from, e.g.
struct A{};
struct B:A{}; struct C:A{};
struct D:B,C{} d;
constexpr A* ap = static_cast<C*>(&d);
constexpr auto dp = static_cast<B*>(ap);
clang++ indeed prints here
note: cannot cast object of dynamic type 'struct D' to type 'B'
My patch (both the original and modified which doesn't work well for
references) actually triggers the case I was looking for,
error: cannot cast object to type ‘B’
because sop isn't ADDR_EXPR here and cxx_fold_indirect_ref fails.
What would be best to print to users?
More correct might be to say "'A*' operand (of dynamic type 'D') is not
a base class subobject of a 'B' object"
Ideal might be to actually represent the inheritance, perhaps as "D::C::A"
Jason