[clang] [clang] Preserve the initializer when variable declaration deduction fails (PR #173546)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 25 22:05:28 PST 2025


================
@@ -13840,8 +13839,15 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
       return;
     }
 
-    if (DeduceVariableDeclarationType(VDecl, DirectInit, Init))
+    if (DeduceVariableDeclarationType(VDecl, DirectInit, Init)) {
+      assert(VDecl->isInvalidDecl() &&
+             "decl should be invalidated when deduce fails");
+      if (auto *RecoveryExpr =
+              CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), {Init})
+                  .get())
+        VDecl->setInit(RecoveryExpr);
----------------
zyn0217 wrote:

I wonder if we should move these to the place where the deduction first failed. That way we will not have to ensure the error recovery for every callers. WDYT?

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


More information about the cfe-commits mailing list