From 7d8e10d3c242c2741d7190db8a9bbc4c919b53c7 Mon Sep 17 00:00:00 2001 From: Jack Huey <31162821+jackh726@users.noreply.github.com> Date: Sun, 17 Apr 2022 15:27:03 -0400 Subject: [PATCH] Resolve vars before emitting coerce suggestions too --- compiler/rustc_typeck/src/check/fn_ctxt/checks.rs | 6 ++++++ src/test/ui/indexing-requires-a-uint.stderr | 4 ++++ src/test/ui/issues/issue-13359.stderr | 8 ++++++++ src/test/ui/mismatched_types/issue-26480.stderr | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs index 166bd451199..fb0fcf5d796 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs @@ -504,6 +504,8 @@ pub(in super::super) fn check_argument_types( TupleMatchFound::Single => { let expected_ty = expected_input_tys[0]; let provided_ty = final_arg_types[0].map(|ty| ty.0).unwrap(); + let expected_ty = self.resolve_vars_if_possible(expected_ty); + let provided_ty = self.resolve_vars_if_possible(provided_ty); let cause = &self.misc(provided_args[0].span); let compatibility = demand_compatible(0, &mut final_arg_types); let type_error = match compatibility { @@ -565,6 +567,8 @@ pub(in super::super) fn check_argument_types( { let expected_ty = expected_input_tys[*input_idx]; let provided_ty = final_arg_types[*input_idx].map(|ty| ty.0).unwrap(); + let expected_ty = self.resolve_vars_if_possible(expected_ty); + let provided_ty = self.resolve_vars_if_possible(provided_ty); let cause = &self.misc(provided_args[*input_idx].span); let trace = TypeTrace::types(cause, true, expected_ty, provided_ty); let mut err = self.report_and_explain_type_error(trace, error); @@ -634,6 +638,8 @@ enum SuggestionText { .and_then(|x| x.as_ref()) .map(|ty| ty.0) .unwrap_or(tcx.ty_error()); + let expected_ty = self.resolve_vars_if_possible(expected_ty); + let provided_ty = self.resolve_vars_if_possible(provided_ty); if let Compatibility::Incompatible(error) = &compatibility { let cause = &self.misc( provided_args.get(input_idx).map(|i| i.span).unwrap_or(call_span), diff --git a/src/test/ui/indexing-requires-a-uint.stderr b/src/test/ui/indexing-requires-a-uint.stderr index a9adff4fade..0a24855a6a7 100644 --- a/src/test/ui/indexing-requires-a-uint.stderr +++ b/src/test/ui/indexing-requires-a-uint.stderr @@ -21,6 +21,10 @@ note: function defined here | LL | fn bar(_: T) {} | ^^^ ---- +help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit + | +LL | bar::(i.try_into().unwrap()); // i should not be re-coerced back to an isize + | ++++++++++++++++++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/issues/issue-13359.stderr b/src/test/ui/issues/issue-13359.stderr index db6283ea11f..fef63680a86 100644 --- a/src/test/ui/issues/issue-13359.stderr +++ b/src/test/ui/issues/issue-13359.stderr @@ -11,6 +11,10 @@ note: function defined here | LL | fn foo(_s: i16) { } | ^^^ ------- +help: you can convert an `isize` to an `i16` and panic if the converted value doesn't fit + | +LL | foo((1*(1 as isize)).try_into().unwrap()); + | + +++++++++++++++++++++ error[E0308]: mismatched types --> $DIR/issue-13359.rs:10:9 @@ -25,6 +29,10 @@ note: function defined here | LL | fn bar(_s: u32) { } | ^^^ ------- +help: you can convert a `usize` to a `u32` and panic if the converted value doesn't fit + | +LL | bar((1*(1 as usize)).try_into().unwrap()); + | + +++++++++++++++++++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/mismatched_types/issue-26480.stderr b/src/test/ui/mismatched_types/issue-26480.stderr index 579a5b7ecb9..ae10a00671e 100644 --- a/src/test/ui/mismatched_types/issue-26480.stderr +++ b/src/test/ui/mismatched_types/issue-26480.stderr @@ -15,6 +15,10 @@ note: function defined here LL | fn write(fildes: i32, buf: *const i8, nbyte: u64) -> i64; | ^^^^^ = note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info) +help: you can convert a `usize` to a `u64` and panic if the converted value doesn't fit + | +LL | ($arr.len() * size_of($arr[0])).try_into().unwrap()); + | + +++++++++++++++++++++ error[E0605]: non-primitive cast: `{integer}` as `()` --> $DIR/issue-26480.rs:22:19 -- 2.44.0