From: Hans Wennborg Date: Tue, 27 Aug 2019 10:38:25 +0000 (+0000) Subject: Merging r369834: X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=26bc32c96eff6988ba7f1889b64a310c7557b93e;p=clang Merging r369834: ------------------------------------------------------------------------ r369834 | rsmith | 2019-08-24 04:30:00 +0200 (Sat, 24 Aug 2019) | 8 lines PR42513: Enter the proper DeclContext before substituting into an default template argument expression. We already did this for type template parameters and template template parameters, but apparently forgot to do so for non-type template parameters. This causes the substituted default argument expression to be substituted in the proper context, and in particular to properly mark its subexpressions as odr-used. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/cfe/branches/release_90@370038 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index ec4b63a2e5..135ca2b25c 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -4692,6 +4692,7 @@ SubstDefaultTemplateArgument(Sema &SemaRef, for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) TemplateArgLists.addOuterTemplateArguments(None); + Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); EnterExpressionEvaluationContext ConstantEvaluated( SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); diff --git a/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp b/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp index c5deb9fe91..85312cf104 100644 --- a/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp +++ b/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp @@ -268,13 +268,16 @@ namespace tuple_tests { // Don't get caught by surprise when X<...> doesn't even exist in the // selected specialization! namespace libcxx_2 { - template struct tuple { // expected-note {{candidate}} + template struct tuple { template struct X { static const bool value = false; }; + // Substitution into X::value succeeds but produces the + // value-dependent expression + // tuple::X<>::value + // FIXME: Is that the right behavior? template::value> tuple(U &&...u); - // expected-note@-1 {{substitution failure [with T = <>, U = ]: cannot reference member of primary template because deduced class template specialization 'tuple<>' is an explicit specialization}} }; template <> class tuple<> {}; - tuple a = {1, 2, 3}; // expected-error {{no viable constructor or deduction guide}} + tuple a = {1, 2, 3}; // expected-error {{excess elements in struct initializer}} } namespace libcxx_3 { diff --git a/test/SemaTemplate/temp_arg_nontype_cxx11.cpp b/test/SemaTemplate/temp_arg_nontype_cxx11.cpp index 313114e2cb..460b6def5d 100644 --- a/test/SemaTemplate/temp_arg_nontype_cxx11.cpp +++ b/test/SemaTemplate/temp_arg_nontype_cxx11.cpp @@ -48,3 +48,20 @@ void Useage() { } } +namespace PR42513 { + template void f(); + constexpr int WidgetCtor(struct X1*); + + struct X1 { + friend constexpr int WidgetCtor(X1*); + }; + template + struct StandardWidget { + friend constexpr int WidgetCtor(X1*) { + return 0; + } + }; + template struct StandardWidget; + + void use() { f(); } +}