[clang] [Clang] Handle consteval expression in array bounds expressions (PR #66222)

Takuya Shimizu via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 14 04:34:36 PDT 2023


hazohelet wrote:

> @hazohelet Does this collide or intersect with the work wrt. (un)evaluated contexts in https://reviews.llvm.org/D155064 ?

I wasn't aware of VLA cases in that patch.
My patch needs modification so that use of `is_constant_evaluated` and consteval-if NOT considered tautologically-true when they happen `InConditionallyConstantEvaluateContext`, which is a few lines of change.

@cor3ntin
Is it alright to set `InConditionallyConstantEvaluateContext` true only when the outer evaluation context `isAlwaysConstantEvaluatedContext()` so that we can say the array bound expr is always constant-evaluated if it happens in always-constant-evaluated context?
Something like this:
```diff
 ExprResult Parser::ParseArrayBoundExpression() {
+  bool IsAlwaysConstantEvaluated = Actions.isAlwaysConstantEvaluatedContext();
   EnterExpressionEvaluationContext ConstantEvaluated(
       Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
   // If we parse the bound of a VLA... we parse a non-constant
   // constant-expression!
-  Actions.ExprEvalContexts.back().InConditionallyConstantEvaluateContext = true;
+  Actions.ExprEvalContexts.back().InConditionallyConstantEvaluateContext = !IsAlwaysConstantEvaluated;
   return ParseConstantExpressionInExprEvalContext(NotTypeCast);
 }
```

Also, does this change suggest that we let some diagnostic mechanisms consider VLA cases? (e.g. `DiagRuntimeBehavior` should not early-return if the context is `InConditionallyConstantEvaluateContext` to diagnose div-by-zero in https://godbolt.org/z/556rKnMTG)

https://github.com/llvm/llvm-project/pull/66222


More information about the cfe-commits mailing list