[clang] [Clang] Avoid assertion failure for initialized extern aliases (PR #223124)

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 23 05:48:18 PDT 2026


================
@@ -14127,6 +14093,15 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     return;
   }
 
+  // handleAliasAttr() runs before the initializer is attached to the VarDecl,
+  // so it cannot determine that an extern declaration with an initializer is a
+  // definition. Diagnose the conflict now that an initializer is present and
+  // remove the attribute before the post-initialization checks.
+  if (const auto *Attr = VDecl->getAttr<AliasAttr>()) {
+    Diag(Attr->getLocation(), diag::err_alias_is_definition) << VDecl << 0;
+    VDecl->dropAttr<AliasAttr>();
----------------
keepyixiao wrote:

Thanks for the suggestion. I considered calling setInvalidDecl() here, but this check is immediately followed by the invalid-declaration recovery path. Marking the declaration invalid would therefore replace the initializer with a
RecoveryExpr, return early, and suppress normal initializer diagnostics such as -Wextern-initializer. It may also affect subsequent redeclaration handling.

Since the existing alias checks diagnose and drop the attribute without invalidating the declaration。I kept the same recovery behavior here. Currently, I’ve followed your suggestion and added VDecl->setInvalidDecl(). Would you still prefer that the declaration be marked invalid despite these recovery differences? 

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


More information about the cfe-commits mailing list