[clang] [Clang][Sema] Don't treat error-recovery dependence as genuine templa… (PR #221440)

Anirudh Mathur via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 5 05:28:27 PDT 2026


https://github.com/AnirudhMathur12 created https://github.com/llvm/llvm-project/pull/221440

…te dependence in ActOnTag

When a template argument fails to resolve (e.g. a namespace name used where a value is expected), Sema substitutes a RecoveryExpr with a dependent type so downstream code can recover gracefully. This error-induced dependence was being conflated with genuine template parameter dependence in ActOnTag's handling of TagUseKind::Reference/ Friend, causing computeDeclContext to report failure the same way it would for an actually-dependent nested-name-specifier. This tripped an assert in ActOnExplicitInstantiation that assumes dependent names can't reach explicit instantiation, when in fact the diagnostic had already been emitted at the point the RecoveryExpr was created.

Fixes #220525

>From d507501ab7b106f7bf9e42a4094cbf8374f6b0f0 Mon Sep 17 00:00:00 2001
From: AnirudhMathur12 <anirudhmathur12 at gmail.com>
Date: Sat, 5 Sep 2026 16:22:58 +0530
Subject: [PATCH] [Clang][Sema] Don't treat error-recovery dependence as
 genuine template dependence in ActOnTag

When a template argument fails to resolve (e.g. a namespace name used
where a value is expected), Sema substitutes a RecoveryExpr with a
dependent type so downstream code can recover gracefully. This
error-induced dependence was being conflated with genuine template
parameter dependence in ActOnTag's handling of TagUseKind::Reference/
Friend, causing computeDeclContext to report failure the same way it
would for an actually-dependent nested-name-specifier. This tripped an
assert in ActOnExplicitInstantiation that assumes dependent names
can't reach explicit instantiation, when in fact the diagnostic had
already been emitted at the point the RecoveryExpr was created.

Fixes #220525
---
 clang/lib/Sema/SemaDecl.cpp     | 10 +++++++++-
 clang/test/SemaCXX/GH220525.cpp |  7 +++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/GH220525.cpp

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d6114ccbae7fe..7f22701376770 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -18356,7 +18356,15 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
     if (TUK == TagUseKind::Friend || TUK == TagUseKind::Reference) {
       DC = computeDeclContext(SS, false);
       if (!DC) {
-        IsDependent = true;
+        if (SS.getScopeRep().getAsType() &&
+            !SS.getScopeRep().getAsType()->containsErrors()) {
+          // This is a genuinely dependent nested-name-specifier (e.g. it
+          // depends on an uninstantiated template parameter), not one that
+          // merely appears dependent because of an embedded error-recovery
+          // placeholder. Only in that genuine case should we defer/treat this
+          // as a dependent name for the caller to handle.
+          IsDependent = true;
+        }
         return true;
       }
     } else {
diff --git a/clang/test/SemaCXX/GH220525.cpp b/clang/test/SemaCXX/GH220525.cpp
new file mode 100644
index 0000000000000..5f34e67d70886
--- /dev/null
+++ b/clang/test/SemaCXX/GH220525.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
+
+namespace foo {
+  template<typename T> struct S {}; // expected-note {{template parameter is declared here}}
+}
+template struct foo::S<foo>::bar; // expected-error {{template argument for template type parameter must be a type}} \
+                                  // expected-error {{unexpected namespace name 'foo': expected expression}}



More information about the cfe-commits mailing list