]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: templated static local var has value-dep addr [PR98930]
authorPatrick Palka <ppalka@redhat.com>
Tue, 5 Oct 2021 13:50:02 +0000 (09:50 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 5 Oct 2021 13:50:02 +0000 (09:50 -0400)
Here uses_template_parms returns false for the dependent type A<&impl::i>,
which causes tsubst_aggr_type to think it's non-dependent and not bother
substituting into it, leading to breakage.

This patch fixes this by making has_value_dependent_address also return
true for templated static local variables.

PR c++/98930

gcc/cp/ChangeLog:

* pt.c (has_value_dependent_address): Return true for a static
local variable from a function template.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/nontype4.C: New test.
* g++.dg/cpp1z/nontype4a.C: New test.

gcc/cp/pt.c
gcc/testsuite/g++.dg/cpp1z/nontype4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/nontype4a.C [new file with mode: 0644]

index 1dcdffe322a16334565ca19136fd483619162274..844b7c14fe42b0a42f404e63d697c26e3d342988 100644 (file)
@@ -6760,8 +6760,15 @@ has_value_dependent_address (tree op)
   if (DECL_P (op))
     {
       tree ctx = CP_DECL_CONTEXT (op);
+
       if (TYPE_P (ctx) && dependent_type_p (ctx))
        return true;
+
+      if (VAR_P (op)
+         && TREE_STATIC (op)
+         && TREE_CODE (ctx) == FUNCTION_DECL
+         && type_dependent_expression_p (ctx))
+       return true;
     }
 
   return false;
diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype4.C b/gcc/testsuite/g++.dg/cpp1z/nontype4.C
new file mode 100644 (file)
index 0000000..ff476dc
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/98930
+// { dg-do compile { target c++17 } }
+
+template<int*>
+struct A { A() { } };
+
+template<class T>
+void impl() {
+  static int i;
+  static A<&i> a;
+}
+
+template void impl<int>();
+template void impl<char>();
diff --git a/gcc/testsuite/g++.dg/cpp1z/nontype4a.C b/gcc/testsuite/g++.dg/cpp1z/nontype4a.C
new file mode 100644 (file)
index 0000000..5b74292
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/98930
+// { dg-do compile { target c++17 } }
+
+template<int*>
+struct A { };
+
+template<class T>
+auto impl() {
+  static int i;
+  return A<&i>();
+}
+
+using type = decltype(impl<int>());
+using type = decltype(impl<char>()); // { dg-error "conflicting declaration" }