[clang] [clang][bytecode] Fix 'if consteval' in non-constant contexts (PR #104707)

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 22 07:31:19 PDT 2024


================
@@ -4368,8 +4363,19 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
     if (!visitDeclStmt(CondDecl))
       return false;
 
-  if (!this->visitBool(IS->getCond()))
-    return false;
+  // Compile condition.
+  if (IS->isNonNegatedConsteval()) {
+    if (!this->emitIsConstantContext(IS))
+      return false;
+  } else if (IS->isNegatedConsteval()) {
+    if (!this->emitIsConstantContext(IS))
+      return false;
+    if (!this->emitInv(IS))
+      return false;
+  } else {
+    if (!this->visitBool(IS->getCond()))
+      return false;
+  }
----------------
cor3ntin wrote:

Ok, it took me a while, but iI think i got it... sorry for the brain fart.
Because the interpreter compile functions before they are evaluated (at least morally), we don't know in which context they are going to be executed.

This is somewhat confusing because that means which assume we can constant evaluate something that is not a constant expression, which i guess is true for constant folding. 

In that light the patch makes sense, I think

I still wants to make sure that we can recover nicely in the presence of things we can't evaluate, which seems to be the case https://godbolt.org/z/bofa9b8de


Do you do the same thing for `is_constant_evaluated`?

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


More information about the cfe-commits mailing list