]> granicus.if.org Git - clang/commitdiff
Fix explicit template parameter reporting for narrowing conversions
authorErich Keane <erich.keane@intel.com>
Mon, 7 May 2018 17:05:20 +0000 (17:05 +0000)
committerErich Keane <erich.keane@intel.com>
Mon, 7 May 2018 17:05:20 +0000 (17:05 +0000)
I found that explicit template parameters that caused a
narrowing integer conversion resulted in the incorrect parameter
being mentioned in the note (see test attached). This is because
the argument checking code doesn't check to see if it caused
SFINAE errors when checking the arguments, so instead of giving
up on the first error, it continues through the list. This
makes the error reporting pick up the last template param every time.

This patch checks these parameters on each argument and gives up
if there is an error. The result is that only the required amount
of arguments are checked, and that the 'Converted' array contains
only the successful arguments before the first failure, as the
calls seem to all expect.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@331651 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaTemplate.cpp
test/SemaTemplate/temp_arg_nontype_cxx11.cpp

index 04fdfaea75f77d607edb95c5b79ee4a480df6de5..d5f6b640a70329b0db64116b41c21c1eecb4b3e6 100644 (file)
@@ -4668,11 +4668,15 @@ bool Sema::CheckTemplateArgument(NamedDecl *Param,
 
     case TemplateArgument::Expression: {
       TemplateArgument Result;
+      unsigned CurSFINAEErrors = NumSFINAEErrors;
       ExprResult Res =
         CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
                               Result, CTAK);
       if (Res.isInvalid())
         return true;
+      // If the current template argument causes an error, give up now.
+      if (CurSFINAEErrors < NumSFINAEErrors)
+        return true;
 
       // If the resulting expression is new, then use it in place of the
       // old expression in the template argument.
index 0b8f0eed160193f1b776d37df552873a27dd3ef4..313114e2cb8ee043b5b03aea89a50317469ec0ed 100644 (file)
@@ -36,3 +36,15 @@ namespace check_conversion_early {
   struct Y { constexpr operator int() const { return 0; } };
   template<Y &y> struct A<y> {}; // expected-error {{cannot be deduced}} expected-note {{'y'}}
 }
+
+namespace ReportCorrectParam {
+template <int a, unsigned b, int c>
+void TempFunc() {}
+
+void Useage() {
+  //expected-error@+2 {{no matching function}}
+  //expected-note@-4 {{candidate template ignored: invalid explicitly-specified argument for template parameter 'b'}}
+  TempFunc<1, -1, 1>();
+}
+}
+