Skip to content

Commit 5c81a18

Browse files
committed
auto merge of #14869 : nick29581/rust/tstore, r=nmatsakis
Use ty_rptr/ty_uniq(ty_trait) rather than TraitStore to represent trait types. Also addresses (but doesn't close) #12470. Part of the work towards DST (#12938).
2 parents 1bb42e5 + 8e7213f commit 5c81a18

30 files changed

+451
-364
lines changed

src/librustc/metadata/tydecode.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,9 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
346346
assert_eq!(next(st), '[');
347347
let def = parse_def(st, NominalType, |x,y| conv(x,y));
348348
let substs = parse_substs(st, |x,y| conv(x,y));
349-
let store = parse_trait_store(st, |x,y| conv(x,y));
350349
let bounds = parse_bounds(st, |x,y| conv(x,y));
351350
assert_eq!(next(st), ']');
352-
return ty::mk_trait(st.tcx, def, substs, store, bounds.builtin_bounds);
351+
return ty::mk_trait(st.tcx, def, substs, bounds.builtin_bounds);
353352
}
354353
'p' => {
355354
let did = parse_def(st, TypeParameter, |x,y| conv(x,y));

src/librustc/metadata/tyencode.rs

-2
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,10 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
232232
ty::ty_trait(box ty::TyTrait {
233233
def_id,
234234
ref substs,
235-
store,
236235
bounds
237236
}) => {
238237
mywrite!(w, "x[{}|", (cx.ds)(def_id));
239238
enc_substs(w, cx, substs);
240-
enc_trait_store(w, cx, store);
241239
let bounds = ty::ParamBounds {builtin_bounds: bounds,
242240
trait_bounds: Vec::new()};
243241
enc_bounds(w, cx, &bounds);

src/librustc/middle/kind.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,12 @@ fn check_bounds_on_type_parameters(cx: &mut Context, e: &Expr) {
361361
fn check_trait_cast(cx: &mut Context, source_ty: ty::t, target_ty: ty::t, span: Span) {
362362
check_cast_for_escaping_regions(cx, source_ty, target_ty, span);
363363
match ty::get(target_ty).sty {
364-
ty::ty_trait(box ty::TyTrait { bounds, .. }) => {
365-
check_trait_cast_bounds(cx, span, source_ty, bounds);
366-
}
364+
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ ty, .. }) => match ty::get(ty).sty {
365+
ty::ty_trait(box ty::TyTrait { bounds, .. }) => {
366+
check_trait_cast_bounds(cx, span, source_ty, bounds);
367+
}
368+
_ => {}
369+
},
367370
_ => {}
368371
}
369372
}
@@ -530,9 +533,8 @@ pub fn check_cast_for_escaping_regions(
530533
{
531534
// Determine what type we are casting to; if it is not a trait, then no
532535
// worries.
533-
match ty::get(target_ty).sty {
534-
ty::ty_trait(..) => {}
535-
_ => { return; }
536+
if !ty::type_is_trait(target_ty) {
537+
return;
536538
}
537539

538540
// Collect up the regions that appear in the target type. We want to

src/librustc/middle/lint.rs

-3
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,6 @@ fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
980980
n_box += 1;
981981
}
982982
ty::ty_uniq(_) |
983-
ty::ty_trait(box ty::TyTrait {
984-
store: ty::UniqTraitStore, ..
985-
}) |
986983
ty::ty_closure(box ty::ClosureTy {
987984
store: ty::UniqTraitStore,
988985
..

src/librustc/middle/mem_categorization.rs

-8
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ pub enum deref_kind {
174174
pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
175175
match ty::get(t).sty {
176176
ty::ty_uniq(_) |
177-
ty::ty_trait(box ty::TyTrait { store: ty::UniqTraitStore, .. }) |
178177
ty::ty_closure(box ty::ClosureTy {store: ty::UniqTraitStore, ..}) => {
179178
Some(deref_ptr(OwnedPtr))
180179
}
@@ -183,13 +182,6 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
183182
let kind = ty::BorrowKind::from_mutbl(mt.mutbl);
184183
Some(deref_ptr(BorrowedPtr(kind, r)))
185184
}
186-
ty::ty_trait(box ty::TyTrait {
187-
store: ty::RegionTraitStore(r, mutbl),
188-
..
189-
}) => {
190-
let kind = ty::BorrowKind::from_mutbl(mutbl);
191-
Some(deref_ptr(BorrowedPtr(kind, r)))
192-
}
193185

194186
ty::ty_closure(box ty::ClosureTy {
195187
store: ty::RegionTraitStore(r, _),

src/librustc/middle/trans/adt.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,11 @@ impl Case {
292292
fn find_ptr(&self) -> Option<uint> {
293293
self.tys.iter().position(|&ty| {
294294
match ty::get(ty).sty {
295-
ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty {
296-
ty::ty_vec(_, None) | ty::ty_str => false,
295+
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
296+
ty::ty_vec(_, None) | ty::ty_str| ty::ty_trait(..) => false,
297297
_ => true,
298298
},
299-
ty::ty_uniq(..) | ty::ty_box(..) |
300-
ty::ty_bare_fn(..) => true,
299+
ty::ty_box(..) | ty::ty_bare_fn(..) => true,
301300
// Is that everything? Would closures or slices qualify?
302301
_ => false
303302
}

src/librustc/middle/trans/base.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,9 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t) -> Vec<(uint, u6
17941794
match ty::get(ret_ty).sty {
17951795
// `~` pointer return values never alias because ownership
17961796
// is transferred
1797+
ty::ty_uniq(it) if match ty::get(it).sty {
1798+
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
1799+
} => {}
17971800
ty::ty_uniq(_) => {
17981801
attrs.push((lib::llvm::ReturnIndex as uint, lib::llvm::NoAliasAttribute as u64));
17991802
}
@@ -1803,9 +1806,9 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t) -> Vec<(uint, u6
18031806
// We can also mark the return value as `nonnull` in certain cases
18041807
match ty::get(ret_ty).sty {
18051808
// These are not really pointers but pairs, (pointer, len)
1806-
ty::ty_rptr(_, ty::mt { ty: it, .. }) |
1809+
ty::ty_uniq(it) |
18071810
ty::ty_rptr(_, ty::mt { ty: it, .. }) if match ty::get(it).sty {
1808-
ty::ty_str | ty::ty_vec(..) => true, _ => false
1811+
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
18091812
} => {}
18101813
ty::ty_uniq(_) | ty::ty_rptr(_, _) => {
18111814
attrs.push((lib::llvm::ReturnIndex as uint, lib::llvm::NonNullAttribute as u64));

src/librustc/middle/trans/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn type_is_immediate(ccx: &CrateContext, ty: ty::t) -> bool {
6767
ty::type_is_unique(ty) || ty::type_is_region_ptr(ty) ||
6868
type_is_newtype_immediate(ccx, ty) || ty::type_is_bot(ty) ||
6969
ty::type_is_simd(tcx, ty);
70-
if simple {
70+
if simple && !ty::type_is_trait(ty) {
7171
return true;
7272
}
7373
match ty::get(ty).sty {

src/librustc/middle/trans/consts.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ fn const_deref(cx: &CrateContext, v: ValueRef, t: ty::t, explicit: bool)
143143
let dv = match ty::get(t).sty {
144144
ty::ty_ptr(mt) | ty::ty_rptr(_, mt) => {
145145
match ty::get(mt.ty).sty {
146-
ty::ty_vec(_, None) | ty::ty_str => cx.sess().bug("unexpected slice"),
146+
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => {
147+
cx.sess().bug("unexpected unsized type")
148+
}
147149
_ => const_deref_ptr(cx, v),
148150
}
149151
}

src/librustc/middle/trans/datum.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ pub fn appropriate_rvalue_mode(ccx: &CrateContext, ty: ty::t) -> RvalueMode {
157157
* on whether type is immediate or not.
158158
*/
159159

160-
if type_is_zero_size(ccx, ty) {
161-
ByValue
162-
} else if type_is_immediate(ccx, ty) {
160+
if type_is_immediate(ccx, ty) {
163161
ByValue
164162
} else {
165163
ByRef

src/librustc/middle/trans/debuginfo.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ impl TypeMap {
285285
// unique ptr (~) -> {~ :pointee-uid:}
286286
// @-ptr (@) -> {@ :pointee-uid:}
287287
// sized vec ([T, ..x]) -> {[:size:] :element-uid:}
288-
// vec slice (&[T]) -> {&<mut> [] :element-uid:}
289-
// trait (~ | &[mut] T) -> {:sigil: trait_:svh: / :node-id:_<(:param-uid:),*> }
288+
// unsized vec ([T]) -> {[] :element-uid:}
289+
// trait (T) -> {trait_:svh: / :node-id:_<(:param-uid:),*> }
290290
// closure -> {<unsafe_> <once_> :store-sigil: |(:param-uid:),* <,_...>| -> \
291291
// :return-type-uid: : (:bounds:)*}
292292
// function -> {<unsafe_> <abi_> fn( (:param-uid:)* <,_...> ) -> \
@@ -361,18 +361,12 @@ impl TypeMap {
361361
let inner_type_id = self.get_unique_type_id_as_string(inner_type_id);
362362
unique_type_id.push_str(inner_type_id.as_slice());
363363
},
364-
ty::ty_vec(ty::mt { ty: inner_type, mutbl }, optional_length) => {
364+
ty::ty_vec(ty::mt { ty: inner_type, .. }, optional_length) => {
365365
match optional_length {
366366
Some(len) => {
367367
unique_type_id.push_str(format!("[{}]", len).as_slice());
368368
}
369369
None => {
370-
unique_type_id.push_char('&');
371-
372-
if mutbl == ast::MutMutable {
373-
unique_type_id.push_str("mut");
374-
}
375-
376370
unique_type_id.push_str("[]");
377371
}
378372
};
@@ -382,12 +376,6 @@ impl TypeMap {
382376
unique_type_id.push_str(inner_type_id.as_slice());
383377
},
384378
ty::ty_trait(ref trait_data) => {
385-
match trait_data.store {
386-
ty::UniqTraitStore => unique_type_id.push_char('~'),
387-
ty::RegionTraitStore(_, ast::MutMutable) => unique_type_id.push_str("&mut"),
388-
ty::RegionTraitStore(_, ast::MutImmutable) => unique_type_id.push_char('&'),
389-
};
390-
391379
unique_type_id.push_str("trait ");
392380

393381
from_def_id_and_substs(self,
@@ -2901,6 +2889,16 @@ fn type_metadata(cx: &CrateContext,
29012889
let i8_t = ty::mk_i8();
29022890
heap_vec_metadata(cx, pointee_type, i8_t, unique_type_id, usage_site_span)
29032891
}
2892+
ty::ty_trait(box ty::TyTrait {
2893+
def_id,
2894+
ref substs,
2895+
ref bounds
2896+
}) => {
2897+
MetadataCreationResult::new(
2898+
trait_metadata(cx, def_id, t, substs, ty::UniqTraitStore,
2899+
bounds, unique_type_id),
2900+
false)
2901+
}
29042902
_ => {
29052903
let pointee_metadata = type_metadata(cx, pointee_type, usage_site_span);
29062904
return_if_created_in_meantime!();
@@ -2917,6 +2915,17 @@ fn type_metadata(cx: &CrateContext,
29172915
ty::ty_str => {
29182916
vec_slice_metadata(cx, t, ty::mk_i8(), unique_type_id, usage_site_span)
29192917
}
2918+
ty::ty_trait(box ty::TyTrait {
2919+
def_id,
2920+
ref substs,
2921+
ref bounds
2922+
}) => {
2923+
MetadataCreationResult::new(
2924+
trait_metadata(cx, def_id, t, substs,
2925+
ty::RegionTraitStore(ty::ReStatic, mt.mutbl),
2926+
bounds, unique_type_id),
2927+
false)
2928+
}
29202929
_ => {
29212930
let pointee = type_metadata(cx, mt.ty, usage_site_span);
29222931
return_if_created_in_meantime!();
@@ -2930,16 +2939,6 @@ fn type_metadata(cx: &CrateContext,
29302939
ty::ty_closure(ref closurety) => {
29312940
subroutine_type_metadata(cx, unique_type_id, &closurety.sig, usage_site_span)
29322941
}
2933-
ty::ty_trait(box ty::TyTrait {
2934-
def_id,
2935-
ref substs,
2936-
store,
2937-
ref bounds
2938-
}) => {
2939-
MetadataCreationResult::new(
2940-
trait_metadata(cx, def_id, t, substs, store, bounds, unique_type_id),
2941-
false)
2942-
}
29432942
ty::ty_struct(def_id, ref substs) => {
29442943
prepare_struct_metadata(cx,
29452944
t,

src/librustc/middle/trans/expr.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -769,15 +769,12 @@ fn trans_rvalue_dps_unadjusted<'a>(bcx: &'a Block<'a>,
769769
}
770770
ast::ExprCast(ref val, _) => {
771771
// DPS output mode means this is a trait cast:
772-
match ty::get(node_id_type(bcx, expr.id)).sty {
773-
ty::ty_trait(..) => {
774-
let datum = unpack_datum!(bcx, trans(bcx, &**val));
775-
meth::trans_trait_cast(bcx, datum, expr.id, dest)
776-
}
777-
_ => {
778-
bcx.tcx().sess.span_bug(expr.span,
779-
"expr_cast of non-trait");
780-
}
772+
if ty::type_is_trait(node_id_type(bcx, expr.id)) {
773+
let datum = unpack_datum!(bcx, trans(bcx, &**val));
774+
meth::trans_trait_cast(bcx, datum, expr.id, dest)
775+
} else {
776+
bcx.tcx().sess.span_bug(expr.span,
777+
"expr_cast of non-trait");
781778
}
782779
}
783780
ast::ExprAssignOp(op, ref dst, ref src) => {
@@ -1578,7 +1575,7 @@ pub fn cast_type_kind(t: ty::t) -> cast_kind {
15781575
ty::ty_float(..) => cast_float,
15791576
ty::ty_ptr(..) => cast_pointer,
15801577
ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty{
1581-
ty::ty_vec(_, None) | ty::ty_str => cast_other,
1578+
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => cast_other,
15821579
_ => cast_pointer,
15831580
},
15841581
ty::ty_bare_fn(..) => cast_pointer,
@@ -1794,8 +1791,8 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
17941791
let r = match ty::get(datum.ty).sty {
17951792
ty::ty_uniq(content_ty) => {
17961793
match ty::get(content_ty).sty {
1797-
ty::ty_vec(_, None) | ty::ty_str
1798-
=> bcx.tcx().sess.span_bug(expr.span, "unexpected ~[T]"),
1794+
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..)
1795+
=> bcx.tcx().sess.span_bug(expr.span, "unexpected unsized box"),
17991796
_ => deref_owned_pointer(bcx, expr, datum, content_ty),
18001797
}
18011798
}
@@ -1812,8 +1809,8 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
18121809
ty::ty_ptr(ty::mt { ty: content_ty, .. }) |
18131810
ty::ty_rptr(_, ty::mt { ty: content_ty, .. }) => {
18141811
match ty::get(content_ty).sty {
1815-
ty::ty_vec(_, None) | ty::ty_str
1816-
=> bcx.tcx().sess.span_bug(expr.span, "unexpected &[T]"),
1812+
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..)
1813+
=> bcx.tcx().sess.span_bug(expr.span, "unexpected unsized reference"),
18171814
_ => {
18181815
assert!(!ty::type_needs_drop(bcx.tcx(), datum.ty));
18191816

src/librustc/middle/trans/glue.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn get_drop_glue_type(ccx: &CrateContext, t: ty::t) -> ty::t {
100100

101101
ty::ty_uniq(typ) if !ty::type_needs_drop(tcx, typ) => {
102102
match ty::get(typ).sty {
103-
ty::ty_vec(_, None) | ty::ty_str => t,
103+
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => t,
104104
_ => {
105105
let llty = sizing_type_of(ccx, typ);
106106
// `Box<ZeroSizeType>` does not allocate.
@@ -295,25 +295,42 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
295295
decr_refcnt_maybe_free(bcx, v0, body_ty)
296296
}
297297
ty::ty_uniq(content_ty) => {
298-
let llbox = Load(bcx, v0);
299-
let not_null = IsNotNull(bcx, llbox);
300298
match ty::get(content_ty).sty {
301299
ty::ty_vec(mt, None) => {
300+
let llbox = Load(bcx, v0);
301+
let not_null = IsNotNull(bcx, llbox);
302302
with_cond(bcx, not_null, |bcx| {
303303
let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, mt.ty);
304304
// FIXME: #13994: the old `Box<[T]>` will not support sized deallocation
305305
trans_exchange_free(bcx, llbox, 0, 8)
306306
})
307307
}
308308
ty::ty_str => {
309+
let llbox = Load(bcx, v0);
310+
let not_null = IsNotNull(bcx, llbox);
309311
with_cond(bcx, not_null, |bcx| {
310312
let unit_ty = ty::sequence_element_type(bcx.tcx(), t);
311313
let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, unit_ty);
312314
// FIXME: #13994: the old `Box<str>` will not support sized deallocation
313315
trans_exchange_free(bcx, llbox, 0, 8)
314316
})
315317
}
318+
ty::ty_trait(..) => {
319+
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
320+
// Only drop the value when it is non-null
321+
with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {
322+
let dtor_ptr = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_vtable]));
323+
let dtor = Load(bcx, dtor_ptr);
324+
Call(bcx,
325+
dtor,
326+
[PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))],
327+
[]);
328+
bcx
329+
})
330+
}
316331
_ => {
332+
let llbox = Load(bcx, v0);
333+
let not_null = IsNotNull(bcx, llbox);
317334
with_cond(bcx, not_null, |bcx| {
318335
let bcx = drop_ty(bcx, llbox, content_ty);
319336
trans_exchange_free_ty(bcx, llbox, content_ty)
@@ -336,16 +353,6 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
336353
}
337354
}
338355
}
339-
ty::ty_trait(box ty::TyTrait { store: ty::UniqTraitStore, .. }) => {
340-
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
341-
// Only drop the value when it is non-null
342-
with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {
343-
let dtor_ptr = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_vtable]));
344-
let dtor = Load(bcx, dtor_ptr);
345-
Call(bcx, dtor, [PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))], []);
346-
bcx
347-
})
348-
}
349356
ty::ty_closure(ref f) if f.store == ty::UniqTraitStore => {
350357
let box_cell_v = GEPi(bcx, v0, [0u, abi::fn_field_box]);
351358
let env = Load(bcx, box_cell_v);

0 commit comments

Comments
 (0)