public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111895] New: error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!='
@ 2023-10-20 14:05 nabijaczleweli at nabijaczleweli dot xyz
  2023-10-20 15:19 ` [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: nabijaczleweli at nabijaczleweli dot xyz @ 2023-10-20 14:05 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111895
           Summary: error: invalid operands of types 'unsigned char:2' and
                    'int' to binary 'operator!='
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nabijaczleweli at nabijaczleweli dot xyz
  Target Milestone: ---

Simplified from other code:
    #include <cstdint>

    enum class o_field : std::uint8_t { no, yes, different_from_s };
    struct fields {
      o_field o : 2;
    };

    bool func(fields f) { return static_cast<bool>(f.o); }
yields
    main.cpp: In function 'bool func(fields)':
    main.cpp:8:50: error: invalid operands of types 'unsigned char:2' and 'int'
to binary 'operator!='
        8 | bool func(fields f) { return static_cast<bool>(f.o); }
          |                                                ~~^

Which is nonsensical. Clang accepts this, and does the expected thing.

Repros on "g++ (GCC) 13.1.0", "g++ (Debian 12.2.0-14) 12.2.0", and "g++ (Debian
13.2.0-5) 13.2.0".

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

* [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool
  2023-10-20 14:05 [Bug c++/111895] New: error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' nabijaczleweli at nabijaczleweli dot xyz
@ 2023-10-20 15:19 ` redi at gcc dot gnu.org
  2023-10-20 22:08 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2023-10-20 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-10-20
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |rejects-valid

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced:

    enum class o_field : unsigned char { no, yes, different_from_s };
    struct fields {
      o_field o : 2;
    };

    bool func(fields f) { return static_cast<bool>(f.o); }

EDG also accepts this.

GCC fails with the same error since 4.8.1 (and before that we didn't allow the
non-integral bit-field so failed for a different reason).

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

* [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool
  2023-10-20 14:05 [Bug c++/111895] New: error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' nabijaczleweli at nabijaczleweli dot xyz
  2023-10-20 15:19 ` [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool redi at gcc dot gnu.org
@ 2023-10-20 22:08 ` mpolacek at gcc dot gnu.org
  2023-10-23 20:34 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-10-20 22:08 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I think we need something like

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index 0efdc677217..138328fe4f7 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -3670,7 +3670,7 @@ c_common_truthvalue_conversion (location_t location, tree
expr)
     CASE_CONVERT:
       {
    tree totype = TREE_TYPE (expr);
-   tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
+   tree fromtype = unlowered_expr_type (TREE_OPERAND (expr, 0));

    if (POINTER_TYPE_P (totype)
        && !c_inhibit_evaluation_warnings
diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc
index 4dfb39fb60b..7889f33f9eb 100644
--- a/gcc/cp/cvt.cc
+++ b/gcc/cp/cvt.cc
@@ -802,7 +802,7 @@ ocp_convert (tree type, tree expr, int convtype, int flags,

   if (INTEGRAL_CODE_P (code))
     {
-      tree intype = TREE_TYPE (e);
+      tree intype = unlowered_expr_type (e);
       tree converted;

       if (TREE_CODE (type) == ENUMERAL_TYPE)


except that unlowered_expr_type isn't available in c-family/, so that would
have to be addressed.

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

* [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool
  2023-10-20 14:05 [Bug c++/111895] New: error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' nabijaczleweli at nabijaczleweli dot xyz
  2023-10-20 15:19 ` [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool redi at gcc dot gnu.org
  2023-10-20 22:08 ` mpolacek at gcc dot gnu.org
@ 2023-10-23 20:34 ` mpolacek at gcc dot gnu.org
  2023-10-25  0:23 ` cvs-commit at gcc dot gnu.org
  2024-02-01 22:20 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-10-23 20:34 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
A better fix would be to use convert_bitfield_to_declared_type I reckon.

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

* [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool
  2023-10-20 14:05 [Bug c++/111895] New: error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' nabijaczleweli at nabijaczleweli dot xyz
                   ` (2 preceding siblings ...)
  2023-10-23 20:34 ` mpolacek at gcc dot gnu.org
@ 2023-10-25  0:23 ` cvs-commit at gcc dot gnu.org
  2024-02-01 22:20 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-10-25  0:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:6fa7284e2824310bb7204d41b5243c677ecb62d3

commit r14-4902-g6fa7284e2824310bb7204d41b5243c677ecb62d3
Author: Marek Polacek <polacek@redhat.com>
Date:   Mon Oct 23 17:06:45 2023 -0400

    c++: error with bit-fields and scoped enums [PR111895]

    Here we issue a bogus error: invalid operands of types 'unsigned char:2'
    and 'int' to binary 'operator!=' when casting a bit-field of scoped enum
    type to bool.

    In build_static_cast_1, perform_direct_initialization_if_possible returns
    NULL_TREE, because the invented declaration T t(e) fails, which is
    correct.  So we go down to ocp_convert, which has code to deal with this
    case:
              /* We can't implicitly convert a scoped enum to bool, so convert
                 to the underlying type first.  */
              if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
                e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
    but the SCOPED_ENUM_P is false since intype is <unnamed-unsigned:2>.
    This could be fixed by using unlowered_expr_type.  But then
    c_common_truthvalue_conversion/CASE_CONVERT has a similar problem, and
    unlowered_expr_type is a C++-only function.

    Rather than adding a dummy unlowered_expr_type to C, I think we should
    follow [expr.static.cast]p3: "the lvalue-to-rvalue conversion is applied
    to the bit-field and the resulting prvalue is used as the operand of the
    static_cast."  There are no prvalue bit-fields, so the l-to-r conversion
    performed in decay_conversion will get us an expression whose type is the
    enum.

            PR c++/111895

    gcc/cp/ChangeLog:

            * typeck.cc (build_static_cast_1): Call decay_conversion.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/scoped_enum12.C: New test.

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

* [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool
  2023-10-20 14:05 [Bug c++/111895] New: error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' nabijaczleweli at nabijaczleweli dot xyz
                   ` (3 preceding siblings ...)
  2023-10-25  0:23 ` cvs-commit at gcc dot gnu.org
@ 2024-02-01 22:20 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-02-01 22:20 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed for GCC 14.

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

end of thread, other threads:[~2024-02-01 22:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-20 14:05 [Bug c++/111895] New: error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' nabijaczleweli at nabijaczleweli dot xyz
2023-10-20 15:19 ` [Bug c++/111895] error: invalid operands of types 'unsigned char:2' and 'int' to binary 'operator!=' on enum class bitfield cast to bool redi at gcc dot gnu.org
2023-10-20 22:08 ` mpolacek at gcc dot gnu.org
2023-10-23 20:34 ` mpolacek at gcc dot gnu.org
2023-10-25  0:23 ` cvs-commit at gcc dot gnu.org
2024-02-01 22:20 ` mpolacek at gcc dot gnu.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).