Skip to content

Commit

Permalink
Add a suggestion to cast target_feature fn items to fn pointers.
Browse files Browse the repository at this point in the history
See
rust-lang#134090 (comment)
for the motivation behind this suggestion.
  • Loading branch information
veluca93 committed Jan 25, 2025
1 parent 203e6c1 commit 6bdc877
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/rfcs/rfc-2396-target_feature-11/fn-traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -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}`
Expand Down
49 changes: 36 additions & 13 deletions tests/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr
Original file line number Diff line number Diff line change
@@ -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}`
Expand All @@ -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}`
Expand All @@ -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}`
Expand All @@ -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 */ }`
Expand All @@ -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 */ }`
Expand All @@ -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 */ }`
Expand All @@ -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`.

0 comments on commit 6bdc877

Please sign in to comment.