Bug 27697 - [4.0 Regression] incorrect warning about constness of pointer to an array in a const struct
Summary: [4.0 Regression] incorrect warning about constness of pointer to an array in ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.1.2
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: 16989 16620
  Show dependency treegraph
 
Reported: 2006-05-21 09:20 UTC by Frank Victor Fischer
Modified: 2007-02-03 17:18 UTC (History)
2 users (show)

See Also:
Host: i586-suse-linux
Target: i586-suse-linux
Build: i586-suse-linux
Known to work: 4.0.2 4.1.2 4.2.0
Known to fail: 4.0.3 4.1.0
Last reconfirmed: 2006-08-16 17:17:52


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Frank Victor Fischer 2006-05-21 09:20:44 UTC
In this snippet of code:

struct foo {
  int x[2][10];
};

void f(void)
{
  const struct foo bar;
  const int (*baz)[10] = bar.x; /* should be ok */
  int (*qoox)[10] = bar.x; /* should warn */

  bar.x[0][1] = 23; /* error: asignment to const */
  (*qoox)[1] = 23;
}

bar is const, so bar.x is (const int *), so an assignment to (const int *) should be ok, while an assignment to (int *) violates the type constraints (all according to 6.7.3 in the Standard).

in gcc 4.1.0, the behaviour is reversed: assignment to (int *) goes unnoticed while assignment to (const int *) issues an "incompatible pointer" warning.
Comment 1 Frank Victor Fischer 2006-05-21 09:31:44 UTC
In the function above:

bar.x[0][0] = 23; // gcc 4.1 gives error
*bar.x[0] = 23; // gcc 4.1 gives NO error

both lines do the same ....
Comment 2 Andrew Pinski 2006-05-21 16:15:04 UTC
This is also rejects valid with -pedantic-errors.
I don't understand what is correct here so I am not going to confirm it.
Comment 3 Neil Booth 2006-05-21 23:17:29 UTC
Subject: Re:  [4.0/4.1/4.2 Regression] incorrect warning about constness of pointer to an array in a const struct

pinskia at gcc dot gnu dot org wrote:-

> 
> 
> ------- Comment #2 from pinskia at gcc dot gnu dot org  2006-05-21 16:15 -------
> This is also rejects valid with -pedantic-errors.
> I don't understand what is correct here so I am not going to confirm it.

If I understand correctly I think the report is correct about
validity for both C90 and C99; another front end gives

"/tmp/bar.c", line 10: error: cannot convert initializer of type "const
        int (*)[10]" to type "int (*)[10]"
  int (*qoox)[10] = bar.x; /* should warn */
                    ^
"/tmp/bar.c", line 11: error: expression must be a modifiable lvalue
  bar.x[0][1] = 23; /* error: asignment to const */
  ^
"/tmp/bar.c", line 9: warning: variable "baz" declared but not used
  const int (*baz)[10] = bar.x; /* should be ok */
              ^

2 errors found compiling "/tmp/bar.c".
Comment 4 Joseph S. Myers 2006-05-21 23:53:40 UTC
Confirmed, my first guess as to a fix would be to make build_component_ref arrange for the type of the COMPONENT_REF to have the necessary qualifiers via c_build_qualified_type.
Comment 5 Mark Mitchell 2006-07-17 03:42:13 UTC
The trick of using -pedantic-errors to turn every invalid warning into rejects-valid is not useful.  Downgrading to P2.
Comment 6 Joseph S. Myers 2006-08-16 17:17:52 UTC
Testing a patch.
Comment 7 Joseph S. Myers 2006-08-16 23:10:57 UTC
Subject: Bug 27697

Author: jsm28
Date: Wed Aug 16 23:10:46 2006
New Revision: 116194

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116194
Log:
	PR c/27697
	* c-typeck.c (build_component_ref): Combine qualifiers of
	structure or union and field.

testsuite:
	* gcc.dg/qual-component-1.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/qual-component-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-typeck.c
    trunk/gcc/testsuite/ChangeLog

Comment 8 Joseph S. Myers 2006-08-16 23:12:44 UTC
Subject: Bug 27697

Author: jsm28
Date: Wed Aug 16 23:12:37 2006
New Revision: 116195

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116195
Log:
	PR c/27697
	* c-typeck.c (build_component_ref): Combine qualifiers of
	structure or union and field.

testsuite:
	* gcc.dg/qual-component-1.c: New test.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/gcc.dg/qual-component-1.c
      - copied unchanged from r116194, trunk/gcc/testsuite/gcc.dg/qual-component-1.c
Modified:
    branches/gcc-4_1-branch/gcc/ChangeLog
    branches/gcc-4_1-branch/gcc/c-typeck.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 9 Brett Albertson 2006-08-17 13:02:35 UTC
(In reply to comment #7)
> Subject: Bug 27697

This fails on i386-pc-solaris2.10 on trunk with:

Executing on host: /u01/var/tmp/gcc_trunk_svn/gcc_20060817/gcc/xgcc -B/u01/var/tmp/gcc_trunk_svn/gcc_20060817/gcc/ /u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c    -fno-show-column -S  -o qual-component-1.s    (timeout = 300)
/u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c: In function 'f':^M
/u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c:45: error: assignment of read-only member 'd'^M
/u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c:46: error: assignment of read-only location^M
/u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c:47: error: assignment of read-only location^M

etc.

and

/u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c: In function 'g':^M
/u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c:113: warning: initialization from in compatible pointer type^M
/u01/var/tmp/gcc_trunk_svn/gcc/gcc/testsuite/gcc.dg/qual-component-1.c:118: warning: assignment from incompatible pointer type^M

etc.

Brett
Comment 10 Joseph S. Myers 2006-08-26 00:11:08 UTC
Not working on 4.0 fix, as noted at http://gcc.gnu.org/ml/gcc-patches/2006-08/msg00566.html one test fails there with the patch applied.
Comment 11 Gabriel Dos Reis 2007-02-03 17:18:20 UTC
Fixed in GCC-4.1.2.