Skip to content

Commit 7324811

Browse files
committed
Implement unsize of adt's (cc rust-lang#14)
1 parent b9b0fc5 commit 7324811

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

examples/mini_core.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub trait Unsize<T: ?Sized> {}
1212
pub trait CoerceUnsized<T> {}
1313

1414
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
15+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
1516

1617
#[lang = "copy"]
1718
pub unsafe trait Copy {}
@@ -203,7 +204,9 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
203204
}
204205

205206
#[lang = "owned_box"]
206-
pub struct Box<T>(*mut T);
207+
pub struct Box<T: ?Sized>(*mut T);
208+
209+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
207210

208211
static mut MY_TINY_HEAP: [u8; 16] = [0; 16];
209212

examples/mini_core_hello_world.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ fn main() {
111111
// TODO remove when jit supports linking rlibs
112112
#[cfg(not(jit))]
113113
{
114-
let world = box "World!\0";
114+
let world: Box<&str> = box "World!\0";
115115
puts(*world as *const str as *const u8);
116+
world as Box<SomeTrait>;
116117
}
117118

118119
assert_eq!(intrinsics::size_of_val(hello) as u8, 6);

src/common.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,22 @@ impl<'tcx> CValue<'tcx> {
252252
};
253253
dest.write_cvalue(fx, CValue::ByValPair(ptr, extra, dest.layout()));
254254
}
255-
ty => unimpl!("unsize of non ptr {:?}", ty),
255+
_ => {
256+
assert!(!self.layout().ty.is_enum(), "Tried to unsize enum");
257+
let field_count = self.layout().fields.count();
258+
let mut found_unsize_field = false;
259+
for idx in 0..field_count {
260+
let field_dest = dest.place_field(fx, mir::Field::new(idx));
261+
let field_src = self.value_field(fx, mir::Field::new(idx));
262+
if field_src.layout().ty != field_dest.layout().ty {
263+
assert!(!found_unsize_field);
264+
found_unsize_field = true;
265+
field_src.unsize_value(fx, field_dest);
266+
} else {
267+
field_dest.write_cvalue(fx, field_src);
268+
}
269+
}
270+
}
256271
}
257272
}
258273

0 commit comments

Comments
 (0)