c++: value category of compound object expr [PR104173]
Commit Message
Here the call to the &&-qualified toLower() is incorrectly rejected
because the object expression is deemed to be an lvalue, even though it's
really a prvalue. The object expression, instance()->applicationName(),
is expressed as an INDIRECT_REF of a COMPOUND_EXPR
*(*instance ();, &TARGET_EXPR <D.2383, QCoreApplication::applicationName ()>;);
which lvalue_kind categorizes as an lvalue.
This issue seems similar to PR88103 except that here the compound object
expression is a prvalue rather than an xvalue. The fix there was to
adjust the result of unary_complex_lvalue in build_class_member_access_expr
so that xvalue-ness is preserved. This patch extends the fix so that
rvalue-ness is preserved more generally.
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
PR c++/104173
gcc/cp/ChangeLog:
* typeck.cc (build_class_member_access_expr): Extend
unary_complex_lvalue result adjustment to preserve all
rvalues, not just xvalues.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/ref-qual21.C: New test.
---
gcc/cp/typeck.cc | 19 ++++++++-----------
gcc/testsuite/g++.dg/cpp0x/ref-qual21.C | 16 ++++++++++++++++
2 files changed, 24 insertions(+), 11 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/ref-qual21.C
Comments
On 1/22/22 16:24, Patrick Palka wrote:
> Here the call to the &&-qualified toLower() is incorrectly rejected
> because the object expression is deemed to be an lvalue, even though it's
> really a prvalue. The object expression, instance()->applicationName(),
> is expressed as an INDIRECT_REF of a COMPOUND_EXPR
>
> *(*instance ();, &TARGET_EXPR <D.2383, QCoreApplication::applicationName ()>;);
>
> which lvalue_kind categorizes as an lvalue.
>
> This issue seems similar to PR88103 except that here the compound object
> expression is a prvalue rather than an xvalue. The fix there was to
> adjust the result of unary_complex_lvalue in build_class_member_access_expr
> so that xvalue-ness is preserved. This patch extends the fix so that
> rvalue-ness is preserved more generally.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
OK.
> PR c++/104173
>
> gcc/cp/ChangeLog:
>
> * typeck.cc (build_class_member_access_expr): Extend
> unary_complex_lvalue result adjustment to preserve all
> rvalues, not just xvalues.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/ref-qual21.C: New test.
> ---
> gcc/cp/typeck.cc | 19 ++++++++-----------
> gcc/testsuite/g++.dg/cpp0x/ref-qual21.C | 16 ++++++++++++++++
> 2 files changed, 24 insertions(+), 11 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/ref-qual21.C
>
> diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
> index 3a28d639cb1..59668203573 100644
> --- a/gcc/cp/typeck.cc
> +++ b/gcc/cp/typeck.cc
> @@ -2726,17 +2726,14 @@ build_class_member_access_expr (cp_expr object, tree member,
> /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
> `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
> in the front end; only _DECLs and _REFs are lvalues in the back end. */
> - {
> - tree temp = unary_complex_lvalue (ADDR_EXPR, object);
> - if (temp)
> - {
> - temp = cp_build_fold_indirect_ref (temp);
> - if (xvalue_p (object) && !xvalue_p (temp))
> - /* Preserve xvalue kind. */
> - temp = move (temp);
> - object = temp;
> - }
> - }
> + if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
> + {
> + temp = cp_build_fold_indirect_ref (temp);
> + if (!lvalue_p (object) && lvalue_p (temp))
> + /* Preserve rvalue-ness. */
> + temp = move (temp);
> + object = temp;
> + }
>
> /* In [expr.ref], there is an explicit list of the valid choices for
> MEMBER. We check for each of those cases here. */
> diff --git a/gcc/testsuite/g++.dg/cpp0x/ref-qual21.C b/gcc/testsuite/g++.dg/cpp0x/ref-qual21.C
> new file mode 100644
> index 00000000000..e2c43c345ab
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/ref-qual21.C
> @@ -0,0 +1,16 @@
> +// PR c++/104173
> +// { dg-do compile { target c++11 } }
> +
> +struct QString {
> + QString toLower() &&;
> +};
> +
> +struct QCoreApplication {
> + static QString applicationName();
> +};
> +
> +QCoreApplication *instance();
> +
> +int main() {
> + instance()->applicationName().toLower();
> +}
@@ -2726,17 +2726,14 @@ build_class_member_access_expr (cp_expr object, tree member,
/* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
`(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
in the front end; only _DECLs and _REFs are lvalues in the back end. */
- {
- tree temp = unary_complex_lvalue (ADDR_EXPR, object);
- if (temp)
- {
- temp = cp_build_fold_indirect_ref (temp);
- if (xvalue_p (object) && !xvalue_p (temp))
- /* Preserve xvalue kind. */
- temp = move (temp);
- object = temp;
- }
- }
+ if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
+ {
+ temp = cp_build_fold_indirect_ref (temp);
+ if (!lvalue_p (object) && lvalue_p (temp))
+ /* Preserve rvalue-ness. */
+ temp = move (temp);
+ object = temp;
+ }
/* In [expr.ref], there is an explicit list of the valid choices for
MEMBER. We check for each of those cases here. */
new file mode 100644
@@ -0,0 +1,16 @@
+// PR c++/104173
+// { dg-do compile { target c++11 } }
+
+struct QString {
+ QString toLower() &&;
+};
+
+struct QCoreApplication {
+ static QString applicationName();
+};
+
+QCoreApplication *instance();
+
+int main() {
+ instance()->applicationName().toLower();
+}