[Bug c/97370] New: comedy of boolean errors for '!a & (b|c)'

eggert at cs dot ucla.edu gcc-bugzilla@gcc.gnu.org
Sun Oct 11 16:35:15 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97370

            Bug ID: 97370
           Summary: comedy of boolean errors for '!a & (b|c)'
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eggert at cs dot ucla.edu
  Target Milestone: ---

I ran into this problem while compiling a proposed patch for GNU grep.

For the following program a.c:

_Bool f (_Bool a, _Bool b, _Bool c) { return !a & (b|c); }
_Bool g (_Bool a, _Bool b, _Bool c) { return !(a) & (b|c); }
_Bool h (_Bool a, _Bool b, _Bool c) { return ~a & (b|c); }
_Bool i  (_Bool a, _Bool b, _Bool c) { return (b|c) & !a; }

The command 'gcc -Wall -S a.c' generates bogus diagnostics for 'f', 'g', and
'h' (see the diagnostics at the end of this comment).

* 'f' is incorrectly diagnosed even though it's the same thing as 'i' after
commuting the operands of '&'. ('i' is correctly allowed.)

* The diagnostic for 'f' suggests 'g', but 'g' produces the same diagnostic.

* The diagnostic for 'f' sugggests 'h', but 'h' produces a different
diagnostic. I understand why 'bool = ~bool' should be diagnosed (bug#77490),
but 'h' should not be diagnosed since 'bool & ~bool' always has the usual
boolean interpretation.

I finally ended up using the equivalent of 'i' in GNU grep, but I should have
been able to use any of 'f', 'g', or 'h' without worrying about generating a
bogus warning.

Here are the bogus diagnostics in question:
----

a.c: In function 'f':
a.c:1:46: warning: suggest parentheses around operand of '!' or change '&' to
'&&' or '!' to '~' [-Wparentheses]
    1 | _Bool f (_Bool a, _Bool b, _Bool c) { return !a & (b|c); }
      |                                              ^~
a.c: In function 'g':
a.c:2:46: warning: suggest parentheses around operand of '!' or change '&' to
'&&' or '!' to '~' [-Wparentheses]
    2 | _Bool g (_Bool a, _Bool b, _Bool c) { return !(a) & (b|c); }
      |                                              ^~~~
a.c: In function 'h':
a.c:3:46: warning: '~' on a boolean expression [-Wbool-operation]
    3 | _Bool h (_Bool a, _Bool b, _Bool c) { return ~a & (b|c); }
      |                                              ^
a.c:3:46: note: did you mean to use logical not?
    3 | _Bool h (_Bool a, _Bool b, _Bool c) { return ~a & (b|c); }
      |                                              ^
      |                                              !


More information about the Gcc-bugs mailing list