Bug 20039 - uninitialized const in `new' of `const struct'
Summary: uninitialized const in `new' of `const struct'
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
: 28990 (view as bug list)
Depends on:
Blocks: 28956 28990 29843
  Show dependency treegraph
 
Reported: 2005-02-17 20:54 UTC by Mo Sadoghi
Modified: 2011-09-25 20:29 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.4.5, 4.5.2, 4.6.0
Known to fail: 2.95.3, 3.0.4, 3.2.3, 3.3.3, 3.4.0, 4.0.0, 4.1.2, 4.2.4, 4.3.5
Last reconfirmed: 2006-02-20 18:37:53


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mo Sadoghi 2005-02-17 20:54:17 UTC
Sourc code for t.cpp:
------------------------------------------------
struct M
	{
	M() : m(0) { }
	int m;
	};

struct X
	{
	M m;			/* Changing this line to "int m" will produce expected error */
	};

int mymain()
	{
	const X *p = new const X[2]; /* error - X must have explicit default constructor */
	return 0;
	}

------------------------------------------------



Expected Behaviour:
------------------------------------------------
t.cpp:14: error: uninitialized const in `new' of `const struct X'
------------------------------------------------



Actual Behaviour:
------------------------------------------------
None.
------------------------------------------------



t.ii generated with -save-temps option
------------------------------------------------
# 1 "t.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "t.cpp"
struct M
        {
        M() : m(0) { }
        int m;
        };

struct X
        {
        M m;
        };

int mymain()
        {
        const X *p = new const X[2];
        return 0;
        }

------------------------------------------------




Release:
------------------------------------------------
GCC Version: 3.2.0
------------------------------------------------




Environment:
System Type:
------------------------------------------------
Reading specs from /usr/local/bin/../lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.2/specs
Configured with: /scratch/gcc-3.2/configure --prefix=/usr/local/gcc.3.2.0
--enable-threads=aix --disable-nls
Thread model: aix
gcc version 3.2
 /usr/local/bin/../lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.2/cpp0 -lang-c++
-D__GNUG__=3 -D__DEPRECATED -D__EXCEPTIONS -v -iprefix
/usr/local/bin/../lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.2/ -D__GNUC__=3
-D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=0 -D__GXX_ABI_VERSION=102 -D_IBMR2
-D_POWER -D_LONG_LONG -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -D_IBMR2
-D_POWER -D_LONG_LONG -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -Asystem=unix
-Asystem=aix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -D_XOPEN_SOURCE=500
-D_XOPEN_SOURCE_EXTENDED=1 -D_LARGE_FILE_API -D_ALL_SOURCE
-D__WCHAR_TYPE__=short unsigned int -D_ARCH_COM
/home/msadoghi/scenario/common/t.cpp t.ii
GNU CPP version 3.2 (cpplib)
ignoring nonexistent directory
"/usr/local/lib/gcc-lib/../../powerpc-ibm-aix5.1.0.0/include"
ignoring nonexistent directory "/usr/local/gcc.3.2.0/powerpc-ibm-aix5.1.0.0/include"
ignoring duplicate directory "/usr/local/gcc.3.2.0/include/c++/3.2"
ignoring duplicate directory
"/usr/local/gcc.3.2.0/include/c++/3.2/powerpc-ibm-aix5.1.0.0"
ignoring duplicate directory "/usr/local/gcc.3.2.0/include/c++/3.2/backward"
ignoring duplicate directory
"/usr/local/gcc.3.2.0/lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.2/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/lib/gcc-lib/../../include/c++/3.2
 /usr/local/lib/gcc-lib/../../include/c++/3.2/powerpc-ibm-aix5.1.0.0
 /usr/local/lib/gcc-lib/../../include/c++/3.2/backward
 /usr/local/lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.2/include
 /usr/local/include
 /usr/local/gcc.3.2.0/include
 /usr/include
End of search list.
 /usr/local/bin/../lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.2/cc1plus -fpreprocessed
