c++: Fix bogus -Wparentheses warning with fold-expression [PR94505]

We issue bogus -Wparentheses warnings (3 of them!) for this fold expression:

  ((B && true) || ...)

Firstly, issuing a warning for a compiler-generated expression is wrong
and secondly, B && true must be wrapped in ( ) otherwise you'll get
error: binary expression in operand of fold-expression.

	PR c++/94505 - bogus -Wparentheses warning with fold-expression.
	* pt.c (fold_expression): Add warning_sentinel for -Wparentheses
	before calling build_x_binary_op.

	* g++.dg/cpp1z/fold11.C: New test.
This commit is contained in:
Marek Polacek 2020-04-19 12:12:01 -04:00
parent c5e4be6b36
commit 5bdd4c5d3f
4 changed files with 24 additions and 0 deletions

View File

@ -1,3 +1,9 @@
2020-04-20 Marek Polacek <polacek@redhat.com>
PR c++/94505 - bogus -Wparentheses warning with fold-expression.
* pt.c (fold_expression): Add warning_sentinel for -Wparentheses
before calling build_x_binary_op.
2020-04-20 Marek Polacek <polacek@redhat.com>
* coroutines.cc (captures_temporary): Don't assign the result of

View File

@ -12379,6 +12379,7 @@ fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
if (FOLD_EXPR_MODIFY_P (t))
return build_x_modify_expr (input_location, left, code, right, complain);
warning_sentinel s(warn_parentheses);
switch (code)
{
case COMPOUND_EXPR:

View File

@ -1,3 +1,8 @@
2020-04-20 Marek Polacek <polacek@redhat.com>
PR c++/94505 - bogus -Wparentheses warning with fold-expression.
* g++.dg/cpp1z/fold11.C: New test.
2020-04-20 Andreas Krebbel <krebbel@linux.ibm.com>
* g++.dg/pr94666.C: New test.

View File

@ -0,0 +1,12 @@
// PR c++/94505 - bogus -Wparentheses warning with fold-expression.
// { dg-do compile { target c++17 } }
// { dg-options "-Wparentheses" }
template <bool... B>
bool foo () {
return ((B && true) || ...); // { dg-bogus "suggest parentheses" }
}
int main () {
foo<true, false, false, true> ();
}