From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id BE0F83858C42; Thu, 25 Jan 2024 23:09:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE0F83858C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1706224155; bh=JAF3U3P4Rq8PxxVXNoFYYQbfVjbODbR8ivxjE957gok=; h=From:To:Subject:Date:From; b=MaygCLqB7+TtyLzeSZM2e/pU20dG6qE0nVN0TVDK5hijTarSQE+411qxG3try60Fo FFjV0TugbcTOqyyDBzBi/uBzt/hbuPPrISvfL/qzkXQQnuoPoxBd2Q7TSokpTcIa/0 zI3YKTgiqyCx4YnH+8SEmNFIZ5Ct/LzDfzjNgtPU= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8439] c++: Fix up build_m_component_ref [PR113599] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 136a828754ff65079a834555582b49d54bd5bc64 X-Git-Newrev: fd620bd3351c6b9821c299035ed17e655d7954b5 Message-Id: <20240125230915.BE0F83858C42@sourceware.org> Date: Thu, 25 Jan 2024 23:09:15 +0000 (GMT) List-Id: https://gcc.gnu.org/g:fd620bd3351c6b9821c299035ed17e655d7954b5 commit r14-8439-gfd620bd3351c6b9821c299035ed17e655d7954b5 Author: Jakub Jelinek Date: Fri Jan 26 00:08:36 2024 +0100 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 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. Diff: --- gcc/cp/typeck2.cc | 2 +- gcc/testsuite/g++.dg/expr/ptrmem11.C | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index ac0fefa24f29..9608bdccd8b2 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -2378,7 +2378,7 @@ build_m_component_ref (tree datum, tree component, tsubst_flags_t complain) /* Build an expression for "object + offset" where offset is the value stored in the pointer-to-data-member. */ ptype = build_pointer_type (type); - datum = cp_convert (ptype, datum, complain); + datum = convert (ptype, datum); if (!processing_template_decl) datum = build2 (POINTER_PLUS_EXPR, ptype, datum, convert_to_ptrofftype (component)); diff --git a/gcc/testsuite/g++.dg/expr/ptrmem11.C b/gcc/testsuite/g++.dg/expr/ptrmem11.C new file mode 100644 index 000000000000..7100a2d034b0 --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/ptrmem11.C @@ -0,0 +1,17 @@ +// PR c++/113599 +// { dg-do run } + +struct A { void *a; }; +struct B { void *b; }; +struct C : public B, public A { A c; }; +static C d; + +int +main () +{ + A C::*e = &C::c; + A *f = &(d.*e); + A *g = &d.c; + if (f != g) + __builtin_abort (); +}