On Thu, Jul 20, 2023 at 10:42:29AM -0400, Jason Merrill wrote: > On 7/20/23 05:35, Nathaniel Shead wrote: > > This adds rudimentary lifetime tracking in C++ constexpr contexts, > > allowing the compiler to report errors with using values after their > > backing has gone out of scope. We don't yet handle other ways of > > accessing values outside their lifetime (e.g. following explicit > > destructor calls). > > Incidentally, much of that should be straightforward to handle by no longer > ignoring clobbers here: > > > case MODIFY_EXPR: > > if (cxx_dialect < cxx14) > > goto fail; > > if (!RECUR (TREE_OPERAND (t, 0), any)) > > return false; > > /* Just ignore clobbers. */ > > if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) > > return true; > > Assignment from a clobber represents end of lifetime to the middle-end. This > can be a follow-up patch.
Thanks, this is very helpful to know. I'll keep this in mind. > > @@ -7051,10 +7065,17 @@ cxx_eval_constant_expression (const constexpr_ctx > > *ctx, tree t, > > return ctx->ctor; > > if (VAR_P (t)) > > if (tree v = ctx->global->get_value (t)) > > - { > > - r = v; > > - break; > > - } > > + { > > + r = v; > > + break; > > + } > > + if (ctx->global->is_outside_lifetime (t)) > > + { > > + if (!ctx->quiet) > > + outside_lifetime_error (loc, t); > > + *non_constant_p = true; > > + break; > > + } > > Shouldn't this new check also be under the if (VAR_P (t))? A CONST_DECL > can't go out of scope. > > Jason > Yup you're right; I didn't properly read the documentation on what a CONST_DECL was and misunderstood. I'll fix this up for the next version.