[clang] 5a684b7 - Ensure we don't strip the ConstantExpr carrying a non-type template

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 19 12:48:51 PST 2021


Author: Richard Smith
Date: 2021-01-19T12:48:39-08:00
New Revision: 5a684b70dc74f9f671f8eb61993a25769ec68117

URL: https://github.com/llvm/llvm-project/commit/5a684b70dc74f9f671f8eb61993a25769ec68117
DIFF: https://github.com/llvm/llvm-project/commit/5a684b70dc74f9f671f8eb61993a25769ec68117.diff

LOG: Ensure we don't strip the ConstantExpr carrying a non-type template
argument's value off it during substitution.

Added: 
    

Modified: 
    clang/lib/Sema/SemaTemplateInstantiate.cpp
    clang/test/SemaTemplate/temp_arg_nontype_cxx17.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 7679063ead71..7d7591ab669c 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1597,7 +1597,9 @@ TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
 ExprResult
 TemplateInstantiator::TransformSubstNonTypeTemplateParmExpr(
                                           SubstNonTypeTemplateParmExpr *E) {
-  ExprResult SubstReplacement = TransformExpr(E->getReplacement());
+  ExprResult SubstReplacement = E->getReplacement();
+  if (!isa<ConstantExpr>(SubstReplacement.get()))
+    SubstReplacement = TransformExpr(E->getReplacement());
   if (SubstReplacement.isInvalid())
     return true;
   QualType SubstType = TransformType(E->getParameterType(getSema().Context));

diff  --git a/clang/test/SemaTemplate/temp_arg_nontype_cxx17.cpp b/clang/test/SemaTemplate/temp_arg_nontype_cxx17.cpp
index bc8a22e89041..4d61b9c7d937 100644
--- a/clang/test/SemaTemplate/temp_arg_nontype_cxx17.cpp
+++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx17.cpp
@@ -511,3 +511,18 @@ namespace dependent_reference {
   // Ensure that we can instantiate the definition of S<...>.
   int n = *v.q + *w.q;
 }
+
+namespace decay {
+  template<typename T, typename C, const char *const A[(int)T::count]> struct X {
+    template<typename CC> void f(const X<T, CC, A> &v) {}
+  };
+  struct A {
+    static constexpr const char *arr[] = {"hello", "world"};
+    static constexpr int count = 2;
+  };
+  void f() {
+    X<A, int, A::arr> x1;
+    X<A, float, A::arr> x2;
+    x1.f(x2);
+  }
+}


        


More information about the cfe-commits mailing list