Skip to content

Commit 4cae2c0

Browse files
committed
Add tests with *const Rc<Self> and similar self types
1 parent bc0439b commit 4cae2c0

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/test/run-pass/arbitrary_self_types_raw_pointer_struct.rs

+13
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@
1010

1111
#![feature(arbitrary_self_types)]
1212

13+
use std::rc::Rc;
14+
1315
struct Foo(String);
1416

1517
impl Foo {
1618
unsafe fn foo(self: *const Self) -> *const str {
1719
(*self).0.as_ref()
1820
}
21+
22+
fn complicated_1(self: *const Rc<Self>) -> &'static str {
23+
"Foo::complicated_1"
24+
}
25+
26+
unsafe fn complicated_2(self: Rc<*const Self>) -> *const str {
27+
(**self).0.as_ref()
28+
}
1929
}
2030

2131
fn main() {
2232
let foo = Foo("abc123".into());
2333
assert_eq!("abc123", unsafe { &*(&foo as *const Foo).foo() });
34+
assert_eq!("Foo::complicated_1", std::ptr::null::<Rc<Foo>>().complicated_1());
35+
let rc = Rc::new(&foo as *const Foo);
36+
assert_eq!("abc123", unsafe { &*rc.complicated_2()});
2437
}

src/test/run-pass/arbitrary_self_types_raw_pointer_trait.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ trait Foo {
1616
fn foo(self: *const Self) -> &'static str;
1717

1818
unsafe fn bar(self: *const Self) -> i64;
19+
20+
unsafe fn complicated(self: *const *const Self) -> i64 where Self: Sized {
21+
(*self).bar()
22+
}
1923
}
2024

2125
impl Foo for i32 {
@@ -39,21 +43,28 @@ impl Foo for u32 {
3943
}
4044

4145
fn main() {
42-
let foo_i32 = ptr::null::<i32>() as *const Foo;
43-
let foo_u32 = ptr::null::<u32>() as *const Foo;
46+
let null_i32 = ptr::null::<i32>() as *const Foo;
47+
let null_u32 = ptr::null::<u32>() as *const Foo;
4448

45-
assert_eq!("I'm an i32!", foo_i32.foo());
46-
assert_eq!("I'm a u32!", foo_u32.foo());
49+
assert_eq!("I'm an i32!", null_i32.foo());
50+
assert_eq!("I'm a u32!", null_u32.foo());
4751

4852
let bar_i32 = 5i32;
4953
let bar_i32_thin = &bar_i32 as *const i32;
54+
assert_eq!("I'm an i32!", bar_i32_thin.foo());
5055
assert_eq!(5, unsafe { bar_i32_thin.bar() });
56+
assert_eq!(5, unsafe { (&bar_i32_thin as *const *const i32).complicated() });
5157
let bar_i32_fat = bar_i32_thin as *const Foo;
58+
assert_eq!("I'm an i32!", bar_i32_fat.foo());
5259
assert_eq!(5, unsafe { bar_i32_fat.bar() });
5360

5461
let bar_u32 = 18u32;
5562
let bar_u32_thin = &bar_u32 as *const u32;
63+
assert_eq!("I'm a u32!", bar_u32_thin.foo());
5664
assert_eq!(18, unsafe { bar_u32_thin.bar() });
65+
assert_eq!(18, unsafe { (&bar_u32_thin as *const *const u32).complicated() });
5766
let bar_u32_fat = bar_u32_thin as *const Foo;
67+
assert_eq!("I'm a u32!", bar_u32_fat.foo());
5868
assert_eq!(18, unsafe { bar_u32_fat.bar() });
69+
5970
}

0 commit comments

Comments
 (0)