Skip to content

Commit ce857e3

Browse files
committed
Parse and typecheck (not kindcheck) bounds on trait paths.
1 parent 394f455 commit ce857e3

37 files changed

+197
-121
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
954954
encode_name(ecx, ebml_w, item.ident);
955955
encode_attributes(ebml_w, item.attrs);
956956
match ty.node {
957-
ast::ty_path(path, _) if path.idents.len() == 1 => {
957+
ast::ty_path(path, bounds, _) if path.idents.len() == 1 => {
958+
assert!(bounds.is_empty());
958959
encode_impl_type_basename(ecx, ebml_w,
959960
ast_util::path_to_ident(path));
960961
}

src/librustc/metadata/tydecode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
311311
let substs = parse_substs(st, conv);
312312
let store = parse_trait_store(st);
313313
let mt = parse_mutability(st);
314+
let bounds = parse_bounds(st, conv);
314315
assert_eq!(next(st), ']');
315-
return ty::mk_trait(st.tcx, def, substs, store, mt);
316+
return ty::mk_trait(st.tcx, def, substs, store, mt, bounds.builtin_bounds);
316317
}
317318
'p' => {
318319
let did = parse_def(st, TypeParameter, conv);

src/librustc/metadata/tyencode.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,16 @@ fn enc_sty(w: @io::Writer, cx: @ctxt, st: ty::sty) {
261261
enc_substs(w, cx, substs);
262262
w.write_char(']');
263263
}
264-
ty::ty_trait(def, ref substs, store, mt) => {
264+
ty::ty_trait(def, ref substs, store, mt, bounds) => {
265265
w.write_str(&"x[");
266266
w.write_str((cx.ds)(def));
267267
w.write_char('|');
268268
enc_substs(w, cx, substs);
269269
enc_trait_store(w, cx, store);
270270
enc_mutability(w, mt);
271+
let bounds = ty::ParamBounds {builtin_bounds: bounds,
272+
trait_bounds: ~[]};
273+
enc_bounds(w, cx, &bounds);
271274
w.write_char(']');
272275
}
273276
ty::ty_tup(ts) => {

src/librustc/middle/kind.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ fn check_item(item: @item, (cx, visitor): (Context, visit::vt<Context>)) {
129129
if cx.tcx.lang_items.drop_trait() == trait_def_id {
130130
// Yes, it's a destructor.
131131
match self_type.node {
132-
ty_path(_, path_node_id) => {
132+
ty_path(_, bounds, path_node_id) => {
133+
assert!(bounds.is_empty());
133134
let struct_def = cx.tcx.def_map.get_copy(
134135
&path_node_id);
135136
let struct_did =
@@ -307,7 +308,7 @@ pub fn check_expr(e: @expr, (cx, v): (Context, visit::vt<Context>)) {
307308

308309
fn check_ty(aty: @Ty, (cx, v): (Context, visit::vt<Context>)) {
309310
match aty.node {
310-
ty_path(_, id) => {
311+
ty_path(_, _, id) => {
311312
let r = cx.tcx.node_type_substs.find(&id);
312313
for r.iter().advance |ts| {
313314
let did = ast_util::def_id_of_def(cx.tcx.def_map.get_copy(&id));
@@ -533,7 +534,8 @@ pub fn check_cast_for_escaping_regions(
533534
pub fn check_kind_bounds_of_cast(cx: Context, source: @expr, target: @expr) {
534535
let target_ty = ty::expr_ty(cx.tcx, target);
535536
match ty::get(target_ty).sty {
536-
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
537+
// FIXME(#3569) kind check bounds here
538+
ty::ty_trait(_, _, ty::UniqTraitStore, _, _bounds) => {
537539
let source_ty = ty::expr_ty(cx.tcx, source);
538540
if !ty::type_is_owned(cx.tcx, source_ty) {
539541
cx.tcx.sess.span_err(

src/librustc/middle/lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ fn check_item_ctypes(cx: &Context, it: @ast::item) {
714714
let tys = vec::map(decl.inputs, |a| a.ty );
715715
for vec::each(vec::append_one(tys, decl.output)) |ty| {
716716
match ty.node {
717-
ast::ty_path(_, id) => {
717+
ast::ty_path(_, _, id) => {
718718
match cx.tcx.def_map.get_copy(&id) {
719719
ast::def_prim_ty(ast::ty_int(ast::ty_i)) => {
720720
cx.span_lint(ctypes, ty.span,

src/librustc/middle/region.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ pub fn determine_rp_in_ty(ty: @ast::Ty,
804804
// then check whether it is region-parameterized and consider
805805
// that as a direct dependency.
806806
match ty.node {
807-
ast::ty_path(path, id) => {
807+
ast::ty_path(path, _bounds, id) => {
808808
match cx.def_map.find(&id) {
809809
Some(&ast::def_ty(did)) |
810810
Some(&ast::def_trait(did)) |
@@ -840,7 +840,7 @@ pub fn determine_rp_in_ty(ty: @ast::Ty,
840840
visit_mt(mt, (cx, visitor));
841841
}
842842

843-
ast::ty_path(path, _) => {
843+
ast::ty_path(path, _bounds, _) => {
844844
// type parameters are---for now, anyway---always invariant
845845
do cx.with_ambient_variance(rv_invariant) {
846846
for path.types.iter().advance |tp| {

src/librustc/middle/resolve.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ impl Resolver {
12501250
// If there are static methods, then create the module
12511251
// and add them.
12521252
match (trait_ref_opt, ty) {
1253-
(None, @Ty { node: ty_path(path, _), _ }) if
1253+
(None, @Ty { node: ty_path(path, _, _), _ }) if
12541254
has_static_methods && path.idents.len() == 1 => {
12551255
let name = path_to_ident(path);
12561256

@@ -4120,7 +4120,7 @@ impl Resolver {
41204120
// Like path expressions, the interpretation of path types depends
41214121
// on whether the path has multiple elements in it or not.
41224122

4123-
ty_path(path, path_id) => {
4123+
ty_path(path, bounds, path_id) => {
41244124
// This is a path in the type namespace. Walk through scopes
41254125
// scopes looking for it.
41264126
let mut result_def = None;
@@ -4179,6 +4179,10 @@ impl Resolver {
41794179
self.idents_to_str(path.idents)));
41804180
}
41814181
}
4182+
4183+
for bounds.each |bound| {
4184+
self.resolve_type_parameter_bound(bound, visitor);
4185+
}
41824186
}
41834187

41844188
ty_closure(c) => {

src/librustc/middle/trans/debuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn create_ty(cx: @mut CrateContext, t: ty::t, span: span) -> DIType {
561561
cx.sess.span_note(span, "debuginfo for closure NYI");
562562
create_unimpl_ty(cx, t)
563563
},
564-
ty::ty_trait(_did, ref _substs, ref _vstore, _) => {
564+
ty::ty_trait(_did, ref _substs, ref _vstore, _, _bounds) => {
565565
cx.sess.span_note(span, "debuginfo for trait NYI");
566566
create_unimpl_ty(cx, t)
567567
},

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ fn trans_rvalue_dps_unadjusted(bcx: block, expr: @ast::expr,
683683
}
684684
ast::expr_cast(val, _) => {
685685
match ty::get(node_id_type(bcx, expr.id)).sty {
686-
ty::ty_trait(_, _, store, _) => {
686+
ty::ty_trait(_, _, store, _, _) => {
687687
return meth::trans_trait_cast(bcx, val, expr.id, dest,
688688
store);
689689
}

src/librustc/middle/trans/glue.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,13 @@ pub fn make_drop_glue(bcx: block, v0: ValueRef, t: ty::t) {
486486
ty::ty_closure(_) => {
487487
closure::make_closure_glue(bcx, v0, t, drop_ty)
488488
}
489-
ty::ty_trait(_, _, ty::BoxTraitStore, _) => {
489+
ty::ty_trait(_, _, ty::BoxTraitStore, _, _) => {
490490
let llbox_ptr = GEPi(bcx, v0, [0u, abi::trt_field_box]);
491491
let llbox = Load(bcx, llbox_ptr);
492492
decr_refcnt_maybe_free(bcx, llbox, Some(llbox_ptr),
493493
ty::mk_opaque_box(ccx.tcx))
494494
}
495-
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
495+
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
496496
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
497497
// Only drop the value when it is non-null
498498
do with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue))) |bcx| {
@@ -571,12 +571,12 @@ pub fn make_take_glue(bcx: block, v: ValueRef, t: ty::t) {
571571
ty::ty_closure(_) => {
572572
closure::make_closure_glue(bcx, v, t, take_ty)
573573
}
574-
ty::ty_trait(_, _, ty::BoxTraitStore, _) => {
574+
ty::ty_trait(_, _, ty::BoxTraitStore, _, _) => {
575575
let llbox = Load(bcx, GEPi(bcx, v, [0u, abi::trt_field_box]));
576576
incr_refcnt_of_boxed(bcx, llbox);
577577
bcx
578578
}
579-
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
579+
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
580580
let lluniquevalue = GEPi(bcx, v, [0, abi::trt_field_box]);
581581
let llvtable = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_vtable]));
582582

src/librustc/middle/trans/monomorphize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub fn normalize_for_monomorphization(tcx: ty::ctxt,
293293
ty::ty_closure(ref fty) => {
294294
Some(normalized_closure_ty(tcx, fty.sigil))
295295
}
296-
ty::ty_trait(_, _, ref store, _) => {
296+
ty::ty_trait(_, _, ref store, _, _) => {
297297
let sigil = match *store {
298298
ty::UniqTraitStore => ast::OwnedSigil,
299299
ty::BoxTraitStore => ast::ManagedSigil,

src/librustc/middle/trans/reachable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ fn traverse_ty<'a>(ty: @Ty, (cx, v): (@mut ctx<'a>, visit::vt<@mut ctx<'a>>)) {
160160
}
161161

162162
match ty.node {
163-
ty_path(p, p_id) => {
163+
ty_path(p, _bounds, p_id) => {
164164
match cx.tcx.def_map.find(&p_id) {
165165
// Kind of a hack to check this here, but I'm not sure what else
166166
// to do

src/librustc/middle/trans/reflect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ impl Reflector {
335335
}
336336

337337
// Miscallaneous extra types
338-
ty::ty_trait(_, _, _, _) => self.leaf(~"trait"),
338+
ty::ty_trait(_, _, _, _, _) => self.leaf(~"trait"),
339339
ty::ty_infer(_) => self.leaf(~"infer"),
340340
ty::ty_err => self.leaf(~"err"),
341341
ty::ty_param(ref p) => {

src/librustc/middle/trans/type_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn sizing_type_of(cx: &mut CrateContext, t: ty::t) -> Type {
140140

141141
ty::ty_bare_fn(*) => Type::i8p(),
142142
ty::ty_closure(*) => Type::struct_([Type::i8p(), Type::i8p()], false),
143-
ty::ty_trait(_, _, store, _) => Type::opaque_trait(cx, store),
143+
ty::ty_trait(_, _, store, _, _) => Type::opaque_trait(cx, store),
144144

145145
ty::ty_estr(ty::vstore_fixed(size)) => Type::array(&Type::i8(), size as u64),
146146
ty::ty_evec(mt, ty::vstore_fixed(size)) => {
@@ -271,7 +271,7 @@ pub fn type_of(cx: &mut CrateContext, t: ty::t) -> Type {
271271
let ty = type_of_fn_from_ty(cx, t);
272272
Type::func_pair(cx, &ty)
273273
}
274-
ty::ty_trait(_, _, store, _) => Type::opaque_trait(cx, store),
274+
ty::ty_trait(_, _, store, _, _) => Type::opaque_trait(cx, store),
275275
ty::ty_type => cx.tydesc_type.ptr_to(),
276276
ty::ty_tup(*) => {
277277
let repr = adt::represent_type(cx, t);

src/librustc/middle/trans/type_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn type_needs_inner(cx: Context,
208208
ty::ty_bare_fn(*) |
209209
ty::ty_ptr(_) |
210210
ty::ty_rptr(_, _) |
211-
ty::ty_trait(_, _, _, _) => false,
211+
ty::ty_trait(*) => false,
212212

213213
ty::ty_enum(did, ref substs) => {
214214
if list::find(enums_seen, |id| *id == did).is_none() {

src/librustc/middle/ty.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ impl to_bytes::IterBytes for ClosureTy {
419419
self.sigil.iter_bytes(lsb0, f) &&
420420
self.onceness.iter_bytes(lsb0, f) &&
421421
self.region.iter_bytes(lsb0, f) &&
422-
self.sig.iter_bytes(lsb0, f)
422+
self.sig.iter_bytes(lsb0, f) &&
423+
self.bounds.iter_bytes(lsb0, f)
423424
}
424425
}
425426

@@ -600,7 +601,7 @@ pub enum sty {
600601
ty_rptr(Region, mt),
601602
ty_bare_fn(BareFnTy),
602603
ty_closure(ClosureTy),
603-
ty_trait(def_id, substs, TraitStore, ast::mutability),
604+
ty_trait(def_id, substs, TraitStore, ast::mutability, BuiltinBounds),
604605
ty_struct(def_id, substs),
605606
ty_tup(~[t]),
606607

@@ -1046,7 +1047,7 @@ fn mk_t(cx: ctxt, st: sty) -> t {
10461047
&ty_infer(_) => flags |= needs_infer as uint,
10471048
&ty_self(_) => flags |= has_self as uint,
10481049
&ty_enum(_, ref substs) | &ty_struct(_, ref substs) |
1049-
&ty_trait(_, ref substs, _, _) => {
1050+
&ty_trait(_, ref substs, _, _, _) => {
10501051
flags |= sflags(substs);
10511052
}
10521053
&ty_box(ref m) | &ty_uniq(ref m) | &ty_evec(ref m, _) |
@@ -1268,10 +1269,11 @@ pub fn mk_trait(cx: ctxt,
12681269
did: ast::def_id,
12691270
substs: substs,
12701271
store: TraitStore,
1271-
mutability: ast::mutability)
1272+
mutability: ast::mutability,
1273+
bounds: BuiltinBounds)
12721274
-> t {
12731275
// take a copy of substs so that we own the vectors inside
1274-
mk_t(cx, ty_trait(did, substs, store, mutability))
1276+
mk_t(cx, ty_trait(did, substs, store, mutability, bounds))
12751277
}
12761278

12771279
pub fn mk_struct(cx: ctxt, struct_id: ast::def_id, substs: substs) -> t {
@@ -1319,7 +1321,7 @@ pub fn maybe_walk_ty(ty: t, f: &fn(t) -> bool) {
13191321
maybe_walk_ty(tm.ty, f);
13201322
}
13211323
ty_enum(_, ref substs) | ty_struct(_, ref substs) |
1322-
ty_trait(_, ref substs, _, _) => {
1324+
ty_trait(_, ref substs, _, _, _) => {
13231325
for (*substs).tps.iter().advance |subty| { maybe_walk_ty(*subty, f); }
13241326
}
13251327
ty_tup(ref ts) => { for ts.iter().advance |tt| { maybe_walk_ty(*tt, f); } }
@@ -1380,8 +1382,8 @@ fn fold_sty(sty: &sty, fldop: &fn(t) -> t) -> sty {
13801382
ty_enum(tid, ref substs) => {
13811383
ty_enum(tid, fold_substs(substs, fldop))
13821384
}
1383-
ty_trait(did, ref substs, st, mutbl) => {
1384-
ty_trait(did, fold_substs(substs, fldop), st, mutbl)
1385+
ty_trait(did, ref substs, st, mutbl, bounds) => {
1386+
ty_trait(did, fold_substs(substs, fldop), st, mutbl, bounds)
13851387
}
13861388
ty_tup(ref ts) => {
13871389
let new_ts = ts.map(|tt| fldop(*tt));
@@ -1470,8 +1472,12 @@ pub fn fold_regions_and_ty(
14701472
ty_struct(def_id, ref substs) => {
14711473
ty::mk_struct(cx, def_id, fold_substs(substs, fldr, fldt))
14721474
}
1473-
ty_trait(def_id, ref substs, st, mutbl) => {
1474-
ty::mk_trait(cx, def_id, fold_substs(substs, fldr, fldt), st, mutbl)
1475+
ty_trait(def_id, ref substs, st, mutbl, bounds) => {
1476+
let st = match st {
1477+
RegionTraitStore(region) => RegionTraitStore(fldr(region)),
1478+
st => st,
1479+
};
1480+
ty::mk_trait(cx, def_id, fold_substs(substs, fldr, fldt), st, mutbl, bounds)
14751481
}
14761482
ty_bare_fn(ref f) => {
14771483
ty::mk_bare_fn(cx, BareFnTy {sig: fold_sig(&f.sig, fldfnt),
@@ -2054,18 +2060,18 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
20542060
TC_MANAGED + statically_sized(nonowned(tc_mt(cx, mt, cache)))
20552061
}
20562062

2057-
ty_trait(_, _, UniqTraitStore, _) => {
2063+
ty_trait(_, _, UniqTraitStore, _, _bounds) => {
20582064
TC_OWNED_CLOSURE
20592065
}
20602066

2061-
ty_trait(_, _, BoxTraitStore, mutbl) => {
2067+
ty_trait(_, _, BoxTraitStore, mutbl, _bounds) => {
20622068
match mutbl {
20632069
ast::m_mutbl => TC_MANAGED + TC_MUTABLE,
20642070
_ => TC_MANAGED
20652071
}
20662072
}
20672073

2068-
ty_trait(_, _, RegionTraitStore(r), mutbl) => {
2074+
ty_trait(_, _, RegionTraitStore(r), mutbl, _bounds) => {
20692075
borrowed_contents(r, mutbl)
20702076
}
20712077

@@ -2347,7 +2353,7 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
23472353
false // unsafe ptrs can always be NULL
23482354
}
23492355

2350-
ty_trait(_, _, _, _) => {
2356+
ty_trait(_, _, _, _, _) => {
23512357
false
23522358
}
23532359

@@ -2500,7 +2506,7 @@ pub fn type_is_pod(cx: ctxt, ty: t) -> bool {
25002506
ty_box(_) | ty_uniq(_) | ty_closure(_) |
25012507
ty_estr(vstore_uniq) | ty_estr(vstore_box) |
25022508
ty_evec(_, vstore_uniq) | ty_evec(_, vstore_box) |
2503-
ty_trait(_, _, _, _) | ty_rptr(_,_) | ty_opaque_box => result = false,
2509+
ty_trait(_, _, _, _, _) | ty_rptr(_,_) | ty_opaque_box => result = false,
25042510
// Structural types
25052511
ty_enum(did, ref substs) => {
25062512
let variants = enum_variants(cx, did);
@@ -2791,12 +2797,13 @@ impl to_bytes::IterBytes for sty {
27912797

27922798
ty_uniq(ref mt) => 19u8.iter_bytes(lsb0, f) && mt.iter_bytes(lsb0, f),
27932799

2794-
ty_trait(ref did, ref substs, ref v, ref mutbl) => {
2800+
ty_trait(ref did, ref substs, ref v, ref mutbl, bounds) => {
27952801
20u8.iter_bytes(lsb0, f) &&
27962802
did.iter_bytes(lsb0, f) &&
27972803
substs.iter_bytes(lsb0, f) &&
27982804
v.iter_bytes(lsb0, f) &&
2799-
mutbl.iter_bytes(lsb0, f)
2805+
mutbl.iter_bytes(lsb0, f) &&
2806+
bounds.iter_bytes(lsb0, f)
28002807
}
28012808

28022809
ty_opaque_closure_ptr(ref ck) => 21u8.iter_bytes(lsb0, f) && ck.iter_bytes(lsb0, f),
@@ -3440,7 +3447,7 @@ pub fn ty_sort_str(cx: ctxt, t: t) -> ~str {
34403447
ty_rptr(_, _) => ~"&-ptr",
34413448
ty_bare_fn(_) => ~"extern fn",
34423449
ty_closure(_) => ~"fn",
3443-
ty_trait(id, _, _, _) => fmt!("trait %s", item_path_str(cx, id)),
3450+
ty_trait(id, _, _, _, _) => fmt!("trait %s", item_path_str(cx, id)),
34443451
ty_struct(id, _) => fmt!("struct %s", item_path_str(cx, id)),
34453452
ty_tup(_) => ~"tuple",
34463453
ty_infer(TyVar(_)) => ~"inferred type",
@@ -3774,7 +3781,7 @@ pub fn impl_trait_ref(cx: ctxt, id: ast::def_id) -> Option<@TraitRef> {
37743781

37753782
pub fn ty_to_def_id(ty: t) -> Option<ast::def_id> {
37763783
match get(ty).sty {
3777-
ty_trait(id, _, _, _) | ty_struct(id, _) | ty_enum(id, _) => Some(id),
3784+
ty_trait(id, _, _, _, _) | ty_struct(id, _) | ty_enum(id, _) => Some(id),
37783785
_ => None
37793786
}
37803787
}
@@ -4454,5 +4461,6 @@ pub fn visitor_object_ty(tcx: ctxt) -> (@TraitRef, t) {
44544461
assert!(tcx.intrinsic_traits.contains_key(&ty_visitor_name));
44554462
let trait_ref = tcx.intrinsic_traits.get_copy(&ty_visitor_name);
44564463
(trait_ref,
4457-
mk_trait(tcx, trait_ref.def_id, copy trait_ref.substs, BoxTraitStore, ast::m_imm))
4464+
mk_trait(tcx, trait_ref.def_id, copy trait_ref.substs,
4465+
BoxTraitStore, ast::m_imm, EmptyBuiltinBounds()))
44584466
}

0 commit comments

Comments
 (0)