Skip to content

Improve fn pointer notes #107287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions compiler/rustc_infer/src/infer/error_reporting/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
return;
}

let (msg, sugg) = match (expected.is_ref(), found.is_ref()) {
let (msg, sug) = match (expected.is_ref(), found.is_ref()) {
(true, false) => {
let msg = "consider using a reference";
let sug = format!("&{fn_name}");
Expand All @@ -404,22 +404,58 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
(msg, sug)
}
};
diag.span_suggestion(span, msg, &sugg, Applicability::MaybeIncorrect);
diag.span_suggestion(span, msg, sug, Applicability::MaybeIncorrect);
}
(ty::FnDef(did1, substs1), ty::FnDef(did2, substs2)) => {
let expected_sig =
&(self.normalize_fn_sig)(self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1));
let found_sig =
&(self.normalize_fn_sig)(self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2));

if self.same_type_modulo_infer(*found_sig, *expected_sig) {
diag.note(
"different fn items have unique types, even if their signatures are the same",
);
if self.same_type_modulo_infer(*expected_sig, *found_sig) {
diag.note("different fn items have unique types, even if their signatures are the same");
}

if !self.same_type_modulo_infer(*found_sig, *expected_sig)
|| !found_sig.is_suggestable(self.tcx, true)
|| !expected_sig.is_suggestable(self.tcx, true)
|| ty::util::is_intrinsic(self.tcx, *did1)
|| ty::util::is_intrinsic(self.tcx, *did2)
{
return;
}

let fn_name = self.tcx.def_path_str_with_substs(*did2, substs2);
let sug = if found.is_ref() {
format!("&({fn_name} as {found_sig})")
} else {
format!("{fn_name} as {found_sig}")
};

let msg = format!(
"consider casting both fn items to fn pointers using `as {expected_sig}`"
);

diag.span_suggestion_hidden(span, msg, sug, Applicability::MaybeIncorrect);
}
(ty::FnDef(_, _), ty::FnPtr(_)) => {
diag.note("fn items are distinct from fn pointers");
(ty::FnDef(did, substs), ty::FnPtr(sig)) => {
let expected_sig =
&(self.normalize_fn_sig)(self.tcx.bound_fn_sig(*did).subst(self.tcx, substs));
let found_sig = &(self.normalize_fn_sig)(*sig);

if !self.same_type_modulo_infer(*found_sig, *expected_sig) {
return;
}

let fn_name = self.tcx.def_path_str_with_substs(*did, substs);

let casting = if expected.is_ref() {
format!("&({fn_name} as {found_sig})")
} else {
format!("{fn_name} as {found_sig}")
};

diag.help(&format!("consider casting the fn item to a fn pointer: `{}`", casting));
}
_ => {
return;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/fn/fn-compare-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ LL | let x = f == g;
= note: expected fn item `fn() {f}`
found fn item `fn() {g}`
= note: different fn items have unique types, even if their signatures are the same
= help: consider casting both fn items to fn pointers using `as fn()`

error: aborting due to 2 previous errors

Expand Down
6 changes: 5 additions & 1 deletion tests/ui/fn/fn-item-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ note: function defined here
|
LL | fn eq<T>(x: T, y: T) {}
| ^^ ----
= help: consider casting both fn items to fn pointers using `as fn(isize) -> isize`

error[E0308]: mismatched types
--> $DIR/fn-item-type.rs:29:19
Expand All @@ -31,6 +32,7 @@ note: function defined here
|
LL | fn eq<T>(x: T, y: T) {}
| ^^ ----
= help: consider casting both fn items to fn pointers using `as fn(isize) -> isize`

error[E0308]: mismatched types
--> $DIR/fn-item-type.rs:34:23
Expand All @@ -48,6 +50,7 @@ note: function defined here
|
LL | fn eq<T>(x: T, y: T) {}
| ^^ ----
= help: consider casting both fn items to fn pointers using `as fn(isize) -> isize`

error[E0308]: mismatched types
--> $DIR/fn-item-type.rs:41:26
Expand All @@ -65,6 +68,7 @@ note: function defined here
|
LL | fn eq<T>(x: T, y: T) {}
| ^^ ----
= help: consider casting both fn items to fn pointers using `as fn()`

error[E0308]: mismatched types
--> $DIR/fn-item-type.rs:46:19
Expand All @@ -76,7 +80,7 @@ LL | eq(foo::<u8>, bar::<u8> as fn(isize) -> isize);
|
= note: expected fn item `fn(_) -> _ {foo::<u8>}`
found fn pointer `fn(_) -> _`
= note: fn items are distinct from fn pointers
= help: consider casting the fn item to a fn pointer: `foo::<u8> as fn(isize) -> isize`
note: function defined here
--> $DIR/fn-item-type.rs:11:4
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/fn/fn-pointer-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LL | let g = if n % 2 == 0 { &foo } else { &bar };
= note: expected reference `&fn(u32) -> u32 {foo}`
found reference `&fn(u32) -> u32 {bar}`
= note: different fn items have unique types, even if their signatures are the same
= help: consider casting both fn items to fn pointers using `as fn(u32) -> u32`

error[E0308]: mismatched types
--> $DIR/fn-pointer-mismatch.rs:23:9
Expand All @@ -21,6 +22,7 @@ LL | a = bar;
= note: expected fn item `fn(_) -> _ {foo}`
found fn item `fn(_) -> _ {bar}`
= note: different fn items have unique types, even if their signatures are the same
= help: consider casting both fn items to fn pointers using `as fn(u32) -> u32`

error[E0308]: mismatched types
--> $DIR/fn-pointer-mismatch.rs:31:18
Expand All @@ -35,6 +37,7 @@ LL | b = Box::new(bar);
= note: different fn items have unique types, even if their signatures are the same
note: associated function defined here
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
= help: consider casting both fn items to fn pointers using `as fn(u32) -> u32`

error[E0308]: mismatched types
--> $DIR/fn-pointer-mismatch.rs:36:29
Expand Down