Bug 40975 - [4.3/4.4/4.5/4.6 Regression] ICE in copy_tree_r on array new
Summary: [4.3/4.4/4.5/4.6 Regression] ICE in copy_tree_r on array new
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.2
: P2 major
Target Milestone: 4.4.7
Assignee: Jason Merrill
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2009-08-05 18:14 UTC by Jonathan Jones
Modified: 2011-05-05 21:06 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work: 3.3.6, 3.4.6, 4.7.0
Known to fail: 4.0.4, 4.1.2, 4.2.4, 4.3.5, 4.4.5, 4.5.2, 4.6.0
Last reconfirmed: 2009-08-06 09:06:04


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Jones 2009-08-05 18:14:16 UTC
Using array new as a default initializer of a constructor argument, when the object being "newed" itself contains a constructor, crashes the compiler with an error similar to the following:

% g++ -c gcc_bug.cpp
    gcc_bug.cpp:13: internal compiler error: in copy_tree_r, at tree-inline.c:3160
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See <file:///usr/share/doc/gcc-4.3/README.Bugs> for instructions.

The following code example clearly demonstrates the problem.  

// BEGIN gcc_bug.cpp
struct data_type
{
    // constructor required to reproduce compiler bug
    data_type() {}
};

struct ptr_type
{
    // array new as default argument required to reproduce compiler bug
    ptr_type (data_type* ptr = new data_type[1]) { delete[] ptr; }
};

ptr_type obj;
// END gcc_bug.cpp

The problem is visible in multiple versions of the compiler on multiple systems:

%  g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-cld --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.2 (Debian 4.3.2-1.1) 

% g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.2 (Debian 4.3.2-1.1) 

% g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release x86_64-linux-gnu
Thread model: posix
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)

% g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-tune=i686 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)


Note that the problem is _not_ visible on the following compilers/platforms:

% g++-4.0 -v
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5484~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5484)

 g++-4.2 -v
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc_42/gcc_42-5564~1/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/usr/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-gxx-include-dir=/usr/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5564)
Comment 1 Mikael Pettersson 2009-08-05 22:42:38 UTC
I can reproduce this ICE on i686-linux with gcc-4.3-20090802 and gcc-4.4-20090804. gcc-4.5 has some nasty new build requirements for c++ so I didn't try it.
Comment 2 Richard Biener 2009-08-06 09:06:03 UTC
Confirmed.

#1  0x00000000007b927a in copy_tree_r (tp=0x7ffff5fcacb0, 
    walk_subtrees=0x7fffffffb6c0, data=0x0)
    at /space/rguenther/src/svn/trunk/gcc/tree-inline.c:3814
3814        gcc_assert (code != STATEMENT_LIST);
#2  0x00000000006d4252 in bot_manip (tp=0x7ffff5fcacb0, 
    walk_subtrees=0x7fffffffb6c0, data=0x18a5fc0)
    at /space/rguenther/src/svn/trunk/gcc/cp/tree.c:1669
#3  0x0000000000dd1432 in walk_tree_1 (tp=0x7ffff5fcacb0, 
    func=0x6d3b8b <bot_manip>, data=0x18a5fc0, pset=0x0, 
    lh=0x6db2e2 <cp_walk_subtrees>)
    at /space/rguenther/src/svn/trunk/gcc/tree.c:9068
#4  0x0000000000dd3924 in walk_tree_1 (tp=0x7ffff5fcac70, 
    func=0x6d3b8b <bot_manip>, data=0x18a5fc0, pset=0x0, 
    lh=0x6db2e2 <cp_walk_subtrees>)
    at /space/rguenther/src/svn/trunk/gcc/tree.c:9319
#5  0x0000000000dd3924 in walk_tree_1 (tp=0x7ffff5fcac38, 
    func=0x6d3b8b <bot_manip>, data=0x18a5fc0, pset=0x0, 
    lh=0x6db2e2 <cp_walk_subtrees>)
    at /space/rguenther/src/svn/trunk/gcc/tree.c:9319
#6  0x00000000006d4324 in break_out_target_exprs (t=0x7ffff5fcabc0)
    at /space/rguenther/src/svn/trunk/gcc/cp/tree.c:1709
