[clang] [clang][Sema] Improve template argument deduction diagnostic (PR #122754)
Matheus Izvekov via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 29 11:32:24 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;
----------------
mizvekov wrote:
I think this is good, thanks!
If you want to make this even better, here is another idea:
1) Remove this fallback diagnostic `diag::note_ovl_candidate_explicit_arg_mismatch_unnamed` entirely.
2) On the diagnostic above, you can select to print the name only in case the SourceRange is empty. Then in case the name is also empty, you print the index instead, as this fallback does.
Like so, you avoid this degradation in diagnostics with unnamed template parameters, where currently your improvements don't apply to them.
https://github.com/llvm/llvm-project/pull/122754
More information about the cfe-commits
mailing list