diff options
| author | bors <bors@rust-lang.org> | 2025-09-18 16:55:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-09-18 16:55:05 +0000 |
| commit | 0c0c58b8e453f552ebd7f3a1545acdd109de028c (patch) | |
| tree | 80fc20e71f5764072222045136debf7ce9297697 /tests/ui/c-variadic/trait-method.rs | |
| parent | 4cd91ef8223ef54111d21aa9e9e71b3b26477dd3 (diff) | |
| parent | 24d6259dce2040994897fc6ef765b06cf0270630 (diff) | |
| download | rust-0c0c58b8e453f552ebd7f3a1545acdd109de028c.tar.gz rust-0c0c58b8e453f552ebd7f3a1545acdd109de028c.zip | |
Auto merge of #146727 - matthiaskrgr:rollup-98812uj, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang/rust#146434 (c-variadic: allow c-variadic inherent and trait methods) - rust-lang/rust#146487 (Improve `core::num` coverage) - rust-lang/rust#146597 (Add span for struct tail recursion limit error) - rust-lang/rust#146622 (Add regression test for issue rust-lang/rust#91831) - rust-lang/rust#146717 (Clean up universe evaluation during type test evaluation) - rust-lang/rust#146723 (Include patch in release notes) r? `@ghost` `@rustbot` modify labels: rollup
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); + } +} |
