Bug 31449 - [4.1] static_cast can remove const-ness
Summary: [4.1] static_cast can remove const-ness
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.0
: P3 normal
Target Milestone: 4.1.3
Assignee: Mark Mitchell
URL:
Keywords: accepts-invalid, monitored
Depends on:
Blocks:
 
Reported: 2007-04-02 23:32 UTC by simon_baldwin
Modified: 2007-04-10 06:07 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.0.2
Known to fail: 4.0.3
Last reconfirmed: 2007-04-04 00:06:46


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description simon_baldwin 2007-04-02 23:32:27 UTC
The following const-incorrect program compiles without error on gcc 4.1.0, and 4.3.0 20070323:

class Foo {};
class Bar : public Foo {};
static const Foo *foo = 0;

static Bar *bar = static_cast<const Bar*>(foo);  // Error, casts away const

void func(const Foo *foo) {
  Bar *bar = static_cast<const Bar*>(foo);  // Error, casts away const
}

gcc 2.95.3, 3.4.5, and 4.0.2 report errors on the commented lines above, so it looks like a compiler regression in 4.1.0.

I looked hard through existing PRs, and couldn't find this reported elsewhere (the closest is PR 20475, but it's concerned with 3.4.4 and the const-ness, or otherwise, of string literals, so not the same).  I'd be a little surprised if I'm the first to see it, but it's possible...
Comment 1 Volker Reichelt 2007-04-04 00:06:46 UTC
Confirmed.
The regression was introduced between GCC 4.0.2 and 4.0.3.

Mark, it looks like the following patch of yours is responsible for the regression:

2006-01-21  Mark Mitchell  <mark@codesourcery.com>

	PR c++/25895
	* class.c (build_base_path): Generate a NOP_EXPR instead of a
	COMPONENT_REF if the base and derived classes are at the same
	address.
Comment 2 Mark Mitchell 2007-04-09 20:46:04 UTC
Subject: Bug 31449

Author: mmitchel
Date: Mon Apr  9 20:45:53 2007
New Revision: 123679

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123679
Log:
	PR c++/31449
	* class.c (build_base_path): Ensure that the converted pointer has
	the same cv-qualification as the input.
	PR c++/31449
	* g++.dg/init/const5.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/init/const5.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c
    trunk/gcc/testsuite/ChangeLog

Comment 3 Mark Mitchell 2007-04-09 20:51:28 UTC
Fixed in 4.3.0.
Comment 4 Mark Mitchell 2007-04-10 04:01:20 UTC
Subject: Bug 31449

Author: mmitchel
Date: Tue Apr 10 04:01:08 2007
New Revision: 123690

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123690
Log:
	PR c++/31449
	* class.c (build_base_path): Ensure that the converted pointer has
	the same cv-qualification as the input.
	PR c++/31449
	* g++.dg/init/const5.C: New test.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/g++.dg/init/const5.C
Modified:
    branches/gcc-4_2-branch/gcc/cp/ChangeLog
    branches/gcc-4_2-branch/gcc/cp/class.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 5 Mark Mitchell 2007-04-10 04:01:50 UTC
Fixed in 4.2.0.
Comment 6 Mark Mitchell 2007-04-10 06:07:52 UTC
Subject: Bug 31449

Author: mmitchel
Date: Tue Apr 10 06:07:41 2007
New Revision: 123691

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123691
Log:
	PR c++/31449
	* class.c (build_base_path): Ensure that the converted pointer has
	the same cv-qualification as the input.
	PR c++/31449
	* g++.dg/init/const5.C: New test.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/g++.dg/init/const5.C
Modified:
    branches/gcc-4_1-branch/gcc/cp/ChangeLog
    branches/gcc-4_1-branch/gcc/cp/class.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 7 Mark Mitchell 2007-04-10 06:07:59 UTC
Fixed in 4.1.3.