[clang] [Clang][Sema] Propagate completed array type to tentative definitions (PR #243855)

Arnab Sarkar via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 18 05:49:07 PST 2026


https://github.com/28arnab created https://github.com/llvm/llvm-project/pull/243855

At translation unit termination (Sema::ActOnEndOfTranslationUnit), tentative array definitions that were completed by later extern declarations (e.g., `extern int b[3];`) were still checking the type of the initial declaration rather than querying the most recent redeclaration. This caused completed tentative arrays to incorrectly fall back to a size of 1 and emit an erroneous warning.

This fix checks `VD->getMostRecentDecl()` during tentative definition processing and updates the variable's type if a later redeclaration has completed the array type.

Fixes: #242842  
Assisted-By: AI

>From bf84df7b16dab71636644ce34edc76efe254d059 Mon Sep 17 00:00:00 2001
From: Arnab Sarkar <arnab at 28arnab.com>
Date: Wed, 18 Nov 2026 19:11:42 +0530
Subject: [PATCH] [Clang][Sema] Propagate completed array type to tentative
 definitions

At translation unit termination (Sema::ActOnEndOfTranslationUnit),
tentative array definitions that were completed by later extern
declarations (e.g., `extern int b[3];`) were still checking the type
of the initial declaration rather than querying the most recent
redeclaration. This caused completed tentative arrays to incorrectly
fall back to a size of 1 and emit an erroneous warning.

This fix checks `VD->getMostRecentDecl()` during tentative definition
processing and updates the variable's type if a later redeclaration
has completed the array type.

Signed-off-by: Arnab Sarkar <arnab at 28arnab.com>
Assisted-By: AI
---
 clang/lib/Sema/Sema.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index ebc8b85cf8a628..ece3421cf104af 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1515,6 +1515,12 @@ void Sema::ActOnEndOfTranslationUnit() {
     if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second)
       continue;
 
+    // If a later redeclaration (e.g. extern int b[3]) completed the type,
+    // update the tentative definition to use that completed type.
+    VarDecl *MostRecent = VD->getMostRecentDecl();
+    if (MostRecent != VD && !MostRecent->getType()->isIncompleteArrayType())
+      VD->setType(MostRecent->getType());
+
     if (const IncompleteArrayType *ArrayT
         = Context.getAsIncompleteArrayType(VD->getType())) {
       // Set the length of the array to 1 (C99 6.9.2p5).



More information about the cfe-commits mailing list