Test and note unsafe ctor to fn ptr coercion

Also remove a note that I don't consider to be very useful in context.
This commit is contained in:
Michael Goulet 2023-08-28 02:37:38 +00:00
parent 0100a94231
commit bf66723c0e
18 changed files with 29 additions and 22 deletions

View file

@ -266,12 +266,10 @@ fn foo(&self, x: T) -> T { x }
}
}
}
(ty::FnPtr(_), ty::FnDef(def, _))
if let hir::def::DefKind::Fn = tcx.def_kind(def) => {
diag.note(
"when the arguments and return types match, functions can be coerced \
to function pointers",
);
(ty::FnPtr(sig), ty::FnDef(def_id, _)) | (ty::FnDef(def_id, _), ty::FnPtr(sig)) => {
if tcx.fn_sig(*def_id).skip_binder().unsafety() < sig.unsafety() {
diag.note("unsafe functions cannot be coerced into safe function pointers");
}
}
_ => {}
}

View file

@ -11,7 +11,6 @@ LL | foo(f, w);
| ^
= note: expected fn pointer `fn(i32)`
found fn item `fn(u32) {f}`
= note: when the arguments and return types match, functions can be coerced to function pointers
note: expected `Wrapper<i32>`, found `Wrapper<isize>`
--> $DIR/two-mismatch-notes.rs:10:12
|

View file

@ -46,7 +46,6 @@ LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
|
= note: expected fn pointer `unsafe extern "C" fn(_, _)`
found fn item `unsafe extern "C" fn(_, _, ...) {foo}`
= note: when the arguments and return types match, functions can be coerced to function pointers
error[E0308]: mismatched types
--> $DIR/variadic-ffi-1.rs:26:54
@ -58,7 +57,6 @@ LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
|
= note: expected fn pointer `extern "C" fn(_, _, ...)`
found fn item `extern "C" fn(_, _) {bar}`
= note: when the arguments and return types match, functions can be coerced to function pointers
error[E0617]: can't pass `f32` to variadic function
--> $DIR/variadic-ffi-1.rs:28:19

View file

@ -80,7 +80,6 @@ LL | let e: &fn(u32) -> u32 = &foo;
= note: expected reference `&fn(u32) -> u32`
found reference `&fn(u32) -> u32 {foo}`
= note: fn items are distinct from fn pointers
= note: when the arguments and return types match, functions can be coerced to function pointers
help: consider casting to a fn pointer
|
LL | let e: &fn(u32) -> u32 = &(foo as fn(u32) -> u32);

View file

@ -12,5 +12,4 @@ fn main() {
//~| NOTE expected fn pointer, found fn item
//~| NOTE expected fn pointer `fn(i32, u32)`
//~| NOTE arguments to this function are incorrect
//~| NOTE when the arguments and return types match, functions can be coerced to function pointers
}

View file

@ -8,7 +8,6 @@ LL | needs_ptr(foo);
|
= note: expected fn pointer `fn(i32, u32)`
found fn item `fn(i32, i32) {foo}`
= note: when the arguments and return types match, functions can be coerced to function pointers
note: function defined here
--> $DIR/signature-error-reporting-under-verbose.rs:5:4
|

View file

@ -8,7 +8,6 @@ LL | fn main() { f(bar) }
|
= note: expected fn pointer `fn()`
found fn item `extern "C" fn() {bar}`
= note: when the arguments and return types match, functions can be coerced to function pointers
note: function defined here
--> $DIR/issue-10764.rs:1:4
|

View file

@ -8,7 +8,6 @@ LL | needs_i32_ref_fn(foo::<()>);
|
= note: expected fn pointer `fn(&'static i32, i32)`
found fn item `fn(i32, &'static i32) {foo::<()>}`
= note: when the arguments and return types match, functions can be coerced to function pointers
note: function defined here
--> $DIR/normalize-fn-sig.rs:11:4
|

View file

@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize) = a;
|
= note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
= note: when the arguments and return types match, functions can be coerced to function pointers
error: aborting due to previous error

View file

@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize, &mut &isize) = a;
|
= note: expected fn pointer `for<'a, 'b, 'c, 'd, 'e, 'f> fn(&'a mut &'b isize, &'c mut &'d isize, &'e mut &'f isize)`
found fn item `for<'a, 'b, 'c> fn(&'a mut &isize, &'b mut &isize, &'c mut &isize) {a::<'_, '_, '_>}`
= note: when the arguments and return types match, functions can be coerced to function pointers
error: aborting due to previous error

View file

@ -8,7 +8,6 @@ LL | want_G(baz);
|
= note: expected fn pointer `for<'cx> fn(&'cx S) -> &'static S`
found fn item `for<'a> fn(&'a S) -> &'a S {baz}`
= note: when the arguments and return types match, functions can be coerced to function pointers
note: function defined here
--> $DIR/regions-fn-subtyping-return-static-fail.rs:20:4
|

View file

@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize) = a;
|
= note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
= note: when the arguments and return types match, functions can be coerced to function pointers
error: aborting due to previous error

View file

@ -8,7 +8,6 @@ LL | let _: unsafe extern "rust-intrinsic" fn(isize) -> usize = std::mem::tr
|
= note: expected fn pointer `unsafe extern "rust-intrinsic" fn(isize) -> usize`
found fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
= note: when the arguments and return types match, functions can be coerced to function pointers
error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
--> $DIR/reify-intrinsic.rs:11:13

View file

@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
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
= note: when the arguments and return types match, functions can be coerced to function pointers
help: consider casting to a fn pointer
|
LL | let foo: fn() = foo as fn();

View file

@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
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
= note: when the arguments and return types match, functions can be coerced to function pointers
help: consider casting to a fn pointer
|
LL | let foo: fn() = foo as fn();

View file

@ -7,7 +7,6 @@ LL | func: &foo,
= note: expected reference `&fn() -> Option<isize>`
found reference `&fn() -> Option<isize> {foo}`
= note: fn items are distinct from fn pointers
= note: when the arguments and return types match, functions can be coerced to function pointers
help: consider casting to a fn pointer
|
LL | func: &(foo as fn() -> Option<isize>),

View file

@ -0,0 +1,10 @@
#![feature(rustc_attrs)]
#[derive(Debug)]
#[rustc_layout_scalar_valid_range_start(2)]
struct NonZeroAndOneU8(u8);
fn main() {
let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
//~^ ERROR mismatched types
}

View file

@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/ranged-ctor-as-fn-ptr.rs:8:40
|
LL | let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
| ------------------------- ^^^^^^^^^^^^^^^ expected normal fn, found unsafe fn
| |
| expected due to this
|
= note: expected fn pointer `fn(_) -> NonZeroAndOneU8`
found struct constructor `unsafe fn(_) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
= note: unsafe functions cannot be coerced into safe function pointers
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.