mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-31 09:04:18 +00:00
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:
parent
0100a94231
commit
bf66723c0e
@ -266,12 +266,10 @@ impl<T> Trait<T> for X {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(ty::FnPtr(_), ty::FnDef(def, _))
|
(ty::FnPtr(sig), ty::FnDef(def_id, _)) | (ty::FnDef(def_id, _), ty::FnPtr(sig)) => {
|
||||||
if let hir::def::DefKind::Fn = tcx.def_kind(def) => {
|
if tcx.fn_sig(*def_id).skip_binder().unsafety() < sig.unsafety() {
|
||||||
diag.note(
|
diag.note("unsafe functions cannot be coerced into safe function pointers");
|
||||||
"when the arguments and return types match, functions can be coerced \
|
}
|
||||||
to function pointers",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ LL | foo(f, w);
|
|||||||
| ^
|
| ^
|
||||||
= note: expected fn pointer `fn(i32)`
|
= note: expected fn pointer `fn(i32)`
|
||||||
found fn item `fn(u32) {f}`
|
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>`
|
note: expected `Wrapper<i32>`, found `Wrapper<isize>`
|
||||||
--> $DIR/two-mismatch-notes.rs:10:12
|
--> $DIR/two-mismatch-notes.rs:10:12
|
||||||
|
|
|
|
||||||
|
@ -46,7 +46,6 @@ LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
|
|||||||
|
|
|
|
||||||
= note: expected fn pointer `unsafe extern "C" fn(_, _)`
|
= note: expected fn pointer `unsafe extern "C" fn(_, _)`
|
||||||
found fn item `unsafe extern "C" fn(_, _, ...) {foo}`
|
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
|
error[E0308]: mismatched types
|
||||||
--> $DIR/variadic-ffi-1.rs:26:54
|
--> $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(_, _, ...)`
|
= note: expected fn pointer `extern "C" fn(_, _, ...)`
|
||||||
found fn item `extern "C" fn(_, _) {bar}`
|
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
|
error[E0617]: can't pass `f32` to variadic function
|
||||||
--> $DIR/variadic-ffi-1.rs:28:19
|
--> $DIR/variadic-ffi-1.rs:28:19
|
||||||
|
@ -80,7 +80,6 @@ LL | let e: &fn(u32) -> u32 = &foo;
|
|||||||
= note: expected reference `&fn(u32) -> u32`
|
= note: expected reference `&fn(u32) -> u32`
|
||||||
found reference `&fn(u32) -> u32 {foo}`
|
found reference `&fn(u32) -> u32 {foo}`
|
||||||
= note: fn items are distinct from fn pointers
|
= 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
|
help: consider casting to a fn pointer
|
||||||
|
|
|
|
||||||
LL | let e: &fn(u32) -> u32 = &(foo as fn(u32) -> u32);
|
LL | let e: &fn(u32) -> u32 = &(foo as fn(u32) -> u32);
|
||||||
|
@ -12,5 +12,4 @@ fn main() {
|
|||||||
//~| NOTE expected fn pointer, found fn item
|
//~| NOTE expected fn pointer, found fn item
|
||||||
//~| NOTE expected fn pointer `fn(i32, u32)`
|
//~| NOTE expected fn pointer `fn(i32, u32)`
|
||||||
//~| NOTE arguments to this function are incorrect
|
//~| NOTE arguments to this function are incorrect
|
||||||
//~| NOTE when the arguments and return types match, functions can be coerced to function pointers
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ LL | needs_ptr(foo);
|
|||||||
|
|
|
|
||||||
= note: expected fn pointer `fn(i32, u32)`
|
= note: expected fn pointer `fn(i32, u32)`
|
||||||
found fn item `fn(i32, i32) {foo}`
|
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
|
note: function defined here
|
||||||
--> $DIR/signature-error-reporting-under-verbose.rs:5:4
|
--> $DIR/signature-error-reporting-under-verbose.rs:5:4
|
||||||
|
|
|
|
||||||
|
@ -8,7 +8,6 @@ LL | fn main() { f(bar) }
|
|||||||
|
|
|
|
||||||
= note: expected fn pointer `fn()`
|
= note: expected fn pointer `fn()`
|
||||||
found fn item `extern "C" fn() {bar}`
|
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
|
note: function defined here
|
||||||
--> $DIR/issue-10764.rs:1:4
|
--> $DIR/issue-10764.rs:1:4
|
||||||
|
|
|
|
||||||
|
@ -8,7 +8,6 @@ LL | needs_i32_ref_fn(foo::<()>);
|
|||||||
|
|
|
|
||||||
= note: expected fn pointer `fn(&'static i32, i32)`
|
= note: expected fn pointer `fn(&'static i32, i32)`
|
||||||
found fn item `fn(i32, &'static i32) {foo::<()>}`
|
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
|
note: function defined here
|
||||||
--> $DIR/normalize-fn-sig.rs:11:4
|
--> $DIR/normalize-fn-sig.rs:11:4
|
||||||
|
|
|
|
||||||
|
@ -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)`
|
= 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::<'_, '_>}`
|
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
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -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)`
|
= 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::<'_, '_, '_>}`
|
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
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ LL | want_G(baz);
|
|||||||
|
|
|
|
||||||
= note: expected fn pointer `for<'cx> fn(&'cx S) -> &'static S`
|
= note: expected fn pointer `for<'cx> fn(&'cx S) -> &'static S`
|
||||||
found fn item `for<'a> fn(&'a S) -> &'a S {baz}`
|
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
|
note: function defined here
|
||||||
--> $DIR/regions-fn-subtyping-return-static-fail.rs:20:4
|
--> $DIR/regions-fn-subtyping-return-static-fail.rs:20:4
|
||||||
|
|
|
|
||||||
|
@ -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)`
|
= 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::<'_, '_>}`
|
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
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -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`
|
= note: expected fn pointer `unsafe extern "rust-intrinsic" fn(isize) -> usize`
|
||||||
found fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
|
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
|
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
|
--> $DIR/reify-intrinsic.rs:11:13
|
||||||
|
@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
|
|||||||
found fn item `fn() {foo}`
|
found fn item `fn() {foo}`
|
||||||
= note: fn items are distinct from fn pointers
|
= note: fn items are distinct from fn pointers
|
||||||
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function 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
|
help: consider casting to a fn pointer
|
||||||
|
|
|
|
||||||
LL | let foo: fn() = foo as fn();
|
LL | let foo: fn() = foo as fn();
|
||||||
|
@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
|
|||||||
found fn item `fn() {foo}`
|
found fn item `fn() {foo}`
|
||||||
= note: fn items are distinct from fn pointers
|
= note: fn items are distinct from fn pointers
|
||||||
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function 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
|
help: consider casting to a fn pointer
|
||||||
|
|
|
|
||||||
LL | let foo: fn() = foo as fn();
|
LL | let foo: fn() = foo as fn();
|
||||||
|
@ -7,7 +7,6 @@ LL | func: &foo,
|
|||||||
= note: expected reference `&fn() -> Option<isize>`
|
= note: expected reference `&fn() -> Option<isize>`
|
||||||
found reference `&fn() -> Option<isize> {foo}`
|
found reference `&fn() -> Option<isize> {foo}`
|
||||||
= note: fn items are distinct from fn pointers
|
= 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
|
help: consider casting to a fn pointer
|
||||||
|
|
|
|
||||||
LL | func: &(foo as fn() -> Option<isize>),
|
LL | func: &(foo as fn() -> Option<isize>),
|
||||||
|
10
tests/ui/unsafe/ranged-ctor-as-fn-ptr.rs
Normal file
10
tests/ui/unsafe/ranged-ctor-as-fn-ptr.rs
Normal 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
|
||||||
|
}
|
15
tests/ui/unsafe/ranged-ctor-as-fn-ptr.stderr
Normal file
15
tests/ui/unsafe/ranged-ctor-as-fn-ptr.stderr
Normal 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`.
|
Loading…
Reference in New Issue
Block a user