------- Additional Comments From aoliva at gcc dot gnu dot org 2005-03-06 07:29 ------- Subject: Re: [PR c++/20103] failure to gimplify constructors for addressable types
On Mar 5, 2005, Mark Mitchell <[EMAIL PROTECTED]> wrote: > Alexandre Oliva wrote: >> Testing now. I was a bit surprised that the casts to (const B&) >> weren't reported as faulty, but didn't check the standard on it. > I'd have to check the rules -- but I'm sure it's not a problem with > your patch. Either its correct, or an already-present bug. >> Ok >> to install if testing passes? > Yes. Unfortunately, g++.dg/template/ctor1.C and g++.dg/template/complit1.C regressed. As it turned out, complit1.C was in error, and had to be adjusted to match g++.dg/ext/complit1.C, since the only significant difference between them was that one of them had the compound-literal within a ctor of a template class. ctor1.C, however, exposed a failure to handle TARGET_EXPRs while instantiating templates. So I extended the tsubst machinery to do it, and now ctor1.C passes again. I also noticed that the patch fixes g++.old-deja/g++.oliva/expr2.C, yay! Here's the latest version of the patch, that so far passed make -C gcc all check-g++. Full bootstrap and regtest on x86_64-linux-gnu still pending. Ok to install if it passes? Index: gcc/cp/ChangeLog from Alexandre Oliva <[EMAIL PROTECTED]> PR c++/20103 * semantics.c (finish_compound_literal): Wrap it in a target_expr. * pt.c (tsubst_copy_and_build): Handle TARGET_EXPR. Index: gcc/cp/semantics.c =================================================================== RCS file: /cvs/gcc/gcc/gcc/cp/semantics.c,v retrieving revision 1.463 diff -u -p -r1.463 semantics.c --- gcc/cp/semantics.c 23 Feb 2005 05:30:48 -0000 1.463 +++ gcc/cp/semantics.c 6 Mar 2005 07:12:11 -0000 @@ -1996,7 +1996,9 @@ finish_compound_literal (tree type, tree complete_array_type (type, compound_literal, 1); } - return compound_literal; + /* A compound-literal is an lvalue in C, but is it going to be + in ISO C++? Assume it's an rvalue for now. */ + return get_target_expr (compound_literal); } /* Return the declaration for the function-name variable indicated by Index: gcc/cp/pt.c =================================================================== RCS file: /cvs/gcc/gcc/gcc/cp/pt.c,v retrieving revision 1.978 diff -u -p -r1.978 pt.c --- gcc/cp/pt.c 21 Feb 2005 23:12:26 -0000 1.978 +++ gcc/cp/pt.c 6 Mar 2005 07:12:19 -0000 @@ -8744,6 +8744,26 @@ tsubst_copy_and_build (tree t, return build_throw (RECUR (TREE_OPERAND (t, 0))); + case TARGET_EXPR: + { + tree r = tsubst_copy (t, args, complain, in_decl); + + TREE_TYPE (r) = RECUR (TREE_TYPE (t)); + TARGET_EXPR_SLOT (r) = RECUR (TARGET_EXPR_SLOT (t)); + TARGET_EXPR_INITIAL (r) = RECUR (TARGET_EXPR_INITIAL (t)); + TARGET_EXPR_CLEANUP (r) = RECUR (TARGET_EXPR_CLEANUP (t)); + + if (TREE_TYPE (TARGET_EXPR_SLOT (t)) + == TREE_TYPE (TARGET_EXPR_INITIAL (t))) + TREE_TYPE (TARGET_EXPR_SLOT (r)) = + TREE_TYPE (TARGET_EXPR_INITIAL (r)); + + if (TREE_TYPE (t) == TREE_TYPE (TARGET_EXPR_SLOT (t))) + TREE_TYPE (r) = TREE_TYPE (TARGET_EXPR_SLOT (r)); + + return r; + } + case CONSTRUCTOR: { tree r; Index: gcc/testsuite/ChangeLog from Alexandre Oliva <[EMAIL PROTECTED]> PR c++/20103 * g++.dg/tree-ssa/pr20103.C: New. * g++.dg/template/complit1.C: Expect error like ext/complit1.C. Index: gcc/testsuite/g++.dg/tree-ssa/pr20103.C =================================================================== RCS file: gcc/testsuite/g++.dg/tree-ssa/pr20103.C diff -N gcc/testsuite/g++.dg/tree-ssa/pr20103.C --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ gcc/testsuite/g++.dg/tree-ssa/pr20103.C 6 Mar 2005 07:12:35 -0000 @@ -0,0 +1,59 @@ +// PR c++/20103 + +// { dg-do compile } + +// { dg-options "" } + +// Gimplification used to fail for (B){x}, because create_tmp_var +// required a non-addressable type, and we didn't wrap the constructor +// in a target_expr, ensuring it's moved to a separate decl. + +// Whether it is an lvalue, like in C, or an rvalue, is up to the ISO +// C++ Commitete to decide in the second version of the C++ Standard. +// We're going with rvalues for now. + +struct A +{ + A(const A&); +}; + +struct B +{ + A a; +}; + +void foo(B); +void bar(B&); // { dg-error "in passing argument" } +void bac(const B&); +void bap(const B*); + +void baz(A &x) +{ + foo ((B){x}); + bar ((B){x}); // { dg-error "non-const reference" } + bac ((B){x}); + bap (&(B){x}); // { dg-error "address of temporary" } + + foo ((const B&)(B){x}); + bar ((B&)(B){x}); // { dg-error "invalid cast" } + bac ((const B&)(B){x}); + bap (&(const B&)(B){x}); +} + +template <typename T, typename U> +void baz(T &x) +{ + foo ((U){x}); + bar ((U){x}); // { dg-error "non-const reference" } + bac ((U){x}); + bap (&(U){x}); // { dg-error "address of temporary" } + + foo ((const U&)(U){x}); + bar ((U&)(U){x}); // { dg-error "invalid cast" } + bac ((const U&)(U){x}); + bap (&(const U&)(U){x}); +} + +void bazT(A &x) { + baz<A,B>(x); +} Index: gcc/testsuite/g++.dg/template/complit1.C =================================================================== RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/template/complit1.C,v retrieving revision 1.3 diff -u -p -r1.3 complit1.C --- gcc/testsuite/g++.dg/template/complit1.C 28 Dec 2002 07:48:08 -0000 1.3 +++ gcc/testsuite/g++.dg/template/complit1.C 6 Mar 2005 07:12:35 -0000 @@ -6,6 +6,6 @@ template <int D> struct C { }; template<int D> -C<D>::C() : d((int[]){1,2,3}) {} +C<D>::C() : d((int[]){1,2,3}) {} // { dg-error "assignment of arrays" } -template class C<1>; +template class C<1>; // { dg-error "instantiated from" } Index: gcc/testsuite/g++.old-deja/g++.oliva/ChangeLog from Alexandre Oliva <[EMAIL PROTECTED]> PR c++/20103 * expr2.C: Fixed. Index: gcc/testsuite/g++.old-deja/g++.oliva/expr2.C =================================================================== RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.oliva/expr2.C,v retrieving revision 1.5 diff -u -p -r1.5 expr2.C --- gcc/testsuite/g++.old-deja/g++.oliva/expr2.C 1 May 2003 02:02:47 -0000 1.5 +++ gcc/testsuite/g++.old-deja/g++.oliva/expr2.C 6 Mar 2005 07:12:35 -0000 @@ -1,4 +1,4 @@ -// { dg-do run { xfail *-*-* } } +// { dg-do run } // Copyright (C) 2000 Free Software Foundation -- Alexandre Oliva http://www.ic.unicamp.br/~oliva/ Red Hat Compiler Engineer [EMAIL PROTECTED], gcc.gnu.org} Free Software Evangelist [EMAIL PROTECTED], gnu.org} -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20103