[clang] [clang][Sema] Improve template argument deduction diagnostic (PR #122754)

Aidan Goldfarb via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 30 09:16:21 PST 2025


================
@@ -11714,13 +11714,38 @@ static void DiagnoseBadDeduction(Sema &S, NamedDecl *Found, Decl *Templated,
     return;
   }
 
-  case TemplateDeductionResult::InvalidExplicitArguments:
+  case TemplateDeductionResult::InvalidExplicitArguments: {
     assert(ParamD && "no parameter found for invalid explicit arguments");
-    if (ParamD->getDeclName())
-      S.Diag(Templated->getLocation(),
-             diag::note_ovl_candidate_explicit_arg_mismatch_named)
-          << ParamD->getDeclName();
-    else {
+    if (ParamD->getDeclName()) {
+      TemplateArgument FirstArg = *DeductionFailure.getFirstArg();
+      TemplateArgument SecondArg = *DeductionFailure.getSecondArg();
+
+      if (auto *TTPD = dyn_cast<TemplateTypeParmDecl>(ParamD)) {
+        S.Diag(Templated->getLocation(),
+               diag::note_ovl_candidate_explicit_arg_mismatch_named)
+            << 1 << ParamD->getDeclName() << FirstArg << SecondArg
+            << TTPD->getSourceRange();
+
+      } else if (auto *NTTPD = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) {
+        if (SecondArg.isNull()) {
+          S.Diag(Templated->getLocation(),
+                 diag::note_ovl_candidate_explicit_arg_mismatch_named)
+              << 3 << ParamD->getDeclName() << NTTPD->getType() << FirstArg
+              << NTTPD->getSourceRange();
+        } else {
+          S.Diag(Templated->getLocation(),
+                 diag::note_ovl_candidate_explicit_arg_mismatch_named)
+              << 2 << ParamD->getDeclName() << FirstArg << SecondArg
+              << NTTPD->getType() << NTTPD->getSourceRange();
+        }
+      } else if (auto *TTempPD = dyn_cast<TemplateTemplateParmDecl>(ParamD)) {
+        // FIXME: Emit a better message here
+        S.Diag(Templated->getLocation(),
+               diag::note_ovl_candidate_explicit_arg_mismatch_named)
+            << 4 << ParamD->getDeclName() << TTempPD->getSourceRange();
+      } else
+        llvm_unreachable("unexpected param decl kind");
+    } else {
       int index = 0;
----------------
AidanGoldfarb wrote:

With the last refactor I think the code is better, but its possible I blissfully ignored some coding practices...

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


More information about the cfe-commits mailing list