PR c++/92062 - ODR-use ignored for static member of class template.
authorMarek Polacek <polacek@redhat.com>
Mon, 21 Oct 2019 18:45:45 +0000 (18:45 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Mon, 21 Oct 2019 18:45:45 +0000 (18:45 +0000)
has_value_dependent_address wasn't stripping location wrappers so it
gave the wrong answer for "&x" in the static_assert.  That led us to
thinking that the expression isn't instantiation-dependent, and we
skipped static initialization of A<0>::x.

This patch adds stripping so that has_value_dependent_address gives the
same answer as it used to before the location wrappers addition.

* pt.c (has_value_dependent_address): Strip location wrappers.

* g++.dg/cpp0x/constexpr-odr1.C: New test.
* g++.dg/cpp0x/constexpr-odr2.C: New test.

From-SVN: r277266

gcc/cp/ChangeLog
gcc/cp/pt.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C [new file with mode: 0644]

index 29c6bbd3230b763df32e9cfffabf3442c157279e..f861fb3e343c96e5a5f67bf536d284ec8d88da78 100644 (file)
@@ -1,3 +1,8 @@
+2019-10-21  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/92062 - ODR-use ignored for static member of class template.
+       * pt.c (has_value_dependent_address): Strip location wrappers.
+
 2019-10-21  Marek Polacek  <polacek@redhat.com>
 
        PR c++/92106 - ICE with structured bindings and -Wreturn-local-addr.
index 7f7f99297c07ba09a1f621637ce74693a00de6c0..e154cac7b21b69e01175c261822352f52219773b 100644 (file)
@@ -6542,6 +6542,8 @@ check_valid_ptrmem_cst_expr (tree type, tree expr,
 static bool
 has_value_dependent_address (tree op)
 {
+  STRIP_ANY_LOCATION_WRAPPER (op);
+
   /* We could use get_inner_reference here, but there's no need;
      this is only relevant for template non-type arguments, which
      can only be expressed as &id-expression.  */
index 7365a393a7dd2569d8c934ab78976a246d08ddde..ba280a1beaff709c7158077f013c4cfe21d618b6 100644 (file)
@@ -1,3 +1,9 @@
+2019-10-21  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/92062 - ODR-use ignored for static member of class template.
+       * g++.dg/cpp0x/constexpr-odr1.C: New test.
+       * g++.dg/cpp0x/constexpr-odr2.C: New test.
+
 2019-10-21  Marek Polacek  <polacek@redhat.com>
 
        PR c++/92106 - ICE with structured bindings and -Wreturn-local-addr.
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C
new file mode 100644 (file)
index 0000000..cf3f95f
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/92062 - ODR-use ignored for static member of class template.
+// { dg-do run { target c++11 } }
+
+template<int> struct A {
+  static const bool x;
+  static_assert(&x, ""); // odr-uses A<...>::x
+};
+
+int g;
+
+template<int I>
+const bool A<I>::x = (g = 42, false);
+
+void f(A<0>) {}        // A<0> must be complete, so is instantiated
+int main()
+{
+  if (g != 42)
+    __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C
new file mode 100644 (file)
index 0000000..0927488
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/92062 - ODR-use ignored for static member of class template.
+// { dg-do run { target c++11 } }
+
+template<int> struct A {
+  static const bool x;
+  enum { force_instantiation =! &x}; // odr-uses A<...>::x
+};
+
+int g;
+
+template<int I>
+const bool A<I>::x = (g = 42, false);
+
+void f(A<0>) {}        // A<0> must be complete, so is instantiated
+int main()
+{
+  if (g != 42)
+    __builtin_abort ();
+}