From 305e5b4268685aa97d6338fe4362d1b73246cbe9 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 4 Jul 2013 01:01:24 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185604 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 4 +- lib/Sema/SemaTemplate.cpp | 12 ++++-- lib/Sema/SemaTemplateDeduction.cpp | 7 +++- test/SemaTemplate/overload-candidates.cpp | 45 +++++++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 1b076a44ca..047cd89ce8 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -5099,7 +5099,9 @@ public: SourceLocation TemplateLoc, SourceLocation RAngleLoc, Decl *Param, - SmallVectorImpl &Converted); + SmallVectorImpl + &Converted, + bool &HasDefaultArg); /// \brief Specifies the context in which a particular template /// argument is being checked. diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 2502db829b..e6006b6bf9 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -2703,11 +2703,16 @@ Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, SourceLocation TemplateLoc, SourceLocation RAngleLoc, Decl *Param, - SmallVectorImpl &Converted) { - if (TemplateTypeParmDecl *TypeParm = dyn_cast(Param)) { + SmallVectorImpl + &Converted, + bool &HasDefaultArg) { + HasDefaultArg = false; + + if (TemplateTypeParmDecl *TypeParm = dyn_cast(Param)) { if (!TypeParm->hasDefaultArgument()) return TemplateArgumentLoc(); + HasDefaultArg = true; TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, TemplateLoc, RAngleLoc, @@ -2724,6 +2729,7 @@ Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, if (!NonTypeParm->hasDefaultArgument()) return TemplateArgumentLoc(); + HasDefaultArg = true; ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, TemplateLoc, RAngleLoc, @@ -2741,7 +2747,7 @@ Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, if (!TempTempParm->hasDefaultArgument()) return TemplateArgumentLoc(); - + HasDefaultArg = true; NestedNameSpecifierLoc QualifierLoc; TemplateName TName = SubstDefaultTemplateArgument(*this, Template, TemplateLoc, diff --git a/lib/Sema/SemaTemplateDeduction.cpp b/lib/Sema/SemaTemplateDeduction.cpp index 8efc7a0263..0fd29ea449 100644 --- a/lib/Sema/SemaTemplateDeduction.cpp +++ b/lib/Sema/SemaTemplateDeduction.cpp @@ -2704,18 +2704,21 @@ Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, } // 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(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. diff --git a/test/SemaTemplate/overload-candidates.cpp b/test/SemaTemplate/overload-candidates.cpp index ad65397865..544d897e24 100644 --- a/test/SemaTemplate/overload-candidates.cpp +++ b/test/SemaTemplate/overload-candidates.cpp @@ -79,3 +79,48 @@ void foo(NS2::array); // expected-note{{candidate template ignored: could not void test() { foo(NS1::array()); // expected-error{{no matching function for call to 'foo'}} } + +namespace std { + template struct enable_if {}; + template struct enable_if { typedef T type; }; + + template struct integral_constant { static const T value = V; }; + typedef integral_constant false_type; + typedef integral_constant true_type; +}; + +namespace PR15673 { + template + struct a_trait : std::false_type {}; + + template::value>::type> // expected-warning {{C++11 extension}} + // expected-note@-1 {{candidate template ignored: disabled by 'enable_if' [with T = int]}} + void foo() {} + void bar() { foo(); } // expected-error {{no matching function for call to 'foo'}} + + + template + struct some_trait : std::false_type {}; + + // FIXME: It would be nice to tunnel the 'disabled by enable_if' diagnostic through here. + template + struct a_pony : std::enable_if::value> {}; + + template::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'}} + void quux() { baz(); } // expected-error {{no matching function for call to 'baz'}} + + + // FIXME: This note doesn't make it clear which candidate we rejected. + template + using unicorns = typename std::enable_if::value>::type; // expected-warning {{C++11 extension}} + // expected-note@-1 {{candidate template ignored: disabled by 'enable_if' [with T = int]}} + + template > // expected-warning {{C++11 extension}} + void wibble() {} + void wobble() { wibble(); } // expected-error {{no matching function for call to 'wibble'}} +} -- 2.40.0