[clang] [Clang][Sema] Diagnose type mismatch on redeclaration after redefinition error (PR #228212)

Ambrose Leeb via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 13 11:47:34 PDT 2026


================
@@ -4814,18 +4814,29 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
     }
 
   // Merge the types.
-  VarDecl *MostRecent = Old->getMostRecentDecl();
-  if (MostRecent != Old) {
-    MergeVarDeclTypes(New, MostRecent,
-                      mergeTypeWithPrevious(*this, New, MostRecent, Previous));
+  //
+  // If Old or any prior declaration in the redeclaration chain is invalid,
+  // MergeVarDeclTypes will return early without diagnosing a type conflict.
+  // Walk the chain (using redecls(), which goes newest-to-oldest) to find the
+  // most recent declaration that is valid so we can still detect type
+  // mismatches on New. If every prior declaration is invalid, skip the type
+  // merge entirely to avoid false positives; the redefinition logic below will
+  // still run and emit the appropriate diagnostic.
+  VarDecl *TypeOld = nullptr;
+  for (VarDecl *Prev : Old->redecls()) {
+    if (!Prev->isInvalidDecl()) {
+      TypeOld = Prev;
+      break;
+    }
+  }
----------------
Sirraide wrote:

This only occurred to me now, but can we use `llvm::find_if` or something similar here?

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


More information about the cfe-commits mailing list