t.ii -quiet -dumpbase t.cpp -version -o t.s
GNU CPP version 3.2 (cpplib)
GNU C++ version 3.2 (powerpc-ibm-aix5.1.0.0)
        compiled by GNU C version 3.2.
 as -u -mcom -o t.o t.s
------------------------------------------------




How-To-Repeat:
------------------------------------------------
g++ -v -save-temps -c -std=iso9899:199409 t.cpp
------------------------------------------------


Note:
------------------------------------------------
This is a diagnostic test case. Compilation failure is expected. However the
compilation
is successful and no error or warning messages are produced. According to C++
ANSI Standard
the test cases should produces an error. Therefore, in the strict mode compiler
should
produce a server error and in the extended mode it should at least prodeces a
warning.

C++ ANSI Standard:
"when new-expression without new-initializer creates object of const-qualified
non-POD 
class T (or array thereof), T must have user-declared default constructor"

The MACOS, LINUX and AIX all have a similar behaviors.

The expected behavior was produced by changing line 9:
M m; -> int m;
------------------------------------------------
Comment 1 Andrew Pinski 2005-02-17 21:04:22 UTC
Confirmed, not a regression.
Comment 2 Wolfgang Bangerth 2006-10-10 03:36:04 UTC
*** Bug 28990 has been marked as a duplicate of this bug. ***
Comment 3 fabien 2011-04-12 21:31:58 UTC
It was fixed in 4.4.x
Comment 4 Jonathan Wakely 2011-04-12 23:55:19 UTC
fixed then
Comment 5 fabien 2011-05-09 20:56:33 UTC
Author: fabien
Date: Mon May  9 20:56:29 2011
New Revision: 173592

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173592
Log:
gcc/testsuite/ChangeLog:

2011-05-09  Fabien Chene  <fabien@gcc.gnu.org>
	PR c++/20039
	* g++.dg/init/pr20039.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/init/pr20039.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 6 Jason Merrill 2011-09-23 21:30:52 UTC
Author: jason
Date: Fri Sep 23 21:30:48 2011
New Revision: 179130

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179130
Log:
	Core 234 - allow const objects with no initializer or
	user-provided default constructor if the defaulted constructor
	initializes all the subobjects.
	PR c++/20039
	PR c++/42844
	* class.c (default_init_uninitialized_part): New.
	* cp-tree.h: Declare it.
	* decl.c (check_for_uninitialized_const_var): Use it.
	* init.c (perform_member_init): Likewise.
	(build_new_1): Likewise.
	* method.c (walk_field_subobs): Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/init/const8.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/init.c
    trunk/gcc/cp/method.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C
    trunk/gcc/testsuite/g++.dg/cpp0x/defaulted2.C
    trunk/gcc/testsuite/g++.dg/cpp0x/pr42844-2.C
    trunk/gcc/testsuite/g++.dg/init/pr20039.C
    trunk/gcc/testsuite/g++.dg/init/pr42844.C
Comment 7 Jason Merrill 2011-09-25 20:29:09 UTC
Author: jason
Date: Sun Sep 25 20:29:04 2011
New Revision: 179170

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179170
Log:
	Core 234 - allow const objects with no initializer or
	user-provided default constructor if the defaulted constructor
	initializes all the subobjects.
	PR c++/20039
	PR c++/42844
	* class.c (default_init_uninitialized_part): New.
	* cp-tree.h: Declare it.
	* decl.c (check_for_uninitialized_const_var): Use it.
	* init.c (perform_member_init): Likewise.
	(build_new_1): Likewise.
	* method.c (walk_field_subobs): Likewise.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/init/const8.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/class.c
    branches/gcc-4_6-branch/gcc/cp/cp-tree.h
    branches/gcc-4_6-branch/gcc/cp/decl.c
    branches/gcc-4_6-branch/gcc/cp/init.c
    branches/gcc-4_6-branch/gcc/cp/method.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/defaulted2.C
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/pr42844-2.C
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/init/pr42844.C
    branches/gcc-4_6-branch/gcc/testsuite/lib/prune.exp