Skip to content

Commit 7531eaf

Browse files
committed
Simplify suggestion.
1 parent 3fe0be9 commit 7531eaf

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -2227,15 +2227,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22272227
) -> bool {
22282228
let tcx = self.tcx;
22292229
let (adt, args, unwrap) = match expected.kind() {
2230-
// In case Option<NonZero*> is wanted, but * is provided, suggest calling new
2230+
// In case `Option<NonZero<T>>` is wanted, but `T` is provided, suggest calling `new`.
22312231
ty::Adt(adt, args) if tcx.is_diagnostic_item(sym::Option, adt.did()) => {
22322232
let nonzero_type = args.type_at(0); // Unwrap option type.
22332233
let ty::Adt(adt, args) = nonzero_type.kind() else {
22342234
return false;
22352235
};
22362236
(adt, args, "")
22372237
}
2238-
// In case `NonZero<*>` is wanted but `*` is provided, also add `.unwrap()` to satisfy types.
2238+
// In case `NonZero<T>` is wanted but `T` is provided, also add `.unwrap()` to satisfy types.
22392239
ty::Adt(adt, args) => (adt, args, ".unwrap()"),
22402240
_ => return false,
22412241
};
@@ -2244,32 +2244,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22442244
return false;
22452245
}
22462246

2247-
// FIXME: This can be simplified once `NonZero<T>` is stable.
2248-
let coercable_types = [
2249-
("NonZeroU8", tcx.types.u8),
2250-
("NonZeroU16", tcx.types.u16),
2251-
("NonZeroU32", tcx.types.u32),
2252-
("NonZeroU64", tcx.types.u64),
2253-
("NonZeroU128", tcx.types.u128),
2254-
("NonZeroI8", tcx.types.i8),
2255-
("NonZeroI16", tcx.types.i16),
2256-
("NonZeroI32", tcx.types.i32),
2257-
("NonZeroI64", tcx.types.i64),
2258-
("NonZeroI128", tcx.types.i128),
2259-
];
2260-
22612247
let int_type = args.type_at(0);
2262-
2263-
let Some(nonzero_alias) = coercable_types.iter().find_map(|(nonzero_alias, t)| {
2264-
if *t == int_type && self.can_coerce(expr_ty, *t) { Some(nonzero_alias) } else { None }
2265-
}) else {
2248+
if !self.can_coerce(expr_ty, int_type) {
22662249
return false;
2267-
};
2250+
}
22682251

22692252
err.multipart_suggestion(
2270-
format!("consider calling `{nonzero_alias}::new`"),
2253+
format!("consider calling `{}::new`", sym::NonZero),
22712254
vec![
2272-
(expr.span.shrink_to_lo(), format!("{nonzero_alias}::new(")),
2255+
(expr.span.shrink_to_lo(), format!("{}::new(", sym::NonZero)),
22732256
(expr.span.shrink_to_hi(), format!("){unwrap}")),
22742257
],
22752258
Applicability::MaybeIncorrect,
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
fn main() {
2-
let _: std::num::NonZeroU64 = 1;
2+
let _: std::num::NonZero<u64> = 1;
33
//~^ ERROR mismatched types
4-
//~| HELP consider calling `NonZeroU64::new`
4+
//~| HELP consider calling `NonZero::new`
55

6-
let _: Option<std::num::NonZeroU64> = 1;
6+
let _: Option<std::num::NonZero<u64>> = 1;
77
//~^ ERROR mismatched types
8-
//~| HELP consider calling `NonZeroU64::new`
8+
//~| HELP consider calling `NonZero::new`
99
}

tests/ui/mismatched_types/non_zero_assigned_something.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
error[E0308]: mismatched types
2-
--> $DIR/non_zero_assigned_something.rs:2:35
2+
--> $DIR/non_zero_assigned_something.rs:2:37
33
|
4-
LL | let _: std::num::NonZeroU64 = 1;
5-
| -------------------- ^ expected `NonZero<u64>`, found integer
4+
LL | let _: std::num::NonZero<u64> = 1;
5+
| ---------------------- ^ expected `NonZero<u64>`, found integer
66
| |
77
| expected due to this
88
|
99
= note: expected struct `NonZero<u64>`
1010
found type `{integer}`
11-
help: consider calling `NonZeroU64::new`
11+
help: consider calling `NonZero::new`
1212
|
13-
LL | let _: std::num::NonZeroU64 = NonZeroU64::new(1).unwrap();
14-
| ++++++++++++++++ ++++++++++
13+
LL | let _: std::num::NonZero<u64> = NonZero::new(1).unwrap();
14+
| +++++++++++++ ++++++++++
1515

1616
error[E0308]: mismatched types
17-
--> $DIR/non_zero_assigned_something.rs:6:43
17+
--> $DIR/non_zero_assigned_something.rs:6:45
1818
|
19-
LL | let _: Option<std::num::NonZeroU64> = 1;
20-
| ---------------------------- ^ expected `Option<NonZero<u64>>`, found integer
19+
LL | let _: Option<std::num::NonZero<u64>> = 1;
20+
| ------------------------------ ^ expected `Option<NonZero<u64>>`, found integer
2121
| |
2222
| expected due to this
2323
|
2424
= note: expected enum `Option<NonZero<u64>>`
2525
found type `{integer}`
26-
help: consider calling `NonZeroU64::new`
26+
help: consider calling `NonZero::new`
2727
|
28-
LL | let _: Option<std::num::NonZeroU64> = NonZeroU64::new(1);
29-
| ++++++++++++++++ +
28+
LL | let _: Option<std::num::NonZero<u64>> = NonZero::new(1);
29+
| +++++++++++++ +
3030

3131
error: aborting due to 2 previous errors
3232

0 commit comments

Comments
 (0)