diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index e5d4d4a9dae2c..cdd7bef2c7fb9 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1164,10 +1164,12 @@ fn convert_enum_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, } else if let Some(disr) = repr_type.disr_incr(tcx, prev_disr) { Some(disr) } else { - span_err!(tcx.sess, v.span, E0370, - "enum discriminant overflowed on value after {}; \ - set explicitly via {} = {} if that is desired outcome", - prev_disr.unwrap(), v.node.name, wrapped_disr); + struct_span_err!(tcx.sess, v.span, E0370, + "enum discriminant overflowed") + .span_label(v.span, &format!("overflowed on value after {}", prev_disr.unwrap())) + .note(&format!("explicitly set `{} = {}` if that is desired outcome", + v.node.name, wrapped_disr)) + .emit(); None }.unwrap_or(wrapped_disr); prev_disr = Some(disr); diff --git a/src/test/compile-fail/discrim-overflow-2.rs b/src/test/compile-fail/discrim-overflow-2.rs index 0ff740212e8fa..213683b580883 100644 --- a/src/test/compile-fail/discrim-overflow-2.rs +++ b/src/test/compile-fail/discrim-overflow-2.rs @@ -24,7 +24,9 @@ fn f_i8() { enum A { Ok = i8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 127i8; set explicitly via OhNo = -128i8 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 127i8 + //~| NOTE explicitly set `OhNo = -128i8` if that is desired outcome } } @@ -33,7 +35,9 @@ fn f_u8() { enum A { Ok = u8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 255u8; set explicitly via OhNo = 0u8 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 255u8 + //~| NOTE explicitly set `OhNo = 0u8` if that is desired outcome } } @@ -42,7 +46,9 @@ fn f_i16() { enum A { Ok = i16::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 32767i16 + //~| NOTE explicitly set `OhNo = -32768i16` if that is desired outcome } } @@ -51,7 +57,9 @@ fn f_u16() { enum A { Ok = u16::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 65535u16 + //~| NOTE explicitly set `OhNo = 0u16` if that is desired outcome } } @@ -60,7 +68,9 @@ fn f_i32() { enum A { Ok = i32::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 2147483647i32 + //~| NOTE explicitly set `OhNo = -2147483648i32` if that is desired outcome } } @@ -69,7 +79,9 @@ fn f_u32() { enum A { Ok = u32::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 4294967295u32 + //~| NOTE explicitly set `OhNo = 0u32` if that is desired outcome } } @@ -78,7 +90,9 @@ fn f_i64() { enum A { Ok = i64::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 9223372036854775807i64 + //~| NOTE explicitly set `OhNo = -9223372036854775808i64` if that is desired outcome } } @@ -87,7 +101,9 @@ fn f_u64() { enum A { Ok = u64::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 18446744073709551615u64 + //~| NOTE explicitly set `OhNo = 0u64` if that is desired outcome } } diff --git a/src/test/compile-fail/discrim-overflow.rs b/src/test/compile-fail/discrim-overflow.rs index 7316e737b6da8..a3039b8d9573a 100644 --- a/src/test/compile-fail/discrim-overflow.rs +++ b/src/test/compile-fail/discrim-overflow.rs @@ -22,7 +22,9 @@ fn f_i8() { enum A { Ok = i8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 127i8; set explicitly via OhNo = -128i8 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 127i8 + //~| NOTE explicitly set `OhNo = -128i8` if that is desired outcome } let x = A::Ok; @@ -33,7 +35,9 @@ fn f_u8() { enum A { Ok = u8::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed on value after 255u8; set explicitly via OhNo = 0u8 if that is desired outcome + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 255u8 + //~| NOTE explicitly set `OhNo = 0u8` if that is desired outcome } let x = A::Ok; @@ -44,7 +48,9 @@ fn f_i16() { enum A { Ok = i16::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| NOTE overflowed on value after 32767i16 + //~| NOTE explicitly set `OhNo = -32768i16` if that is desired outcome } let x = A::Ok; @@ -55,7 +61,9 @@ fn f_u16() { enum A { Ok = u16::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 65535u16 + //~| NOTE explicitly set `OhNo = 0u16` if that is desired outcome } let x = A::Ok; @@ -66,7 +74,9 @@ fn f_i32() { enum A { Ok = i32::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 2147483647i32 + //~| NOTE explicitly set `OhNo = -2147483648i32` if that is desired outcome } let x = A::Ok; @@ -77,7 +87,9 @@ fn f_u32() { enum A { Ok = u32::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 4294967295u32 + //~| NOTE explicitly set `OhNo = 0u32` if that is desired outcome } let x = A::Ok; @@ -88,7 +100,9 @@ fn f_i64() { enum A { Ok = i64::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 9223372036854775807i64 + //~| NOTE explicitly set `OhNo = -9223372036854775808i64` if that is desired outcome } let x = A::Ok; @@ -99,7 +113,9 @@ fn f_u64() { enum A { Ok = u64::MAX - 1, Ok2, - OhNo, //~ ERROR enum discriminant overflowed + OhNo, //~ ERROR enum discriminant overflowed [E0370] + //~| overflowed on value after 18446744073709551615u64 + //~| NOTE explicitly set `OhNo = 0u64` if that is desired outcome } let x = A::Ok;