https://github.com/memehdi14 updated https://github.com/llvm/llvm-project/pull/228212
>From 2e2accca8d8df05aaeddb01ec6f0377c07f753b7 Mon Sep 17 00:00:00 2001 From: MeMehdi14 <[email protected]> Date: Thu, 1 Oct 2026 19:26:45 +0000 Subject: [PATCH] [Clang][Sema] Diagnose type mismatch on redeclaration after redefinition error When a variable redefinition error occurs, the rejected declaration is marked invalid. In Sema::MergeVarDecl, this invalid declaration was passed to MergeVarDeclTypes, which returns early when either declaration is invalid. As a result, subsequent redeclarations with conflicting types were never diagnosed. Later, constant evaluation of expressions reading the variable would walk the redeclaration chain and locate the original initializer with a mismatched type, causing an assertion failure in IntExprEvaluator::Success. Fix this by merging types against the most recent valid declaration in the redeclaration chain, ensuring type incompatibility errors are emitted and invalid declarations do not bypass semantic type checking. Also verified that related issue #114174 does not crash. Fixes #227733 --- clang/docs/ReleaseNotes.md | 3 +++ clang/lib/Sema/SemaDecl.cpp | 18 +++++++++++++++--- .../Sema/redefinition-then-different-type.c | 10 ++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 clang/test/Sema/redefinition-then-different-type.c diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 32116077622b58..0b0d6503fe99af 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -618,6 +618,9 @@ features cannot lower the translation-unit ABI level; ### Bug Fixes in This Version +- Fixed an assertion failure in the constant evaluator after a variable redefinition + error followed by a redeclaration with a different type. (#227733) + - Fixed incorrect handling of C++ import preprocessing token when a digraph character after import. (#GH190693) - Fixed a crash when emitting RTTI for a `dllexport` class, or the fundamental type descriptors for `__cxxabiv1::__fundamental_type_info`, under `-fvisibility=hidden`. (#GH207963) - Fixed an assertion failure when passing a wide string literal to `__builtin_nan`. (#GH212108) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 1459b71326375a..c744c69aeb999f 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -4814,15 +4814,27 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) { } // Merge the types. - VarDecl *MostRecent = Old->getMostRecentDecl(); - if (MostRecent != Old) { + VarDecl *TypeOld = Old; + if (TypeOld->isInvalidDecl()) { + for (VarDecl *Prev = Old->getPreviousDecl(); Prev; + Prev = Prev->getPreviousDecl()) { + if (!Prev->isInvalidDecl()) { + TypeOld = Prev; + break; + } + } + } + + VarDecl *MostRecent = TypeOld->getMostRecentDecl(); + if (MostRecent != TypeOld) { MergeVarDeclTypes(New, MostRecent, mergeTypeWithPrevious(*this, New, MostRecent, Previous)); if (New->isInvalidDecl()) return; } - MergeVarDeclTypes(New, Old, mergeTypeWithPrevious(*this, New, Old, Previous)); + MergeVarDeclTypes(New, TypeOld, + mergeTypeWithPrevious(*this, New, TypeOld, Previous)); if (New->isInvalidDecl()) return; diff --git a/clang/test/Sema/redefinition-then-different-type.c b/clang/test/Sema/redefinition-then-different-type.c new file mode 100644 index 00000000000000..f859ad69aa6e89 --- /dev/null +++ b/clang/test/Sema/redefinition-then-different-type.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -fsyntax-only -verify -x c++ %s + +// Previously crashed with an assertion in IntExprEvaluator::Success. +// https://github.com/llvm/llvm-project/issues/227733 + +const char e = 1; // expected-note 2 {{previous definition is here}} +const char e = 1; // expected-error {{redefinition of 'e'}} +const unsigned e; // expected-error {{redefinition of 'e' with a different type: 'const unsigned int' vs 'const char'}} +int x = e; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