#7  0x00000000004b261a in convert_default_arg (type=0x7ffff5fc2300, 
    arg=0x7ffff5fca300, fn=0x7ffff5fc3900, parmnum=0)
    at /space/rguenther/src/svn/trunk/gcc/cp/call.c:5233
#8  0x00000000004b4f8e in build_over_call (cand=0x18b3bd0, flags=3, complain=3)
    at /space/rguenther/src/svn/trunk/gcc/cp/call.c:5586
#9  0x00000000004ba608 in build_new_method_call (instance=0x7ffff5fb1e60, 
    fns=0x7ffff5fc5960, args=0x7fffffffd188, conversion_path=0x7ffff5fc7000, 
    flags=3, fn_p=0x0, complain=3)
    at /space/rguenther/src/svn/trunk/gcc/cp/call.c:6320
Comment 3 Vanuan 2011-02-12 10:12:34 UTC
The same problem  using c++0x initializer list:

int some_func(int some_array[] = new int[2]{1,2}) {
}

int main() {
  some_func();
}

$ g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:5: internal compiler error: in copy_tree_r, at tree-inline.c:3725
Comment 4 Zdenek Sojka 2011-04-19 13:47:53 UTC
This is still happening in current trunk (4.7).

Tested revisions:
4.0.4, 4.1.2, 4.2.4, 4.3.5, 4.4.5, 4.5.2, 4.6.0, 4.7.0 - crash
3.3.6, 3.4.6 - OK (without checking)

It might be a regression from 3.4, but I don't have a build with checking enabled to verify.
Comment 5 Zdenek Sojka 2011-04-19 15:08:36 UTC
Indeed 3.4 works even with checking enabled:

$ /mnt/sda1/gcc-3_4/binary-172709/bin/g++ pr40975.C -c
$ g++ -v
Reading specs from /mnt/sda1/gcc-3_4/binary-172709/bin/../lib/gcc/x86_64-unknown-linux-gnu/3.4.6/specs
Configured with: /mnt/sda1/gcc-3_4/configure --enable-checking=yes --enable-languages=c,c++ --prefix=/mnt/sda1/gcc-3_4/binary-172709/ --disable-werror --disable-bootstrap --disable-multilib
Thread model: posix
gcc version 3.4.6

