Skip to content

Commit c18a132

Browse files
committed
Make TyTrait embed a TraitRef, so that when we extend TraitRef, it naturally carries over to object types.
I wanted to embed an `Rc<TraitRef>`, but I was foiled by the current static rules, which prohibit non-Sync values from being stored in static locations. This means that the constants for `ty_int` and so forth cannot be initialized.
1 parent 0a3cbf8 commit c18a132

File tree

19 files changed

+178
-192
lines changed

19 files changed

+178
-192
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,10 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
389389
}
390390
'x' => {
391391
assert_eq!(next(st), '[');
392-
let def = parse_def(st, NominalType, |x,y| conv(x,y));
393-
let substs = parse_substs(st, |x,y| conv(x,y));
392+
let trait_ref = parse_trait_ref(st, |x,y| conv(x,y));
394393
let bounds = parse_existential_bounds(st, |x,y| conv(x,y));
395394
assert_eq!(next(st), ']');
396-
return ty::mk_trait(st.tcx, def, substs, bounds);
395+
return ty::mk_trait(st.tcx, trait_ref, bounds);
397396
}
398397
'p' => {
399398
let did = parse_def(st, TypeParameter, |x,y| conv(x,y));

src/librustc/metadata/tyencode.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,10 @@ fn enc_sty(w: &mut SeekableMemWriter, cx: &ctxt, st: &ty::sty) {
231231
enc_substs(w, cx, substs);
232232
mywrite!(w, "]");
233233
}
234-
ty::ty_trait(box ty::TyTrait {
235-
def_id,
236-
ref substs,
237-
ref bounds
238-
}) => {
239-
mywrite!(w, "x[{}|", (cx.ds)(def_id));
240-
enc_substs(w, cx, substs);
234+
ty::ty_trait(box ty::TyTrait { ref principal,
235+
ref bounds }) => {
236+
mywrite!(w, "x[");
237+
enc_trait_ref(w, cx, principal);
241238
enc_existential_bounds(w, cx, bounds);
242239
mywrite!(w, "]");
243240
}

src/librustc/middle/astencode.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,16 +1085,19 @@ impl<'a> rbml_writer_helpers for Encoder<'a> {
10851085
this.emit_enum_variant_arg(1, |this| idx.encode(this))
10861086
})
10871087
}
1088-
ty::UnsizeVtable(ty::TyTrait { def_id,
1089-
bounds: ref b,
1090-
ref substs },
1088+
ty::UnsizeVtable(ty::TyTrait { ref principal,
1089+
bounds: ref b },
10911090
self_ty) => {
10921091
this.emit_enum_variant("UnsizeVtable", 2, 4, |this| {
1093-
this.emit_enum_variant_arg(
1094-
0, |this| Ok(this.emit_existential_bounds(ecx, b)));
1095-
this.emit_enum_variant_arg(1, |this| def_id.encode(this));
1096-
this.emit_enum_variant_arg(2, |this| Ok(this.emit_ty(ecx, self_ty)));
1097-
this.emit_enum_variant_arg(3, |this| Ok(this.emit_substs(ecx, substs)))
1092+
this.emit_enum_variant_arg(0, |this| {
1093+
try!(this.emit_struct_field("principal", 0, |this| {
1094+
Ok(this.emit_trait_ref(ecx, &*principal))
1095+
}));
1096+
this.emit_struct_field("bounds", 1, |this| {
1097+
Ok(this.emit_existential_bounds(ecx, b))
1098+
})
1099+
});
1100+
this.emit_enum_variant_arg(1, |this| Ok(this.emit_ty(ecx, self_ty)))
10981101
})
10991102
}
11001103
}
@@ -1693,18 +1696,19 @@ impl<'a> rbml_decoder_decoder_helpers for reader::Decoder<'a> {
16931696
ty::UnsizeStruct(box uk, idx)
16941697
}
16951698
2 => {
1696-
let b =
1697-
this.read_enum_variant_arg(
1698-
0, |this| Ok(this.read_existential_bounds(dcx))).unwrap();
1699-
let def_id: ast::DefId =
1700-
this.read_enum_variant_arg(1, |this| Decodable::decode(this)).unwrap();
1699+
let ty_trait = try!(this.read_enum_variant_arg(0, |this| {
1700+
let principal = try!(this.read_struct_field("principal", 0, |this| {
1701+
Ok(this.read_trait_ref(dcx))
1702+
}));
1703+
Ok(ty::TyTrait {
1704+
principal: (*principal).clone(),
1705+
bounds: try!(this.read_struct_field("bounds", 1, |this| {
1706+
Ok(this.read_existential_bounds(dcx))
1707+
})),
1708+
})
1709+
}));
17011710
let self_ty =
1702-
this.read_enum_variant_arg(2, |this| Ok(this.read_ty(dcx))).unwrap();
1703-
let substs = this.read_enum_variant_arg(3,
1704-
|this| Ok(this.read_substs(dcx))).unwrap();
1705-
let ty_trait = ty::TyTrait { def_id: def_id.tr(dcx),
1706-
bounds: b,
1707-
substs: substs };
1711+
this.read_enum_variant_arg(1, |this| Ok(this.read_ty(dcx))).unwrap();
17081712
ty::UnsizeVtable(ty_trait, self_ty)
17091713
}
17101714
_ => panic!("bad enum variant for ty::UnsizeKind")

