public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <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 12:34:57 +0000	[thread overview]
Message-ID: <bug-104948-4-GBhEdz7w2x@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 #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to dagelf from comment #8)
> 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. 

No, in C bool is a distinct data type, and sizeof(bool) == 1.

Values of that type other than 0 or 1 result in undefined behaviour.


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

This will never be true.

The result of (a && 12) is either 0 or 1, and so never equal to 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. 

Your expectation is wrong.

> 
> In my defence, when I last did a C course all boolean operators were
> bitwise.

I doubt that is true.


> I suddenly feel really old that even C has changed. Even the
> definition of 'logical' and 'bitwise' has changed. 

I don't think that's true either.


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

It would confuse people who know C, because "non-bitwise boolean expression" is
meaningless.

> When expecting the result of an '&&' evaluation to be a bitwise AND,

Your expectation is simply wrong, that's not how C works. We can't write
diagnostics to suit every potential misunderstanding of how C works.

The warning text is accurate and correct.

  parent reply	other threads:[~2022-03-16 12:34 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
2022-03-16 12:34 ` redi at gcc dot gnu.org [this message]
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-GBhEdz7w2x@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).