Skip to content

Commit 549f861

Browse files
committed
Use correct article in help message for conversion or cast
Before it always used `an`; now it uses the correct article for the type.
1 parent 1d216fe commit 549f861

14 files changed

+169
-147
lines changed

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 12 additions & 0 deletions
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+
///
216+
/// **Panics if `self` is [`TyKind::Error`].**
217+
pub fn article(&self) -> &'static str {
218+
match self {
219+
Int(_) | Float(_) | Array(_, _) => "an",
220+
Adt(def, _) if def.is_enum() => "an",
221+
Error(_) => panic!(),
222+
_ => "a",
223+
}
224+
}
213225
}
214226

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

compiler/rustc_typeck/src/check/demand.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
752752
}
753753
}
754754

755-
let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
756-
let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);
755+
let msg = format!(
756+
"you can convert {} `{}` to `{}`",
757+
checked_ty.kind().article(),
758+
checked_ty,
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
766+
);
757767
let lit_msg = format!(
758768
"change the type of the numeric literal from `{}` to `{}`",
759769
checked_ty, expected_ty,

src/test/ui/associated-types/associated-types-path-2.stderr

Lines changed: 1 addition & 1 deletion
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 `i32` and panic if the converted value wouldn't fit
5151
|
5252
LL | let _: i32 = f2(2i32).try_into().unwrap();
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/indexing-requires-a-uint.stderr

Lines changed: 1 addition & 1 deletion
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 `isize` and panic if the converted value wouldn't fit
1717
|
1818
LL | bar::<isize>(i.try_into().unwrap()); // i should not be re-coerced back to an isize
1919
| ^^^^^^^^^^^^^^^^^^^^^

src/test/ui/integer-literal-suffix-inference.stderr

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ error[E0308]: mismatched types
328328
LL | id_u8(b16);
329329
| ^^^ expected `u8`, found `u16`
330330
|
331-
help: you can convert an `u16` to `u8` and panic if the converted value wouldn't fit
331+
help: you can convert a `u16` to `u8` and panic if the converted value wouldn't fit
332332
|
333333
LL | id_u8(b16.try_into().unwrap());
334334
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -339,7 +339,7 @@ error[E0308]: mismatched types
339339
LL | id_u8(b32);
340340
| ^^^ expected `u8`, found `u32`
341341
|
342-
help: you can convert an `u32` to `u8` and panic if the converted value wouldn't fit
342+
help: you can convert a `u32` to `u8` and panic if the converted value wouldn't fit
343343
|
344344
LL | id_u8(b32.try_into().unwrap());
345345
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -350,7 +350,7 @@ error[E0308]: mismatched types
350350
LL | id_u8(b64);
351351
| ^^^ expected `u8`, found `u64`
352352
|
353-
help: you can convert an `u64` to `u8` and panic if the converted value wouldn't fit
353+
help: you can convert a `u64` to `u8` and panic if the converted value wouldn't fit
354354
|
355355
LL | id_u8(b64.try_into().unwrap());
356356
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -361,7 +361,7 @@ error[E0308]: mismatched types
361361
LL | id_u8(bsize);
362362
| ^^^^^ expected `u8`, found `usize`
363363
|
364-
help: you can convert an `usize` to `u8` and panic if the converted value wouldn't fit
364+
help: you can convert a `usize` to `u8` and panic if the converted value wouldn't fit
365365
|
366366
LL | id_u8(bsize.try_into().unwrap());
367367
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -373,15 +373,15 @@ LL | id_u16(b8);
373373
| ^^
374374
| |
375375
| expected `u16`, found `u8`
376-
| help: you can convert an `u8` to `u16`: `b8.into()`
376+
| help: you can convert a `u8` to `u16`: `b8.into()`
377377

378378
error[E0308]: mismatched types
379379
--> $DIR/integer-literal-suffix-inference.rs:169:12
380380
|
381381
LL | id_u16(b32);
382382
| ^^^ expected `u16`, found `u32`
383383
|
384-
help: you can convert an `u32` to `u16` and panic if the converted value wouldn't fit
384+
help: you can convert a `u32` to `u16` and panic if the converted value wouldn't fit
385385
|
386386
LL | id_u16(b32.try_into().unwrap());
387387
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -392,7 +392,7 @@ error[E0308]: mismatched types
392392
LL | id_u16(b64);
393393
| ^^^ expected `u16`, found `u64`
394394
|
395-
help: you can convert an `u64` to `u16` and panic if the converted value wouldn't fit
395+
help: you can convert a `u64` to `u16` and panic if the converted value wouldn't fit
396396
|
397397
LL | id_u16(b64.try_into().unwrap());
398398
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -403,7 +403,7 @@ error[E0308]: mismatched types
403403
LL | id_u16(bsize);
404404
| ^^^^^ expected `u16`, found `usize`
405405
|
406-
help: you can convert an `usize` to `u16` and panic if the converted value wouldn't fit
406+
help: you can convert a `usize` to `u16` and panic if the converted value wouldn't fit
407407
|
408408
LL | id_u16(bsize.try_into().unwrap());
409409
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -415,7 +415,7 @@ LL | id_u32(b8);
415415
| ^^
416416
| |
417417
| expected `u32`, found `u8`
418-
| help: you can convert an `u8` to `u32`: `b8.into()`
418+
| help: you can convert a `u8` to `u32`: `b8.into()`
419419

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

429429
error[E0308]: mismatched types
430430
--> $DIR/integer-literal-suffix-inference.rs:186:12
431431
|
432432
LL | id_u32(b64);
433433
| ^^^ expected `u32`, found `u64`
434434
|
435-
help: you can convert an `u64` to `u32` and panic if the converted value wouldn't fit
435+
help: you can convert a `u64` to `u32` and panic if the converted value wouldn't fit
436436
|
437437
LL | id_u32(b64.try_into().unwrap());
438438
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -443,7 +443,7 @@ error[E0308]: mismatched types
443443
LL | id_u32(bsize);
444444
| ^^^^^ expected `u32`, found `usize`
445445
|
446-
help: you can convert an `usize` to `u32` and panic if the converted value wouldn't fit
446+
help: you can convert a `usize` to `u32` and panic if the converted value wouldn't fit
447447
|
448448
LL | id_u32(bsize.try_into().unwrap());
449449
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -455,7 +455,7 @@ LL | id_u64(b8);
455455
| ^^
456456
| |
457457
| expected `u64`, found `u8`
458-
| help: you can convert an `u8` to `u64`: `b8.into()`
458+
| help: you can convert a `u8` to `u64`: `b8.into()`
459459

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

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

478478
error[E0308]: mismatched types
479479
--> $DIR/integer-literal-suffix-inference.rs:203:12
480480
|
481481
LL | id_u64(bsize);
482482
| ^^^^^ expected `u64`, found `usize`
483483
|
484-
help: you can convert an `usize` to `u64` and panic if the converted value wouldn't fit
484+
help: you can convert a `usize` to `u64` and panic if the converted value wouldn't fit
485485
|
486486
LL | id_u64(bsize.try_into().unwrap());
487487
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -493,7 +493,7 @@ LL | id_usize(b8);
493493
| ^^
494494
| |
495495
| expected `usize`, found `u8`
496-
| help: you can convert an `u8` to `usize`: `b8.into()`
496+
| help: you can convert a `u8` to `usize`: `b8.into()`
497497

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

507507
error[E0308]: mismatched types
508508
--> $DIR/integer-literal-suffix-inference.rs:213:14
509509
|
510510
LL | id_usize(b32);
511511
| ^^^ expected `usize`, found `u32`
512512
|
513-
help: you can convert an `u32` to `usize` and panic if the converted value wouldn't fit
513+
help: you can convert a `u32` to `usize` and panic if the converted value wouldn't fit
514514
|
515515
LL | id_usize(b32.try_into().unwrap());
516516
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -521,7 +521,7 @@ error[E0308]: mismatched types
521521
LL | id_usize(b64);
522522
| ^^^ expected `usize`, found `u64`
523523
|
524-
help: you can convert an `u64` to `usize` and panic if the converted value wouldn't fit
524+
help: you can convert a `u64` to `usize` and panic if the converted value wouldn't fit
525525
|
526526
LL | id_usize(b64.try_into().unwrap());
527527
| ^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/issues/issue-13359.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ error[E0308]: mismatched types
1515
LL | bar(1*(1 as usize));
1616
| ^^^^^^^^^^^^^^ expected `u32`, found `usize`
1717
|
18-
help: you can convert an `usize` to `u32` and panic if the converted value wouldn't fit
18+
help: you can convert a `usize` to `u32` and panic if the converted value wouldn't fit
1919
|
2020
LL | bar((1*(1 as usize)).try_into().unwrap());
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/mismatched_types/issue-26480.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | write!(hello);
88
| -------------- in this macro invocation
99
|
1010
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
11-
help: you can convert an `usize` to `u64` and panic if the converted value wouldn't fit
11+
help: you can convert a `usize` to `u64` and panic if the converted value wouldn't fit
1212
|
1313
LL | ($arr.len() * size_of($arr[0])).try_into().unwrap());
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/numeric/len.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched types
44
LL | test(array.len());
55
| ^^^^^^^^^^^ expected `u32`, found `usize`
66
|
7-
help: you can convert an `usize` to `u32` and panic if the converted value wouldn't fit
7+
help: you can convert a `usize` to `u32` and panic if the converted value wouldn't fit
88
|
99
LL | test(array.len().try_into().unwrap());
1010
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/numeric/numeric-cast-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | let y: i64 = x + x;
1818
| --- ^^^^^
1919
| | |
2020
| | expected `i64`, found `u16`
21-
| | help: you can convert an `u16` to `i64`: `(x + x).into()`
21+
| | help: you can convert a `u16` to `i64`: `(x + x).into()`
2222
| expected due to this
2323

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

3434
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)