diff mbox

[C++] Propagate CLASSTYPE_HAS_MUTABLE from bases to derived classes (PR c++/77375)

Message ID 20160829193734.GH14857@tucnak.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek Aug. 29, 2016, 7:37 p.m. UTC
Hi!

The following testcase fails (foo is allocated in .rodata and modified)
because while we clear TREE_READONLY for classes with mutable members, we
don't do that if they only have bases with mutable members.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux.  Ok for
trunk?  What about release branches?

2016-08-29  Jakub Jelinek  <jakub@redhat.com>

	PR c++/77375
	* class.c (check_bases): Set CLASSTYPE_HAS_MUTABLE if any TYPE_HAS_MUTABLE_P
	for any bases.

	* g++.dg/cpp0x/mutable1.C: New test.


	Jakub

Comments

Jason Merrill Sept. 16, 2016, 7:44 p.m. UTC | #1
OK.

On Mon, Aug 29, 2016 at 3:37 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> Hi!
>
> The following testcase fails (foo is allocated in .rodata and modified)
> because while we clear TREE_READONLY for classes with mutable members, we
> don't do that if they only have bases with mutable members.
>
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux.  Ok for
> trunk?  What about release branches?
>
> 2016-08-29  Jakub Jelinek  <jakub@redhat.com>
>
>         PR c++/77375
>         * class.c (check_bases): Set CLASSTYPE_HAS_MUTABLE if any TYPE_HAS_MUTABLE_P
>         for any bases.
>
>         * g++.dg/cpp0x/mutable1.C: New test.
>
> --- gcc/cp/class.c.jj   2016-08-12 17:33:42.000000000 +0200
> +++ gcc/cp/class.c      2016-08-29 11:54:10.387061502 +0200
> @@ -1796,6 +1796,8 @@ check_bases (tree t,
>        SET_CLASSTYPE_REF_FIELDS_NEED_INIT
>         (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
>          | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
> +      if (TYPE_HAS_MUTABLE_P (basetype))
> +       CLASSTYPE_HAS_MUTABLE (t) = 1;
>
>        /*  A standard-layout class is a class that:
>           ...
> --- gcc/testsuite/g++.dg/cpp0x/mutable1.C.jj    2016-08-29 12:05:13.700212552 +0200
> +++ gcc/testsuite/g++.dg/cpp0x/mutable1.C       2016-08-29 12:03:51.000000000 +0200
> @@ -0,0 +1,12 @@
> +// PR c++/77375
> +// { dg-do run { target c++11 } }
> +
> +struct Base { mutable int i; };
> +struct Derived : Base {};
> +const Derived foo{};
> +
> +int
> +main ()
> +{
> +  foo.i = 42;
> +}
>
>         Jakub
diff mbox

Patch

--- gcc/cp/class.c.jj	2016-08-12 17:33:42.000000000 +0200
+++ gcc/cp/class.c	2016-08-29 11:54:10.387061502 +0200
@@ -1796,6 +1796,8 @@  check_bases (tree t,
       SET_CLASSTYPE_REF_FIELDS_NEED_INIT
 	(t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
 	 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
+      if (TYPE_HAS_MUTABLE_P (basetype))
+	CLASSTYPE_HAS_MUTABLE (t) = 1;
 
       /*  A standard-layout class is a class that:
 	  ...
--- gcc/testsuite/g++.dg/cpp0x/mutable1.C.jj	2016-08-29 12:05:13.700212552 +0200
+++ gcc/testsuite/g++.dg/cpp0x/mutable1.C	2016-08-29 12:03:51.000000000 +0200
@@ -0,0 +1,12 @@ 
+// PR c++/77375
+// { dg-do run { target c++11 } }
+
+struct Base { mutable int i; };
+struct Derived : Base {};
+const Derived foo{};
+
+int
+main ()
+{
+  foo.i = 42;
+}