Add a suggestion to cast target_feature fn items to fn pointers.
See https://github.com/rust-lang/rust/pull/134090#issuecomment-2612197095 for the motivation behind this suggestion.
This commit is contained in:
parent
203e6c127c
commit
6bdc8778db
3 changed files with 47 additions and 13 deletions
|
|
@ -462,6 +462,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
|||
err.note(
|
||||
"`#[target_feature]` functions do not implement the `Fn` traits",
|
||||
);
|
||||
err.note(
|
||||
"try casting the function to a `fn` pointer or wrapping it in a closure",
|
||||
);
|
||||
}
|
||||
|
||||
self.try_to_add_help_message(
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@
|
|||
#[target_feature(enable = "avx")]
|
||||
fn foo() {}
|
||||
|
||||
#[target_feature(enable = "avx")]
|
||||
fn bar(arg: i32) {}
|
||||
|
||||
#[target_feature(enable = "avx")]
|
||||
unsafe fn foo_unsafe() {}
|
||||
|
||||
|
|
@ -20,10 +23,15 @@ fn call_once(f: impl FnOnce()) {
|
|||
f()
|
||||
}
|
||||
|
||||
fn call_once_i32(f: impl FnOnce(i32)) {
|
||||
f(0)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
call(foo); //~ ERROR expected a `Fn()` closure, found `#[target_features] fn() {foo}`
|
||||
call_mut(foo); //~ ERROR expected a `FnMut()` closure, found `#[target_features] fn() {foo}`
|
||||
call_once(foo); //~ ERROR expected a `FnOnce()` closure, found `#[target_features] fn() {foo}`
|
||||
call_once_i32(bar); //~ ERROR expected a `FnOnce(i32)` closure, found `#[target_features] fn(i32) {bar}`
|
||||
|
||||
call(foo_unsafe);
|
||||
//~^ ERROR expected a `Fn()` closure, found `unsafe fn() {foo_unsafe}`
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error[E0277]: expected a `Fn()` closure, found `#[target_features] fn() {foo}`
|
||||
--> $DIR/fn-traits.rs:24:10
|
||||
--> $DIR/fn-traits.rs:31:10
|
||||
|
|
||||
LL | call(foo);
|
||||
| ---- ^^^ expected an `Fn()` closure, found `#[target_features] fn() {foo}`
|
||||
|
|
@ -9,14 +9,15 @@ LL | call(foo);
|
|||
= help: the trait `Fn()` is not implemented for fn item `#[target_features] fn() {foo}`
|
||||
= note: wrap the `#[target_features] fn() {foo}` in a closure with no arguments: `|| { /* code */ }`
|
||||
= note: `#[target_feature]` functions do not implement the `Fn` traits
|
||||
= note: try casting the function to a `fn` pointer or wrapping it in a closure
|
||||
note: required by a bound in `call`
|
||||
--> $DIR/fn-traits.rs:11:17
|
||||
--> $DIR/fn-traits.rs:14:17
|
||||
|
|
||||
LL | fn call(f: impl Fn()) {
|
||||
| ^^^^ required by this bound in `call`
|
||||
|
||||
error[E0277]: expected a `FnMut()` closure, found `#[target_features] fn() {foo}`
|
||||
--> $DIR/fn-traits.rs:25:14
|
||||
--> $DIR/fn-traits.rs:32:14
|
||||
|
|
||||
LL | call_mut(foo);
|
||||
| -------- ^^^ expected an `FnMut()` closure, found `#[target_features] fn() {foo}`
|
||||
|
|
@ -26,14 +27,15 @@ LL | call_mut(foo);
|
|||
= help: the trait `FnMut()` is not implemented for fn item `#[target_features] fn() {foo}`
|
||||
= note: wrap the `#[target_features] fn() {foo}` in a closure with no arguments: `|| { /* code */ }`
|
||||
= note: `#[target_feature]` functions do not implement the `Fn` traits
|
||||
= note: try casting the function to a `fn` pointer or wrapping it in a closure
|
||||
note: required by a bound in `call_mut`
|
||||
--> $DIR/fn-traits.rs:15:25
|
||||
--> $DIR/fn-traits.rs:18:25
|
||||
|
|
||||
LL | fn call_mut(mut f: impl FnMut()) {
|
||||
| ^^^^^^^ required by this bound in `call_mut`
|
||||
|
||||
error[E0277]: expected a `FnOnce()` closure, found `#[target_features] fn() {foo}`
|
||||
--> $DIR/fn-traits.rs:26:15
|
||||
--> $DIR/fn-traits.rs:33:15
|
||||
|
|
||||
LL | call_once(foo);
|
||||
| --------- ^^^ expected an `FnOnce()` closure, found `#[target_features] fn() {foo}`
|
||||
|
|
@ -43,14 +45,32 @@ LL | call_once(foo);
|
|||
= help: the trait `FnOnce()` is not implemented for fn item `#[target_features] fn() {foo}`
|
||||
= note: wrap the `#[target_features] fn() {foo}` in a closure with no arguments: `|| { /* code */ }`
|
||||
= note: `#[target_feature]` functions do not implement the `Fn` traits
|
||||
= note: try casting the function to a `fn` pointer or wrapping it in a closure
|
||||
note: required by a bound in `call_once`
|
||||
--> $DIR/fn-traits.rs:19:22
|
||||
--> $DIR/fn-traits.rs:22:22
|
||||
|
|
||||
LL | fn call_once(f: impl FnOnce()) {
|
||||
| ^^^^^^^^ required by this bound in `call_once`
|
||||
|
||||
error[E0277]: expected a `FnOnce(i32)` closure, found `#[target_features] fn(i32) {bar}`
|
||||
--> $DIR/fn-traits.rs:34:19
|
||||
|
|
||||
LL | call_once_i32(bar);
|
||||
| ------------- ^^^ expected an `FnOnce(i32)` closure, found `#[target_features] fn(i32) {bar}`
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: the trait `FnOnce(i32)` is not implemented for fn item `#[target_features] fn(i32) {bar}`
|
||||
= note: `#[target_feature]` functions do not implement the `Fn` traits
|
||||
= note: try casting the function to a `fn` pointer or wrapping it in a closure
|
||||
note: required by a bound in `call_once_i32`
|
||||
--> $DIR/fn-traits.rs:26:26
|
||||
|
|
||||
LL | fn call_once_i32(f: impl FnOnce(i32)) {
|
||||
| ^^^^^^^^^^^ required by this bound in `call_once_i32`
|
||||
|
||||
error[E0277]: expected a `Fn()` closure, found `unsafe fn() {foo_unsafe}`
|
||||
--> $DIR/fn-traits.rs:28:10
|
||||
--> $DIR/fn-traits.rs:36:10
|
||||
|
|
||||
LL | call(foo_unsafe);
|
||||
| ---- ^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
|
||||
|
|
@ -61,14 +81,15 @@ LL | call(foo_unsafe);
|
|||
= note: unsafe function cannot be called generically without an unsafe block
|
||||
= note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ }`
|
||||
= note: `#[target_feature]` functions do not implement the `Fn` traits
|
||||
= note: try casting the function to a `fn` pointer or wrapping it in a closure
|
||||
note: required by a bound in `call`
|
||||
--> $DIR/fn-traits.rs:11:17
|
||||
--> $DIR/fn-traits.rs:14:17
|
||||
|
|
||||
LL | fn call(f: impl Fn()) {
|
||||
| ^^^^ required by this bound in `call`
|
||||
|
||||
error[E0277]: expected a `FnMut()` closure, found `unsafe fn() {foo_unsafe}`
|
||||
--> $DIR/fn-traits.rs:30:14
|
||||
--> $DIR/fn-traits.rs:38:14
|
||||
|
|
||||
LL | call_mut(foo_unsafe);
|
||||
| -------- ^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
|
||||
|
|
@ -79,14 +100,15 @@ LL | call_mut(foo_unsafe);
|
|||
= note: unsafe function cannot be called generically without an unsafe block
|
||||
= note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ }`
|
||||
= note: `#[target_feature]` functions do not implement the `Fn` traits
|
||||
= note: try casting the function to a `fn` pointer or wrapping it in a closure
|
||||
note: required by a bound in `call_mut`
|
||||
--> $DIR/fn-traits.rs:15:25
|
||||
--> $DIR/fn-traits.rs:18:25
|
||||
|
|
||||
LL | fn call_mut(mut f: impl FnMut()) {
|
||||
| ^^^^^^^ required by this bound in `call_mut`
|
||||
|
||||
error[E0277]: expected a `FnOnce()` closure, found `unsafe fn() {foo_unsafe}`
|
||||
--> $DIR/fn-traits.rs:32:15
|
||||
--> $DIR/fn-traits.rs:40:15
|
||||
|
|
||||
LL | call_once(foo_unsafe);
|
||||
| --------- ^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
|
||||
|
|
@ -97,12 +119,13 @@ LL | call_once(foo_unsafe);
|
|||
= note: unsafe function cannot be called generically without an unsafe block
|
||||
= note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ }`
|
||||
= note: `#[target_feature]` functions do not implement the `Fn` traits
|
||||
= note: try casting the function to a `fn` pointer or wrapping it in a closure
|
||||
note: required by a bound in `call_once`
|
||||
--> $DIR/fn-traits.rs:19:22
|
||||
--> $DIR/fn-traits.rs:22:22
|
||||
|
|
||||
LL | fn call_once(f: impl FnOnce()) {
|
||||
| ^^^^^^^^ required by this bound in `call_once`
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0277`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue