* C++ PATCH for c++/86184, rejects-valid with ?: and omitted operand
@ 2018-06-21 18:22 Marek Polacek
2018-06-27 13:05 ` Marek Polacek
2018-06-27 21:47 ` Jason Merrill
0 siblings, 2 replies; 4+ messages in thread
From: Marek Polacek @ 2018-06-21 18:22 UTC (permalink / raw)
To: GCC Patches, Jason Merrill
The following testcase is rejected because, for this line:
bool b = X() ?: false;
arg2 is missing and arg1 is a TARGET_EXPR. A TARGET_EXPR is a class
prvalue so we wrap it in a SAVE_EXPR. Later when building 'this' we
call build_this (SAVE_EXPR <TARGET_EXPR <...>>) which triggers lvalue_error:
5856 cp_lvalue_kind kind = lvalue_kind (arg);
5857 if (kind == clk_none)
5858 {
5859 if (complain & tf_error)
5860 lvalue_error (input_location, lv_addressof);
because all SAVE_EXPRs are non-lvalue.
Since
a) cp_build_addr_expr_1 can process xvalues and class prvalues,
b) TARGET_EXPRs are only evaluated once (gimplify_target_expr),
I thought we could do the following. The testcase ensures that
with the omitted operand we only construct X once.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2018-06-21 Marek Polacek <polacek@redhat.com>
PR c++/86184
* call.c (build_conditional_expr_1): Don't wrap TARGET_EXPRs
in a SAVE_EXPR.
* g++.dg/ext/cond3.C: New test.
--- gcc/cp/call.c
+++ gcc/cp/call.c
@@ -4806,6 +4806,10 @@ build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
/* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
if (lvalue_p (arg1))
arg2 = arg1 = cp_stabilize_reference (arg1);
+ else if (TREE_CODE (arg1) == TARGET_EXPR)
+ /* TARGET_EXPRs are only expanded once, don't wrap it in a SAVE_EXPR,
+ rendering it clk_none of clk_class. */
+ arg2 = arg1;
else
arg2 = arg1 = cp_save_expr (arg1);
}
--- gcc/testsuite/g++.dg/ext/cond3.C
+++ gcc/testsuite/g++.dg/ext/cond3.C
@@ -0,0 +1,20 @@
+// PR c++/86184
+// { dg-do run }
+// { dg-options "" }
+
+int j;
+struct X {
+ X() { j++; }
+ operator bool() { return true; }
+};
+
+/* Only create X once. */
+bool b = X() ?: false;
+bool b2 = X() ? X() : false;
+
+int
+main ()
+{
+ if (j != 3)
+ __builtin_abort ();
+}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: C++ PATCH for c++/86184, rejects-valid with ?: and omitted operand
2018-06-21 18:22 C++ PATCH for c++/86184, rejects-valid with ?: and omitted operand Marek Polacek
@ 2018-06-27 13:05 ` Marek Polacek
2018-06-27 21:47 ` Jason Merrill
1 sibling, 0 replies; 4+ messages in thread
From: Marek Polacek @ 2018-06-27 13:05 UTC (permalink / raw)
To: GCC Patches, Jason Merrill
Ping.
On Thu, Jun 21, 2018 at 02:22:31PM -0400, Marek Polacek wrote:
> The following testcase is rejected because, for this line:
>
> bool b = X() ?: false;
>
> arg2 is missing and arg1 is a TARGET_EXPR. A TARGET_EXPR is a class
> prvalue so we wrap it in a SAVE_EXPR. Later when building 'this' we
> call build_this (SAVE_EXPR <TARGET_EXPR <...>>) which triggers lvalue_error:
> 5856 cp_lvalue_kind kind = lvalue_kind (arg);
> 5857 if (kind == clk_none)
> 5858 {
> 5859 if (complain & tf_error)
> 5860 lvalue_error (input_location, lv_addressof);
> because all SAVE_EXPRs are non-lvalue.
>
> Since
> a) cp_build_addr_expr_1 can process xvalues and class prvalues,
> b) TARGET_EXPRs are only evaluated once (gimplify_target_expr),
> I thought we could do the following. The testcase ensures that
> with the omitted operand we only construct X once.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2018-06-21 Marek Polacek <polacek@redhat.com>
>
> PR c++/86184
> * call.c (build_conditional_expr_1): Don't wrap TARGET_EXPRs
> in a SAVE_EXPR.
>
> * g++.dg/ext/cond3.C: New test.
>
> --- gcc/cp/call.c
> +++ gcc/cp/call.c
> @@ -4806,6 +4806,10 @@ build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
> /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
> if (lvalue_p (arg1))
> arg2 = arg1 = cp_stabilize_reference (arg1);
> + else if (TREE_CODE (arg1) == TARGET_EXPR)
> + /* TARGET_EXPRs are only expanded once, don't wrap it in a SAVE_EXPR,
> + rendering it clk_none of clk_class. */
> + arg2 = arg1;
> else
> arg2 = arg1 = cp_save_expr (arg1);
> }
> --- gcc/testsuite/g++.dg/ext/cond3.C
> +++ gcc/testsuite/g++.dg/ext/cond3.C
> @@ -0,0 +1,20 @@
> +// PR c++/86184
> +// { dg-do run }
> +// { dg-options "" }
> +
> +int j;
> +struct X {
> + X() { j++; }
> + operator bool() { return true; }
> +};
> +
> +/* Only create X once. */
> +bool b = X() ?: false;
> +bool b2 = X() ? X() : false;
> +
> +int
> +main ()
> +{
> + if (j != 3)
> + __builtin_abort ();
> +}
Marek
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: C++ PATCH for c++/86184, rejects-valid with ?: and omitted operand
2018-06-21 18:22 C++ PATCH for c++/86184, rejects-valid with ?: and omitted operand Marek Polacek
2018-06-27 13:05 ` Marek Polacek
@ 2018-06-27 21:47 ` Jason Merrill
2018-06-29 15:25 ` Marek Polacek
1 sibling, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2018-06-27 21:47 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches
On Thu, Jun 21, 2018 at 2:22 PM, Marek Polacek <polacek@redhat.com> wrote:
> The following testcase is rejected because, for this line:
>
> bool b = X() ?: false;
>
> arg2 is missing and arg1 is a TARGET_EXPR. A TARGET_EXPR is a class
> prvalue so we wrap it in a SAVE_EXPR. Later when building 'this' we
> call build_this (SAVE_EXPR <TARGET_EXPR <...>>) which triggers lvalue_error:
> 5856 cp_lvalue_kind kind = lvalue_kind (arg);
> 5857 if (kind == clk_none)
> 5858 {
> 5859 if (complain & tf_error)
> 5860 lvalue_error (input_location, lv_addressof);
> because all SAVE_EXPRs are non-lvalue.
>
> Since
> a) cp_build_addr_expr_1 can process xvalues and class prvalues,
> b) TARGET_EXPRs are only evaluated once (gimplify_target_expr),
> I thought we could do the following. The testcase ensures that
> with the omitted operand we only construct X once.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2018-06-21 Marek Polacek <polacek@redhat.com>
>
> PR c++/86184
> * call.c (build_conditional_expr_1): Don't wrap TARGET_EXPRs
> in a SAVE_EXPR.
>
> * g++.dg/ext/cond3.C: New test.
>
> --- gcc/cp/call.c
> +++ gcc/cp/call.c
> @@ -4806,6 +4806,10 @@ build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
> /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
> if (lvalue_p (arg1))
> arg2 = arg1 = cp_stabilize_reference (arg1);
> + else if (TREE_CODE (arg1) == TARGET_EXPR)
> + /* TARGET_EXPRs are only expanded once, don't wrap it in a SAVE_EXPR,
> + rendering it clk_none of clk_class. */
> + arg2 = arg1;
> else
> arg2 = arg1 = cp_save_expr (arg1);
How about adding the special handling in cp_save_expr rather than
here, so other callers also benefit?
OK with that change.
Jason
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: C++ PATCH for c++/86184, rejects-valid with ?: and omitted operand
2018-06-27 21:47 ` Jason Merrill
@ 2018-06-29 15:25 ` Marek Polacek
0 siblings, 0 replies; 4+ messages in thread
From: Marek Polacek @ 2018-06-29 15:25 UTC (permalink / raw)
To: Jason Merrill; +Cc: GCC Patches
On Wed, Jun 27, 2018 at 05:47:25PM -0400, Jason Merrill wrote:
> On Thu, Jun 21, 2018 at 2:22 PM, Marek Polacek <polacek@redhat.com> wrote:
> > The following testcase is rejected because, for this line:
> >
> > bool b = X() ?: false;
> >
> > arg2 is missing and arg1 is a TARGET_EXPR. A TARGET_EXPR is a class
> > prvalue so we wrap it in a SAVE_EXPR. Later when building 'this' we
> > call build_this (SAVE_EXPR <TARGET_EXPR <...>>) which triggers lvalue_error:
> > 5856 cp_lvalue_kind kind = lvalue_kind (arg);
> > 5857 if (kind == clk_none)
> > 5858 {
> > 5859 if (complain & tf_error)
> > 5860 lvalue_error (input_location, lv_addressof);
> > because all SAVE_EXPRs are non-lvalue.
> >
> > Since
> > a) cp_build_addr_expr_1 can process xvalues and class prvalues,
> > b) TARGET_EXPRs are only evaluated once (gimplify_target_expr),
> > I thought we could do the following. The testcase ensures that
> > with the omitted operand we only construct X once.
> >
> > Bootstrapped/regtested on x86_64-linux, ok for trunk?
> >
> > 2018-06-21 Marek Polacek <polacek@redhat.com>
> >
> > PR c++/86184
> > * call.c (build_conditional_expr_1): Don't wrap TARGET_EXPRs
> > in a SAVE_EXPR.
> >
> > * g++.dg/ext/cond3.C: New test.
> >
> > --- gcc/cp/call.c
> > +++ gcc/cp/call.c
> > @@ -4806,6 +4806,10 @@ build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
> > /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
> > if (lvalue_p (arg1))
> > arg2 = arg1 = cp_stabilize_reference (arg1);
> > + else if (TREE_CODE (arg1) == TARGET_EXPR)
> > + /* TARGET_EXPRs are only expanded once, don't wrap it in a SAVE_EXPR,
> > + rendering it clk_none of clk_class. */
> > + arg2 = arg1;
> > else
> > arg2 = arg1 = cp_save_expr (arg1);
>
> How about adding the special handling in cp_save_expr rather than
> here, so other callers also benefit?
>
> OK with that change.
Thanks, this is what I'll commit (bootstrap/regtest passed on x86_64):
2018-06-29 Marek Polacek <polacek@redhat.com>
PR c++/86184
* tree.c (cp_save_expr): Don't call save_expr for TARGET_EXPRs.
* g++.dg/ext/cond3.C: New test.
diff --git gcc/cp/tree.c gcc/cp/tree.c
index e7bd79b6276..361248d4b52 100644
--- gcc/cp/tree.c
+++ gcc/cp/tree.c
@@ -4918,6 +4918,11 @@ cp_save_expr (tree expr)
tree codes. */
if (processing_template_decl)
return expr;
+
+ /* TARGET_EXPRs are only expanded once. */
+ if (TREE_CODE (expr) == TARGET_EXPR)
+ return expr;
+
return save_expr (expr);
}
diff --git gcc/testsuite/g++.dg/ext/cond3.C gcc/testsuite/g++.dg/ext/cond3.C
index e69de29bb2d..6390dc4270b 100644
--- gcc/testsuite/g++.dg/ext/cond3.C
+++ gcc/testsuite/g++.dg/ext/cond3.C
@@ -0,0 +1,20 @@
+// PR c++/86184
+// { dg-do run }
+// { dg-options "" }
+
+int j;
+struct X {
+ X() { j++; }
+ operator bool() { return true; }
+};
+
+/* Only create X once. */
+bool b = X() ?: false;
+bool b2 = X() ? X() : false;
+
+int
+main ()
+{
+ if (j != 3)
+ __builtin_abort ();
+}
Marek
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-06-29 15:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-21 18:22 C++ PATCH for c++/86184, rejects-valid with ?: and omitted operand Marek Polacek
2018-06-27 13:05 ` Marek Polacek
2018-06-27 21:47 ` Jason Merrill
2018-06-29 15:25 ` Marek Polacek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).