c++: Fix up build_m_component_ref [PR113599]
commitfd620bd3351c6b9821c299035ed17e655d7954b5
authorJakub Jelinek <jakub@redhat.com>
Thu, 25 Jan 2024 23:08:36 +0000 (26 00:08 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 25 Jan 2024 23:08:36 +0000 (26 00:08 +0100)
treed984393493e0f2f74216c469331d2ba4a30737c1
parent136a828754ff65079a834555582b49d54bd5bc64
c++: Fix up build_m_component_ref [PR113599]

The following testcase reduced from GDB is miscompiled starting with
r14-5503 PR112427 change.
The problem is in the build_m_component_ref hunk, which changed
-      datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
+      datum = cp_convert (ptype, datum, complain);
+      if (!processing_template_decl)
+       datum = build2 (POINTER_PLUS_EXPR, ptype,
+                       datum, convert_to_ptrofftype (component));
+      datum = cp_fully_fold (datum);
Component is e, (sizetype) e is 16, offset of c inside of C.
ptype is A *, pointer to type of C::c and datum is &d.
Now, previously the above created ((A *) &d) p+ (sizetype) e which is correct,
but in the new code cp_convert sees that C has A as base class and
instead of returning (A *) &d, it returns &d.D.2800 where D.2800 is
the FIELD_DECL for the A base at offset 8 into C.
So, instead of computing ((A *) &d) p+ (sizetype) e it computes
&d.D.2800 p+ (sizetype) e, which is ((A *) &d) p+ 24.

The following patch fixes it by using convert instead of cp_convert which
eventually calls build_nop (ptype, datum).

2024-01-26  Jakub Jelinek  <jakub@redhat.com>

PR c++/113599
* typeck2.cc (build_m_component_ref): Use convert instead of
cp_convert for pointer conversion.

* g++.dg/expr/ptrmem11.C: New test.
gcc/cp/typeck2.cc
gcc/testsuite/g++.dg/expr/ptrmem11.C [new file with mode: 0644]