Skip to content

Commit 78307d8

Browse files
committed
Auto merge of rust-lang#77278 - camelid:use-correct-article, r=estebank
Use correct article in help message for conversion or cast Before it always used `an`; now it uses the correct article for the type.
2 parents e42cbe8 + 3eab21e commit 78307d8

22 files changed

+325
-301
lines changed

Diff for: compiler/rustc_middle/src/ty/sty.rs

+12
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ impl TyKind<'tcx> {
210210
_ => false,
211211
}
212212
}
213+
214+
/// Get the article ("a" or "an") to use with this type.
215+
pub fn article(&self) -> &'static str {
216+
match self {
217+
Int(_) | Float(_) | Array(_, _) => "an",
218+
Adt(def, _) if def.is_enum() => "an",
219+
// This should never happen, but ICEing and causing the user's code
220+
// to not compile felt too harsh.
221+
Error(_) => "a",
222+
_ => "a",
223+
}
224+
}
213225
}
214226

215227
// `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.

Diff for: compiler/rustc_typeck/src/check/demand.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
751751
}
752752
}
753753

754-
let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
755-
let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);
754+
let msg = format!(
755+
"you can convert {} `{}` to {} `{}`",
756+
checked_ty.kind().article(),
757+
checked_ty,
758+
expected_ty.kind().article(),
759+
expected_ty,
760+
);
761+
let cast_msg = format!(
762+
"you can cast {} `{}` to {} `{}`",
763+
checked_ty.kind().article(),
764+
checked_ty,
765+
expected_ty.kind().article(),
766+
expected_ty,
767+
);
756768
let lit_msg = format!(
757769
"change the type of the numeric literal from `{}` to `{}`",
758770
checked_ty, expected_ty,
@@ -814,7 +826,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
814826
let suggestion = format!("{}::from({})", checked_ty, lhs_src);
815827
(lhs_expr.span, msg, suggestion)
816828
} else {
817-
let msg = format!("{} and panic if the converted value wouldn't fit", msg);
829+
let msg = format!("{} and panic if the converted value doesn't fit", msg);
818830
let suggestion =
819831
format!("{}{}.try_into().unwrap()", prefix, with_opt_paren(&src));
820832
(expr.span, msg, suggestion)

Diff for: src/test/ui/associated-types/associated-types-path-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ LL | let _: i32 = f2(2i32);
4747
| |
4848
| expected due to this
4949
|
50-
help: you can convert an `u32` to `i32` and panic if the converted value wouldn't fit
50+
help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit
5151
|
5252
LL | let _: i32 = f2(2i32).try_into().unwrap();
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/indexing-requires-a-uint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0308]: mismatched types
1313
LL | bar::<isize>(i); // i should not be re-coerced back to an isize
1414
| ^ expected `isize`, found `usize`
1515
|
16-
help: you can convert an `usize` to `isize` and panic if the converted value wouldn't fit
16+
help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit
1717
|
1818
LL | bar::<isize>(i.try_into().unwrap()); // i should not be re-coerced back to an isize
1919
| ^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)