diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 133778a416cc2..bda156c70deb2 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2142,7 +2142,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) -> bool { let tcx = self.tcx; let (adt, args, unwrap) = match expected.kind() { - // In case Option is wanted, but * is provided, suggest calling new + // In case `Option>` 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 { @@ -2150,7 +2150,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; (adt, args, "") } - // In case `NonZero<*>` is wanted but `*` is provided, also add `.unwrap()` to satisfy types. + // In case `NonZero` is wanted but `T` is provided, also add `.unwrap()` to satisfy types. ty::Adt(adt, args) => (adt, args, ".unwrap()"), _ => return false, }; @@ -2159,32 +2159,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } - // FIXME: This can be simplified once `NonZero` 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, diff --git a/tests/ui/mismatched_types/non_zero_assigned_something.rs b/tests/ui/mismatched_types/non_zero_assigned_something.rs index d2adbe01c1828..e94d5249d6a84 100644 --- a/tests/ui/mismatched_types/non_zero_assigned_something.rs +++ b/tests/ui/mismatched_types/non_zero_assigned_something.rs @@ -1,9 +1,9 @@ fn main() { - let _: std::num::NonZeroU64 = 1; + let _: std::num::NonZero = 1; //~^ ERROR mismatched types - //~| HELP consider calling `NonZeroU64::new` + //~| HELP consider calling `NonZero::new` - let _: Option = 1; + let _: Option> = 1; //~^ ERROR mismatched types - //~| HELP consider calling `NonZeroU64::new` + //~| HELP consider calling `NonZero::new` } diff --git a/tests/ui/mismatched_types/non_zero_assigned_something.stderr b/tests/ui/mismatched_types/non_zero_assigned_something.stderr index f8e86905ab9bb..fe5a096af04f1 100644 --- a/tests/ui/mismatched_types/non_zero_assigned_something.stderr +++ b/tests/ui/mismatched_types/non_zero_assigned_something.stderr @@ -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 = 1; | -------------------- ^ expected `NonZero`, found integer | | | expected due to this | = note: expected struct `NonZero` 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 = NonZero::new(1).unwrap(); + | +++++++++++++ ++++++++++ error[E0308]: mismatched types --> $DIR/non_zero_assigned_something.rs:6:43 | -LL | let _: Option = 1; +LL | let _: Option> = 1; | ---------------------------- ^ expected `Option>`, found integer | | | expected due to this | = note: expected enum `Option>` found type `{integer}` -help: consider calling `NonZeroU64::new` +help: consider calling `NonZero::new` | -LL | let _: Option = NonZeroU64::new(1); - | ++++++++++++++++ + +LL | let _: Option> = NonZero::new(1); + | +++++++++++++ + error: aborting due to 2 previous errors