Skip to content

Commit

Permalink
Simplify suggestion.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed May 3, 2024
1 parent 18a137b commit 50a4126
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
29 changes: 6 additions & 23 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2142,15 +2142,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> bool {
let tcx = self.tcx;
let (adt, args, unwrap) = match expected.kind() {
// In case Option<NonZero*> is wanted, but * is provided, suggest calling new
// In case `Option<NonZero<T>>` is wanted, but `T` is provided, suggest calling `new`.
ty::Adt(adt, args) if tcx.is_diagnostic_item(sym::Option, adt.did()) => {
let nonzero_type = args.type_at(0); // Unwrap option type.
let ty::Adt(adt, args) = nonzero_type.kind() else {
return false;
};
(adt, args, "")
}
// In case `NonZero<*>` is wanted but `*` is provided, also add `.unwrap()` to satisfy types.
// In case `NonZero<T>` is wanted but `T` is provided, also add `.unwrap()` to satisfy types.
ty::Adt(adt, args) => (adt, args, ".unwrap()"),
_ => return false,
};
Expand All @@ -2159,32 +2159,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}

// FIXME: This can be simplified once `NonZero<T>` is stable.
let coercable_types = [
("NonZeroU8", tcx.types.u8),
("NonZeroU16", tcx.types.u16),
("NonZeroU32", tcx.types.u32),
("NonZeroU64", tcx.types.u64),
("NonZeroU128", tcx.types.u128),
("NonZeroI8", tcx.types.i8),
("NonZeroI16", tcx.types.i16),
("NonZeroI32", tcx.types.i32),
("NonZeroI64", tcx.types.i64),
("NonZeroI128", tcx.types.i128),
];

let int_type = args.type_at(0);

let Some(nonzero_alias) = coercable_types.iter().find_map(|(nonzero_alias, t)| {
if *t == int_type && self.can_coerce(expr_ty, *t) { Some(nonzero_alias) } else { None }
}) else {
if !self.can_coerce(expr_ty, int_type) {
return false;
};
}

err.multipart_suggestion(
format!("consider calling `{nonzero_alias}::new`"),
format!("consider calling `{}::new`", sym::NonZero),
vec![
(expr.span.shrink_to_lo(), format!("{nonzero_alias}::new(")),
(expr.span.shrink_to_lo(), format!("{}::new(", sym::NonZero)),
(expr.span.shrink_to_hi(), format!("){unwrap}")),
],
Applicability::MaybeIncorrect,
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/mismatched_types/non_zero_assigned_something.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
fn main() {
let _: std::num::NonZeroU64 = 1;
let _: std::num::NonZero<u64> = 1;
//~^ ERROR mismatched types
//~| HELP consider calling `NonZeroU64::new`
//~| HELP consider calling `NonZero::new`

let _: Option<std::num::NonZeroU64> = 1;
let _: Option<std::num::NonZero<u64>> = 1;
//~^ ERROR mismatched types
//~| HELP consider calling `NonZeroU64::new`
//~| HELP consider calling `NonZero::new`
}
16 changes: 8 additions & 8 deletions tests/ui/mismatched_types/non_zero_assigned_something.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
error[E0308]: mismatched types
--> $DIR/non_zero_assigned_something.rs:2:35
|
LL | let _: std::num::NonZeroU64 = 1;
LL | let _: std::num::NonZero<u64> = 1;
| -------------------- ^ expected `NonZero<u64>`, found integer
| |
| expected due to this
|
= note: expected struct `NonZero<u64>`
found type `{integer}`
help: consider calling `NonZeroU64::new`
help: consider calling `NonZero::new`
|
LL | let _: std::num::NonZeroU64 = NonZeroU64::new(1).unwrap();
| ++++++++++++++++ ++++++++++
LL | let _: std::num::NonZero<u64> = NonZero::new(1).unwrap();
| +++++++++++++ ++++++++++

error[E0308]: mismatched types
--> $DIR/non_zero_assigned_something.rs:6:43
|
LL | let _: Option<std::num::NonZeroU64> = 1;
LL | let _: Option<std::num::NonZero<u64>> = 1;
| ---------------------------- ^ expected `Option<NonZero<u64>>`, found integer
| |
| expected due to this
|
= note: expected enum `Option<NonZero<u64>>`
found type `{integer}`
help: consider calling `NonZeroU64::new`
help: consider calling `NonZero::new`
|
LL | let _: Option<std::num::NonZeroU64> = NonZeroU64::new(1);
| ++++++++++++++++ +
LL | let _: Option<std::num::NonZero<u64>> = NonZero::new(1);
| +++++++++++++ +

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 50a4126

Please sign in to comment.