public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "coenraad at wish dot org.za" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/104948] When '&&' present in a comparison, a warning should be generated
Date: Wed, 16 Mar 2022 11:20:06 +0000	[thread overview]
Message-ID: <bug-104948-4-WDd0Luwx0n@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104948-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #8 from dagelf <coenraad at wish dot org.za> ---
Makes perfect sense now. && is "logical" in that it can only produce a bool,
which in C is an int and anything except 0 or 1 is evaluated to false at
compile time. 

There was a time when 'logical' and 'bitwise' were used interchangeably, based
on the fact that 'boolean operators' work on 'boolean logic'. 

This is what lead me here:

$ cat test.c
int f(int a) {
  if ((a && 12) == 12 ) 
     return 11;
  return 10;
}

$ gcc -c -O0 test.c && objdump -d test1.o
test1.o:     file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <f>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   b8 00 00 00 00          mov    $0xa,%eax
   c:   5d                      pop    %rbp
   d:   c3                      retq   

With a single `&` it works as expected. 

In my defence, when I last did a C course all boolean operators were bitwise. I
suddenly feel really old that even C has changed. Even the definition of
'logical' and 'bitwise' has changed. 

Apologies for not testing the obvious '-Wall'. 

Also apologies for just skimming over the output of icc, clang and msvc... I
just noticed that they include jumps where gcc didn't, so I was mistaken. 

The optimizations are impressive.

Still, searching for the issues logged here with '&&' in an evaluation, does
point to the fact that the error message could be improved. Might I recommend
'non-bitwise boolean' in the message instead of just 'boolean'. Or even better,
add '(did you mean bitwise AND & instead of &&) if that's present.

$ gcc -Wall -c -O0  test.c 
test1.c: In function ‘f’:
test1.c:5:22: warning: comparison of constant ‘12’ with boolean expression is
always false (Did you mean & instead of &&?) [-Wbool-compare] 

Compare to "warning: comparison of constant ‘12’ with non-bitwise boolean
expression is always false [-Wbool-compare]" might lead to less confusion.

When expecting the result of an '&&' evaluation to be a bitwise AND, this
distinction can make a world of difference and could've pointed at least me in
the right direction.

  parent reply	other threads:[~2022-03-16 11:20 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-16  8:42 [Bug c/104948] New: " coenraad at wish dot org.za
2022-03-16  8:44 ` [Bug c/104948] " coenraad at wish dot org.za
2022-03-16  8:51 ` pinskia at gcc dot gnu.org
2022-03-16  9:07 ` coenraad at wish dot org.za
2022-03-16  9:10 ` pinskia at gcc dot gnu.org
2022-03-16  9:10 ` pinskia at gcc dot gnu.org
2022-03-16  9:13 ` rguenth at gcc dot gnu.org
2022-03-16  9:33 ` redi at gcc dot gnu.org
2022-03-16 11:20 ` coenraad at wish dot org.za [this message]
2022-03-16 12:34 ` redi at gcc dot gnu.org
2022-03-16 12:40 ` jakub at gcc dot gnu.org
2022-03-16 12:56 ` schwab@linux-m68k.org
2022-03-16 13:04 ` redi at gcc dot gnu.org
2022-03-17  9:03 ` coenraad at wish dot org.za
2022-03-17 10:43 ` [Bug c/104948] When '&&' has non bool parameters, a better " redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104948-4-WDd0Luwx0n@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).