src/librustc/middle/traits/coherence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn ty_is_local(tcx: &ty::ctxt,
143143
}
144144

145145
ty::ty_trait(ref tt) => {
146-
tt.def_id.krate == ast::LOCAL_CRATE
146+
tt.principal.def_id.krate == ast::LOCAL_CRATE
147147
}
148148

149149
// Type parameters may be bound to types that are not local to

src/librustc/middle/trans/debuginfo.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ impl TypeMap {
423423

424424
from_def_id_and_substs(self,
425425
cx,
426-
trait_data.def_id,
427-
&trait_data.substs,
426+
trait_data.principal.def_id,
427+
&trait_data.principal.substs,
428428
&mut unique_type_id);
429429
},
430430
ty::ty_bare_fn(ty::BareFnTy{ fn_style, abi, ref sig } ) => {
@@ -2813,7 +2813,7 @@ fn trait_pointer_metadata(cx: &CrateContext,
28132813
// But it does not describe the trait's methods.
28142814

28152815
let def_id = match ty::get(trait_type).sty {
2816-
ty::ty_trait(box ty::TyTrait { def_id, .. }) => def_id,
2816+
ty::ty_trait(box ty::TyTrait { ref principal, .. }) => principal.def_id,
28172817
_ => {
28182818
let pp_type_name = ppaux::ty_to_string(cx.tcx(), trait_type);
28192819
cx.sess().bug(format!("debuginfo: Unexpected trait-object type in \
@@ -3739,8 +3739,8 @@ fn push_debuginfo_type_name(cx: &CrateContext,
37393739
output.push(']');
37403740
},
37413741
ty::ty_trait(ref trait_data) => {
3742-
push_item_name(cx, trait_data.def_id, false, output);
3743-
push_type_params(cx, &trait_data.substs, output);
3742+
push_item_name(cx, trait_data.principal.def_id, false, output);
3743+
push_type_params(cx, &trait_data.principal.substs, output);
37443744
},
37453745
ty::ty_bare_fn(ty::BareFnTy{ fn_style, abi, ref sig } ) => {
37463746
if fn_style == ast::UnsafeFn {

src/librustc/middle/trans/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
324324
_ => bcx.sess().bug(format!("UnsizeStruct with bad sty: {}",
325325
bcx.ty_to_string(unsized_ty)).as_slice())
326326
},
327-
&ty::UnsizeVtable(ty::TyTrait { def_id, ref substs, .. }, _) => {
328-
let substs = substs.with_self_ty(unsized_ty);
327+
&ty::UnsizeVtable(ty::TyTrait { ref principal, .. }, _) => {
328+
let substs = principal.substs.with_self_ty(unsized_ty);
329329
let trait_ref =
330-
Rc::new(ty::TraitRef { def_id: def_id,
330+
Rc::new(ty::TraitRef { def_id: principal.def_id,
331331
substs: substs });
332332
let trait_ref =
333333
trait_ref.subst(bcx.tcx(), &bcx.fcx.param_substs.substs);

src/librustc/middle/ty.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,14 @@ pub fn type_of_adjust(cx: &ctxt, adj: &AutoAdjustment) -> Option<t> {
378378
fn type_of_autoref(cx: &ctxt, autoref: &AutoRef) -> Option<t> {
379379
match autoref {
380380
&AutoUnsize(ref k) => match k {
381-
&UnsizeVtable(TyTrait { def_id, ref substs, bounds }, _) => {
382-
Some(mk_trait(cx, def_id, substs.clone(), bounds))
381+
&UnsizeVtable(TyTrait { ref principal, bounds }, _) => {
382+
Some(mk_trait(cx, (*principal).clone(), bounds))
383383
}
384384
_ => None
385385
},
386386
&AutoUnsizeUniq(ref k) => match k {
387-
&UnsizeVtable(TyTrait { def_id, ref substs, bounds }, _) => {
388-
Some(mk_uniq(cx, mk_trait(cx, def_id, substs.clone(), bounds)))
387+
&UnsizeVtable(TyTrait { ref principal, bounds }, _) => {
388+
Some(mk_uniq(cx, mk_trait(cx, (*principal).clone(), bounds)))
389389
}
390390
_ => None
391391
},
@@ -983,12 +983,12 @@ pub enum sty {
983983

984984
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
985985
pub struct TyTrait {
986-
pub def_id: DefId,
987-
pub substs: Substs,
986+
// Principal trait reference.
987+
pub principal: TraitRef, // would use Rc<TraitRef>, but it runs afoul of some static rules
988988
pub bounds: ExistentialBounds
989989
}
990990

991-
#[deriving(PartialEq, Eq, Hash, Show)]
991+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
992992
pub struct TraitRef {
993993
pub def_id: DefId,
994994
pub substs: Substs,
@@ -1643,8 +1643,8 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
16431643
&ty_enum(_, ref substs) | &ty_struct(_, ref substs) => {
16441644
flags = flags | sflags(substs);
16451645
}
1646-
&ty_trait(box TyTrait { ref substs, ref bounds, .. }) => {
1647-
flags = flags | sflags(substs);
1646+
&ty_trait(box TyTrait { ref principal, ref bounds }) => {
1647+
flags = flags | sflags(&principal.substs);
16481648
flags = flags | flags_for_bounds(bounds);
16491649
}
16501650
&ty_uniq(tt) | &ty_vec(tt, _) | &ty_open(tt) => {
@@ -1874,14 +1874,12 @@ pub fn mk_ctor_fn(cx: &ctxt,
18741874

18751875

18761876
pub fn mk_trait(cx: &ctxt,
1877-
did: ast::DefId,
1878-
substs: Substs,
1877+
principal: ty::TraitRef,
18791878
bounds: ExistentialBounds)
18801879
-> t {
18811880
// take a copy of substs so that we own the vectors inside
18821881
let inner = box TyTrait {
1883-
def_id: did,
1884-
substs: substs,
1882+
principal: principal,
18851883
bounds: bounds
18861884
};
18871885
mk_t(cx, ty_trait(inner))
@@ -1934,9 +1932,15 @@ pub fn maybe_walk_ty(ty: t, f: |t| -> bool) {
19341932
ty_ptr(ref tm) | ty_rptr(_, ref tm) => {
19351933
maybe_walk_ty(tm.ty, f);
19361934
}
1937-
ty_enum(_, ref substs) | ty_struct(_, ref substs) | ty_unboxed_closure(_, _, ref substs) |
1938-
ty_trait(box TyTrait { ref substs, .. }) => {
1939-
for subty in (*substs).types.iter() {
1935+
ty_trait(box TyTrait { ref principal, .. }) => {
1936+
for subty in principal.substs.types.iter() {
1937+
maybe_walk_ty(*subty, |x| f(x));
1938+
}
1939+
}
1940+
ty_enum(_, ref substs) |
1941+
ty_struct(_, ref substs) |
1942+
ty_unboxed_closure(_, _, ref substs) => {
1943+
for subty in substs.types.iter() {
19401944
maybe_walk_ty(*subty, |x| f(x));
19411945
}
19421946
}
@@ -3554,8 +3558,8 @@ pub fn unsize_ty(cx: &ctxt,
35543558
format!("UnsizeStruct with bad sty: {}",
35553559
ty_to_string(cx, ty)).as_slice())
35563560
},
3557-
&UnsizeVtable(TyTrait { def_id, ref substs, bounds }, _) => {
3558-
mk_trait(cx, def_id, substs.clone(), bounds)
3561+
&UnsizeVtable(TyTrait { ref principal, bounds }, _) => {
3562+
mk_trait(cx, (*principal).clone(), bounds)
35593563
}
35603564
}
35613565
}
@@ -3808,7 +3812,7 @@ pub fn ty_sort_string(cx: &ctxt, t: t) -> String {
38083812
ty_bare_fn(_) => "extern fn".to_string(),
38093813
ty_closure(_) => "fn".to_string(),
38103814
ty_trait(ref inner) => {
3811-
format!("trait {}", item_path_str(cx, inner.def_id))
3815+
format!("trait {}", item_path_str(cx, inner.principal.def_id))
38123816
}
38133817
ty_struct(id, _) => {
38143818
format!("struct {}", item_path_str(cx, id))
@@ -4230,11 +4234,14 @@ pub fn try_add_builtin_trait(
42304234

42314235
pub fn ty_to_def_id(ty: t) -> Option<ast::DefId> {
42324236
match get(ty).sty {
4233-
ty_trait(box TyTrait { def_id: id, .. }) |
4237+
ty_trait(ref tt) =>
4238+
Some(tt.principal.def_id),
42344239
ty_struct(id, _) |
42354240
ty_enum(id, _) |
4236-
ty_unboxed_closure(id, _, _) => Some(id),
4237-
_ => None
4241+
ty_unboxed_closure(id, _, _) =>
4242+
Some(id),
4243+
_ =>
4244+
None
42384245
}
42394246
}
42404247

@@ -5213,9 +5220,9 @@ pub fn hash_crate_independent(tcx: &ctxt, t: t, svh: &Svh) -> u64 {
52135220
}
52145221
}
52155222
}
5216-
ty_trait(box TyTrait { def_id: d, bounds, .. }) => {
5223+
ty_trait(box TyTrait { ref principal, bounds }) => {
52175224
byte!(17);
5218-
did(&mut state, d);
5225+
did(&mut state, principal.def_id);
52195226
hash!(bounds);
52205227
}
52215228
ty_struct(d, _) => {
@@ -5504,12 +5511,13 @@ pub fn accumulate_lifetimes_in_type(accumulator: &mut Vec<ty::Region>,
55045511
typ: t) {
55055512
walk_ty(typ, |typ| {
55065513
match get(typ).sty {
5507-
ty_rptr(region, _) => accumulator.push(region),
5514+
ty_rptr(region, _) => {
5515+
accumulator.push(region)
5516+
}
5517+
ty_trait(ref t) => {
5518+
accumulator.push_all(t.principal.substs.regions().as_slice());
5519+
}
55085520
ty_enum(_, ref substs) |
5509-
ty_trait(box TyTrait {
5510-
ref substs,
5511-
..
5512-
}) |
55135521
ty_struct(_, ref substs) => {
55145522
accum_substs(accumulator, substs);
55155523
}

src/librustc/middle/ty_fold.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,11 @@ impl TypeFoldable for ty::UnsizeKind {
373373
match *self {
374374
ty::UnsizeLength(len) => ty::UnsizeLength(len),
375375
ty::UnsizeStruct(box ref k, n) => ty::UnsizeStruct(box k.fold_with(folder), n),
376-
ty::UnsizeVtable(ty::TyTrait{bounds, def_id, ref substs}, self_ty) => {
376+
ty::UnsizeVtable(ty::TyTrait{ref principal, bounds}, self_ty) => {
377377
ty::UnsizeVtable(
378378
ty::TyTrait {
379+
principal: principal.fold_with(folder),
379380
bounds: bounds.fold_with(folder),
380-
def_id: def_id,
381-
substs: substs.fold_with(folder)
382381
},
383382
self_ty.fold_with(folder))
384383
}
@@ -529,15 +528,10 @@ pub fn super_fold_sty<'tcx, T: TypeFolder<'tcx>>(this: &mut T,
529528
ty::ty_enum(tid, ref substs) => {
530529
ty::ty_enum(tid, substs.fold_with(this))
531530
}
532-
ty::ty_trait(box ty::TyTrait {
533-
def_id,
534-
ref substs,
535-
bounds
536-
}) => {
531+
ty::ty_trait(box ty::TyTrait { ref principal, bounds }) => {
537532
ty::ty_trait(box ty::TyTrait {
538-
def_id: def_id,
539-
substs: substs.fold_with(this),
540-
bounds: this.fold_existential_bounds(bounds),
533+
principal: (*principal).fold_with(this),
534+
bounds: bounds.fold_with(this),
541535
})
542536
}
543537
ty::ty_tup(ref ts) => {

0 commit comments

Comments
 (0)