Skip to content

Use correct article in help message for conversion or cast #77278

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

Merged
merged 4 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
pub fn article(&self) -> &'static str {
match self {
Int(_) | Float(_) | Array(_, _) => "an",
Adt(def, _) if def.is_enum() => "an",
// This should never happen, but ICEing and causing the user's code
// to not compile felt too harsh.
Error(_) => "a",
_ => "a",
}
}
}

// `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.
Expand Down
18 changes: 15 additions & 3 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,20 @@ 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.kind().article(),
expected_ty,
);
let cast_msg = format!(
"you can cast {} `{}` to {} `{}`",
checked_ty.kind().article(),
checked_ty,
expected_ty.kind().article(),
expected_ty,
);
let lit_msg = format!(
"change the type of the numeric literal from `{}` to `{}`",
checked_ty, expected_ty,
Expand Down Expand Up @@ -815,7 +827,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let suggestion = format!("{}::from({})", checked_ty, lhs_src);
(lhs_expr.span, msg, suggestion)
} else {
let msg = format!("{} and panic if the converted value wouldn't fit", msg);
let msg = format!("{} and panic if the converted value doesn't fit", msg);
let suggestion =
format!("{}{}.try_into().unwrap()", prefix, with_opt_paren(&src));
(expr.span, msg, suggestion)
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 an `i32` and panic if the converted value doesn'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 an `isize` and panic if the converted value doesn't fit
|
LL | bar::<isize>(i.try_into().unwrap()); // i should not be re-coerced back to an isize
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading