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?
Jakub