[clang][Sema] Assertion "Invalid evaluation result." in IntExprEvaluator::Success after a redefinition error
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):
- Line 2 is rejected:
checkVarDeclRedefinitionmarks it invalid, and its initializer is not attached. MergeVarDeclmerges line 3 with the declaration found by lookup, which is the invalid line 2.MergeVarDeclTypesreturns early when either declaration is invalid, soesilently becomesconst unsigned.- When
int x = e;is checked for a constant initializer,getAnyInitializerfinds 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.
@llvm/issue-subscribers-clang-frontend
Author: skaeads12
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):
- Line 2 is rejected:
checkVarDeclRedefinitionmarks it invalid, and its initializer is not attached. MergeVarDeclmerges line 3 with the declaration found by lookup, which is the invalid line 2.MergeVarDeclTypesreturns early when either declaration is invalid, soesilently becomesconst unsigned.- When
int x = e;is checked for a constant initializer,getAnyInitializerfinds 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 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.
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.