diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-09-18 17:20:56 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-18 17:20:56 +0200 |
| commit | 185926c99f34985f1823b1f085ef6f446a5ee4f3 (patch) | |
| tree | fdef1b67cefe5303af471b7fe5327c27fa2afd50 /tests/ui/c-variadic/trait-method.rs | |
| parent | 32e3d9f59bae4bcf436bc1e28723c696d2c75b11 (diff) | |
| parent | 321a76b5f95325f20c9e88e87bfb4ddf536b6950 (diff) | |
| download | rust-185926c99f34985f1823b1f085ef6f446a5ee4f3.tar.gz rust-185926c99f34985f1823b1f085ef6f446a5ee4f3.zip | |
Rollup merge of #146434 - folkertdev:c-variadic-inherent-methods, r=workingjubilee
c-variadic: allow c-variadic inherent and trait methods tracking issue: https://github.com/rust-lang/rust/issues/44930 Continuing the work of https://github.com/rust-lang/rust/pull/146342, allow inherent and trait methods to be c-variadic. However, a trait that contains a c-variadic method is no longer dyn-compatible. There is, presumably, some way to make c-variadic methods dyn-compatible. However currently, we don't have confidence that it'll work reliably: when methods from a `dyn` object are cast to a function pointer, a `ReifyShim` is created. If that shim is c-variadic, it would need to forward the C variable argument list. That does appear to work, because the `va_list` is not represented in MIR at all in this case, so the registers from the call site are untouched by the shim and can be read by the actual implementation. That just does not seem like a solid implementation. Also, intuitively, why would c-variadic function, primarily needed for FFI, need to be used with `dyn` objects at all? We can revisit this limitation if a need arises. r? `@workingjubilee`
Diffstat (limited to 'tests/ui/c-variadic/trait-method.rs')
| -rw-r--r-- | tests/ui/c-variadic/trait-method.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/ui/c-variadic/trait-method.rs b/tests/ui/c-variadic/trait-method.rs new file mode 100644 index 00000000000..97da0706a3a --- /dev/null +++ b/tests/ui/c-variadic/trait-method.rs @@ -0,0 +1,73 @@ +//@ run-pass +#![feature(c_variadic)] + +#[repr(transparent)] +struct Struct(i32); + +impl Struct { + unsafe extern "C" fn associated_function(mut ap: ...) -> i32 { + unsafe { ap.arg() } + } + + unsafe extern "C" fn method(&self, mut ap: ...) -> i32 { + self.0 + unsafe { ap.arg::<i32>() } + } +} + +trait Trait: Sized { + fn get(&self) -> i32; + + unsafe extern "C" fn trait_associated_function(mut ap: ...) -> i32 { + unsafe { ap.arg() } + } + + unsafe extern "C" fn trait_method_owned(self, mut ap: ...) -> i32 { + self.get() + unsafe { ap.arg::<i32>() } + } + + unsafe extern "C" fn trait_method_ref(&self, mut ap: ...) -> i32 { + self.get() + unsafe { ap.arg::<i32>() } + } + + unsafe extern "C" fn trait_method_mut(&mut self, mut ap: ...) -> i32 { + self.get() + unsafe { ap.arg::<i32>() } + } + + unsafe extern "C" fn trait_fat_pointer(self: Box<Self>, mut ap: ...) -> i32 { + self.get() + unsafe { ap.arg::<i32>() } + } +} + +impl Trait for Struct { + fn get(&self) -> i32 { + self.0 + } +} + +fn main() { + unsafe { + assert_eq!(Struct::associated_function(32), 32); + assert_eq!(Struct(100).method(32), 132); + + assert_eq!(Struct::trait_associated_function(32), 32); + assert_eq!(Struct(100).trait_method_owned(32), 132); + assert_eq!(Struct(100).trait_method_ref(32), 132); + assert_eq!(Struct(100).trait_method_mut(32), 132); + assert_eq!(Struct::trait_fat_pointer(Box::new(Struct(100)), 32), 132); + + assert_eq!(<Struct as Trait>::trait_associated_function(32), 32); + assert_eq!(Trait::trait_method_owned(Struct(100), 32), 132); + assert_eq!(Trait::trait_method_ref(&Struct(100), 32), 132); + assert_eq!(Trait::trait_method_mut(&mut Struct(100), 32), 132); + assert_eq!(Trait::trait_fat_pointer(Box::new(Struct(100)), 32), 132); + + type Associated = unsafe extern "C" fn(...) -> i32; + type Method<T> = unsafe extern "C" fn(T, ...) -> i32; + + assert_eq!((Struct::trait_associated_function as Associated)(32), 32); + assert_eq!((Struct::trait_method_owned as Method<_>)(Struct(100), 32), 132); + assert_eq!((Struct::trait_method_ref as Method<_>)(&Struct(100), 32), 132); + assert_eq!((Struct::trait_method_mut as Method<_>)(&mut Struct(100), 32), 132); + assert_eq!((Struct::trait_fat_pointer as Method<_>)(Box::new(Struct(100)), 32), 132); + } +} |
