Skip to content

Commit

Permalink
Use correct article in help message for conversion or cast
Browse files Browse the repository at this point in the history
Before it always used `an`; now it uses the correct article for the type.
  • Loading branch information
camelid committed Sep 27, 2020
1 parent 1d216fe commit 549f861
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 147 deletions.
12 changes: 12 additions & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ impl TyKind<'tcx> {
_ => false,
}
}

/// Get the article ("a" or "an") to use with this type.
///
/// **Panics if `self` is [`TyKind::Error`].**
pub fn article(&self) -> &'static str {
match self {
Int(_) | Float(_) | Array(_, _) => "an",
Adt(def, _) if def.is_enum() => "an",
Error(_) => panic!(),
_ => "a",
}
}
}

// `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.
Expand Down
14 changes: 12 additions & 2 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);
let msg = format!(
"you can convert {} `{}` to `{}`",
checked_ty.kind().article(),
checked_ty,
expected_ty
);
let cast_msg = format!(
"you can cast {} `{} to `{}`",
checked_ty.kind().article(),
checked_ty,
expected_ty
);
let lit_msg = format!(
"change the type of the numeric literal from `{}` to `{}`",
checked_ty, expected_ty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ LL | let _: i32 = f2(2i32);
| |
| expected due to this
|
help: you can convert an `u32` to `i32` and panic if the converted value wouldn't fit
help: you can convert a `u32` to `i32` and panic if the converted value wouldn't fit
|
LL | let _: i32 = f2(2i32).try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/indexing-requires-a-uint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[E0308]: mismatched types
LL | bar::<isize>(i); // i should not be re-coerced back to an isize
| ^ expected `isize`, found `usize`
|
help: you can convert an `usize` to `isize` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `isize` and panic if the converted value wouldn't fit
|
LL | bar::<isize>(i.try_into().unwrap()); // i should not be re-coerced back to an isize
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
40 changes: 20 additions & 20 deletions src/test/ui/integer-literal-suffix-inference.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ error[E0308]: mismatched types
LL | id_u8(b16);
| ^^^ expected `u8`, found `u16`
|
help: you can convert an `u16` to `u8` and panic if the converted value wouldn't fit
help: you can convert a `u16` to `u8` and panic if the converted value wouldn't fit
|
LL | id_u8(b16.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -339,7 +339,7 @@ error[E0308]: mismatched types
LL | id_u8(b32);
| ^^^ expected `u8`, found `u32`
|
help: you can convert an `u32` to `u8` and panic if the converted value wouldn't fit
help: you can convert a `u32` to `u8` and panic if the converted value wouldn't fit
|
LL | id_u8(b32.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -350,7 +350,7 @@ error[E0308]: mismatched types
LL | id_u8(b64);
| ^^^ expected `u8`, found `u64`
|
help: you can convert an `u64` to `u8` and panic if the converted value wouldn't fit
help: you can convert a `u64` to `u8` and panic if the converted value wouldn't fit
|
LL | id_u8(b64.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -361,7 +361,7 @@ error[E0308]: mismatched types
LL | id_u8(bsize);
| ^^^^^ expected `u8`, found `usize`
|
help: you can convert an `usize` to `u8` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `u8` and panic if the converted value wouldn't fit
|
LL | id_u8(bsize.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -373,15 +373,15 @@ LL | id_u16(b8);
| ^^
| |
| expected `u16`, found `u8`
| help: you can convert an `u8` to `u16`: `b8.into()`
| help: you can convert a `u8` to `u16`: `b8.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:169:12
|
LL | id_u16(b32);
| ^^^ expected `u16`, found `u32`
|
help: you can convert an `u32` to `u16` and panic if the converted value wouldn't fit
help: you can convert a `u32` to `u16` and panic if the converted value wouldn't fit
|
LL | id_u16(b32.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -392,7 +392,7 @@ error[E0308]: mismatched types
LL | id_u16(b64);
| ^^^ expected `u16`, found `u64`
|
help: you can convert an `u64` to `u16` and panic if the converted value wouldn't fit
help: you can convert a `u64` to `u16` and panic if the converted value wouldn't fit
|
LL | id_u16(b64.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -403,7 +403,7 @@ error[E0308]: mismatched types
LL | id_u16(bsize);
| ^^^^^ expected `u16`, found `usize`
|
help: you can convert an `usize` to `u16` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `u16` and panic if the converted value wouldn't fit
|
LL | id_u16(bsize.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -415,7 +415,7 @@ LL | id_u32(b8);
| ^^
| |
| expected `u32`, found `u8`
| help: you can convert an `u8` to `u32`: `b8.into()`
| help: you can convert a `u8` to `u32`: `b8.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:182:12
Expand All @@ -424,15 +424,15 @@ LL | id_u32(b16);
| ^^^
| |
| expected `u32`, found `u16`
| help: you can convert an `u16` to `u32`: `b16.into()`
| help: you can convert a `u16` to `u32`: `b16.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:186:12
|
LL | id_u32(b64);
| ^^^ expected `u32`, found `u64`
|
help: you can convert an `u64` to `u32` and panic if the converted value wouldn't fit
help: you can convert a `u64` to `u32` and panic if the converted value wouldn't fit
|
LL | id_u32(b64.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -443,7 +443,7 @@ error[E0308]: mismatched types
LL | id_u32(bsize);
| ^^^^^ expected `u32`, found `usize`
|
help: you can convert an `usize` to `u32` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `u32` and panic if the converted value wouldn't fit
|
LL | id_u32(bsize.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -455,7 +455,7 @@ LL | id_u64(b8);
| ^^
| |
| expected `u64`, found `u8`
| help: you can convert an `u8` to `u64`: `b8.into()`
| help: you can convert a `u8` to `u64`: `b8.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:196:12
Expand All @@ -464,7 +464,7 @@ LL | id_u64(b16);
| ^^^
| |
| expected `u64`, found `u16`
| help: you can convert an `u16` to `u64`: `b16.into()`
| help: you can convert a `u16` to `u64`: `b16.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:199:12
Expand All @@ -473,15 +473,15 @@ LL | id_u64(b32);
| ^^^
| |
| expected `u64`, found `u32`
| help: you can convert an `u32` to `u64`: `b32.into()`
| help: you can convert a `u32` to `u64`: `b32.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:203:12
|
LL | id_u64(bsize);
| ^^^^^ expected `u64`, found `usize`
|
help: you can convert an `usize` to `u64` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `u64` and panic if the converted value wouldn't fit
|
LL | id_u64(bsize.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -493,7 +493,7 @@ LL | id_usize(b8);
| ^^
| |
| expected `usize`, found `u8`
| help: you can convert an `u8` to `usize`: `b8.into()`
| help: you can convert a `u8` to `usize`: `b8.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:210:14
Expand All @@ -502,15 +502,15 @@ LL | id_usize(b16);
| ^^^
| |
| expected `usize`, found `u16`
| help: you can convert an `u16` to `usize`: `b16.into()`
| help: you can convert a `u16` to `usize`: `b16.into()`

error[E0308]: mismatched types
--> $DIR/integer-literal-suffix-inference.rs:213:14
|
LL | id_usize(b32);
| ^^^ expected `usize`, found `u32`
|
help: you can convert an `u32` to `usize` and panic if the converted value wouldn't fit
help: you can convert a `u32` to `usize` and panic if the converted value wouldn't fit
|
LL | id_usize(b32.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -521,7 +521,7 @@ error[E0308]: mismatched types
LL | id_usize(b64);
| ^^^ expected `usize`, found `u64`
|
help: you can convert an `u64` to `usize` and panic if the converted value wouldn't fit
help: you can convert a `u64` to `usize` and panic if the converted value wouldn't fit
|
LL | id_usize(b64.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-13359.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ error[E0308]: mismatched types
LL | bar(1*(1 as usize));
| ^^^^^^^^^^^^^^ expected `u32`, found `usize`
|
help: you can convert an `usize` to `u32` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `u32` and panic if the converted value wouldn't fit
|
LL | bar((1*(1 as usize)).try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/mismatched_types/issue-26480.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | write!(hello);
| -------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can convert an `usize` to `u64` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `u64` and panic if the converted value wouldn't fit
|
LL | ($arr.len() * size_of($arr[0])).try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/numeric/len.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0308]: mismatched types
LL | test(array.len());
| ^^^^^^^^^^^ expected `u32`, found `usize`
|
help: you can convert an `usize` to `u32` and panic if the converted value wouldn't fit
help: you can convert a `usize` to `u32` and panic if the converted value wouldn't fit
|
LL | test(array.len().try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/numeric/numeric-cast-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LL | let y: i64 = x + x;
| --- ^^^^^
| | |
| | expected `i64`, found `u16`
| | help: you can convert an `u16` to `i64`: `(x + x).into()`
| | help: you can convert a `u16` to `i64`: `(x + x).into()`
| expected due to this

error[E0308]: mismatched types
Expand All @@ -28,7 +28,7 @@ LL | let z: i32 = x + x;
| --- ^^^^^
| | |
| | expected `i32`, found `u16`
| | help: you can convert an `u16` to `i32`: `(x + x).into()`
| | help: you can convert a `u16` to `i32`: `(x + x).into()`
| expected due to this

error: aborting due to 3 previous errors
Expand Down
Loading

0 comments on commit 549f861

Please sign in to comment.