Skip to content

Commit 6d18fbb

Browse files
committed
diagnostics: tweak error message to give more rationale to unsafe Fn
1 parent dcf7ce8 commit 6d18fbb

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

library/core/src/ops/function.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@
6161
),
6262
on(
6363
_Self = "unsafe fn",
64+
note = "unsafe function cannot be called generically without an unsafe block",
6465
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
65-
note = "unsafe functions must be wrapped in closures: `|| unsafe {{ /* code */ }}`"
66+
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
6667
),
6768
message = "expected a `{Fn}<{Args}>` closure, found `{Self}`",
6869
label = "expected an `Fn<{Args}>` closure, found `{Self}`"
@@ -146,8 +147,9 @@ pub trait Fn<Args>: FnMut<Args> {
146147
),
147148
on(
148149
_Self = "unsafe fn",
150+
note = "unsafe function cannot be called generically without an unsafe block",
149151
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
150-
note = "unsafe functions must be wrapped in closures: `|| unsafe {{ /* code */ }}`"
152+
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
151153
),
152154
message = "expected a `{FnMut}<{Args}>` closure, found `{Self}`",
153155
label = "expected an `FnMut<{Args}>` closure, found `{Self}`"
@@ -223,8 +225,9 @@ pub trait FnMut<Args>: FnOnce<Args> {
223225
),
224226
on(
225227
_Self = "unsafe fn",
228+
note = "unsafe function cannot be called generically without an unsafe block",
226229
// SAFETY: tidy is not smart enough to tell that the below unsafe block is a string
227-
note = "unsafe functions must be wrapped in closures: `|| unsafe {{ /* code */ }}`"
230+
label = "call the function in a closure: `|| unsafe {{ /* code */ }}`"
228231
),
229232
message = "expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
230233
label = "expected an `FnOnce<{Args}>` closure, found `{Self}`"

src/test/ui/closures/coerce-unsafe-to-closure.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ error[E0277]: expected a `FnOnce<(&str,)>` closure, found `unsafe extern "rust-i
22
--> $DIR/coerce-unsafe-to-closure.rs:2:44
33
|
44
LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
5-
| --- ^^^^^^^^^^^^^^^^^^^ expected an `FnOnce<(&str,)>` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
5+
| --- ^^^^^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
66
| |
77
| required by a bound introduced by this call
88
|
99
= help: the trait `FnOnce<(&str,)>` is not implemented for `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
10-
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
10+
= note: unsafe function cannot be called generically without an unsafe block
1111
note: required by a bound in `Option::<T>::map`
1212
--> $SRC_DIR/core/src/option.rs:LL:COL
1313
|

src/test/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ error[E0277]: expected a `Fn<()>` closure, found `unsafe fn() {foo_unsafe}`
5353
--> $DIR/fn-traits.rs:28:10
5454
|
5555
LL | call(foo_unsafe);
56-
| ---- ^^^^^^^^^^ expected an `Fn<()>` closure, found `unsafe fn() {foo_unsafe}`
56+
| ---- ^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
5757
| |
5858
| required by a bound introduced by this call
5959
|
@@ -70,7 +70,7 @@ error[E0277]: expected a `FnMut<()>` closure, found `unsafe fn() {foo_unsafe}`
7070
--> $DIR/fn-traits.rs:30:14
7171
|
7272
LL | call_mut(foo_unsafe);
73-
| -------- ^^^^^^^^^^ expected an `FnMut<()>` closure, found `unsafe fn() {foo_unsafe}`
73+
| -------- ^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
7474
| |
7575
| required by a bound introduced by this call
7676
|
@@ -87,7 +87,7 @@ error[E0277]: expected a `FnOnce<()>` closure, found `unsafe fn() {foo_unsafe}`
8787
--> $DIR/fn-traits.rs:32:15
8888
|
8989
LL | call_once(foo_unsafe);
90-
| --------- ^^^^^^^^^^ expected an `FnOnce<()>` closure, found `unsafe fn() {foo_unsafe}`
90+
| --------- ^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
9191
| |
9292
| required by a bound introduced by this call
9393
|

