https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99287

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #4)
> (In reply to Patrick Palka from comment #3)
> > IIUC, those two types are actually the same, it's just that one of them was
> > obtained through the char_type alias, and it seems debug_tree prefers to
> > show the name of the alias when the type came from one.
> > 
> > It seems the issue ultimately is in cxx_eval_increment_expression: during
> > evaluation of ++__first with __first = &"mystr"[n] + m and with lval=false,
> > since cxx_fold_pointer_plus_expression and not fold_build2 is responsible
> > for simplifying this POINTER_PLUS_EXPR to &"mystr"[n+m], returning 'mod'
> > actually returns the unreduced &"mystr"[n] + m rather than the reduced
> > &"mystr"[n+m] that we obtain as part of constexpr evaluation of the
> > temporary MODIFY_EXPR.  This unreduced return value of cxx_eval_increment
> > interfers with later constexpr evaluation, e.g. folding of the
> > POINTER_DIFF_EXPR.
> > 
> > I'm testing the following which updates 'mod' with the result of evaluation
> > of the MODIFY_EXPR:
> > 
> > --- a/gcc/cp/constexpr.c
> > +++ b/gcc/cp/constexpr.c
> > @@ -5582,20 +5582,14 @@ cxx_eval_increment_expression (const constexpr_ctx
> > *ctx, tree t,
> >    /* Storing the modified value.  */
> >    tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
> >                            MODIFY_EXPR, type, op, mod);
> > -  cxx_eval_constant_expression (ctx, store,
> > -                               true, non_constant_p, overflow_p);
> > +  mod = cxx_eval_constant_expression (ctx, store, false,
> > +                                     non_constant_p, overflow_p);
> >    ggc_free (store);
> 
> Maybe; I'm a little bit worried here though because
> cxx_eval_constant_expression
> can in various cases return the tree passed to it on failure (i.e. the
> store) or can return NULL_TREE.
> And store is then ggc_freed, so shouldn't be really used afterwards.
> So perhaps
>   tree new_mod = cxx_eval_constant_expression (ctx, store, false,
> non_constant_p, overflow_p);
>   if (new_mod && new_mod != store)
>     mod = new_mod;
> 
>   ggc_free (store);
> ?

Good point.  Though I don't see how cxx_eval_constant_expression could
plausibly return NULL_TREE here since we're dealing with a MODIFY_EXPR?  And I
think checking *non_constant_p could subsume checking new_mod != store.  So
perhaps just

@@ -5582,20 +5582,16 @@ cxx_eval_increment_expression (const constexpr_ctx
*ctx, tree t,
   /* Storing the modified value.  */
   tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
                           MODIFY_EXPR, type, op, mod);
-  cxx_eval_constant_expression (ctx, store,
-                               true, non_constant_p, overflow_p);
+  mod = cxx_eval_constant_expression (ctx, store, false,
+                                     non_constant_p, overflow_p);
   ggc_free (store);
+  if (*non_constant_p)
+    return t;

would cover our bases.

(In reply to Jakub Jelinek from comment #5)
> Or perhaps another option would be instead of
> return mod;
> do
> return cxx_eval_constant_expression (ctx, mod, false, non_constant_p,
> overflow_p);
> ?

I think should work, though it'd mean we'd be redundantly evaluating 'mod'
twice in the !lval case I suppose.

Reply via email to