Skip to content

Commit

Permalink
Auto merge of #77278 - camelid:use-correct-article, r=estebank
Browse files Browse the repository at this point in the history
Use correct article in help message for conversion or cast

Before it always used `an`; now it uses the correct article for the type.
  • Loading branch information
bors committed Oct 19, 2020
2 parents e42cbe8 + 3eab21e commit 78307d8
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 301 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.
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 @@ -751,8 +751,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 @@ -814,7 +826,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

0 comments on commit 78307d8

Please sign in to comment.