Skip to content

Commit 21abd9b

Browse files
author
Orion Gonzalez
committed
bless the tests and add a new one
1 parent c83d5bd commit 21abd9b

File tree

7 files changed

+42
-12
lines changed

7 files changed

+42
-12
lines changed

compiler/rustc_hir_typeck/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,8 @@ pub(crate) struct PassToVariadicFunction<'a, 'tcx> {
800800

801801
#[derive(Diagnostic)]
802802
#[diag(hir_typeck_fn_item_to_variadic_function, code = E0617)]
803+
#[help]
804+
#[note]
803805
pub(crate) struct PassFnItemToVariadicFunction {
804806
#[primary_span]
805807
pub span: Span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://github.com/rust-lang/rust/issues/69232
2+
3+
extern "C" {
4+
fn foo(x: usize, ...);
5+
}
6+
7+
fn test() -> u8 {
8+
127
9+
}
10+
11+
fn main() {
12+
unsafe { foo(1, test) }; //~ ERROR can't pass a function item to a variadic function
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0617]: can't pass a function item to a variadic function
2+
--> $DIR/fn-item-diagnostic-issue-69232.rs:12:21
3+
|
4+
LL | unsafe { foo(1, test) };
5+
| ^^^^- help: use a function pointer instead: `as fn() -> u8`
6+
|
7+
= help: a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
8+
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0617`.

tests/ui/c-variadic/issue-32201.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ fn bar(_: *const u8) {}
77
fn main() {
88
unsafe {
99
foo(0, bar);
10-
//~^ ERROR can't pass `fn(*const u8) {bar}` to variadic function
11-
//~| HELP cast the value to `fn(*const u8)`
10+
//~^ ERROR can't pass a function item to a variadic function
11+
//~| HELP a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
12+
////~| HELP use a function pointer instead
1213
}
1314
}

tests/ui/c-variadic/issue-32201.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error[E0617]: can't pass `fn(*const u8) {bar}` to variadic function
1+
error[E0617]: can't pass a function item to a variadic function
22
--> $DIR/issue-32201.rs:9:16
33
|
44
LL | foo(0, bar);
5-
| ^^^ help: cast the value to `fn(*const u8)`: `bar as fn(*const u8)`
5+
| ^^^- help: use a function pointer instead: `as fn(*const u8)`
6+
|
7+
= help: a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
8+
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
69

710
error: aborting due to 1 previous error
811

tests/ui/error-codes/E0617.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ fn main() {
2020
//~^ ERROR can't pass `u16` to variadic function
2121
//~| HELP cast the value to `c_uint`
2222
printf(::std::ptr::null(), printf);
23-
//~^ ERROR can't pass `unsafe extern "C" fn(*const i8, ...) {printf}` to variadic function
24-
//~| HELP cast the value to `unsafe extern "C" fn(*const i8, ...)`
23+
//~^ ERROR can't pass a function item to a variadic function
24+
//~| HELP a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
25+
//~| HELP use a function pointer instead
2526
}
2627
}

tests/ui/error-codes/E0617.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ error[E0617]: can't pass `u16` to variadic function
2828
LL | printf(::std::ptr::null(), 0u16);
2929
| ^^^^ help: cast the value to `c_uint`: `0u16 as c_uint`
3030

31-
error[E0617]: can't pass `unsafe extern "C" fn(*const i8, ...) {printf}` to variadic function
31+
error[E0617]: can't pass a function item to a variadic function
3232
--> $DIR/E0617.rs:22:36
3333
|
3434
LL | printf(::std::ptr::null(), printf);
35-
| ^^^^^^
35+
| ^^^^^^- help: use a function pointer instead: `as unsafe extern "C" fn(*const i8, ...)`
3636
|
37-
help: cast the value to `unsafe extern "C" fn(*const i8, ...)`
38-
|
39-
LL | printf(::std::ptr::null(), printf as unsafe extern "C" fn(*const i8, ...));
40-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37+
= help: a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
38+
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
4139

4240
error: aborting due to 6 previous errors
4341

0 commit comments

Comments
 (0)