From 1e22280f231e02b9ec8624e16b473b4082d7dfad Mon Sep 17 00:00:00 2001 From: Matthew J Perez Date: Sun, 11 Dec 2022 02:49:07 -0500 Subject: [PATCH] Add suggestions for function pointers - On compiler-error's suggestion of moving this lower down the stack, along the path of `report_mismatched_types()`, which is used by `rustc_hir_analysis` and `rustc_hir_typeck`. - update ui tests, add test - add suggestions for references to fn pointers - modify `TypeErrCtxt::same_type_modulo_infer` to take `T: relate::Relate` instead of `Ty` --- compiler/rustc_hir_typeck/src/coercion.rs | 2 + compiler/rustc_hir_typeck/src/demand.rs | 1 - .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 37 --------- .../src/infer/error_reporting/mod.rs | 3 +- .../src/infer/error_reporting/suggest.rs | 78 +++++++++++++++++- tests/ui/fn/fn-compare-mismatch.stderr | 1 + tests/ui/fn/fn-item-type.rs | 37 +++++---- tests/ui/fn/fn-item-type.stderr | 49 +++++------ tests/ui/fn/fn-pointer-mismatch.rs | 56 +++++++++++++ tests/ui/fn/fn-pointer-mismatch.stderr | 81 +++++++++++++++++++ tests/ui/reify-intrinsic.stderr | 4 +- .../fn-ptr.mir.stderr | 7 +- .../fn-ptr.thir.stderr | 7 +- .../ui/static/static-reference-to-fn-1.stderr | 6 +- 14 files changed, 273 insertions(+), 96 deletions(-) create mode 100644 tests/ui/fn/fn-pointer-mismatch.rs create mode 100644 tests/ui/fn/fn-pointer-mismatch.stderr diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index bbf7b81a2cc..1a0715a91cb 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1613,12 +1613,14 @@ pub(crate) fn coerce_inner<'a>( if visitor.ret_exprs.len() > 0 && let Some(expr) = expression { self.note_unreachable_loop_return(&mut err, &expr, &visitor.ret_exprs); } + let reported = err.emit_unless(unsized_return); self.final_ty = Some(fcx.tcx.ty_error_with_guaranteed(reported)); } } } + fn note_unreachable_loop_return( &self, err: &mut Diagnostic, diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index bd1626dff79..d02d77afa0a 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -81,7 +81,6 @@ pub fn emit_coerce_suggestions( self.annotate_expected_due_to_let_ty(err, expr, error); self.emit_type_mismatch_suggestions(err, expr, expr_ty, expected, expected_ty_expr, error); self.note_type_is_not_clone(err, expected, expr_ty, expr); - self.note_need_for_fn_pointer(err, expected, expr_ty); self.note_internal_mutation_in_method(err, expr, expected, expr_ty); self.check_for_range_as_method_call(err, expr, expr_ty, expected); self.check_for_binding_assigned_block_without_tail_expression(err, expr, expr_ty, expected); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 6ed8adb4742..e9858aef6d0 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -926,43 +926,6 @@ pub(in super::super) fn note_internal_mutation_in_method( } } - pub(in super::super) fn note_need_for_fn_pointer( - &self, - err: &mut Diagnostic, - expected: Ty<'tcx>, - found: Ty<'tcx>, - ) { - let (sig, did, substs) = match (&expected.kind(), &found.kind()) { - (ty::FnDef(did1, substs1), ty::FnDef(did2, substs2)) => { - let sig1 = self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1); - let sig2 = self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2); - if sig1 != sig2 { - return; - } - err.note( - "different `fn` items always have unique types, even if their signatures are \ - the same", - ); - (sig1, *did1, substs1) - } - (ty::FnDef(did, substs), ty::FnPtr(sig2)) => { - let sig1 = self.tcx.bound_fn_sig(*did).subst(self.tcx, substs); - if sig1 != *sig2 { - return; - } - (sig1, *did, substs) - } - _ => return, - }; - err.help(&format!("change the expected type to be function pointer `{}`", sig)); - err.help(&format!( - "if the expected type is due to type inference, cast the expected `fn` to a function \ - pointer: `{} as {}`", - self.tcx.def_path_str_with_substs(did, substs), - sig - )); - } - // Instantiates the given path, which must refer to an item with the given // number of type parameters and type. #[instrument(skip(self, span), level = "debug")] diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 28fd03b878b..212683a5429 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1841,6 +1841,7 @@ enum Similar<'tcx> { self.suggest_as_ref_where_appropriate(span, &exp_found, diag); self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag); self.suggest_await_on_expect_found(cause, span, &exp_found, diag); + self.suggest_function_pointers(cause, span, &exp_found, diag); } } @@ -2585,7 +2586,7 @@ pub fn is_try_conversion(&self, span: Span, trait_def_id: DefId) -> bool { /// with the other type. A TyVar inference type is compatible with any type, and an IntVar or /// FloatVar inference type are compatible with themselves or their concrete types (Int and /// Float types, respectively). When comparing two ADTs, these rules apply recursively. - pub fn same_type_modulo_infer(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool { + pub fn same_type_modulo_infer>(&self, a: T, b: T) -> bool { let (a, b) = self.resolve_vars_if_possible((a, b)); SameTypeModuloInfer(self).relate(a, b).is_ok() } diff --git a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs index 5b02956a106..b2ab39630bd 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs @@ -8,7 +8,7 @@ StatementAsExpression, }; use rustc_middle::ty::print::with_no_trimmed_paths; -use rustc_middle::ty::{self as ty, Ty, TypeVisitable}; +use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TypeVisitable}; use rustc_span::{sym, BytePos, Span}; use crate::errors::SuggAddLetForLetChains; @@ -351,6 +351,82 @@ pub(super) fn suggest_as_ref_where_appropriate( } } + pub(super) fn suggest_function_pointers( + &self, + cause: &ObligationCause<'tcx>, + span: Span, + exp_found: &ty::error::ExpectedFound>, + diag: &mut Diagnostic, + ) { + debug!("suggest_function_pointers(cause={:?}, exp_found={:?})", cause, exp_found); + let ty::error::ExpectedFound { expected, found } = exp_found; + let expected_inner = expected.peel_refs(); + let found_inner = found.peel_refs(); + if !expected_inner.is_fn() || !found_inner.is_fn() { + return; + } + match (&expected_inner.kind(), &found_inner.kind()) { + (ty::FnPtr(sig), ty::FnDef(did, substs)) => { + let expected_sig = &(self.normalize_fn_sig)(*sig); + let found_sig = + &(self.normalize_fn_sig)(self.tcx.bound_fn_sig(*did).subst(self.tcx, substs)); + + let fn_name = self.tcx.def_path_str_with_substs(*did, substs); + + if !self.same_type_modulo_infer(*found_sig, *expected_sig) + || !sig.is_suggestable(self.tcx, true) + || ty::util::is_intrinsic(self.tcx, *did) + { + return; + } + + let (msg, sugg) = match (expected.is_ref(), found.is_ref()) { + (true, false) => { + let msg = "consider using a reference"; + let sug = format!("&{fn_name}"); + (msg, sug) + } + (false, true) => { + let msg = "consider removing the reference"; + let sug = format!("{fn_name}"); + (msg, sug) + } + (true, true) => { + diag.note("fn items are distinct from fn pointers"); + let msg = "consider casting to a fn pointer"; + let sug = format!("&({fn_name} as {sig})"); + (msg, sug) + } + (false, false) => { + diag.note("fn items are distinct from fn pointers"); + let msg = "consider casting to a fn pointer"; + let sug = format!("{fn_name} as {sig}"); + (msg, sug) + } + }; + diag.span_suggestion(span, msg, &sugg, 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", + ); + } + } + (ty::FnDef(_, _), ty::FnPtr(_)) => { + diag.note("fn items are distinct from fn pointers"); + } + _ => { + return; + } + }; + } + pub fn should_suggest_as_ref(&self, expected: Ty<'tcx>, found: Ty<'tcx>) -> Option<&str> { if let (ty::Adt(exp_def, exp_substs), ty::Ref(_, found_ty, _)) = (expected.kind(), found.kind()) diff --git a/tests/ui/fn/fn-compare-mismatch.stderr b/tests/ui/fn/fn-compare-mismatch.stderr index df838cb1181..f247ff6cf3f 100644 --- a/tests/ui/fn/fn-compare-mismatch.stderr +++ b/tests/ui/fn/fn-compare-mismatch.stderr @@ -19,6 +19,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 error: aborting due to 2 previous errors diff --git a/tests/ui/fn/fn-item-type.rs b/tests/ui/fn/fn-item-type.rs index 1831e6cbf10..b6ebc867d28 100644 --- a/tests/ui/fn/fn-item-type.rs +++ b/tests/ui/fn/fn-item-type.rs @@ -1,13 +1,22 @@ // Test that the types of distinct fn items are not compatible by // default. See also `run-pass/fn-item-type-*.rs`. -fn foo(x: isize) -> isize { x * 2 } -fn bar(x: isize) -> isize { x * 4 } +fn foo(x: isize) -> isize { + x * 2 +} +fn bar(x: isize) -> isize { + x * 4 +} -fn eq(x: T, y: T) { } +fn eq(x: T, y: T) {} -trait Foo { fn foo() { /* this is a default fn */ } } -impl Foo for T { /* `foo` is still default here */ } +trait Foo { + fn foo() { /* this is a default fn */ + } +} +impl Foo for T { + /* `foo` is still default here */ +} fn main() { eq(foo::, bar::); @@ -15,39 +24,29 @@ fn main() { //~| expected fn item `fn(_) -> _ {foo::}` //~| found fn item `fn(_) -> _ {bar::}` //~| expected fn item, found a different fn item - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| different fn items have unique types, even if their signatures are the same eq(foo::, foo::); //~^ ERROR mismatched types //~| expected `u8`, found `i8` - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| different fn items have unique types, even if their signatures are the same eq(bar::, bar::>); //~^ ERROR mismatched types //~| found fn item `fn(_) -> _ {bar::>}` //~| expected struct `String`, found struct `Vec` - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| different fn items have unique types, even if their signatures are the same // Make sure we distinguish between trait methods correctly. eq(::foo, ::foo); //~^ ERROR mismatched types //~| expected `u8`, found `u16` - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| different fn items have unique types, even if their signatures are the same eq(foo::, bar:: as fn(isize) -> isize); //~^ ERROR mismatched types //~| found fn pointer `fn(_) -> _` //~| expected fn item, found fn pointer - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer eq(foo:: as fn(isize) -> isize, bar::); // ok! } diff --git a/tests/ui/fn/fn-item-type.stderr b/tests/ui/fn/fn-item-type.stderr index f03a47d5c2c..cb1b88c7ab8 100644 --- a/tests/ui/fn/fn-item-type.stderr +++ b/tests/ui/fn/fn-item-type.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:13:19 + --> $DIR/fn-item-type.rs:22:19 | LL | eq(foo::, bar::); | -- ^^^^^^^^^ expected fn item, found a different fn item @@ -8,17 +8,15 @@ LL | eq(foo::, bar::); | = note: expected fn item `fn(_) -> _ {foo::}` found fn item `fn(_) -> _ {bar::}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo:: as fn(isize) -> isize` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq(x: T, y: T) { } +LL | fn eq(x: T, y: T) {} | ^^ ---- error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:22:19 + --> $DIR/fn-item-type.rs:29:19 | LL | eq(foo::, foo::); | -- ^^^^^^^^^ expected `u8`, found `i8` @@ -27,17 +25,15 @@ LL | eq(foo::, foo::); | = note: expected fn item `fn(_) -> _ {foo::}` found fn item `fn(_) -> _ {foo::}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo:: as fn(isize) -> isize` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq(x: T, y: T) { } +LL | fn eq(x: T, y: T) {} | ^^ ---- error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:29:23 + --> $DIR/fn-item-type.rs:34:23 | LL | eq(bar::, bar::>); | -- ^^^^^^^^^^^^^^ expected struct `String`, found struct `Vec` @@ -46,17 +42,15 @@ LL | eq(bar::, bar::>); | = note: expected fn item `fn(_) -> _ {bar::}` found fn item `fn(_) -> _ {bar::>}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `bar:: as fn(isize) -> isize` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq(x: T, y: T) { } +LL | fn eq(x: T, y: T) {} | ^^ ---- error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:38:26 + --> $DIR/fn-item-type.rs:41:26 | LL | eq(::foo, ::foo); | -- ^^^^^^^^^^^^^^^^^ expected `u8`, found `u16` @@ -65,17 +59,15 @@ LL | eq(::foo, ::foo); | = note: expected fn item `fn() {::foo}` found fn item `fn() {::foo}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn()` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `::foo as fn()` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq(x: T, y: T) { } +LL | fn eq(x: T, y: T) {} | ^^ ---- error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:45:19 + --> $DIR/fn-item-type.rs:46:19 | LL | eq(foo::, bar:: as fn(isize) -> isize); | -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn item, found fn pointer @@ -84,12 +76,11 @@ LL | eq(foo::, bar:: as fn(isize) -> isize); | = note: expected fn item `fn(_) -> _ {foo::}` found fn pointer `fn(_) -> _` - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo:: as fn(isize) -> isize` + = note: fn items are distinct from fn pointers note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq(x: T, y: T) { } +LL | fn eq(x: T, y: T) {} | ^^ ---- error: aborting due to 5 previous errors diff --git a/tests/ui/fn/fn-pointer-mismatch.rs b/tests/ui/fn/fn-pointer-mismatch.rs new file mode 100644 index 00000000000..0597478cb42 --- /dev/null +++ b/tests/ui/fn/fn-pointer-mismatch.rs @@ -0,0 +1,56 @@ +fn foo(x: u32) -> u32 { + x * 2 +} + +fn bar(x: u32) -> u32 { + x * 3 +} + +// original example from Issue #102608 +fn foobar(n: u32) -> u32 { + let g = if n % 2 == 0 { &foo } else { &bar }; + //~^ ERROR `if` and `else` have incompatible types + //~| different fn items have unique types, even if their signatures are the same + g(n) +} + +fn main() { + assert_eq!(foobar(7), 21); + assert_eq!(foobar(8), 16); + + // general mismatch of fn item types + let mut a = foo; + a = bar; + //~^ ERROR mismatched types + //~| expected fn item `fn(_) -> _ {foo}` + //~| found fn item `fn(_) -> _ {bar}` + //~| different fn items have unique types, even if their signatures are the same + + // display note even when boxed + let mut b = Box::new(foo); + b = Box::new(bar); + //~^ ERROR mismatched types + //~| different fn items have unique types, even if their signatures are the same + + // suggest removing reference + let c: fn(u32) -> u32 = &foo; + //~^ ERROR mismatched types + //~| expected fn pointer `fn(u32) -> u32` + //~| found reference `&fn(u32) -> u32 {foo}` + + // suggest using reference + let d: &fn(u32) -> u32 = foo; + //~^ ERROR mismatched types + //~| expected reference `&fn(u32) -> u32` + //~| found fn item `fn(u32) -> u32 {foo}` + + // suggest casting with reference + let e: &fn(u32) -> u32 = &foo; + //~^ ERROR mismatched types + //~| expected reference `&fn(u32) -> u32` + //~| found reference `&fn(u32) -> u32 {foo}` + + // OK + let mut z: fn(u32) -> u32 = foo as fn(u32) -> u32; + z = bar; +} diff --git a/tests/ui/fn/fn-pointer-mismatch.stderr b/tests/ui/fn/fn-pointer-mismatch.stderr new file mode 100644 index 00000000000..2dc0710e27e --- /dev/null +++ b/tests/ui/fn/fn-pointer-mismatch.stderr @@ -0,0 +1,81 @@ +error[E0308]: `if` and `else` have incompatible types + --> $DIR/fn-pointer-mismatch.rs:11:43 + | +LL | let g = if n % 2 == 0 { &foo } else { &bar }; + | ---- ^^^^ expected fn item, found a different fn item + | | + | expected because of this + | + = 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 + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:23:9 + | +LL | let mut a = foo; + | --- expected due to this value +LL | a = bar; + | ^^^ expected fn item, found a different fn item + | + = 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 + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:31:18 + | +LL | b = Box::new(bar); + | -------- ^^^ expected fn item, found a different fn item + | | + | arguments to this function are incorrect + | + = 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 +note: associated function defined here + --> $SRC_DIR/alloc/src/boxed.rs:LL:COL + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:36:29 + | +LL | let c: fn(u32) -> u32 = &foo; + | -------------- ^^^^ + | | | + | | expected fn pointer, found reference + | | help: consider removing the reference: `foo` + | expected due to this + | + = note: expected fn pointer `fn(u32) -> u32` + found reference `&fn(u32) -> u32 {foo}` + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:42:30 + | +LL | let d: &fn(u32) -> u32 = foo; + | --------------- ^^^ + | | | + | | expected `&fn(u32) -> u32`, found fn item + | | help: consider using a reference: `&foo` + | expected due to this + | + = note: expected reference `&fn(u32) -> u32` + found fn item `fn(u32) -> u32 {foo}` + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:48:30 + | +LL | let e: &fn(u32) -> u32 = &foo; + | --------------- ^^^^ + | | | + | | expected fn pointer, found fn item + | | help: consider casting to a fn pointer: `&(foo as fn(u32) -> u32)` + | expected due to this + | + = note: expected reference `&fn(u32) -> u32` + found reference `&fn(u32) -> u32 {foo}` + = note: fn items are distinct from fn pointers + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/reify-intrinsic.stderr b/tests/ui/reify-intrinsic.stderr index f78f1d822bf..310b6c224e0 100644 --- a/tests/ui/reify-intrinsic.stderr +++ b/tests/ui/reify-intrinsic.stderr @@ -23,9 +23,7 @@ LL | std::intrinsics::unlikely, | = note: expected fn item `extern "rust-intrinsic" fn(_) -> _ {likely}` found fn item `extern "rust-intrinsic" fn(_) -> _ {unlikely}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `extern "rust-intrinsic" fn(bool) -> bool` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `likely as extern "rust-intrinsic" fn(bool) -> bool` + = note: different fn items have unique types, even if their signatures are the same error: aborting due to 3 previous errors diff --git a/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr b/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr index cf5815df56e..07f6dc906c6 100644 --- a/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr +++ b/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr @@ -5,12 +5,15 @@ LL | #[target_feature(enable = "sse2")] | ---------------------------------- `#[target_feature]` added here ... LL | let foo: fn() = foo; - | ---- ^^^ cannot coerce functions with `#[target_feature]` to safe function pointers - | | + | ---- ^^^ + | | | + | | cannot coerce functions with `#[target_feature]` to safe function pointers + | | help: consider casting to a fn pointer: `foo as fn()` | expected due to this | = note: expected fn pointer `fn()` found fn item `fn() {foo}` + = note: fn items are distinct from fn pointers = note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers error: aborting due to previous error diff --git a/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr b/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr index cf5815df56e..07f6dc906c6 100644 --- a/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr +++ b/tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr @@ -5,12 +5,15 @@ LL | #[target_feature(enable = "sse2")] | ---------------------------------- `#[target_feature]` added here ... LL | let foo: fn() = foo; - | ---- ^^^ cannot coerce functions with `#[target_feature]` to safe function pointers - | | + | ---- ^^^ + | | | + | | cannot coerce functions with `#[target_feature]` to safe function pointers + | | help: consider casting to a fn pointer: `foo as fn()` | expected due to this | = note: expected fn pointer `fn()` found fn item `fn() {foo}` + = note: fn items are distinct from fn pointers = note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers error: aborting due to previous error diff --git a/tests/ui/static/static-reference-to-fn-1.stderr b/tests/ui/static/static-reference-to-fn-1.stderr index 67b478bdb75..f68939d0ec8 100644 --- a/tests/ui/static/static-reference-to-fn-1.stderr +++ b/tests/ui/static/static-reference-to-fn-1.stderr @@ -2,10 +2,14 @@ error[E0308]: mismatched types --> $DIR/static-reference-to-fn-1.rs:17:15 | LL | func: &foo, - | ^^^^ expected fn pointer, found fn item + | ^^^^ + | | + | expected fn pointer, found fn item + | help: consider casting to a fn pointer: `&(foo as fn() -> Option)` | = note: expected reference `&fn() -> Option` found reference `&fn() -> Option {foo}` + = note: fn items are distinct from fn pointers error: aborting due to previous error