Skip to content

Commit

Permalink
bless the tests and add a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
Orion Gonzalez committed Nov 28, 2024
1 parent b36dcc1 commit 1e4817c
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,8 @@ pub(crate) struct PassToVariadicFunction<'a, 'tcx> {

#[derive(Diagnostic)]
#[diag(hir_typeck_fn_item_to_variadic_function, code = E0617)]
#[help]
#[note]
pub(crate) struct PassFnItemToVariadicFunction {
#[primary_span]
pub span: Span,
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/c-variadic/fn-item-diagnostic-issue-69232.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// https://github.com/rust-lang/rust/issues/69232

extern "C" {
fn foo(x: usize, ...);
}

fn test() -> u8 {
127
}

fn main() {
unsafe { foo(1, test) }; //~ ERROR can't pass a function item to a variadic function
}
16 changes: 16 additions & 0 deletions tests/ui/c-variadic/fn-item-diagnostic-issue-69232.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0617]: can't pass a function item to a variadic function
--> $DIR/fn-item-diagnostic-issue-69232.rs:12:21
|
LL | unsafe { foo(1, test) };
| ^^^^
|
= help: a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
help: use a function pointer instead
|
LL | unsafe { foo(1, test as fn() -> u8) };
| +++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0617`.
5 changes: 3 additions & 2 deletions tests/ui/c-variadic/issue-32201.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ fn bar(_: *const u8) {}
fn main() {
unsafe {
foo(0, bar);
//~^ ERROR can't pass `fn(*const u8) {bar}` to variadic function
//~| HELP cast the value to `fn(*const u8)`
//~^ ERROR can't pass a function item to a variadic function
//~| HELP a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
////~| HELP use a function pointer instead
}
}
11 changes: 9 additions & 2 deletions tests/ui/c-variadic/issue-32201.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
error[E0617]: can't pass `fn(*const u8) {bar}` to variadic function
error[E0617]: can't pass a function item to a variadic function
--> $DIR/issue-32201.rs:9:16
|
LL | foo(0, bar);
| ^^^ help: cast the value to `fn(*const u8)`: `bar as fn(*const u8)`
| ^^^
|
= help: a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
help: use a function pointer instead
|
LL | foo(0, bar as fn(*const u8));
| ++++++++++++++++

error: aborting due to 1 previous error

Expand Down
5 changes: 3 additions & 2 deletions tests/ui/error-codes/E0617.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ fn main() {
//~^ ERROR can't pass `u16` to variadic function
//~| HELP cast the value to `c_uint`
printf(::std::ptr::null(), printf);
//~^ ERROR can't pass `unsafe extern "C" fn(*const i8, ...) {printf}` to variadic function
//~| HELP cast the value to `unsafe extern "C" fn(*const i8, ...)`
//~^ ERROR can't pass a function item to a variadic function
//~| HELP a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
//~| HELP use a function pointer instead
}
}
8 changes: 5 additions & 3 deletions tests/ui/error-codes/E0617.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ error[E0617]: can't pass `u16` to variadic function
LL | printf(::std::ptr::null(), 0u16);
| ^^^^ help: cast the value to `c_uint`: `0u16 as c_uint`

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

error: aborting due to 6 previous errors

Expand Down

0 comments on commit 1e4817c

Please sign in to comment.