src/test/ui/unboxed-closures/unboxed-closures-unsafe-extern-fn.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ error[E0277]: expected a `Fn<(&isize,)>` closure, found `for<'r> unsafe fn(&'r i
22
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:20:21
33
|
44
LL | let x = call_it(&square, 22);
5-
| ------- ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
5+
| ------- ^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
66
| |
77
| required by a bound introduced by this call
88
|
99
= help: the trait `for<'r> Fn<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
10-
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
10+
= note: unsafe function cannot be called generically without an unsafe block
1111
note: required by a bound in `call_it`
1212
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:9:15
1313
|
@@ -18,12 +18,12 @@ error[E0277]: expected a `FnMut<(&isize,)>` closure, found `for<'r> unsafe fn(&'
1818
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:25:25
1919
|
2020
LL | let y = call_it_mut(&mut square, 22);
21-
| ----------- ^^^^^^^^^^^ expected an `FnMut<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
21+
| ----------- ^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
2222
| |
2323
| required by a bound introduced by this call
2424
|
2525
= help: the trait `for<'r> FnMut<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
26-
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
26+
= note: unsafe function cannot be called generically without an unsafe block
2727
note: required by a bound in `call_it_mut`
2828
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:12:19
2929
|
@@ -34,12 +34,12 @@ error[E0277]: expected a `FnOnce<(&isize,)>` closure, found `for<'r> unsafe fn(&
3434
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:30:26
3535
|
3636
LL | let z = call_it_once(square, 22);
37-
| ------------ ^^^^^^ expected an `FnOnce<(&isize,)>` closure, found `for<'r> unsafe fn(&'r isize) -> isize {square}`
37+
| ------------ ^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
3838
| |
3939
| required by a bound introduced by this call
4040
|
4141
= help: the trait `for<'r> FnOnce<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
42-
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
42+
= note: unsafe function cannot be called generically without an unsafe block
4343
note: required by a bound in `call_it_once`
4444
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:15:20
4545
|

src/test/ui/unboxed-closures/unboxed-closures-wrong-arg-type-extern-fn.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ error[E0277]: expected a `Fn<(&isize,)>` closure, found `unsafe fn(isize) -> isi
22
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:21:21
33
|
44
LL | let x = call_it(&square, 22);
5-
| ------- ^^^^^^^ expected an `Fn<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
5+
| ------- ^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
66
| |
77
| required by a bound introduced by this call
88
|
99
= help: the trait `for<'r> Fn<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
10-
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
10+
= note: unsafe function cannot be called generically without an unsafe block
1111
note: required by a bound in `call_it`
1212
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:10:15
1313
|
@@ -18,12 +18,12 @@ error[E0277]: expected a `FnMut<(&isize,)>` closure, found `unsafe fn(isize) ->
1818
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:26:25
1919
|
2020
LL | let y = call_it_mut(&mut square, 22);
21-
| ----------- ^^^^^^^^^^^ expected an `FnMut<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
21+
| ----------- ^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
2222
| |
2323
| required by a bound introduced by this call
2424
|
2525
= help: the trait `for<'r> FnMut<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
26-
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
26+
= note: unsafe function cannot be called generically without an unsafe block
2727
note: required by a bound in `call_it_mut`
2828
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:13:19
2929
|
@@ -34,12 +34,12 @@ error[E0277]: expected a `FnOnce<(&isize,)>` closure, found `unsafe fn(isize) ->
3434
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:31:26
3535
|
3636
LL | let z = call_it_once(square, 22);
37-
| ------------ ^^^^^^ expected an `FnOnce<(&isize,)>` closure, found `unsafe fn(isize) -> isize {square}`
37+
| ------------ ^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
3838
| |
3939
| required by a bound introduced by this call
4040
|
4141
= help: the trait `for<'r> FnOnce<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
42-
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
42+
= note: unsafe function cannot be called generically without an unsafe block
4343
note: required by a bound in `call_it_once`
4444
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:16:20
4545
|

0 commit comments

Comments
 (0)