===================================================================
@@ -1658,8 +1658,9 @@ build_expr_type_conversion (int desires, tree expr
{
error ("ambiguous default type conversion from %qT",
basetype);
- error (" candidate conversions include %qD and %qD",
- winner, cand);
+ inform (input_location,
+ " candidate conversions include %qD and %qD",
+ winner, cand);
}
return error_mark_node;
}
===================================================================
@@ -12966,14 +12966,32 @@ build_enumerator (tree name, tree value, tree enum
/* Validate and default VALUE. */
if (value != NULL_TREE)
{
- value = cxx_constant_value (value);
+ if (ENUM_UNDERLYING_TYPE (enumtype))
+ value = perform_implicit_conversion_flags
+ (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
+ LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
+ else
+ {
+ tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
+ value, true);
+ if (tmp_value)
+ value = tmp_value;
+ }
- if (TREE_CODE (value) != INTEGER_CST
- || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
+ if (value == error_mark_node)
+ value = NULL_TREE;
+
+ if (value != NULL_TREE)
{
- error ("enumerator value for %qD is not an integer constant",
- name);
- value = NULL_TREE;
+ value = cxx_constant_value (value);
+
+ if (TREE_CODE (value) != INTEGER_CST
+ || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
+ {
+ error ("enumerator value for %qD is not an integer constant",
+ name);
+ value = NULL_TREE;
+ }
}
}
@@ -13036,25 +13054,25 @@ incremented enumerator value is too large for %<lo
}
else
value = integer_zero_node;
- }
- /* Remove no-op casts from the value. */
- STRIP_TYPE_NOPS (value);
+ /* Remove no-op casts from the value. */
+ STRIP_TYPE_NOPS (value);
- /* If the underlying type of the enum is fixed, check whether
- the enumerator values fits in the underlying type. If it
- does not fit, the program is ill-formed [C++0x dcl.enum]. */
- if (ENUM_UNDERLYING_TYPE (enumtype)
- && value
- && TREE_CODE (value) == INTEGER_CST)
- {
- if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
- error ("enumerator value %E is outside the range of underlying "
- "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
+ /* If the underlying type of the enum is fixed, check whether
+ the enumerator values fits in the underlying type. If it
+ does not fit, the program is ill-formed [C++0x dcl.enum]. */
+ if (ENUM_UNDERLYING_TYPE (enumtype)
+ && value
+ && TREE_CODE (value) == INTEGER_CST)
+ {
+ if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
+ error ("enumerator value %E is outside the range of underlying "
+ "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
- /* Convert the value to the appropriate type. */
- value = convert (ENUM_UNDERLYING_TYPE (enumtype), value);
- }
+ /* Convert the value to the appropriate type. */
+ value = convert (ENUM_UNDERLYING_TYPE (enumtype), value);
+ }
+ }
}
/* C++ associates enums with global, function, or class declarations. */
===================================================================
@@ -1,4 +1,4 @@
// PR c++/53745
// { dg-do compile { target c++11 } }
-enum E : unsigned { e = -1 }; // { dg-error "outside the range" }
+enum E : unsigned { e = -1 }; // { dg-error "narrowing" }
===================================================================
@@ -0,0 +1,58 @@
+// PR c++/51312
+// { dg-do compile { target c++11 } }
+
+struct X0
+{
+ constexpr operator int() const { return 1; }
+};
+
+struct X1
+{
+ enum RE1 { re1 = 1 };
+ constexpr operator RE1() const { return re1; }
+};
+
+struct X2
+{
+ constexpr operator int() const { return __INT_MAX__; }
+};
+
+struct X3
+{
+ enum RE3 { re3 = __INT_MAX__ };
+ constexpr operator RE3() const { return re3; }
+};
+
+struct X4
+{
+ constexpr operator double() const { return 1.0; }
+};
+
+struct X5
+{
+ constexpr operator int() const { return __INT_MAX__; }
+ constexpr operator unsigned() const { return __INT_MAX__ * 2U + 1; }
+};
+
+enum E0 { e0 = X0() };
+enum E1 { e1 = X1() };
+enum E2 { e2 = X2() };
+enum E3 { e3 = X3() };
+enum E4 { e4 = X4() }; // { dg-error "integer constant" }
+enum E5 { e5 = X5() }; // { dg-error "ambiguous" }
+
+enum F0 : int { f0 = X0() };
+enum F1 : int { f1 = X1() };
+enum F2 : int { f2 = X2() };
+enum F3 : int { f3 = X3() };
+enum F4 : int { f4 = X4() }; // { dg-error "narrowing" }
+enum F5 : int { f5 = X5() };
+
+enum G0 : signed char { g0 = X0() };
+enum G1 : signed char { g1 = X1() };
+enum G2 : signed char { g2 = X2() }; // { dg-error "narrowing" }
+// { dg-warning "overflow" "" { target *-*-* } 53 }
+enum G3 : signed char { g3 = X3() }; // { dg-error "narrowing" }
+// { dg-warning "overflow" "" { target *-*-* } 55 }
+enum G4 : signed char { g4 = X4() }; // { dg-error "narrowing" }
+enum G5 : signed char { g5 = X5() }; // { dg-error "ambiguous" }
===================================================================
@@ -6,11 +6,13 @@ enum E1 : char { };
enum E2 : signed const short { };
enum E3 : uvlonglong { };
enum E4 : char {
- val = 500 // { dg-error "outside the range" }
+ val = 500 // { dg-error "narrowing" }
+// { dg-warning "overflow" "" { target *-*-* } 9 }
};
enum class E5 {
- val = (unsigned long long)-1 // { dg-error "outside the range" }
+ val = (unsigned long long)-1 // { dg-error "narrowing" }
+// { dg-warning "overflow" "" { target *-*-* } 14 }
};
typedef float Float;