llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][Sema] Assertion "Invalid evaluation result." in IntExprEvaluator::Success after a redefinition error

Open skaeads12 opened this issue 2 months ago • 4 comments

After a redefinition error, a further redeclaration with a different type is not diagnosed, and the constant evaluator then returns a signed value for an unsigned expression and asserts. Invalid code (error recovery); assertions builds only.

Reproducer

https://godbolt.org/z/87ahW8hhx (assertions trunk and release trunk)

const char e = 1;
const char e = 1;   // error: redefinition of 'e'
const unsigned e;   // not diagnosed
int x = e;          // assertion

clang --target=x86_64-unknown-linux-gnu -fsyntax-only repro.c

<source>:2:12: error: redefinition of 'e'
clang: clang/lib/AST/ExprConstant.cpp:15989: bool {anonymous}::IntExprEvaluator::Success(const llvm::APSInt&, const clang::Expr*, clang::APValue&): Assertion `SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && "Invalid evaluation result."' failed.
 #11 (anonymous namespace)::IntExprEvaluator::VisitCastExpr(clang::CastExpr const*)
 #19 clang::Expr::isConstantInitializer(clang::ASTContext&, bool, clang::Expr const**) const
 #20 clang::Sema::CheckForConstantInitializer(clang::Expr*, unsigned int)
 #21 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool)

Line 3 should get redefinition of 'e' with a different type: 'const unsigned int' vs 'const char'. Clang says exactly that when line 2 is removed, and GCC 16.2 reports conflicting types for 'e' on line 3 even with line 2 present.

Root cause

From reading SemaDecl.cpp (not traced in a debugger):

  1. Line 2 is rejected: checkVarDeclRedefinition marks it invalid, and its initializer is not attached.
  2. MergeVarDecl merges line 3 with the declaration found by lookup, which is the invalid line 2. MergeVarDeclTypes returns early when either declaration is invalid, so e silently becomes const unsigned.
  3. When int x = e; is checked for a constant initializer, getAnyInitializer finds the initializer of line 1 (const char, signed) through the redeclaration chain, and the evaluator returns a signed value for an unsigned expression.

Suggested fix (untested)

Check the type of a new redeclaration against the most recent valid declaration instead of skipping the check, or keep the evaluator from using an initializer whose declaration has a different type from the variable being read.

Details

  • Classification: crash on invalid code (error recovery). The missing diagnostic for line 3 is a secondary problem.
  • Release builds: only error: redefinition of 'e', exit status 1.
  • History: long-standing. Asserts on every assertions build tried from clang 2.9 to trunk; clang 2.8 still reported the type mismatch on line 3 and did not crash. Not bisected.
  • Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29).
  • Related: #114174 (open) hits the same assertion from a C++ template static data member. The trigger here is different, but the fix may overlap.
  • Found by a fuzzer (generated C program), reduced by delta debugging.

skaeads12 avatar Sep 30 '26 14:09 skaeads12

@llvm/issue-subscribers-clang-frontend

Author: skaeads12

After a redefinition error, a further redeclaration with a different type is not diagnosed, and the constant evaluator then returns a signed value for an unsigned expression and asserts. Invalid code (error recovery); assertions builds only.

Reproducer

https://godbolt.org/z/87ahW8hhx (assertions trunk and release trunk)

const char e = 1;
const char e = 1;   // error: redefinition of 'e'
const unsigned e;   // not diagnosed
int x = e;          // assertion

clang --target=x86_64-unknown-linux-gnu -fsyntax-only repro.c

&lt;source&gt;:2:12: error: redefinition of 'e'
clang: clang/lib/AST/ExprConstant.cpp:15989: bool {anonymous}::IntExprEvaluator::Success(const llvm::APSInt&amp;, const clang::Expr*, clang::APValue&amp;): Assertion `SI.isSigned() == E-&gt;getType()-&gt;isSignedIntegerOrEnumerationType() &amp;&amp; "Invalid evaluation result."' failed.
 #<!-- -->11 (anonymous namespace)::IntExprEvaluator::VisitCastExpr(clang::CastExpr const*)
 #<!-- -->19 clang::Expr::isConstantInitializer(clang::ASTContext&amp;, bool, clang::Expr const**) const
 #<!-- -->20 clang::Sema::CheckForConstantInitializer(clang::Expr*, unsigned int)
 #<!-- -->21 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool)

Line 3 should get redefinition of 'e' with a different type: 'const unsigned int' vs 'const char'. Clang says exactly that when line 2 is removed, and GCC 16.2 reports conflicting types for 'e' on line 3 even with line 2 present.

Root cause

From reading SemaDecl.cpp (not traced in a debugger):

  1. Line 2 is rejected: checkVarDeclRedefinition marks it invalid, and its initializer is not attached.
  2. MergeVarDecl merges line 3 with the declaration found by lookup, which is the invalid line 2. MergeVarDeclTypes returns early when either declaration is invalid, so e silently becomes const unsigned.
  3. When int x = e; is checked for a constant initializer, getAnyInitializer finds the initializer of line 1 (const char, signed) through the redeclaration chain, and the evaluator returns a signed value for an unsigned expression.

Suggested fix (untested)

Check the type of a new redeclaration against the most recent valid declaration instead of skipping the check, or keep the evaluator from using an initializer whose declaration has a different type from the variable being read.

Details

  • Classification: crash on invalid code (error recovery). The missing diagnostic for line 3 is a secondary problem.
  • Release builds: only error: redefinition of 'e', exit status 1.
  • History: long-standing. Asserts on every assertions build tried from clang 2.9 to trunk; clang 2.8 still reported the type mismatch on line 3 and did not crash. Not bisected.
  • Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29).
  • Related: #114174 (open) hits the same assertion from a C++ template static data member. The trigger here is different, but the fix may overlap.
  • Found by a fuzzer (generated C program), reduced by delta debugging.

Since clang 2.9.0 https://godbolt.org/z/58M4M1c4j

TPPPP72 avatar Sep 30 '26 15:09 TPPPP72

@TPPPP72 I'd like to take a shot at fixing this. I can reproduce the assertion on an assertions build and will start by tracing the invalid-redeclaration path through SemaDecl.cpp, particularly the interaction between checkVarDeclRedefinition / MergeVarDeclTypes and the later initializer lookup.

My initial plan is:

  • add the reduced regression test for the reproducer;
  • understand why the invalid redeclaration becomes the declaration used for the later merge;
  • make the smallest Sema-side change that preserves the type diagnostic and prevents the invalid initializer from reaching constant evaluation;
  • run the focused Clang Sema/constant-evaluation tests and an assertions build.

I haven't started coding yet, so please let me know if someone is already working on this or if there is a preferred direction for the fix.

memehdi14 avatar Oct 01 '26 12:10 memehdi14

I haven't started coding yet, so please let me know if someone is already working on this or if there is a preferred direction for the fix.

No, not yet. Please feel free to work on it.

TPPPP72 avatar Oct 01 '26 12:10 TPPPP72