On Fri, 10 Nov 2023, Patrick Palka wrote: > On Thu, 9 Nov 2023, Jason Merrill wrote: > > > On 11/8/23 16:59, Patrick Palka wrote: > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > > > trunk? > > > > > > -- >8 -- > > > > > > Here when building up the non-dependent .* expression, we crash from > > > fold_convert on 'b.a' due to this (templated) COMPONENT_REF having an > > > IDENTIFIER_NODE instead of FIELD_DECL operand that middle-end routines > > > expect. Like in r14-4899-gd80a26cca02587, this patch fixes this by > > > replacing the problematic piecemeal folding with a single call to > > > cp_fully_fold. > > > > > > PR c++/112427 > > > > > > gcc/cp/ChangeLog: > > > > > > * typeck2.cc (build_m_component_ref): Use cp_convert, build2 and > > > cp_fully_fold instead of fold_build_pointer_plus and fold_convert. > > > > > gcc/testsuite/ChangeLog: > > > > > > * g++.dg/template/non-dependent29.C: New test. > > > --- > > > gcc/cp/typeck2.cc | 5 ++++- > > > gcc/testsuite/g++.dg/template/non-dependent29.C | 13 +++++++++++++ > > > 2 files changed, 17 insertions(+), 1 deletion(-) > > > create mode 100644 gcc/testsuite/g++.dg/template/non-dependent29.C > > > > > > diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc > > > index 309903afed8..208004221da 100644 > > > --- a/gcc/cp/typeck2.cc > > > +++ b/gcc/cp/typeck2.cc > > > @@ -2378,7 +2378,10 @@ 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 = fold_build_pointer_plus (fold_convert (ptype, datum), > > > component); > > > + datum = cp_convert (ptype, datum, complain); > > > + datum = build2 (POINTER_PLUS_EXPR, ptype, > > > + datum, convert_to_ptrofftype (component)); > > > > We shouldn't need to build the POINTER_PLUS_EXPR at all in template context. > > OK with that change. > > Hmm, that seems harmless at first glance, but I noticed > build_min_non_dep (called from build_x_binary_op in this case) is > careful to propagate TREE_SIDE_EFFECTS of the given tree, and so eliding > POINTER_PLUS_EXPR here could potentially mean that the tree we > ultimately return from build_x_binary_op when in a template context has > TREE_SIDE_EFFECTS not set when it used to. Shall we still elide the > POINTER_PLUS_EXPR in a template context despite this? > > (The TREE_SIDE_EFFECTS propagation in build_min_non_dep was added in > r71108 to avoid bogus ahead of time -Wunused-value warnings. But then > r105273 later made us stop issuing -Wunused-value warnings ahead of time > altogether. So perhaps we don't need to maintain the TREE_SIDE_EFFECTS > flag on templated trees at all anymore?)
IMO it'd be nice to restore ahead of time -Wunused-value warnings; it seems the original motivation for r105273 / PR8057 was to avoid redundantly issuing a warning twice, once ahead of time and once at instantiation time, which we now could do in a better way with warning_suppressed_p etc. If so, then IIUC eliding the POINTER_PLUS_EXPR could mean we'd incorrectly issue a -Wunused-value warning for e.g. 'a.*f()' in a template context? > > > > > Jason > > > > >