Bug 94505 - Logical operator inside fold-expression: strange warning
Summary: Logical operator inside fold-expression: strange warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2020-04-06 18:57 UTC by Frank Winklmeier
Modified: 2020-04-20 19:46 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-04-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Frank Winklmeier 2020-04-06 18:57:27 UTC
This was first reported in [1] but I could not find any bug report. The following code compiled with `gcc -Wall -std=c++17`:

template <bool... B>
bool foo () {
    return ((B && true) || ...);
}

int main () {
    foo<true, false, false, true> ();
} 

produces this warning:

t.cxx:7:38:   required from here
t.cxx:3:18: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
       return ((B && true) || ...);
               ~~~^~~~~~~~
t.cxx:3:18: warning: suggest parentheses around '&&' within '||' [-Wparentheses]

Reproducible with any recent gcc version including gcc-trunk-20200406 (on godbolt).


[1] http://gcc.1065356.n8.nabble.com/Logical-operator-inside-fold-expression-strange-warning-td1627740.html
Comment 1 GCC Commits 2020-04-20 19:35:40 UTC
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:5bdd4c5d3fc9c143e8edea3b10828e4b75d7a385

commit r10-7830-g5bdd4c5d3fc9c143e8edea3b10828e4b75d7a385
Author: Marek Polacek <polacek@redhat.com>
Date:   Sun Apr 19 12:12:01 2020 -0400

    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.
Comment 2 GCC Commits 2020-04-20 19:45:03 UTC
The releases/gcc-9 branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:83eeda5f004c3b9cbeccd3da1c3fe58b3015e55f

commit r9-8518-g83eeda5f004c3b9cbeccd3da1c3fe58b3015e55f
Author: Marek Polacek <polacek@redhat.com>
Date:   Mon Apr 20 15:43:49 2020 -0400

    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.
Comment 3 Marek Polacek 2020-04-20 19:46:28 UTC
Fixed in GCC 9.4 and 10.