-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use MIN
/MAX
constant names in integer pattern coverage messages
#57073
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2624,7 +2624,13 @@ pub fn fmt_const_val(f: &mut impl Write, const_val: &ty::Const<'_>) -> fmt::Resu | |||||
Bool if bits == 1 => return write!(f, "true"), | ||||||
Float(ast::FloatTy::F32) => return write!(f, "{}f32", Single::from_bits(bits)), | ||||||
Float(ast::FloatTy::F64) => return write!(f, "{}f64", Double::from_bits(bits)), | ||||||
Uint(ui) => return write!(f, "{:?}{}", bits, ui), | ||||||
Uint(ui) => { | ||||||
return match bits { | ||||||
// writing 0 as uX::MIN wouldn't clarify | ||||||
n if n == ui.max_as_u128() => write!(f, "::std::{}::MAX", ui), | ||||||
_ => write!(f, "{:?}{}", bits, ui) | ||||||
} | ||||||
} | ||||||
Int(i) => { | ||||||
let bit_width = ty::tls::with(|tcx| { | ||||||
let ty = tcx.lift_to_global(&ty).unwrap(); | ||||||
|
@@ -2634,7 +2640,12 @@ pub fn fmt_const_val(f: &mut impl Write, const_val: &ty::Const<'_>) -> fmt::Resu | |||||
.bits() | ||||||
}); | ||||||
let shift = 128 - bit_width; | ||||||
return write!(f, "{:?}{}", ((bits as i128) << shift) >> shift, i); | ||||||
let n = ((bits as i128) << shift) >> shift; | ||||||
return match n { | ||||||
m if m == i.min_as_i128() => write!(f, "::std::{}::MIN", i), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is just stylistic, but I think it's clearer what value you're testing if you don't mind |
||||||
m if m == i.max_as_i128() => write!(f, "::std::{}::MAX", i), | ||||||
_ => write!(f, "{:?}{}", n, i) | ||||||
} | ||||||
} | ||||||
Char => return write!(f, "{:?}", ::std::char::from_u32(bits as u32).unwrap()), | ||||||
_ => {} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1458,6 +1458,29 @@ impl IntTy { | |
IntTy::I128 => 128, | ||
}) | ||
} | ||
|
||
pub fn min_as_i128(&self) -> i128 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can avoid enumerating the integer types using the bit manipulations that are already used for range pattern matching, e.g.: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although these should be refactored as part of #49937 anyway, it probably makes more sense to have these in |
||
match *self { | ||
IntTy::Isize => ::std::isize::MIN as i128, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to use the host |
||
IntTy::I8 => ::std::i8::MIN as i128, | ||
IntTy::I16 => ::std::i16::MIN as i128, | ||
IntTy::I32 => ::std::i32::MIN as i128, | ||
IntTy::I64 => ::std::i64::MIN as i128, | ||
IntTy::I128 => ::std::i128::MIN, | ||
} | ||
} | ||
|
||
pub fn max_as_i128(&self) -> i128 { | ||
match *self { | ||
IntTy::Isize => ::std::isize::MAX as i128, | ||
IntTy::I8 => ::std::i8::MAX as i128, | ||
IntTy::I16 => ::std::i16::MAX as i128, | ||
IntTy::I32 => ::std::i32::MAX as i128, | ||
IntTy::I64 => ::std::i64::MAX as i128, | ||
IntTy::I128 => ::std::i128::MAX, | ||
} | ||
} | ||
|
||
} | ||
|
||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Copy)] | ||
|
@@ -1496,6 +1519,17 @@ impl UintTy { | |
UintTy::U128 => 128, | ||
}) | ||
} | ||
|
||
pub fn max_as_u128(&self) -> u128 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
match *self { | ||
UintTy::Usize => ::std::usize::MAX as u128, | ||
UintTy::U8 => ::std::u8::MAX as u128, | ||
UintTy::U16 => ::std::u16::MAX as u128, | ||
UintTy::U32 => ::std::u32::MAX as u128, | ||
UintTy::U64 => ::std::u64::MAX as u128, | ||
UintTy::U128 => ::std::u128::MAX, | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for UintTy { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,11 @@ note: lint level defined here | |
LL | #![deny(unreachable_patterns)] | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error[E0004]: non-exhaustive patterns: `128u8..=255u8` not covered | ||
error[E0004]: non-exhaustive patterns: `128u8..=::std::u8::MAX` not covered | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's quite a lot of syntax in here ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it's quite noisy, but considering that the compiler doesn't currently suggest importing |
||
--> $DIR/exhaustive_integer_patterns.rs:38:11 | ||
| | ||
LL | match x { //~ ERROR non-exhaustive patterns | ||
| ^ pattern `128u8..=255u8` not covered | ||
| ^ pattern `128u8..=::std::u8::MAX` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `11u8..=19u8`, `31u8..=34u8`, `36u8..=69u8` and 1 more not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:43:11 | ||
|
@@ -28,53 +28,53 @@ error: unreachable pattern | |
LL | -2..=20 => {} //~ ERROR unreachable pattern | ||
| ^^^^^^^ | ||
|
||
error[E0004]: non-exhaustive patterns: `-128i8..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered | ||
error[E0004]: non-exhaustive patterns: `::std::i8::MIN..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:51:11 | ||
| | ||
LL | match x { //~ ERROR non-exhaustive patterns | ||
| ^ patterns `-128i8..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered | ||
| ^ patterns `::std::i8::MIN..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `-128i8` not covered | ||
error[E0004]: non-exhaustive patterns: `::std::i8::MIN` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:92:11 | ||
| | ||
LL | match 0i8 { //~ ERROR non-exhaustive patterns | ||
| ^^^ pattern `-128i8` not covered | ||
| ^^^ pattern `::std::i8::MIN` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `0i16` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:100:11 | ||
| | ||
LL | match 0i16 { //~ ERROR non-exhaustive patterns | ||
| ^^^^ pattern `0i16` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `128u8..=255u8` not covered | ||
error[E0004]: non-exhaustive patterns: `128u8..=::std::u8::MAX` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:118:11 | ||
| | ||
LL | match 0u8 { //~ ERROR non-exhaustive patterns | ||
| ^^^ pattern `128u8..=255u8` not covered | ||
| ^^^ pattern `128u8..=::std::u8::MAX` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `(0u8, Some(_))` and `(2u8..=255u8, Some(_))` not covered | ||
error[E0004]: non-exhaustive patterns: `(0u8, Some(_))` and `(2u8..=::std::u8::MAX, Some(_))` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:130:11 | ||
| | ||
LL | match (0u8, Some(())) { //~ ERROR non-exhaustive patterns | ||
| ^^^^^^^^^^^^^^^ patterns `(0u8, Some(_))` and `(2u8..=255u8, Some(_))` not covered | ||
| ^^^^^^^^^^^^^^^ patterns `(0u8, Some(_))` and `(2u8..=::std::u8::MAX, Some(_))` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `(126u8..=127u8, false)` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:135:11 | ||
| | ||
LL | match (0u8, true) { //~ ERROR non-exhaustive patterns | ||
| ^^^^^^^^^^^ pattern `(126u8..=127u8, false)` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `340282366920938463463374607431768211455u128` not covered | ||
error[E0004]: non-exhaustive patterns: `::std::u128::MAX` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:155:11 | ||
| | ||
LL | match 0u128 { //~ ERROR non-exhaustive patterns | ||
| ^^^^^ pattern `340282366920938463463374607431768211455u128` not covered | ||
| ^^^^^ pattern `::std::u128::MAX` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `5u128..=340282366920938463463374607431768211455u128` not covered | ||
error[E0004]: non-exhaustive patterns: `5u128..=::std::u128::MAX` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:159:11 | ||
| | ||
LL | match 0u128 { //~ ERROR non-exhaustive patterns | ||
| ^^^^^ pattern `5u128..=340282366920938463463374607431768211455u128` not covered | ||
| ^^^^^ pattern `5u128..=::std::u128::MAX` not covered | ||
|
||
error[E0004]: non-exhaustive patterns: `0u128..=3u128` not covered | ||
--> $DIR/exhaustive_integer_patterns.rs:163:11 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.