r258110 - Fix PR26134: When substituting into default template arguments, keep CurContext unchanged.
Faisal Vali via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 18 19:58:55 PST 2016
Author: faisalv
Date: Mon Jan 18 21:58:55 2016
New Revision: 258110
URL: http://llvm.org/viewvc/llvm-project?rev=258110&view=rev
Log:
Fix PR26134: When substituting into default template arguments, keep CurContext unchanged.
Or, do not set Sema's CurContext to the template declaration's when substituting into default template arguments of said template declaration.
If we do push the template declaration context on to Sema, and the template declaration is at namespace scope, Sema can get confused and try and do odr analysis when substituting into default template arguments, even though the substitution could be occurring within a dependent context.
I'm not sure why this was being done, perhaps there was concern that if a default template argument referred to a previous template parameter, it might not be found during substitution - but all regression tests pass, and I can't craft a test that would cause it to fails (if some one does, please inform me, and i'll craft a different fix for the PR).
This patch removes a single line of code, but unfortunately adds more than it removes, because of the tests. Some day I still hope to commit a patch that removes far more lines than it adds, while leaving clang better for it ;)
Sorry that r253590 ("Change the expression evaluation context from Unevaluated to ConstantEvaluated while substituting into non-type template argument defaults") caused the PR!
Modified:
cfe/trunk/lib/Sema/SemaTemplate.cpp
cfe/trunk/test/SemaTemplate/default-arguments.cpp
Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=258110&r1=258109&r2=258110&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Mon Jan 18 21:58:55 2016
@@ -3281,7 +3281,6 @@ SubstDefaultTemplateArgument(Sema &SemaR
for (unsigned i = 0, e = Param->getDepth(); i != e; ++i)
TemplateArgLists.addOuterTemplateArguments(None);
- Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext());
EnterExpressionEvaluationContext ConstantEvaluated(SemaRef,
Sema::ConstantEvaluated);
return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists);
Modified: cfe/trunk/test/SemaTemplate/default-arguments.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/default-arguments.cpp?rev=258110&r1=258109&r2=258110&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/default-arguments.cpp (original)
+++ cfe/trunk/test/SemaTemplate/default-arguments.cpp Mon Jan 18 21:58:55 2016
@@ -179,3 +179,31 @@ struct C {
C(T t = ); // expected-error {{expected expression}}
};
C<int> obj;
+
+namespace PR26134 {
+// Make sure when substituting default template arguments we do it in the current context.
+template<class T, bool Val = T::value>
+struct X {};
+
+template<bool B> struct Y {
+ void f() { X<Y> xy; }
+ static const bool value = B;
+};
+
+namespace ns1 {
+template<class T0>
+struct X {
+ template<bool B = T0::value> struct XInner { static const bool value = B; };
+};
+template<bool B> struct S { static const bool value = B; };
+#if __cplusplus > 199711L
+template<bool B> struct Y {
+ static constexpr bool f() { return typename X<S<B>>::template XInner<>{}.value; }
+ static_assert(f() == B, "");
+};
+Y<true> y;
+Y<false> y2;
+#endif
+
+} // end ns1
+} // end ns PR26134
More information about the cfe-commits
mailing list