Skip to content

WIP: fix assert in hasInitWithSideEffects #146468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hnrklssn
Copy link
Member

@hnrklssn hnrklssn commented Jul 1, 2025

This fixes another instance of Assertion failed: (NumCurrentElementsDeserializing == 0 && "should not be called while already deserializing"). I ran into it while importing clang modules from Swift, but I haven't been able to reproduce it in a test case yet. The error seems to be that an initializer expression can be deserialized and contain a call to a function whose function body is not yet deserialized. When evaluateValue() is called the constexpr evaluation triggers deserialization of the function body.

rdar://154717930

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2025

@llvm/pr-subscribers-clang

Author: Henrik G. Olsson (hnrklssn)

Changes

This fixes another instance of Assertion failed: (NumCurrentElementsDeserializing == 0 && "should not be called while already deserializing"). I ran into it while importing clang modules from Swift, but I haven't been able to reproduce it in a test case yet. The error seems to be that an initializer expression can be deserialized and contain a call to a function whose function body is not yet deserialized. When evaluateValue() is called the constexpr evaluation triggers deserialization of the function body.

rdar://154717930


Full diff: https://github.com/llvm/llvm-project/pull/146468.diff

1 Files Affected:

  • (modified) clang/lib/AST/Decl.cpp (+3-9)
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 5cdf75d71e4d7..5e91566e87f74 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2444,20 +2444,14 @@ bool VarDecl::hasInitWithSideEffects() const {
   if (!hasInit())
     return false;
 
-  // Check if we can get the initializer without deserializing
-  const Expr *E = nullptr;
+  // Check if we can get the initializer directly without an external source
   if (auto *S = dyn_cast<Stmt *>(Init)) {
-    E = cast<Expr>(S);
-  } else {
-    E = cast_or_null<Expr>(getEvaluatedStmt()->Value.getWithoutDeserializing());
-  }
-
-  if (E)
+    const Expr *E = cast<Expr>(S);
     return E->HasSideEffects(getASTContext()) &&
            // We can get a value-dependent initializer during error recovery.
            (E->isValueDependent() || !evaluateValue());
+  }
 
-  assert(getEvaluatedStmt()->Value.isOffset());
   // ASTReader tracks this without having to deserialize the initializer
   if (auto Source = getASTContext().getExternalSource())
     return Source->hasInitializerWithSideEffects(this);

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change LGTM but I want a test to avoid further regression.

@hnrklssn
Copy link
Member Author

hnrklssn commented Jul 1, 2025

The change LGTM but I want a test to avoid further regression.

Yup, working on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants