* C++ PATCH for c++/91129 - wrong error with binary op in template argument
@ 2019-08-29 19:54 Marek Polacek
2019-09-01 22:04 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2019-08-29 19:54 UTC (permalink / raw)
To: GCC Patches, Jason Merrill
We reject this test with errors like
nontype1.C:22:14: error: taking address of rvalue [-fpermissive]
22 | A<int, N / C<int, 6>{}> a2;
| ^~~~~~~~~~~
that happens because for converting "C<int, 6>{}" to int we generate
"C<int, 6>::operator int (&TARGET_EXPR <D.1234, {}>)". The second
template argument is a binary operator, so while parsing the template
arguments in a function template foo we end up in cp_build_binary_op.
cp_build_binary_op calls, for certain binary ops, fold_non_dependent_expr.
Since <https://gcc.gnu.org/ml/gcc-patches/2018-06/msg01475.html> these
calls are no longer tf_none. fold_non_dependent_expr, when in a template,
will instantiate, which gives the "taking address of rvalue" error.
In this particular case the fix seems to be using fold_for_warn instead,
which in a template is fold_non_dependent_expr with tf_none; all the
fold calls I'm changing in this patch are used for diagnostic purposes
only, and it fixes all the bogus errors.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2019-08-29 Marek Polacek <polacek@redhat.com>
PR c++/91129 - wrong error with binary op in template argument.
* typeck.c (warn_for_null_address): Use fold_for_warn instead of
fold_non_dependent_expr.
(cp_build_binary_op): Likewise.
* g++.dg/cpp1y/nontype1.C: New test.
diff --git gcc/cp/typeck.c gcc/cp/typeck.c
index c09bb309142..31414453524 100644
--- gcc/cp/typeck.c
+++ gcc/cp/typeck.c
@@ -4305,7 +4305,7 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
|| TREE_NO_WARNING (op))
return;
- tree cop = fold_non_dependent_expr (op, complain);
+ tree cop = fold_for_warn (op);
if (TREE_CODE (cop) == ADDR_EXPR
&& decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
@@ -4628,9 +4628,8 @@ cp_build_binary_op (const op_location_t &location,
|| code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
{
enum tree_code tcode0 = code0, tcode1 = code1;
- tree cop1 = fold_non_dependent_expr (op1, complain);
doing_div_or_mod = true;
- warn_for_div_by_zero (location, cop1);
+ warn_for_div_by_zero (location, fold_for_warn (op1));
if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
@@ -4669,11 +4668,8 @@ cp_build_binary_op (const op_location_t &location,
case TRUNC_MOD_EXPR:
case FLOOR_MOD_EXPR:
- {
- tree cop1 = fold_non_dependent_expr (op1, complain);
- doing_div_or_mod = true;
- warn_for_div_by_zero (location, cop1);
- }
+ doing_div_or_mod = true;
+ warn_for_div_by_zero (location, fold_for_warn (op1));
if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
&& TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
@@ -4766,7 +4762,7 @@ cp_build_binary_op (const op_location_t &location,
}
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
- tree const_op1 = fold_non_dependent_expr (op1, complain);
+ tree const_op1 = fold_for_warn (op1);
if (TREE_CODE (const_op1) != INTEGER_CST)
const_op1 = op1;
result_type = type0;
@@ -4812,10 +4808,10 @@ cp_build_binary_op (const op_location_t &location,
}
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
- tree const_op0 = fold_non_dependent_expr (op0, complain);
+ tree const_op0 = fold_for_warn (op0);
if (TREE_CODE (const_op0) != INTEGER_CST)
const_op0 = op0;
- tree const_op1 = fold_non_dependent_expr (op1, complain);
+ tree const_op1 = fold_for_warn (op1);
if (TREE_CODE (const_op1) != INTEGER_CST)
const_op1 = op1;
result_type = type0;
diff --git gcc/testsuite/g++.dg/cpp1y/nontype1.C gcc/testsuite/g++.dg/cpp1y/nontype1.C
new file mode 100644
index 00000000000..a37e996a3ff
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/nontype1.C
@@ -0,0 +1,42 @@
+// PR c++/91129 - wrong error with binary op in template argument.
+// { dg-do compile { target c++14 } }
+
+template<class T, T v>
+struct C
+{
+ constexpr operator T() const { return v; }
+ constexpr auto operator()() const { return v; }
+};
+
+template<class T, int N>
+struct A
+{
+};
+
+template<int N>
+void foo ()
+{
+ A<int, C<int, 6>{}> a0;
+ A<int, !C<int, 6>{}> a1;
+ A<int, N / C<int, 6>{}> a2;
+ A<int, N % C<int, 6>{}> a3;
+ A<int, N * C<int, 6>{}> a4;
+ A<int, N ^ C<int, 6>{}> a5;
+ A<int, N | C<int, 6>{}> a6;
+ A<int, N & C<int, 6>{}> a7;
+ A<int, N + C<int, 6>{}> a8;
+ A<int, N - C<int, 6>{}> a9;
+ A<int, -C<int, 6>{}> a10;
+ A<int, (N >> C<int, 6>{})> a11;
+ A<int, N << C<int, 6>{}> a12;
+ A<int, ~C<int, 6>{}> a13;
+ A<int, N || C<int, 6>{}> a14;
+ A<int, N && C<int, 6>{}> a15;
+ A<int, N == C<int, 6>{}> a16;
+ A<int, N != C<int, 6>{}> a17;
+}
+
+int main()
+{
+ foo<10>();
+}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: C++ PATCH for c++/91129 - wrong error with binary op in template argument
2019-08-29 19:54 C++ PATCH for c++/91129 - wrong error with binary op in template argument Marek Polacek
@ 2019-09-01 22:04 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2019-09-01 22:04 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
On 8/29/19 12:36 PM, Marek Polacek wrote:
> We reject this test with errors like
>
> nontype1.C:22:14: error: taking address of rvalue [-fpermissive]
> 22 | A<int, N / C<int, 6>{}> a2;
> | ^~~~~~~~~~~
>
> that happens because for converting "C<int, 6>{}" to int we generate
> "C<int, 6>::operator int (&TARGET_EXPR <D.1234, {}>)". The second
> template argument is a binary operator, so while parsing the template
> arguments in a function template foo we end up in cp_build_binary_op.
>
> cp_build_binary_op calls, for certain binary ops, fold_non_dependent_expr.
> Since <https://gcc.gnu.org/ml/gcc-patches/2018-06/msg01475.html> these
> calls are no longer tf_none. fold_non_dependent_expr, when in a template,
> will instantiate, which gives the "taking address of rvalue" error.
>
> In this particular case the fix seems to be using fold_for_warn instead,
> which in a template is fold_non_dependent_expr with tf_none; all the
> fold calls I'm changing in this patch are used for diagnostic purposes
> only, and it fixes all the bogus errors.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2019-08-29 Marek Polacek <polacek@redhat.com>
>
> PR c++/91129 - wrong error with binary op in template argument.
> * typeck.c (warn_for_null_address): Use fold_for_warn instead of
> fold_non_dependent_expr.
> (cp_build_binary_op): Likewise.
>
> * g++.dg/cpp1y/nontype1.C: New test.
>
> diff --git gcc/cp/typeck.c gcc/cp/typeck.c
> index c09bb309142..31414453524 100644
> --- gcc/cp/typeck.c
> +++ gcc/cp/typeck.c
> @@ -4305,7 +4305,7 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
> || TREE_NO_WARNING (op))
> return;
>
> - tree cop = fold_non_dependent_expr (op, complain);
> + tree cop = fold_for_warn (op);
>
> if (TREE_CODE (cop) == ADDR_EXPR
> && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
> @@ -4628,9 +4628,8 @@ cp_build_binary_op (const op_location_t &location,
> || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
> {
> enum tree_code tcode0 = code0, tcode1 = code1;
> - tree cop1 = fold_non_dependent_expr (op1, complain);
> doing_div_or_mod = true;
> - warn_for_div_by_zero (location, cop1);
> + warn_for_div_by_zero (location, fold_for_warn (op1));
>
> if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
> tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
> @@ -4669,11 +4668,8 @@ cp_build_binary_op (const op_location_t &location,
>
> case TRUNC_MOD_EXPR:
> case FLOOR_MOD_EXPR:
> - {
> - tree cop1 = fold_non_dependent_expr (op1, complain);
> - doing_div_or_mod = true;
> - warn_for_div_by_zero (location, cop1);
> - }
> + doing_div_or_mod = true;
> + warn_for_div_by_zero (location, fold_for_warn (op1));
>
> if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
> && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
> @@ -4766,7 +4762,7 @@ cp_build_binary_op (const op_location_t &location,
> }
> else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
> {
> - tree const_op1 = fold_non_dependent_expr (op1, complain);
> + tree const_op1 = fold_for_warn (op1);
> if (TREE_CODE (const_op1) != INTEGER_CST)
> const_op1 = op1;
> result_type = type0;
> @@ -4812,10 +4808,10 @@ cp_build_binary_op (const op_location_t &location,
> }
> else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
> {
> - tree const_op0 = fold_non_dependent_expr (op0, complain);
> + tree const_op0 = fold_for_warn (op0);
> if (TREE_CODE (const_op0) != INTEGER_CST)
> const_op0 = op0;
> - tree const_op1 = fold_non_dependent_expr (op1, complain);
> + tree const_op1 = fold_for_warn (op1);
> if (TREE_CODE (const_op1) != INTEGER_CST)
> const_op1 = op1;
> result_type = type0;
> diff --git gcc/testsuite/g++.dg/cpp1y/nontype1.C gcc/testsuite/g++.dg/cpp1y/nontype1.C
> new file mode 100644
> index 00000000000..a37e996a3ff
> --- /dev/null
> +++ gcc/testsuite/g++.dg/cpp1y/nontype1.C
> @@ -0,0 +1,42 @@
> +// PR c++/91129 - wrong error with binary op in template argument.
> +// { dg-do compile { target c++14 } }
> +
> +template<class T, T v>
> +struct C
> +{
> + constexpr operator T() const { return v; }
> + constexpr auto operator()() const { return v; }
> +};
> +
> +template<class T, int N>
> +struct A
> +{
> +};
> +
> +template<int N>
> +void foo ()
> +{
> + A<int, C<int, 6>{}> a0;
> + A<int, !C<int, 6>{}> a1;
> + A<int, N / C<int, 6>{}> a2;
> + A<int, N % C<int, 6>{}> a3;
> + A<int, N * C<int, 6>{}> a4;
> + A<int, N ^ C<int, 6>{}> a5;
> + A<int, N | C<int, 6>{}> a6;
> + A<int, N & C<int, 6>{}> a7;
> + A<int, N + C<int, 6>{}> a8;
> + A<int, N - C<int, 6>{}> a9;
> + A<int, -C<int, 6>{}> a10;
> + A<int, (N >> C<int, 6>{})> a11;
> + A<int, N << C<int, 6>{}> a12;
> + A<int, ~C<int, 6>{}> a13;
> + A<int, N || C<int, 6>{}> a14;
> + A<int, N && C<int, 6>{}> a15;
> + A<int, N == C<int, 6>{}> a16;
> + A<int, N != C<int, 6>{}> a17;
> +}
> +
> +int main()
> +{
> + foo<10>();
> +}
>
OK.
Jason
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-09-01 22:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-29 19:54 C++ PATCH for c++/91129 - wrong error with binary op in template argument Marek Polacek
2019-09-01 22:04 ` Jason Merrill
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).