Skip to content

Commit 42eeb75

Browse files
committed
Merge branch 'wip_unsized_types'
cc rust-lang#14
2 parents 8111eee + 9878eea commit 42eeb75

File tree

9 files changed

+201
-40
lines changed

9 files changed

+201
-40
lines changed

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ gcc target/out/mini_core.o target/out/mini_core_hello_world.o -o target/out/mini
4141
./target/out/mini_core_hello_world
4242

4343
$RUSTC target/libcore/src/libcore/lib.rs --color=always --crate-type lib -Cincremental=target/incremental 2>&1 | (head -n 20; echo "===="; tail -n 1000)
44-
cat target/out/log.txt | sort | uniq -c | grep -v "rval unsize move" | grep -v "rval len"
44+
cat target/out/log.txt | sort | uniq -c

examples/example.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ unsafe fn call_uninit() -> u8 {
141141
}
142142

143143
// TODO: enable when fat pointers are supported
144-
/*unsafe fn deref_str_ptr(s: *const str) -> &'static str {
144+
unsafe fn deref_str_ptr(s: *const str) -> &'static str {
145145
&*s
146-
}*/
146+
}
147147

148148
fn use_array(arr: [u8; 3]) -> u8 {
149149
arr[1]
@@ -153,6 +153,10 @@ fn repeat_array() -> [u8; 3] {
153153
[0; 3]
154154
}
155155

156+
fn array_as_slice(arr: &[u8; 3]) -> &[u8] {
157+
arr
158+
}
159+
156160
/*unsafe fn use_ctlz_nonzero(a: u16) -> u16 {
157161
intrinsics::ctlz_nonzero(a)
158162
}*/
@@ -176,3 +180,7 @@ fn make_array() -> [u8; 3] {
176180
fn some_promoted_tuple() -> &'static (&'static str, &'static str) {
177181
&("abc", "some")
178182
}
183+
184+
fn index_slice(s: &[u8]) -> u8 {
185+
s[2]
186+
}

examples/mini_core.rs

+8
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,11 @@ impl<T> Index<usize> for [T; 3] {
207207
&self[index]
208208
}
209209
}
210+
211+
impl<T> Index<usize> for [T] {
212+
type Output = T;
213+
214+
fn index(&self, index: usize) -> &Self::Output {
215+
&self[index]
216+
}
217+
}

examples/mini_core_hello_world.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ static NUM_REF: &'static u8 = unsafe { &NUM };
4343

4444
fn main() {
4545
unsafe {
46-
let (ptr, _): (*const u8, usize) = intrinsics::transmute("Hello!\0");
46+
let slice: &[u8] = b"Hello!\0" as &[u8; 7];
47+
let ptr: *const u8 = slice as *const [u8] as *const u8;
4748
puts(ptr);
4849
}
4950

src/abi.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ fn get_pass_mode<'a, 'tcx: 'a>(
2828
ty: Ty<'tcx>,
2929
is_return: bool,
3030
) -> PassMode {
31+
assert!(
32+
!tcx.layout_of(ParamEnv::reveal_all().and(ty))
33+
.unwrap()
34+
.is_unsized()
35+
);
3136
if ty.sty == tcx.mk_nil().sty {
3237
if is_return {
3338
//if false {
@@ -312,7 +317,7 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
312317
//unimplemented!("pass mode nopass");
313318
fx.local_map.insert(
314319
RETURN_PLACE,
315-
CPlace::Addr(null, fx.layout_of(fx.return_type())),
320+
CPlace::Addr(null, None, fx.layout_of(fx.return_type())),
316321
);
317322
}
318323
PassMode::ByVal(ret_ty) => {
@@ -321,8 +326,10 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
321326
.insert(RETURN_PLACE, CPlace::Var(RETURN_PLACE, ret_layout));
322327
}
323328
PassMode::ByRef => {
324-
fx.local_map
325-
.insert(RETURN_PLACE, CPlace::Addr(ret_param.unwrap(), ret_layout));
329+
fx.local_map.insert(
330+
RETURN_PLACE,
331+
CPlace::Addr(ret_param.unwrap(), None, ret_layout),
332+
);
326333
}
327334
}
328335

src/base.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
309309
}
310310
Rvalue::Ref(_, _, place) => {
311311
let place = trans_place(fx, place);
312-
let addr = place.expect_addr();
313-
lval.write_cvalue(fx, CValue::ByVal(addr, dest_layout));
312+
place.write_place_ref(fx, lval);
314313
}
315314
Rvalue::BinaryOp(bin_op, lhs, rhs) => {
316315
let ty = fx.monomorphize(&lhs.ty(&fx.mir.local_decls, fx.tcx));
@@ -459,7 +458,8 @@ fn trans_stmt<'a, 'tcx: 'a>(
459458
unimplemented!("rval closure_fn_ptr {:?} {:?}", operand, ty)
460459
}
461460
Rvalue::Cast(CastKind::Unsize, operand, ty) => {
462-
unimpl!("rval unsize {:?} {:?}", operand, ty);
461+
let operand = trans_operand(fx, operand);
462+
operand.unsize_value(fx, lval);
463463
}
464464
Rvalue::Discriminant(place) => {
465465
let place = trans_place(fx, place).to_cvalue(fx);
@@ -474,7 +474,15 @@ fn trans_stmt<'a, 'tcx: 'a>(
474474
to.write_cvalue(fx, operand);
475475
}
476476
}
477-
Rvalue::Len(lval) => unimpl!("rval len {:?}", lval),
477+
Rvalue::Len(place) => {
478+
let place = trans_place(fx, place);
479+
let size = match place {
480+
CPlace::Addr(_, size, _) => size.unwrap(),
481+
CPlace::Var(_, _) => unreachable!(),
482+
};
483+
let usize_layout = fx.layout_of(fx.tcx.types.usize);
484+
lval.write_cvalue(fx, CValue::ByVal(size, usize_layout));
485+
}
478486
Rvalue::NullaryOp(NullOp::Box, ty) => unimplemented!("rval box {:?}", ty),
479487
Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
480488
assert!(
@@ -893,13 +901,7 @@ pub fn trans_place<'a, 'tcx: 'a>(
893901
Place::Projection(projection) => {
894902
let base = trans_place(fx, &projection.base);
895903
match projection.elem {
896-
ProjectionElem::Deref => {
897-
let layout = fx.layout_of(place.ty(&*fx.mir, fx.tcx).to_ty(fx.tcx));
898-
if layout.is_unsized() {
899-
unimpl!("Unsized places are not yet implemented");
900-
}
901-
CPlace::Addr(base.to_cvalue(fx).load_value(fx), layout)
902-
}
904+
ProjectionElem::Deref => base.place_deref(fx),
903905
ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
904906
ProjectionElem::Index(local) => {
905907
let index = fx.get_local_place(local).to_cvalue(fx).load_value(fx);

0 commit comments

Comments
 (0)