From: Richard Smith Date: Wed, 28 Sep 2016 22:08:38 +0000 (+0000) Subject: Fix bug where template argument deduction of a non-type template parameter used X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=43a65cdfeae45e0508c7bcd6b57af26fb8ac4cb5;p=clang Fix bug where template argument deduction of a non-type template parameter used as a template argument in a template-id, from a null non-type template argument, failed. Extracted from a patch by James Touton! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@282641 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaTemplateDeduction.cpp b/lib/Sema/SemaTemplateDeduction.cpp index f8e825ff94..2311b2b744 100644 --- a/lib/Sema/SemaTemplateDeduction.cpp +++ b/lib/Sema/SemaTemplateDeduction.cpp @@ -285,7 +285,7 @@ checkDeducedTemplateArguments(ASTContext &Context, } /// \brief Deduce the value of the given non-type template parameter -/// from the given constant. +/// from the given integral constant. static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( Sema &S, NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, @@ -309,6 +309,32 @@ static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( return Sema::TDK_Success; } +/// \brief Deduce the value of the given non-type template parameter +/// from the given null pointer template argument type. +static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( + Sema &S, NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, + TemplateDeductionInfo &Info, + SmallVectorImpl &Deduced) { + Expr *Value = + S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( + S.Context.NullPtrTy, NTTP->getLocation()), + NullPtrType, CK_NullToPointer) + .get(); + DeducedTemplateArgument NewDeduced(Value); + DeducedTemplateArgument Result = checkDeducedTemplateArguments( + S.Context, Deduced[NTTP->getIndex()], NewDeduced); + + if (Result.isNull()) { + Info.Param = NTTP; + Info.FirstArg = Deduced[NTTP->getIndex()]; + Info.SecondArg = NewDeduced; + return Sema::TDK_Inconsistent; + } + + Deduced[NTTP->getIndex()] = Result; + return Sema::TDK_Success; +} + /// \brief Deduce the value of the given non-type template parameter /// from the given type- or value-dependent expression. /// @@ -1758,6 +1784,9 @@ DeduceTemplateArguments(Sema &S, Arg.getIntegralType(), /*ArrayBound=*/false, Info, Deduced); + if (Arg.getKind() == TemplateArgument::NullPtr) + return DeduceNullPtrTemplateArgument(S, NTTP, Arg.getNullPtrType(), + Info, Deduced); if (Arg.getKind() == TemplateArgument::Expression) return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), Info, Deduced); diff --git a/test/SemaTemplate/deduction.cpp b/test/SemaTemplate/deduction.cpp index d024c31477..7c7b0ab40d 100644 --- a/test/SemaTemplate/deduction.cpp +++ b/test/SemaTemplate/deduction.cpp @@ -264,4 +264,12 @@ int main() { return 0; } } // end ns2 -} \ No newline at end of file +} + +namespace nullptr_deduction { + template struct X {}; + template void f(X) { + static_assert(!v, ""); + } + void g() { f(X()); } +} diff --git a/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp b/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp index 548f7f8f34..98b3aa4710 100644 --- a/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp +++ b/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp @@ -122,7 +122,7 @@ namespace DeduceDifferentType { int a_exp = a<3>(A<3>()); template struct B {}; - template int b(B

); // expected-note {{could not match}} expected-note {{not implicitly convertible}} + template int b(B

); // expected-note {{does not have the same type}} expected-note {{not implicitly convertible}} int b_imp = b(B()); // expected-error {{no matching function}} int b_exp = b(B()); // expected-error {{no matching function}}