r185604 - Part of PR15673: If a function template has a default argument in which

Richard Smith richard-llvm at metafoo.co.uk
Wed Jul 3 18:01:25 PDT 2013


Author: rsmith
Date: Wed Jul  3 20:01:24 2013
New Revision: 185604

URL: http://llvm.org/viewvc/llvm-project?rev=185604&view=rev
Log:
Part of PR15673: If a function template has a default argument in which
substitution failed, report that as a substitution failure rather than
pretending that there was no default argument.

The test cases in PR15673 have exposed some pre-existing poor diagnostics here.

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
    cfe/trunk/test/SemaTemplate/overload-candidates.cpp

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=185604&r1=185603&r2=185604&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Wed Jul  3 20:01:24 2013
@@ -5099,7 +5099,9 @@ public:
                                           SourceLocation TemplateLoc,
                                           SourceLocation RAngleLoc,
                                           Decl *Param,
-                          SmallVectorImpl<TemplateArgument> &Converted);
+                                          SmallVectorImpl<TemplateArgument>
+                                            &Converted,
+                                          bool &HasDefaultArg);
 
   /// \brief Specifies the context in which a particular template
   /// argument is being checked.

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=185604&r1=185603&r2=185604&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Wed Jul  3 20:01:24 2013
@@ -2703,11 +2703,16 @@ Sema::SubstDefaultTemplateArgumentIfAvai
                                               SourceLocation TemplateLoc,
                                               SourceLocation RAngleLoc,
                                               Decl *Param,
-                      SmallVectorImpl<TemplateArgument> &Converted) {
-   if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
+                                              SmallVectorImpl<TemplateArgument>
+                                                &Converted,
+                                              bool &HasDefaultArg) {
+  HasDefaultArg = false;
+
+  if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
     if (!TypeParm->hasDefaultArgument())
       return TemplateArgumentLoc();
 
+    HasDefaultArg = true;
     TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
                                                       TemplateLoc,
                                                       RAngleLoc,
@@ -2724,6 +2729,7 @@ Sema::SubstDefaultTemplateArgumentIfAvai
     if (!NonTypeParm->hasDefaultArgument())
       return TemplateArgumentLoc();
 
+    HasDefaultArg = true;
     ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
                                                   TemplateLoc,
                                                   RAngleLoc,
@@ -2741,7 +2747,7 @@ Sema::SubstDefaultTemplateArgumentIfAvai
   if (!TempTempParm->hasDefaultArgument())
     return TemplateArgumentLoc();
 
-
+  HasDefaultArg = true;
   NestedNameSpecifierLoc QualifierLoc;
   TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
                                                     TemplateLoc,

Modified: cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp?rev=185604&r1=185603&r2=185604&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp Wed Jul  3 20:01:24 2013
@@ -2704,18 +2704,21 @@ Sema::FinishTemplateArgumentDeduction(Fu
     }
 
     // Substitute into the default template argument, if available.
+    bool HasDefaultArg = false;
     TemplateArgumentLoc DefArg
       = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
                                               FunctionTemplate->getLocation(),
                                   FunctionTemplate->getSourceRange().getEnd(),
                                                 Param,
-                                                Builder);
+                                                Builder, HasDefaultArg);
 
     // If there was no default argument, deduction is incomplete.
     if (DefArg.getArgument().isNull()) {
       Info.Param = makeTemplateParameter(
                          const_cast<NamedDecl *>(TemplateParams->getParam(I)));
-      return TDK_Incomplete;
+      Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
+                                                  Builder.size()));
+      return HasDefaultArg ? TDK_SubstitutionFailure : TDK_Incomplete;
     }
 
     // Check whether we can actually use the default argument.

Modified: cfe/trunk/test/SemaTemplate/overload-candidates.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/overload-candidates.cpp?rev=185604&r1=185603&r2=185604&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/overload-candidates.cpp (original)
+++ cfe/trunk/test/SemaTemplate/overload-candidates.cpp Wed Jul  3 20:01:24 2013
@@ -79,3 +79,48 @@ void foo(NS2::array<A>); // expected-not
 void test() {
   foo(NS1::array<int>()); // expected-error{{no matching function for call to 'foo'}}
 }
+
+namespace std {
+  template<bool, typename = void> struct enable_if {};
+  template<typename T> struct enable_if<true, T> { typedef T type; };
+
+  template<typename T, T V> struct integral_constant { static const T value = V; };
+  typedef integral_constant<bool, false> false_type;
+  typedef integral_constant<bool, true> true_type;
+};
+
+namespace PR15673 {
+  template<typename T>
+  struct a_trait : std::false_type {};
+
+  template<typename T,
+           typename Requires = typename std::enable_if<a_trait<T>::value>::type> // expected-warning {{C++11 extension}}
+  // expected-note at -1 {{candidate template ignored: disabled by 'enable_if' [with T = int]}}
+  void foo() {}
+  void bar() { foo<int>(); } // expected-error {{no matching function for call to 'foo'}}
+
+
+  template<typename T>
+  struct some_trait : std::false_type {};
+
+  // FIXME: It would be nice to tunnel the 'disabled by enable_if' diagnostic through here.
+  template<typename T>
+  struct a_pony : std::enable_if<some_trait<T>::value> {};
+
+  template<typename T,
+           typename Requires = typename a_pony<T>::type> // expected-warning {{C++11 extension}}
+  // FIXME: The source location here is poor.
+  void baz() { } // expected-note {{candidate template ignored: substitution failure [with T = int]: no type named 'type' in 'PR15673::a_pony<int>'}}
+  void quux() { baz<int>(); } // expected-error {{no matching function for call to 'baz'}}
+
+
+  // FIXME: This note doesn't make it clear which candidate we rejected.
+  template <typename T>
+  using unicorns = typename std::enable_if<some_trait<T>::value>::type; // expected-warning {{C++11 extension}}
+  // expected-note at -1 {{candidate template ignored: disabled by 'enable_if' [with T = int]}}
+
+  template<typename T,
+           typename Requires = unicorns<T> > // expected-warning {{C++11 extension}}
+  void wibble() {}
+  void wobble() { wibble<int>(); } // expected-error {{no matching function for call to 'wibble'}}
+}





More information about the cfe-commits mailing list