C++ PATCH for c++/92062 - ODR-use ignored for static member of class template

Message ID 20191011202334.GT2949@redhat.com
State New
Headers show
Series
  • C++ PATCH for c++/92062 - ODR-use ignored for static member of class template
Related show

Commit Message

Marek Polacek Oct. 11, 2019, 8:23 p.m.
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.

Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?

2019-10-11  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.

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

Comments

Marek Polacek Oct. 18, 2019, 2:54 p.m. | #1
Ping.

On Fri, Oct 11, 2019 at 04:23:34PM -0400, Marek Polacek wrote:
> 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.

> 

> Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?

> 

> 2019-10-11  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.

> 

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

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

> 

> diff --git gcc/cp/pt.c gcc/cp/pt.c

> index 84464436991..521d0c56002 100644

> --- gcc/cp/pt.c

> +++ gcc/cp/pt.c

> @@ -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.  */

> diff --git gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C

> new file mode 100644

> index 00000000000..cf3f95f0565

> --- /dev/null

> +++ gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C

> @@ -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 gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C

> new file mode 100644

> index 00000000000..0927488e569

> --- /dev/null

> +++ gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C

> @@ -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 ();

> +}


Marek
Jason Merrill Oct. 21, 2019, 6:22 p.m. | #2
On 10/11/19 4:23 PM, Marek Polacek wrote:
> 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.

> 

> Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?

> 

> 2019-10-11  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.

> 

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

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

> 

> diff --git gcc/cp/pt.c gcc/cp/pt.c

> index 84464436991..521d0c56002 100644

> --- gcc/cp/pt.c

> +++ gcc/cp/pt.c

> @@ -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.  */

> diff --git gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C

> new file mode 100644

> index 00000000000..cf3f95f0565

> --- /dev/null

> +++ gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C

> @@ -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 gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C

> new file mode 100644

> index 00000000000..0927488e569

> --- /dev/null

> +++ gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C

> @@ -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 ();

> +}

> 


OK.

Jason

Patch

diff --git gcc/cp/pt.c gcc/cp/pt.c
index 84464436991..521d0c56002 100644
--- gcc/cp/pt.c
+++ gcc/cp/pt.c
@@ -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.  */
diff --git gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C
new file mode 100644
index 00000000000..cf3f95f0565
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/constexpr-odr1.C
@@ -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 gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C
new file mode 100644
index 00000000000..0927488e569
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/constexpr-odr2.C
@@ -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 ();
+}