public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106371] New: Bogus narrowing conversion reported due to bitfield
@ 2022-07-20 14:15 barry.revzin at gmail dot com
  2022-07-20 15:21 ` [Bug c++/106371] " mpolacek at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: barry.revzin at gmail dot com @ 2022-07-20 14:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106371
           Summary: Bogus narrowing conversion reported due to bitfield
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Consider:

#include <cstdint>

struct A {
  uint64_t x : 32;
};

struct B {
  uint32_t x;
};

void f() {
    auto a = A{.x = 1};
    auto b = B{.x = a.x};
}

gcc currently emits a narrowing warning here:

<source>:13:24: warning: narrowing conversion of '(long unsigned int)a.A::x'
from 'long unsigned int' to 'uint32_t' {aka 'unsigned int'} [-Wnarrowing]
   13 |     auto b = B{ .x = a.x };
      |                      ~~^

It is true that a.x is a uint64_t, but also it's only a 32-bit bitfield, there
isn't actually any narrowing possible. This code should be fine. 

clang doesn't warn here (only if A::x were actually a uint64_t, or the width of
the bitfield was larger than 32).

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

* [Bug c++/106371] Bogus narrowing conversion reported due to bitfield
  2022-07-20 14:15 [Bug c++/106371] New: Bogus narrowing conversion reported due to bitfield barry.revzin at gmail dot com
@ 2022-07-20 15:21 ` mpolacek at gcc dot gnu.org
  2022-07-20 15:24 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-07-20 15:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
May be a missing unlowered_expr_type call.

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

* [Bug c++/106371] Bogus narrowing conversion reported due to bitfield
  2022-07-20 14:15 [Bug c++/106371] New: Bogus narrowing conversion reported due to bitfield barry.revzin at gmail dot com
  2022-07-20 15:21 ` [Bug c++/106371] " mpolacek at gcc dot gnu.org
@ 2022-07-20 15:24 ` pinskia at gcc dot gnu.org
  2022-07-23 10:30 ` egallager at gcc dot gnu.org
  2023-12-19 21:51 ` leni536 at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-20 15:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
MSVC produces an error though:
<source>(14): error C2397: conversion from 'unsigned __int64' to 'unsigned int'
requires a narrowing conversion

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

* [Bug c++/106371] Bogus narrowing conversion reported due to bitfield
  2022-07-20 14:15 [Bug c++/106371] New: Bogus narrowing conversion reported due to bitfield barry.revzin at gmail dot com
  2022-07-20 15:21 ` [Bug c++/106371] " mpolacek at gcc dot gnu.org
  2022-07-20 15:24 ` pinskia at gcc dot gnu.org
@ 2022-07-23 10:30 ` egallager at gcc dot gnu.org
  2023-12-19 21:51 ` leni536 at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-07-23 10:30 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=39170

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
Hm I was thinking that this might also get a warning from -Wconversion due to
bug 39170, but apparently it doesn't...

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

* [Bug c++/106371] Bogus narrowing conversion reported due to bitfield
  2022-07-20 14:15 [Bug c++/106371] New: Bogus narrowing conversion reported due to bitfield barry.revzin at gmail dot com
                   ` (2 preceding siblings ...)
  2022-07-23 10:30 ` egallager at gcc dot gnu.org
@ 2023-12-19 21:51 ` leni536 at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: leni536 at gmail dot com @ 2023-12-19 21:51 UTC (permalink / raw)
  To: gcc-bugs

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

Lénárd Szolnoki <leni536 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |leni536 at gmail dot com

--- Comment #4 from Lénárd Szolnoki <leni536 at gmail dot com> ---
Rejects valid code when -pedantic-errors is added:

struct S {
    long l : 8;
};

int main() {
    S s;
    int i = {s.l};
}

https://godbolt.org/z/5bq6cMaMx

Produces wrong code when it's used as a constraint:

struct S {
    long l : 8;
};

template <typename T>
requires requires (T t) {
    int{t.l};
}
void foo(T); // 1

void foo(...); // 2

int main() {
    foo(S{}); // calls 2
}

https://godbolt.org/z/We4M1MP1M

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

end of thread, other threads:[~2023-12-19 21:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-20 14:15 [Bug c++/106371] New: Bogus narrowing conversion reported due to bitfield barry.revzin at gmail dot com
2022-07-20 15:21 ` [Bug c++/106371] " mpolacek at gcc dot gnu.org
2022-07-20 15:24 ` pinskia at gcc dot gnu.org
2022-07-23 10:30 ` egallager at gcc dot gnu.org
2023-12-19 21:51 ` leni536 at gmail dot com

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