public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/97370] New: comedy of boolean errors for '!a & (b|c)'
@ 2020-10-11 16:35 eggert at cs dot ucla.edu
  2020-10-11 17:01 ` [Bug c/97370] " harald at gigawatt dot nl
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: eggert at cs dot ucla.edu @ 2020-10-11 16:35 UTC (permalink / raw)
  To: gcc-bugs

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); }
      |                                              ^
      |                                              !

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-10-13  3:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-11 16:35 [Bug c/97370] New: comedy of boolean errors for '!a & (b|c)' eggert at cs dot ucla.edu
2020-10-11 17:01 ` [Bug c/97370] " harald at gigawatt dot nl
2020-10-12  7:22 ` eggert at cs dot ucla.edu
2020-10-12 18:16 ` harald at gigawatt dot nl
2020-10-13  3:36 ` eggert at gnu dot org

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).