Skip to content

Commit fd620bd

Browse files
committed
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.
1 parent 136a828 commit fd620bd

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

gcc/cp/typeck2.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2378,7 +2378,7 @@ build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
23782378
/* Build an expression for "object + offset" where offset is the
23792379
value stored in the pointer-to-data-member. */
23802380
ptype = build_pointer_type (type);
2381-
datum = cp_convert (ptype, datum, complain);
2381+
datum = convert (ptype, datum);
23822382
if (!processing_template_decl)
23832383
datum = build2 (POINTER_PLUS_EXPR, ptype,
23842384
datum, convert_to_ptrofftype (component));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// PR c++/113599
2+
// { dg-do run }
3+
4+
struct A { void *a; };
5+
struct B { void *b; };
6+
struct C : public B, public A { A c; };
7+
static C d;
8+
9+
int
10+
main ()
11+
{
12+
A C::*e = &C::c;
13+
A *f = &(d.*e);
14+
A *g = &d.c;
15+
if (f != g)
16+
__builtin_abort ();
17+
}

0 commit comments

Comments
 (0)