1
Fork 0

diagnostics: give a special note for unsafe fn / Fn/FnOnce/FnMut

Fixes #90073
This commit is contained in:
Michael Howell 2022-04-04 17:37:59 -07:00
parent 60e50fc1cf
commit bec8dbdb60
5 changed files with 28 additions and 0 deletions

View file

@ -208,6 +208,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
flags.push((sym::_Self, Some("&[]".to_owned()))); flags.push((sym::_Self, Some("&[]".to_owned())));
} }
if self_ty.is_fn() {
let fn_sig = self_ty.fn_sig(self.tcx);
let shortname = match fn_sig.unsafety() {
hir::Unsafety::Normal => "fn",
hir::Unsafety::Unsafe => "unsafe fn",
};
flags.push((sym::_Self, Some(shortname.to_owned())));
}
if let ty::Array(aty, len) = self_ty.kind() { if let ty::Array(aty, len) = self_ty.kind() {
flags.push((sym::_Self, Some("[]".to_owned()))); flags.push((sym::_Self, Some("[]".to_owned())));
flags.push((sym::_Self, Some(format!("[{}]", aty)))); flags.push((sym::_Self, Some(format!("[{}]", aty))));

View file

@ -59,6 +59,10 @@
Args = "()", Args = "()",
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`" note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
), ),
on(
_Self = "unsafe fn",
note = "unsafe functions must be wrapped in closures: `|| unsafe {{ /* code */ }}`"
),
message = "expected a `{Fn}<{Args}>` closure, found `{Self}`", message = "expected a `{Fn}<{Args}>` closure, found `{Self}`",
label = "expected an `Fn<{Args}>` closure, found `{Self}`" label = "expected an `Fn<{Args}>` closure, found `{Self}`"
)] )]
@ -139,6 +143,10 @@ pub trait Fn<Args>: FnMut<Args> {
Args = "()", Args = "()",
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`" note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
), ),
on(
_Self = "unsafe fn",
note = "unsafe functions must be wrapped in closures: `|| unsafe {{ /* code */ }}`"
),
message = "expected a `{FnMut}<{Args}>` closure, found `{Self}`", message = "expected a `{FnMut}<{Args}>` closure, found `{Self}`",
label = "expected an `FnMut<{Args}>` closure, found `{Self}`" label = "expected an `FnMut<{Args}>` closure, found `{Self}`"
)] )]
@ -211,6 +219,10 @@ pub trait FnMut<Args>: FnOnce<Args> {
Args = "()", Args = "()",
note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`" note = "wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}`"
), ),
on(
_Self = "unsafe fn",
note = "unsafe functions must be wrapped in closures: `|| unsafe {{ /* code */ }}`"
),
message = "expected a `{FnOnce}<{Args}>` closure, found `{Self}`", message = "expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
label = "expected an `FnOnce<{Args}>` closure, found `{Self}`" label = "expected an `FnOnce<{Args}>` closure, found `{Self}`"
)] )]

View file

@ -7,6 +7,7 @@ LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= help: the trait `FnOnce<(&str,)>` is not implemented for `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}` = help: the trait `FnOnce<(&str,)>` is not implemented for `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
note: required by a bound in `Option::<T>::map` note: required by a bound in `Option::<T>::map`
--> $SRC_DIR/core/src/option.rs:LL:COL --> $SRC_DIR/core/src/option.rs:LL:COL
| |

View file

@ -7,6 +7,7 @@ LL | let x = call_it(&square, 22);
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= help: the trait `for<'r> Fn<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}` = help: the trait `for<'r> Fn<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
note: required by a bound in `call_it` note: required by a bound in `call_it`
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:9:15 --> $DIR/unboxed-closures-unsafe-extern-fn.rs:9:15
| |
@ -22,6 +23,7 @@ LL | let y = call_it_mut(&mut square, 22);
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= help: the trait `for<'r> FnMut<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}` = help: the trait `for<'r> FnMut<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
note: required by a bound in `call_it_mut` note: required by a bound in `call_it_mut`
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:12:19 --> $DIR/unboxed-closures-unsafe-extern-fn.rs:12:19
| |
@ -37,6 +39,7 @@ LL | let z = call_it_once(square, 22);
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= help: the trait `for<'r> FnOnce<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}` = help: the trait `for<'r> FnOnce<(&'r isize,)>` is not implemented for `for<'r> unsafe fn(&'r isize) -> isize {square}`
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
note: required by a bound in `call_it_once` note: required by a bound in `call_it_once`
--> $DIR/unboxed-closures-unsafe-extern-fn.rs:15:20 --> $DIR/unboxed-closures-unsafe-extern-fn.rs:15:20
| |

View file

@ -7,6 +7,7 @@ LL | let x = call_it(&square, 22);
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= help: the trait `for<'r> Fn<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}` = help: the trait `for<'r> Fn<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
note: required by a bound in `call_it` note: required by a bound in `call_it`
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:10:15 --> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:10:15
| |
@ -22,6 +23,7 @@ LL | let y = call_it_mut(&mut square, 22);
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= help: the trait `for<'r> FnMut<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}` = help: the trait `for<'r> FnMut<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
note: required by a bound in `call_it_mut` note: required by a bound in `call_it_mut`
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:13:19 --> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:13:19
| |
@ -37,6 +39,7 @@ LL | let z = call_it_once(square, 22);
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= help: the trait `for<'r> FnOnce<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}` = help: the trait `for<'r> FnOnce<(&'r isize,)>` is not implemented for `unsafe fn(isize) -> isize {square}`
= note: unsafe functions must be wrapped in closures: `|| unsafe { /* code */ }`
note: required by a bound in `call_it_once` note: required by a bound in `call_it_once`
--> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:16:20 --> $DIR/unboxed-closures-wrong-arg-type-extern-fn.rs:16:20
| |