(I can't edit the "known to work/fail" fields, nor the summary)
Comment 6 Jason Merrill 2011-04-28 01:53:38 UTC
Author: jason
Date: Thu Apr 28 01:53:34 2011
New Revision: 173056

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173056
Log:
	PR c++/40975
	* cp-tree.def (VEC_INIT_EXPR): Add third operand.
	* cp-tree.h (VEC_INIT_EXPR_NELTS): New.
	* cp-gimplify.c (cp_gimplify_expr) [VEC_INIT_EXPR]: Handle it.
	* tree.c (build_vec_init_expr): Handle getting pointer/nelts.
	(build_vec_init_elt): Don't expect an array type.
	(build_array_copy): Adjust.
	* init.c (perform_member_init): Adjust.
	(build_new_1): Use build_vec_init_expr.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/initlist49.C
    trunk/gcc/testsuite/g++.dg/init/new30.C
Modified:
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/c-family/c-common.h
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-gimplify.c
    trunk/gcc/cp/cp-tree.def
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/init.c
    trunk/gcc/cp/method.c
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Jason Merrill 2011-04-28 15:57:06 UTC
This was broken by the tree-ssa merge, r81764, which introduced STATEMENT_LIST and caused copy_tree_r to abort on it.  Diego, do you happen to remember the rationale for that?  Why can't we copy a STATEMENT_LIST in a statement-expression?
Comment 8 dnovillo@google.com 2011-04-28 17:37:29 UTC
On Thu, Apr 28, 2011 at 13:01, jason at gcc dot gnu.org
<gcc-bugzilla@gcc.gnu.org> wrote:
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40975
>
> Jason Merrill <jason at gcc dot gnu.org> changed:
>
>           What    |Removed                     |Added
> ----------------------------------------------------------------------------
>                 CC|                            |dnovillo at gcc dot gnu.org
>
> --- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> 2011-04-28 15:57:06 UTC ---
> This was broken by the tree-ssa merge, r81764, which introduced STATEMENT_LIST
> and caused copy_tree_r to abort on it.  Diego, do you happen to remember the
> rationale for that?  Why can't we copy a STATEMENT_LIST in a
> statement-expression?

Oh, boy.  Sorry.  I do not remember why we added that assertion.  It
may have been to avoid recursing twice, since copy_tree_r is typically
called to copy individual statements in a list.  So, we never expected
to find STATEMENT_LISTs inside a single statement.

This may be largely unnecessary now.


Diego.
>
> --
> Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
> ------- You are receiving this mail because: -------
> You are on the CC list for the bug.
>
Comment 9 Jason Merrill 2011-05-02 21:59:59 UTC
Author: jason
Date: Mon May  2 21:59:57 2011
New Revision: 173273

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173273
Log:
	PR c++/40975
	* tree-inline.c (copy_tree_r): Handle STATEMENT_LIST.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/tree-inline.c
Comment 10 Jason Merrill 2011-05-02 22:00:13 UTC
Author: jason
Date: Mon May  2 22:00:07 2011
New Revision: 173274

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173274
Log:
	Revert:
	PR c++/40975
	* cp-tree.def (VEC_INIT_EXPR): Add third operand.
	* cp-tree.h (VEC_INIT_EXPR_NELTS): New.
	* cp-gimplify.c (cp_gimplify_expr) [VEC_INIT_EXPR]: Handle it.
	* tree.c (build_vec_init_expr): Handle getting pointer/nelts.
	(build_vec_init_elt): Don't expect an array type.
	(build_array_copy): Adjust.
	* init.c (perform_member_init): Adjust.
	(build_new_1): Use build_vec_init_expr.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-gimplify.c
    trunk/gcc/cp/cp-tree.def
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/init.c
    trunk/gcc/cp/method.c
    trunk/gcc/cp/tree.c
Comment 11 Jason Merrill 2011-05-05 20:51:00 UTC
Author: jason
Date: Thu May  5 20:50:57 2011
New Revision: 173451

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173451
Log:
	PR c++/40975
	* tree-inline.c (copy_tree_r): Use copy_statement_list.
	(copy_statement_list): Don't recurse.
	* stor-layout.c (copy_self_referential_tree_r): Don't allow
	STATEMENT_LIST.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/stor-layout.c
    trunk/gcc/tree-inline.c
Comment 12 Jason Merrill 2011-05-05 21:02:09 UTC
Author: jason
Date: Thu May  5 21:02:06 2011
New Revision: 173452

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173452
Log:
	PR c++/40975
	* tree-inline.c (copy_tree_r): Handle STATEMENT_LIST.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/init/new30.C
Modified:
    branches/gcc-4_6-branch/gcc/ChangeLog
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_6-branch/gcc/tree-inline.c
Comment 13 Jason Merrill 2011-05-05 21:02:48 UTC
Author: jason
Date: Thu May  5 21:02:45 2011
New Revision: 173453

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173453
Log:
	PR c++/40975
	* tree-inline.c (copy_tree_r): Handle STATEMENT_LIST.

Added:
    branches/gcc-4_5-branch/gcc/testsuite/g++.dg/init/new30.C
Modified:
    branches/gcc-4_5-branch/gcc/ChangeLog
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_5-branch/gcc/tree-inline.c
Comment 14 Jason Merrill 2011-05-05 21:02:53 UTC
Author: jason
Date: Thu May  5 21:02:51 2011
New Revision: 173454

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173454
Log:
	PR c++/40975
	* tree-inline.c (copy_tree_r): Handle STATEMENT_LIST.

Added:
    branches/gcc-4_4-branch/gcc/testsuite/g++.dg/init/new30.C
Modified:
    branches/gcc-4_4-branch/gcc/ChangeLog
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_4-branch/gcc/tree-inline.c
Comment 15 Jason Merrill 2011-05-05 21:06:56 UTC
Fixed in 4.4-4.7 branches.  The patch doesn't fix the bug in the 4.3 branch, so I'm going to leave it unfixed there.