Skip to content

Commit

Permalink
Remove TraitStore from ty_trait
Browse files Browse the repository at this point in the history
Use ty_rptr/ty_uniq(ty_trait) rather than TraitStore to represent trait types.
Also addresses (but doesn't close) rust-lang#12470.
Part of the work towards DST (rust-lang#12938).

[breaking-change] lifetime parameters in `&mut trait` are now invariant. They used to be contravariant.
  • Loading branch information
nrc committed Jun 17, 2014
1 parent db29814 commit 8e7213f
Show file tree
Hide file tree
Showing 30 changed files with 451 additions and 364 deletions.
3 changes: 1 addition & 2 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,9 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
assert_eq!(next(st), '[');
let def = parse_def(st, NominalType, |x,y| conv(x,y));
let substs = parse_substs(st, |x,y| conv(x,y));
let store = parse_trait_store(st, |x,y| conv(x,y));
let bounds = parse_bounds(st, |x,y| conv(x,y));
assert_eq!(next(st), ']');
return ty::mk_trait(st.tcx, def, substs, store, bounds.builtin_bounds);
return ty::mk_trait(st.tcx, def, substs, bounds.builtin_bounds);
}
'p' => {
let did = parse_def(st, TypeParameter, |x,y| conv(x,y));
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,10 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
store,
bounds
}) => {
mywrite!(w, "x[{}|", (cx.ds)(def_id));
enc_substs(w, cx, substs);
enc_trait_store(w, cx, store);
let bounds = ty::ParamBounds {builtin_bounds: bounds,
trait_bounds: Vec::new()};
enc_bounds(w, cx, &bounds);
Expand Down
14 changes: 8 additions & 6 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,12 @@ fn check_bounds_on_type_parameters(cx: &mut Context, e: &Expr) {
fn check_trait_cast(cx: &mut Context, source_ty: ty::t, target_ty: ty::t, span: Span) {
check_cast_for_escaping_regions(cx, source_ty, target_ty, span);
match ty::get(target_ty).sty {
ty::ty_trait(box ty::TyTrait { bounds, .. }) => {
check_trait_cast_bounds(cx, span, source_ty, bounds);
}
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ ty, .. }) => match ty::get(ty).sty {
ty::ty_trait(box ty::TyTrait { bounds, .. }) => {
check_trait_cast_bounds(cx, span, source_ty, bounds);
}
_ => {}
},
_ => {}
}
}
Expand Down Expand Up @@ -530,9 +533,8 @@ pub fn check_cast_for_escaping_regions(
{
// Determine what type we are casting to; if it is not a trait, then no
// worries.
match ty::get(target_ty).sty {
ty::ty_trait(..) => {}
_ => { return; }
if !ty::type_is_trait(target_ty) {
return;
}

// Collect up the regions that appear in the target type. We want to
Expand Down
3 changes: 0 additions & 3 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,6 @@ fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
n_box += 1;
}
ty::ty_uniq(_) |
ty::ty_trait(box ty::TyTrait {
store: ty::UniqTraitStore, ..
}) |
ty::ty_closure(box ty::ClosureTy {
store: ty::UniqTraitStore,
..
Expand Down
8 changes: 0 additions & 8 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ pub enum deref_kind {
pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
match ty::get(t).sty {
ty::ty_uniq(_) |
ty::ty_trait(box ty::TyTrait { store: ty::UniqTraitStore, .. }) |
ty::ty_closure(box ty::ClosureTy {store: ty::UniqTraitStore, ..}) => {
Some(deref_ptr(OwnedPtr))
}
Expand All @@ -183,13 +182,6 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
let kind = ty::BorrowKind::from_mutbl(mt.mutbl);
Some(deref_ptr(BorrowedPtr(kind, r)))
}
ty::ty_trait(box ty::TyTrait {
store: ty::RegionTraitStore(r, mutbl),
..
}) => {
let kind = ty::BorrowKind::from_mutbl(mutbl);
Some(deref_ptr(BorrowedPtr(kind, r)))
}

ty::ty_closure(box ty::ClosureTy {
store: ty::RegionTraitStore(r, _),
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/middle/trans/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,11 @@ impl Case {
fn find_ptr(&self) -> Option<uint> {
self.tys.iter().position(|&ty| {
match ty::get(ty).sty {
ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty {
ty::ty_vec(_, None) | ty::ty_str => false,
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
ty::ty_vec(_, None) | ty::ty_str| ty::ty_trait(..) => false,
_ => true,
},
ty::ty_uniq(..) | ty::ty_box(..) |
ty::ty_bare_fn(..) => true,
ty::ty_box(..) | ty::ty_bare_fn(..) => true,
// Is that everything? Would closures or slices qualify?
_ => false
}
Expand Down
7 changes: 5 additions & 2 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,9 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t) -> Vec<(uint, u6
match ty::get(ret_ty).sty {
// `~` pointer return values never alias because ownership
// is transferred
ty::ty_uniq(it) if match ty::get(it).sty {
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
} => {}
ty::ty_uniq(_) => {
attrs.push((lib::llvm::ReturnIndex as uint, lib::llvm::NoAliasAttribute as u64));
}
Expand All @@ -1803,9 +1806,9 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t) -> Vec<(uint, u6
// We can also mark the return value as `nonnull` in certain cases
match ty::get(ret_ty).sty {
// These are not really pointers but pairs, (pointer, len)
ty::ty_rptr(_, ty::mt { ty: it, .. }) |
ty::ty_uniq(it) |
ty::ty_rptr(_, ty::mt { ty: it, .. }) if match ty::get(it).sty {
ty::ty_str | ty::ty_vec(..) => true, _ => false
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
} => {}
ty::ty_uniq(_) | ty::ty_rptr(_, _) => {
attrs.push((lib::llvm::ReturnIndex as uint, lib::llvm::NonNullAttribute as u64));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn type_is_immediate(ccx: &CrateContext, ty: ty::t) -> bool {
ty::type_is_unique(ty) || ty::type_is_region_ptr(ty) ||
type_is_newtype_immediate(ccx, ty) || ty::type_is_bot(ty) ||
ty::type_is_simd(tcx, ty);
if simple {
if simple && !ty::type_is_trait(ty) {
return true;
}
match ty::get(ty).sty {
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/middle/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ fn const_deref(cx: &CrateContext, v: ValueRef, t: ty::t, explicit: bool)
let dv = match ty::get(t).sty {
ty::ty_ptr(mt) | ty::ty_rptr(_, mt) => {
match ty::get(mt.ty).sty {
ty::ty_vec(_, None) | ty::ty_str => cx.sess().bug("unexpected slice"),
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => {
cx.sess().bug("unexpected unsized type")
}
_ => const_deref_ptr(cx, v),
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/middle/trans/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ pub fn appropriate_rvalue_mode(ccx: &CrateContext, ty: ty::t) -> RvalueMode {
* on whether type is immediate or not.
*/

if type_is_zero_size(ccx, ty) {
ByValue
} else if type_is_immediate(ccx, ty) {
if type_is_immediate(ccx, ty) {
ByValue
} else {
ByRef
Expand Down
49 changes: 24 additions & 25 deletions src/librustc/middle/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ impl TypeMap {
// unique ptr (~) -> {~ :pointee-uid:}
// @-ptr (@) -> {@ :pointee-uid:}
// sized vec ([T, ..x]) -> {[:size:] :element-uid:}
// vec slice (&[T]) -> {&<mut> [] :element-uid:}
// trait (~ | &[mut] T) -> {:sigil: trait_:svh: / :node-id:_<(:param-uid:),*> }
// unsized vec ([T]) -> {[] :element-uid:}
// trait (T) -> {trait_:svh: / :node-id:_<(:param-uid:),*> }
// closure -> {<unsafe_> <once_> :store-sigil: |(:param-uid:),* <,_...>| -> \
// :return-type-uid: : (:bounds:)*}
// function -> {<unsafe_> <abi_> fn( (:param-uid:)* <,_...> ) -> \
Expand Down Expand Up @@ -361,18 +361,12 @@ impl TypeMap {
let inner_type_id = self.get_unique_type_id_as_string(inner_type_id);
unique_type_id.push_str(inner_type_id.as_slice());
},
ty::ty_vec(ty::mt { ty: inner_type, mutbl }, optional_length) => {
ty::ty_vec(ty::mt { ty: inner_type, .. }, optional_length) => {
match optional_length {
Some(len) => {
unique_type_id.push_str(format!("[{}]", len).as_slice());
}
None => {
unique_type_id.push_char('&');

if mutbl == ast::MutMutable {
unique_type_id.push_str("mut");
}

unique_type_id.push_str("[]");
}
};
Expand All @@ -382,12 +376,6 @@ impl TypeMap {
unique_type_id.push_str(inner_type_id.as_slice());
},
ty::ty_trait(ref trait_data) => {
match trait_data.store {
ty::UniqTraitStore => unique_type_id.push_char('~'),
ty::RegionTraitStore(_, ast::MutMutable) => unique_type_id.push_str("&mut"),
ty::RegionTraitStore(_, ast::MutImmutable) => unique_type_id.push_char('&'),
};

unique_type_id.push_str("trait ");

from_def_id_and_substs(self,
Expand Down Expand Up @@ -2901,6 +2889,16 @@ fn type_metadata(cx: &CrateContext,
let i8_t = ty::mk_i8();
heap_vec_metadata(cx, pointee_type, i8_t, unique_type_id, usage_site_span)
}
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
ref bounds
}) => {
MetadataCreationResult::new(
trait_metadata(cx, def_id, t, substs, ty::UniqTraitStore,
bounds, unique_type_id),
false)
}
_ => {
let pointee_metadata = type_metadata(cx, pointee_type, usage_site_span);
return_if_created_in_meantime!();
Expand All @@ -2917,6 +2915,17 @@ fn type_metadata(cx: &CrateContext,
ty::ty_str => {
vec_slice_metadata(cx, t, ty::mk_i8(), unique_type_id, usage_site_span)
}
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
ref bounds
}) => {
MetadataCreationResult::new(
trait_metadata(cx, def_id, t, substs,
ty::RegionTraitStore(ty::ReStatic, mt.mutbl),
bounds, unique_type_id),
false)
}
_ => {
let pointee = type_metadata(cx, mt.ty, usage_site_span);
return_if_created_in_meantime!();
Expand All @@ -2930,16 +2939,6 @@ fn type_metadata(cx: &CrateContext,
ty::ty_closure(ref closurety) => {
subroutine_type_metadata(cx, unique_type_id, &closurety.sig, usage_site_span)
}
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
store,
ref bounds
}) => {
MetadataCreationResult::new(
trait_metadata(cx, def_id, t, substs, store, bounds, unique_type_id),
false)
}
ty::ty_struct(def_id, ref substs) => {
prepare_struct_metadata(cx,
t,
Expand Down
25 changes: 11 additions & 14 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,15 +769,12 @@ fn trans_rvalue_dps_unadjusted<'a>(bcx: &'a Block<'a>,
}
ast::ExprCast(ref val, _) => {
// DPS output mode means this is a trait cast:
match ty::get(node_id_type(bcx, expr.id)).sty {
ty::ty_trait(..) => {
let datum = unpack_datum!(bcx, trans(bcx, &**val));
meth::trans_trait_cast(bcx, datum, expr.id, dest)
}
_ => {
bcx.tcx().sess.span_bug(expr.span,
"expr_cast of non-trait");
}
if ty::type_is_trait(node_id_type(bcx, expr.id)) {
let datum = unpack_datum!(bcx, trans(bcx, &**val));
meth::trans_trait_cast(bcx, datum, expr.id, dest)
} else {
bcx.tcx().sess.span_bug(expr.span,
"expr_cast of non-trait");
}
}
ast::ExprAssignOp(op, ref dst, ref src) => {
Expand Down Expand Up @@ -1578,7 +1575,7 @@ pub fn cast_type_kind(t: ty::t) -> cast_kind {
ty::ty_float(..) => cast_float,
ty::ty_ptr(..) => cast_pointer,
ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty{
ty::ty_vec(_, None) | ty::ty_str => cast_other,
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => cast_other,
_ => cast_pointer,
},
ty::ty_bare_fn(..) => cast_pointer,
Expand Down Expand Up @@ -1794,8 +1791,8 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
let r = match ty::get(datum.ty).sty {
ty::ty_uniq(content_ty) => {
match ty::get(content_ty).sty {
ty::ty_vec(_, None) | ty::ty_str
=> bcx.tcx().sess.span_bug(expr.span, "unexpected ~[T]"),
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..)
=> bcx.tcx().sess.span_bug(expr.span, "unexpected unsized box"),
_ => deref_owned_pointer(bcx, expr, datum, content_ty),
}
}
Expand All @@ -1812,8 +1809,8 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
ty::ty_ptr(ty::mt { ty: content_ty, .. }) |
ty::ty_rptr(_, ty::mt { ty: content_ty, .. }) => {
match ty::get(content_ty).sty {
ty::ty_vec(_, None) | ty::ty_str
=> bcx.tcx().sess.span_bug(expr.span, "unexpected &[T]"),
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..)
=> bcx.tcx().sess.span_bug(expr.span, "unexpected unsized reference"),
_ => {
assert!(!ty::type_needs_drop(bcx.tcx(), datum.ty));

Expand Down
33 changes: 20 additions & 13 deletions src/librustc/middle/trans/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn get_drop_glue_type(ccx: &CrateContext, t: ty::t) -> ty::t {

ty::ty_uniq(typ) if !ty::type_needs_drop(tcx, typ) => {
match ty::get(typ).sty {
ty::ty_vec(_, None) | ty::ty_str => t,
ty::ty_vec(_, None) | ty::ty_str | ty::ty_trait(..) => t,
_ => {
let llty = sizing_type_of(ccx, typ);
// `Box<ZeroSizeType>` does not allocate.
Expand Down Expand Up @@ -295,25 +295,42 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
decr_refcnt_maybe_free(bcx, v0, body_ty)
}
ty::ty_uniq(content_ty) => {
let llbox = Load(bcx, v0);
let not_null = IsNotNull(bcx, llbox);
match ty::get(content_ty).sty {
ty::ty_vec(mt, None) => {
let llbox = Load(bcx, v0);
let not_null = IsNotNull(bcx, llbox);
with_cond(bcx, not_null, |bcx| {
let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, mt.ty);
// FIXME: #13994: the old `Box<[T]>` will not support sized deallocation
trans_exchange_free(bcx, llbox, 0, 8)
})
}
ty::ty_str => {
let llbox = Load(bcx, v0);
let not_null = IsNotNull(bcx, llbox);
with_cond(bcx, not_null, |bcx| {
let unit_ty = ty::sequence_element_type(bcx.tcx(), t);
let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, unit_ty);
// FIXME: #13994: the old `Box<str>` will not support sized deallocation
trans_exchange_free(bcx, llbox, 0, 8)
})
}
ty::ty_trait(..) => {
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
// Only drop the value when it is non-null
with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {
let dtor_ptr = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_vtable]));
let dtor = Load(bcx, dtor_ptr);
Call(bcx,
dtor,
[PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))],
[]);
bcx
})
}
_ => {
let llbox = Load(bcx, v0);
let not_null = IsNotNull(bcx, llbox);
with_cond(bcx, not_null, |bcx| {
let bcx = drop_ty(bcx, llbox, content_ty);
trans_exchange_free_ty(bcx, llbox, content_ty)
Expand All @@ -336,16 +353,6 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
}
}
}
ty::ty_trait(box ty::TyTrait { store: ty::UniqTraitStore, .. }) => {
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
// Only drop the value when it is non-null
with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {
let dtor_ptr = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_vtable]));
let dtor = Load(bcx, dtor_ptr);
Call(bcx, dtor, [PointerCast(bcx, lluniquevalue, Type::i8p(bcx.ccx()))], []);
bcx
})
}
ty::ty_closure(ref f) if f.store == ty::UniqTraitStore => {
let box_cell_v = GEPi(bcx, v0, [0u, abi::fn_field_box]);
let env = Load(bcx, box_cell_v);
Expand Down
Loading

1 comment on commit 8e7213f

@nrc
Copy link
Owner Author

@nrc nrc commented on 8e7213f Jun 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=nmatsakis

Please sign in to comment.