|
1 |
| -// For now C-variadic arguments in trait methods are rejected, though we aim to lift this |
2 |
| -// restriction in the future. In particular we need to think about the interaction with |
3 |
| -// `dyn Trait` and the `ReifyShim`s that it may generate for methods. |
| 1 | +//@ run-pass |
4 | 2 | #![feature(c_variadic)]
|
5 |
| -#![crate_type = "lib"] |
6 |
| -struct S; |
7 | 3 |
|
8 |
| -impl S { |
| 4 | +#[repr(transparent)] |
| 5 | +struct Struct(i32); |
| 6 | + |
| 7 | +impl Struct { |
9 | 8 | unsafe extern "C" fn associated_function(mut ap: ...) -> i32 {
|
10 | 9 | unsafe { ap.arg() }
|
11 | 10 | }
|
12 | 11 |
|
13 | 12 | unsafe extern "C" fn method(&self, mut ap: ...) -> i32 {
|
14 |
| - unsafe { ap.arg() } |
| 13 | + self.0 + unsafe { ap.arg::<i32>() } |
15 | 14 | }
|
16 | 15 | }
|
17 | 16 |
|
18 |
| -trait T { |
| 17 | +trait Trait: Sized { |
| 18 | + fn get(&self) -> i32; |
| 19 | + |
19 | 20 | unsafe extern "C" fn trait_associated_function(mut ap: ...) -> i32 {
|
20 |
| - //~^ ERROR: associated functions cannot have a C variable argument list |
21 | 21 | unsafe { ap.arg() }
|
22 | 22 | }
|
23 | 23 |
|
24 |
| - unsafe extern "C" fn trait_method(&self, mut ap: ...) -> i32 { |
25 |
| - //~^ ERROR: associated functions cannot have a C variable argument list |
26 |
| - unsafe { ap.arg() } |
| 24 | + unsafe extern "C" fn trait_method_owned(self, mut ap: ...) -> i32 { |
| 25 | + self.get() + unsafe { ap.arg::<i32>() } |
| 26 | + } |
| 27 | + |
| 28 | + unsafe extern "C" fn trait_method_ref(&self, mut ap: ...) -> i32 { |
| 29 | + self.get() + unsafe { ap.arg::<i32>() } |
| 30 | + } |
| 31 | + |
| 32 | + unsafe extern "C" fn trait_method_mut(&mut self, mut ap: ...) -> i32 { |
| 33 | + self.get() + unsafe { ap.arg::<i32>() } |
| 34 | + } |
| 35 | + |
| 36 | + unsafe extern "C" fn trait_fat_pointer(self: Box<Self>, mut ap: ...) -> i32 { |
| 37 | + self.get() + unsafe { ap.arg::<i32>() } |
27 | 38 | }
|
28 | 39 | }
|
29 | 40 |
|
30 |
| -impl T for S {} |
| 41 | +impl Trait for Struct { |
| 42 | + fn get(&self) -> i32 { |
| 43 | + self.0 |
| 44 | + } |
| 45 | +} |
31 | 46 |
|
32 | 47 | fn main() {
|
33 | 48 | unsafe {
|
34 |
| - assert_eq!(S::associated_function(32), 32); |
35 |
| - assert_eq!(S.method(32), 32); |
| 49 | + assert_eq!(Struct::associated_function(32), 32); |
| 50 | + assert_eq!(Struct(100).method(32), 132); |
| 51 | + |
| 52 | + assert_eq!(Struct::trait_associated_function(32), 32); |
| 53 | + assert_eq!(Struct(100).trait_method_owned(32), 132); |
| 54 | + assert_eq!(Struct(100).trait_method_ref(32), 132); |
| 55 | + assert_eq!(Struct(100).trait_method_mut(32), 132); |
| 56 | + assert_eq!(Struct::trait_fat_pointer(Box::new(Struct(100)), 32), 132); |
| 57 | + |
| 58 | + assert_eq!(<Struct as Trait>::trait_associated_function(32), 32); |
| 59 | + assert_eq!(Trait::trait_method_owned(Struct(100), 32), 132); |
| 60 | + assert_eq!(Trait::trait_method_ref(&Struct(100), 32), 132); |
| 61 | + assert_eq!(Trait::trait_method_mut(&mut Struct(100), 32), 132); |
| 62 | + assert_eq!(Trait::trait_fat_pointer(Box::new(Struct(100)), 32), 132); |
| 63 | + |
| 64 | + type Associated = unsafe extern "C" fn(...) -> i32; |
| 65 | + type Method<T> = unsafe extern "C" fn(T, ...) -> i32; |
36 | 66 |
|
37 |
| - assert_eq!(S::trait_associated_function(32), 32); |
38 |
| - assert_eq!(S.trait_method(32), 32); |
| 67 | + assert_eq!((Struct::trait_associated_function as Associated)(32), 32); |
| 68 | + assert_eq!((Struct::trait_method_owned as Method<_>)(Struct(100), 32), 132); |
| 69 | + assert_eq!((Struct::trait_method_ref as Method<_>)(&Struct(100), 32), 132); |
| 70 | + assert_eq!((Struct::trait_method_mut as Method<_>)(&mut Struct(100), 32), 132); |
| 71 | + assert_eq!((Struct::trait_fat_pointer as Method<_>)(Box::new(Struct(100)), 32), 132); |
39 | 72 | }
|
40 | 73 | }
|
0 commit comments