Skip to content

rustc: put ty_trait and ty_closure behind some indirection, nearly halving the size of ty::sty #13013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
enc_substs(w, cx, substs);
mywrite!(w, "]");
}
ty::ty_trait(def, ref substs, store, mt, bounds) => {
mywrite!(w, "x[{}|", (cx.ds)(def));
ty::ty_trait(~ty::TyTrait { def_id, ref substs, store, mutability, bounds }) => {
mywrite!(w, "x[{}|", (cx.ds)(def_id));
enc_substs(w, cx, substs);
enc_trait_store(w, cx, store);
enc_mutability(w, mt);
enc_mutability(w, mutability);
let bounds = ty::ParamBounds {builtin_bounds: bounds,
trait_bounds: Vec::new()};
enc_bounds(w, cx, &bounds);
Expand Down Expand Up @@ -315,7 +315,7 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
ty::ty_unboxed_vec(mt) => { mywrite!(w, "U"); enc_mt(w, cx, mt); }
ty::ty_closure(ref f) => {
mywrite!(w, "f");
enc_closure_ty(w, cx, f);
enc_closure_ty(w, cx, *f);
}
ty::ty_bare_fn(ref f) => {
mywrite!(w, "F");
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,21 @@ fn with_appropriate_checker(cx: &Context,

let fty = ty::node_id_to_type(cx.tcx, id);
match ty::get(fty).sty {
ty::ty_closure(ty::ClosureTy {
ty::ty_closure(~ty::ClosureTy {
sigil: OwnedSigil,
bounds: bounds,
..
}) => {
b(|cx, fv| check_for_uniq(cx, fv, bounds))
}
ty::ty_closure(ty::ClosureTy {
ty::ty_closure(~ty::ClosureTy {
sigil: ManagedSigil,
..
}) => {
// can't happen
fail!("internal error: saw closure with managed sigil (@fn)");
}
ty::ty_closure(ty::ClosureTy {
ty::ty_closure(~ty::ClosureTy {
sigil: BorrowedSigil,
bounds: bounds,
region: region,
Expand Down Expand Up @@ -358,7 +358,7 @@ pub fn check_expr(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(_, _, _, _, bounds) => {
ty::ty_trait(~ty::TyTrait { bounds, .. }) => {
check_trait_cast_bounds(cx, span, source_ty, bounds);
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
}
ty::ty_uniq(_) | ty::ty_str(ty::vstore_uniq) |
ty::ty_vec(_, ty::vstore_uniq) |
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) => {
n_uniq += 1;
}
ty::ty_closure(ref c) if c.sigil == ast::OwnedSigil => {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ 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(_, _, ty::UniqTraitStore, _, _) |
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) |
ty::ty_vec(_, ty::vstore_uniq) |
ty::ty_str(ty::vstore_uniq) |
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
ty::ty_closure(~ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
Some(deref_ptr(OwnedPtr))
}

Expand All @@ -183,13 +183,13 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
Some(deref_ptr(BorrowedPtr(kind, r)))
}

ty::ty_trait(_, _, ty::RegionTraitStore(r), m, _) => {
ty::ty_trait(~ty::TyTrait { store: ty::RegionTraitStore(r), mutability: m, .. }) => {
let kind = ty::BorrowKind::from_mutbl(m);
Some(deref_ptr(BorrowedPtr(kind, r)))
}

ty::ty_str(ty::vstore_slice(r)) |
ty::ty_closure(ty::ClosureTy {sigil: ast::BorrowedSigil,
ty::ty_closure(~ty::ClosureTy {sigil: ast::BorrowedSigil,
region: r, ..}) => {
Some(deref_ptr(BorrowedPtr(ty::ImmBorrow, r)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub fn decl_rust_fn(ccx: &CrateContext, has_env: bool,
// noalias because the actual object pointer is nested.
ty::ty_uniq(..) | // ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
ty::ty_vec(_, ty::vstore_uniq) | ty::ty_str(ty::vstore_uniq) |
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
ty::ty_closure(~ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
unsafe {
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/middle/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2126,7 +2126,9 @@ fn type_metadata(cx: &CrateContext,
ty::ty_closure(ref closurety) => {
subroutine_type_metadata(cx, &closurety.sig, usage_site_span)
},
ty::ty_trait(def_id, ref substs, trait_store, mutability, ref bounds) => {
ty::ty_trait(~ty::TyTrait { def_id, ref substs,
store: trait_store, mutability,
ref bounds }) => {
trait_metadata(cx, def_id, t, substs, trait_store, mutability, bounds)
},
ty::ty_struct(def_id, ref substs) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
}
}
}
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
ty::ty_trait(~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| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl<'a> Reflector<'a> {
})
}

ty::ty_trait(_, _, _, _, _) => {
ty::ty_trait(..) => {
let extra = [
self.c_slice(token::intern_and_get_ident(ty_to_str(tcx, t)))
];
Expand Down
56 changes: 36 additions & 20 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,8 @@ pub enum sty {
ty_ptr(mt),
ty_rptr(Region, mt),
ty_bare_fn(BareFnTy),
ty_closure(ClosureTy),
ty_trait(DefId, substs, TraitStore, ast::Mutability, BuiltinBounds),
ty_closure(~ClosureTy),
ty_trait(~TyTrait),
ty_struct(DefId, substs),
ty_tup(Vec<t>),

Expand All @@ -764,6 +764,15 @@ pub enum sty {
ty_unboxed_vec(mt),
}

#[deriving(Clone, Eq, Hash)]
pub struct TyTrait {
def_id: DefId,
substs: substs,
store: TraitStore,
mutability: ast::Mutability,
bounds: BuiltinBounds
}

#[deriving(Eq, Hash)]
pub struct TraitRef {
def_id: DefId,
Expand Down Expand Up @@ -1205,10 +1214,10 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
&ty_infer(_) => flags |= needs_infer as uint,
&ty_self(_) => flags |= has_self as uint,
&ty_enum(_, ref substs) | &ty_struct(_, ref substs) |
&ty_trait(_, ref substs, _, _, _) => {
&ty_trait(~ty::TyTrait { ref substs, .. }) => {
flags |= sflags(substs);
match st {
ty_trait(_, _, RegionTraitStore(r), _, _) => {
ty_trait(~ty::TyTrait { store: RegionTraitStore(r), .. }) => {
flags |= rflags(r);
}
_ => {}
Expand Down Expand Up @@ -1398,7 +1407,7 @@ pub fn mk_mut_unboxed_vec(cx: &ctxt, ty: t) -> t {
pub fn mk_tup(cx: &ctxt, ts: Vec<t>) -> t { mk_t(cx, ty_tup(ts)) }

pub fn mk_closure(cx: &ctxt, fty: ClosureTy) -> t {
mk_t(cx, ty_closure(fty))
mk_t(cx, ty_closure(~fty))
}

pub fn mk_bare_fn(cx: &ctxt, fty: BareFnTy) -> t {
Expand Down Expand Up @@ -1432,7 +1441,14 @@ pub fn mk_trait(cx: &ctxt,
bounds: BuiltinBounds)
-> t {
// take a copy of substs so that we own the vectors inside
mk_t(cx, ty_trait(did, substs, store, mutability, bounds))
let inner = ~TyTrait {
def_id: did,
substs: substs,
store: store,
mutability: mutability,
bounds: bounds
};
mk_t(cx, ty_trait(inner))
}

pub fn mk_struct(cx: &ctxt, struct_id: ast::DefId, substs: substs) -> t {
Expand Down Expand Up @@ -1472,7 +1488,7 @@ pub fn maybe_walk_ty(ty: t, f: |t| -> bool) {
maybe_walk_ty(tm.ty, f);
}
ty_enum(_, ref substs) | ty_struct(_, ref substs) |
ty_trait(_, ref substs, _, _, _) => {
ty_trait(~TyTrait { ref substs, .. }) => {
for subty in (*substs).tps.iter() { maybe_walk_ty(*subty, |x| f(x)); }
}
ty_tup(ref ts) => { for tt in ts.iter() { maybe_walk_ty(*tt, |x| f(x)); } }
Expand Down Expand Up @@ -2133,7 +2149,7 @@ pub fn type_contents(cx: &ctxt, ty: t) -> TypeContents {
}

ty_closure(ref c) => {
closure_contents(cx, c)
closure_contents(cx, *c)
}

ty_box(typ) => {
Expand All @@ -2144,8 +2160,8 @@ pub fn type_contents(cx: &ctxt, ty: t) -> TypeContents {
tc_ty(cx, typ, cache).owned_pointer()
}

ty_trait(_, _, store, mutbl, bounds) => {
object_contents(cx, store, mutbl, bounds)
ty_trait(~ty::TyTrait { store, mutability, bounds, .. }) => {
object_contents(cx, store, mutability, bounds)
}

ty_ptr(ref mt) => {
Expand Down Expand Up @@ -2437,7 +2453,7 @@ pub fn is_instantiable(cx: &ctxt, r_ty: t) -> bool {
false // unsafe ptrs can always be NULL
}

ty_trait(_, _, _, _, _) => {
ty_trait(..) => {
false
}

Expand Down Expand Up @@ -2854,7 +2870,7 @@ pub fn ty_region(tcx: &ctxt,
pub fn replace_fn_sig(cx: &ctxt, fsty: &sty, new_sig: FnSig) -> t {
match *fsty {
ty_bare_fn(ref f) => mk_bare_fn(cx, BareFnTy {sig: new_sig, ..*f}),
ty_closure(ref f) => mk_closure(cx, ClosureTy {sig: new_sig, ..*f}),
ty_closure(ref f) => mk_closure(cx, ClosureTy {sig: new_sig, ..**f}),
ref s => {
cx.sess.bug(
format!("ty_fn_sig() called on non-fn type: {:?}", s));
Expand All @@ -2872,7 +2888,7 @@ pub fn replace_closure_return_type(tcx: &ctxt, fn_type: t, ret_type: t) -> t {
ty::ty_closure(ref fty) => {
ty::mk_closure(tcx, ClosureTy {
sig: FnSig {output: ret_type, ..fty.sig.clone()},
..(*fty).clone()
..(**fty).clone()
})
}
_ => {
Expand Down Expand Up @@ -3124,7 +3140,7 @@ pub fn adjust_ty(cx: &ctxt,
ty::mk_closure(cx, ClosureTy {
sigil: BorrowedSigil,
region: r,
..(*fty).clone()
..(**fty).clone()
})
}

Expand All @@ -3140,9 +3156,9 @@ pub fn adjust_ty(cx: &ctxt,
fn borrow_obj(cx: &ctxt, span: Span, r: Region,
m: ast::Mutability, ty: ty::t) -> ty::t {
match get(ty).sty {
ty_trait(trt_did, ref trt_substs, _, _, b) => {
ty::mk_trait(cx, trt_did, trt_substs.clone(),
RegionTraitStore(r), m, b)
ty_trait(~ty::TyTrait {def_id, ref substs, bounds, .. }) => {
ty::mk_trait(cx, def_id, substs.clone(),
RegionTraitStore(r), m, bounds)
}
ref s => {
cx.sess.span_bug(
Expand Down Expand Up @@ -3479,7 +3495,7 @@ pub fn ty_sort_str(cx: &ctxt, t: t) -> ~str {
ty_rptr(_, _) => ~"&-ptr",
ty_bare_fn(_) => ~"extern fn",
ty_closure(_) => ~"fn",
ty_trait(id, _, _, _, _) => format!("trait {}", item_path_str(cx, id)),
ty_trait(ref inner) => format!("trait {}", item_path_str(cx, inner.def_id)),
ty_struct(id, _) => format!("struct {}", item_path_str(cx, id)),
ty_tup(_) => ~"tuple",
ty_infer(TyVar(_)) => ~"inferred type",
Expand Down Expand Up @@ -3865,7 +3881,7 @@ pub fn try_add_builtin_trait(tcx: &ctxt,

pub fn ty_to_def_id(ty: t) -> Option<ast::DefId> {
match get(ty).sty {
ty_trait(id, _, _, _, _) | ty_struct(id, _) | ty_enum(id, _) => Some(id),
ty_trait(~TyTrait { def_id: id, .. }) | ty_struct(id, _) | ty_enum(id, _) => Some(id),
_ => None
}
}
Expand Down Expand Up @@ -4951,7 +4967,7 @@ pub fn hash_crate_independent(tcx: &ctxt, t: t, svh: &Svh) -> u64 {
hash!(c.bounds);
region(&mut state, c.region);
}
ty_trait(d, _, store, m, bounds) => {
ty_trait(~ty::TyTrait { def_id: d, store, mutability: m, bounds, .. }) => {
byte!(17);
did(&mut state, d);
match store {
Expand Down
16 changes: 9 additions & 7 deletions src/librustc/middle/ty_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
ty::ty_enum(tid, ref substs) => {
ty::ty_enum(tid, this.fold_substs(substs))
}
ty::ty_trait(did, ref substs, st, mutbl, bounds) => {
ty::ty_trait(did,
this.fold_substs(substs),
this.fold_trait_store(st),
mutbl,
bounds)
ty::ty_trait(~ty::TyTrait { def_id, ref substs, store, mutability, bounds }) => {
ty::ty_trait(~ty::TyTrait{
def_id: def_id,
substs: this.fold_substs(substs),
store: this.fold_trait_store(store),
mutability: mutability,
bounds: bounds
})
}
ty::ty_tup(ref ts) => {
ty::ty_tup(fold_ty_vec(this, ts.as_slice()))
Expand All @@ -173,7 +175,7 @@ pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
ty::ty_bare_fn(this.fold_bare_fn_ty(f))
}
ty::ty_closure(ref f) => {
ty::ty_closure(this.fold_closure_ty(f))
ty::ty_closure(~this.fold_closure_ty(*f))
}
ty::ty_rptr(r, ref tm) => {
ty::ty_rptr(this.fold_region(r),
Expand Down
22 changes: 14 additions & 8 deletions src/librustc/middle/typeck/check/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,9 @@ impl<'a> LookupContext<'a> {
let span = self.self_expr.map_or(self.span, |e| e.span);
check::autoderef(self.fcx, span, self_ty, None, PreferMutLvalue, |self_ty, _| {
match get(self_ty).sty {
ty_trait(did, ref substs, _, _, _) => {
self.push_inherent_candidates_from_object(did, substs);
self.push_inherent_impl_candidates_for_type(did);
ty_trait(~TyTrait { def_id, ref substs, .. }) => {
self.push_inherent_candidates_from_object(def_id, substs);
self.push_inherent_impl_candidates_for_type(def_id);
}
ty_enum(did, _) | ty_struct(did, _) => {
if self.check_traits == CheckTraitsAndInherentMethods {
Expand Down Expand Up @@ -775,10 +775,12 @@ impl<'a> LookupContext<'a> {
autoderefs: autoderefs,
autoref: Some(ty::AutoBorrowVec(region, self_mt.mutbl))})
}
ty::ty_trait(did, ref substs, ty::RegionTraitStore(_), mutbl, bounds) => {
ty::ty_trait(~ty::TyTrait {
def_id, ref substs, store: ty::RegionTraitStore(_), mutability: mutbl, bounds
}) => {
let region =
self.infcx().next_region_var(infer::Autoref(self.span));
(ty::mk_trait(tcx, did, substs.clone(),
(ty::mk_trait(tcx, def_id, substs.clone(),
ty::RegionTraitStore(region),
mutbl, bounds),
ty::AutoDerefRef {
Expand Down Expand Up @@ -860,7 +862,7 @@ impl<'a> LookupContext<'a> {
})
}

ty_trait(trt_did, trt_substs, _, _, b) => {
ty_trait(~ty::TyTrait { def_id: trt_did, substs: trt_substs, bounds: b, .. }) => {
// Coerce ~/@/&Trait instances to &Trait.

self.search_for_some_kind_of_autorefd_method(
Expand Down Expand Up @@ -1301,7 +1303,9 @@ impl<'a> LookupContext<'a> {
rcvr_matches_ty(self.fcx, mt.ty, candidate)
}

ty::ty_trait(self_did, _, RegionTraitStore(_), self_m, _) => {
ty::ty_trait(~ty::TyTrait {
def_id: self_did, store: RegionTraitStore(_), mutability: self_m, ..
}) => {
mutability_matches(self_m, m) &&
rcvr_matches_object(self_did, candidate)
}
Expand All @@ -1317,7 +1321,9 @@ impl<'a> LookupContext<'a> {
rcvr_matches_ty(self.fcx, typ, candidate)
}

ty::ty_trait(self_did, _, UniqTraitStore, _, _) => {
ty::ty_trait(~ty::TyTrait {
def_id: self_did, store: UniqTraitStore, ..
}) => {
rcvr_matches_object(self_did, candidate)
}

Expand Down
Loading