Skip to content

Commit bf66723

Browse files
Test and note unsafe ctor to fn ptr coercion
Also remove a note that I don't consider to be very useful in context.
1 parent 0100a94 commit bf66723

18 files changed

+29
-22
lines changed

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,10 @@ impl<T> Trait<T> for X {
266266
}
267267
}
268268
}
269-
(ty::FnPtr(_), ty::FnDef(def, _))
270-
if let hir::def::DefKind::Fn = tcx.def_kind(def) => {
271-
diag.note(
272-
"when the arguments and return types match, functions can be coerced \
273-
to function pointers",
274-
);
269+
(ty::FnPtr(sig), ty::FnDef(def_id, _)) | (ty::FnDef(def_id, _), ty::FnPtr(sig)) => {
270+
if tcx.fn_sig(*def_id).skip_binder().unsafety() < sig.unsafety() {
271+
diag.note("unsafe functions cannot be coerced into safe function pointers");
272+
}
275273
}
276274
_ => {}
277275
}

tests/ui/argument-suggestions/two-mismatch-notes.stderr

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

tests/ui/c-variadic/variadic-ffi-1.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
4646
|
4747
= note: expected fn pointer `unsafe extern "C" fn(_, _)`
4848
found fn item `unsafe extern "C" fn(_, _, ...) {foo}`
49-
= note: when the arguments and return types match, functions can be coerced to function pointers
5049

5150
error[E0308]: mismatched types
5251
--> $DIR/variadic-ffi-1.rs:26:54
@@ -58,7 +57,6 @@ LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
5857
|
5958
= note: expected fn pointer `extern "C" fn(_, _, ...)`
6059
found fn item `extern "C" fn(_, _) {bar}`
61-
= note: when the arguments and return types match, functions can be coerced to function pointers
6260

6361
error[E0617]: can't pass `f32` to variadic function
6462
--> $DIR/variadic-ffi-1.rs:28:19

tests/ui/fn/fn-pointer-mismatch.stderr

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

tests/ui/fn/signature-error-reporting-under-verbose.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ fn main() {
1212
//~| NOTE expected fn pointer, found fn item
1313
//~| NOTE expected fn pointer `fn(i32, u32)`
1414
//~| NOTE arguments to this function are incorrect
15-
//~| NOTE when the arguments and return types match, functions can be coerced to function pointers
1615
}

tests/ui/fn/signature-error-reporting-under-verbose.stderr

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

tests/ui/issues/issue-10764.stderr

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

tests/ui/mismatched_types/normalize-fn-sig.stderr

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

tests/ui/regions/region-lifetime-bounds-on-fns-where-clause.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize) = a;
88
|
99
= note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
1010
found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error: aborting due to previous error
1413

tests/ui/regions/region-multiple-lifetime-bounds-on-fns-where-clause.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize, &mut &isize) = a;
88
|
99
= note: expected fn pointer `for<'a, 'b, 'c, 'd, 'e, 'f> fn(&'a mut &'b isize, &'c mut &'d isize, &'e mut &'f isize)`
1010
found fn item `for<'a, 'b, 'c> fn(&'a mut &isize, &'b mut &isize, &'c mut &isize) {a::<'_, '_, '_>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error: aborting due to previous error
1413

tests/ui/regions/regions-fn-subtyping-return-static-fail.stderr

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

tests/ui/regions/regions-lifetime-bounds-on-fns.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: fn(&mut &isize, &mut &isize) = a;
88
|
99
= note: expected fn pointer `for<'a, 'b, 'c, 'd> fn(&'a mut &'b isize, &'c mut &'d isize)`
1010
found fn item `for<'a, 'b> fn(&'a mut &isize, &'b mut &isize) {a::<'_, '_>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error: aborting due to previous error
1413

tests/ui/reify-intrinsic.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | let _: unsafe extern "rust-intrinsic" fn(isize) -> usize = std::mem::tr
88
|
99
= note: expected fn pointer `unsafe extern "rust-intrinsic" fn(isize) -> usize`
1010
found fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
11-
= note: when the arguments and return types match, functions can be coerced to function pointers
1211

1312
error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
1413
--> $DIR/reify-intrinsic.rs:11:13

tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
1313
found fn item `fn() {foo}`
1414
= note: fn items are distinct from fn pointers
1515
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
16-
= note: when the arguments and return types match, functions can be coerced to function pointers
1716
help: consider casting to a fn pointer
1817
|
1918
LL | let foo: fn() = foo as fn();

tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LL | let foo: fn() = foo;
1313
found fn item `fn() {foo}`
1414
= note: fn items are distinct from fn pointers
1515
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
16-
= note: when the arguments and return types match, functions can be coerced to function pointers
1716
help: consider casting to a fn pointer
1817
|
1918
LL | let foo: fn() = foo as fn();

tests/ui/static/static-reference-to-fn-1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | func: &foo,
77
= note: expected reference `&fn() -> Option<isize>`
88
found reference `&fn() -> Option<isize> {foo}`
99
= note: fn items are distinct from fn pointers
10-
= note: when the arguments and return types match, functions can be coerced to function pointers
1110
help: consider casting to a fn pointer
1211
|
1312
LL | func: &(foo as fn() -> Option<isize>),
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[derive(Debug)]
4+
#[rustc_layout_scalar_valid_range_start(2)]
5+
struct NonZeroAndOneU8(u8);
6+
7+
fn main() {
8+
let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
9+
//~^ ERROR mismatched types
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/ranged-ctor-as-fn-ptr.rs:8:40
3+
|
4+
LL | let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
5+
| ------------------------- ^^^^^^^^^^^^^^^ expected normal fn, found unsafe fn
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected fn pointer `fn(_) -> NonZeroAndOneU8`
10+
found struct constructor `unsafe fn(_) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
11+
= note: unsafe functions cannot be coerced into safe function pointers
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)