diff options
author | Marek Polacek <polacek@redhat.com> | 2023-10-23 17:06:45 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2023-10-24 20:22:48 -0400 |
commit | 6fa7284e2824310bb7204d41b5243c677ecb62d3 (patch) | |
tree | 3949cac9cb07a73b2d6d93f4d0e1918a60393f32 | |
parent | 444a485f8f7c04757ccc2b4c5ff0d0645ebcf4e2 (diff) | |
download | gcc-6fa7284e2824310bb7204d41b5243c677ecb62d3.zip gcc-6fa7284e2824310bb7204d41b5243c677ecb62d3.tar.gz gcc-6fa7284e2824310bb7204d41b5243c677ecb62d3.tar.bz2 |
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.
-rw-r--r-- | gcc/cp/typeck.cc | 7 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C | 8 |
2 files changed, 15 insertions, 0 deletions
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index f3dc80c..3b71932 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -8405,6 +8405,13 @@ build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p, return expr; if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) expr = TREE_OPERAND (expr, 0); + /* [expr.static.cast]: "If the value is not a bit-field, the result + refers to the object or the specified base class subobject thereof; + otherwise, 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; the l-to-r conversion + will give us an object of the underlying type of the bit-field. */ + expr = decay_conversion (expr, complain); return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain); } diff --git a/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C b/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C new file mode 100644 index 0000000..1d10431 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C @@ -0,0 +1,8 @@ +// PR c++/111895 +// { dg-do compile { target c++11 } } + +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); } |