[clang] [Clang][Sema] Handle invalid variable template specialization whose type depends on itself (PR #134522)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 28 09:11:05 PDT 2025


================
@@ -4368,15 +4385,63 @@ Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc,
   // Produce a placeholder value if the specialization is dependent.
   if (Template->getDeclContext()->isDependentContext() ||
       TemplateSpecializationType::anyDependentTemplateArguments(
-          TemplateArgs, CTAI.CanonicalConverted))
+          TemplateArgs, CTAI.CanonicalConverted)) {
+    if (ParsingInitForAutoVars.empty())
+      return DeclResult();
+
+    if (VarDecl *Var = Template->getTemplatedDecl();
+        ParsingInitForAutoVars.count(Var) &&
+        llvm::equal(
+            CTAI.CanonicalConverted,
+            Template->getTemplateParameters()->getInjectedTemplateArgs(Context),
+            TemplateArgEqualityComparator{Context})) {
+      Diag(TemplateNameLoc,
+           diag::err_auto_variable_cannot_appear_in_own_initializer)
+          << diag::ParsingInitFor::VarTemplate << Var << Var->getType()
+          << SourceRange(TemplateNameLoc, TemplateArgs.getRAngleLoc());
----------------
zyn0217 wrote:

I'm also wondering if it is necessary to draw underlines under the RHS of the declaration - would that be noisy if the declaration is too long?

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


More information about the cfe-commits mailing list