Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

0 commit comments

Comments
 (0)