Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegation: reject C-variadics #138407

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_hir_analysis/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ fn check_constraints<'tcx>(
emit("recursive delegation is not supported yet");
}

if tcx.fn_sig(sig_id).skip_binder().skip_binder().c_variadic {
// See issue #127443 for explanation.
emit("delegation to C-variadic functions is not allowed");
}

ret
}

Expand Down
25 changes: 25 additions & 0 deletions tests/ui/delegation/fn-header-variadic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ aux-crate:fn_header_aux=fn-header-aux.rs

#![feature(c_variadic)]
#![feature(fn_delegation)]
#![allow(incomplete_features)]

mod to_reuse {
pub unsafe extern "C" fn variadic_fn(n: usize, mut args: ...) {}
}

reuse to_reuse::variadic_fn;
//~^ ERROR delegation to C-variadic functions is not allowed
reuse fn_header_aux::variadic_fn_extern;
//~^ ERROR delegation to C-variadic functions is not allowed

fn main() {
unsafe {
variadic_fn(0);
variadic_fn(0, 1);
variadic_fn_extern(0);
variadic_fn_extern(0, 1);
}
let _: unsafe extern "C" fn(usize, ...) = variadic_fn;
let _: unsafe extern "C" fn(usize, ...) = variadic_fn_extern;
}
22 changes: 22 additions & 0 deletions tests/ui/delegation/fn-header-variadic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: delegation to C-variadic functions is not allowed
--> $DIR/fn-header-variadic.rs:11:17
|
LL | pub unsafe extern "C" fn variadic_fn(n: usize, mut args: ...) {}
| ------------------------------------------------------------- callee defined here
...
LL | reuse to_reuse::variadic_fn;
| ^^^^^^^^^^^

error: delegation to C-variadic functions is not allowed
--> $DIR/fn-header-variadic.rs:13:22
|
LL | reuse fn_header_aux::variadic_fn_extern;
| ^^^^^^^^^^^^^^^^^^
|
::: $DIR/auxiliary/fn-header-aux.rs:7:1
|
LL | pub unsafe extern "C" fn variadic_fn_extern(n: usize, mut args: ...) {}
| -------------------------------------------------------------------- callee defined here

error: aborting due to 2 previous errors

11 changes: 0 additions & 11 deletions tests/ui/delegation/fn-header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
mod to_reuse {
pub unsafe fn unsafe_fn() {}
pub extern "C" fn extern_fn() {}
pub unsafe extern "C" fn variadic_fn(n: usize, mut args: ...) {}
pub const fn const_fn() {}
pub async fn async_fn() {}
}

reuse to_reuse::unsafe_fn;
reuse to_reuse::extern_fn;
reuse to_reuse::variadic_fn;
reuse to_reuse::const_fn;
reuse to_reuse::async_fn;

reuse fn_header_aux::unsafe_fn_extern;
reuse fn_header_aux::extern_fn_extern;
reuse fn_header_aux::variadic_fn_extern;
reuse fn_header_aux::const_fn_extern;
reuse fn_header_aux::async_fn_extern;

Expand All @@ -46,12 +43,4 @@ fn main() {
extern_fn_extern();
let _: extern "C" fn() = extern_fn;
let _: extern "C" fn() = extern_fn_extern;
unsafe {
variadic_fn(0);
variadic_fn(0, 1);
variadic_fn_extern(0);
variadic_fn_extern(0, 1);
}
let _: unsafe extern "C" fn(usize, ...) = variadic_fn;
let _: unsafe extern "C" fn(usize, ...) = variadic_fn_extern;
}
Loading