Skip to content

Commit 319d778

Browse files
committed
Restructure AST so that the associated type definition carries
bounds like any other "type parameter".
1 parent 01b81c0 commit 319d778

File tree

13 files changed

+105
-98
lines changed

13 files changed

+105
-98
lines changed

src/librustc/middle/astencode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn encode_inlined_item(ecx: &e::EncodeContext,
8181
e::IIForeignRef(i) => i.id,
8282
e::IITraitItemRef(_, &ast::ProvidedMethod(ref m)) => m.id,
8383
e::IITraitItemRef(_, &ast::RequiredMethod(ref m)) => m.id,
84-
e::IITraitItemRef(_, &ast::TypeTraitItem(ref ti)) => ti.id,
84+
e::IITraitItemRef(_, &ast::TypeTraitItem(ref ti)) => ti.ty_param.id,
8585
e::IIImplItemRef(_, &ast::MethodImplItem(ref m)) => m.id,
8686
e::IIImplItemRef(_, &ast::TypeImplItem(ref ti)) => ti.id,
8787
};
@@ -156,7 +156,7 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
156156
match *ti {
157157
ast::ProvidedMethod(ref m) => m.pe_ident(),
158158
ast::RequiredMethod(ref ty_m) => ty_m.ident,
159-
ast::TypeTraitItem(ref ti) => ti.ident,
159+
ast::TypeTraitItem(ref ti) => ti.ty_param.ident,
160160
}
161161
},
162162
ast::IIImplItem(_, ref m) => {

src/librustc/middle/privacy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EmbargoVisitor<'a, 'tcx> {
296296
self.exported_items.insert(m.id);
297297
}
298298
ast::TypeTraitItem(ref t) => {
299-
debug!("typedef {}", t.id);
300-
self.exported_items.insert(t.id);
299+
debug!("typedef {}", t.ty_param.id);
300+
self.exported_items.insert(t.ty_param.id);
301301
}
302302
}
303303
}

src/librustc/middle/resolve.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,19 +1559,19 @@ impl<'a> Resolver<'a> {
15591559
}
15601560
ast::TypeTraitItem(ref associated_type) => {
15611561
let def = DefAssociatedTy(local_def(
1562-
associated_type.id));
1562+
associated_type.ty_param.id));
15631563

15641564
let name_bindings =
1565-
self.add_child(associated_type.ident.name,
1565+
self.add_child(associated_type.ty_param.ident.name,
15661566
module_parent.clone(),
15671567
ForbidDuplicateTypesAndValues,
1568-
associated_type.span);
1568+
associated_type.ty_param.span);
15691569
// NB: not IMPORTABLE
15701570
name_bindings.define_type(def,
1571-
associated_type.span,
1571+
associated_type.ty_param.span,
15721572
PUBLIC);
15731573

1574-
(associated_type.ident.name, TypeTraitItemKind)
1574+
(associated_type.ty_param.ident.name, TypeTraitItemKind)
15751575
}
15761576
};
15771577

@@ -4218,7 +4218,7 @@ impl<'a> Resolver<'a> {
42184218
impl_items.as_slice());
42194219
}
42204220

4221-
ItemTrait(ref generics, ref unbound, ref bounds, ref methods) => {
4221+
ItemTrait(ref generics, ref unbound, ref bounds, ref trait_items) => {
42224222
// Create a new rib for the self type.
42234223
let mut self_type_rib = Rib::new(ItemRibKind);
42244224

@@ -4246,13 +4246,13 @@ impl<'a> Resolver<'a> {
42464246
_ => {}
42474247
}
42484248

4249-
for method in (*methods).iter() {
4250-
// Create a new rib for the method-specific type
4249+
for trait_item in (*trait_items).iter() {
4250+
// Create a new rib for the trait_item-specific type
42514251
// parameters.
42524252
//
42534253
// FIXME #4951: Do we need a node ID here?
42544254

4255-
match *method {
4255+
match *trait_item {
42564256
ast::RequiredMethod(ref ty_m) => {
42574257
this.with_type_parameter_rib
42584258
(HasTypeParameters(&ty_m.generics,
@@ -4287,8 +4287,9 @@ impl<'a> Resolver<'a> {
42874287
ProvidedMethod(m.id)),
42884288
&**m)
42894289
}
4290-
ast::TypeTraitItem(_) => {
4291-
visit::walk_trait_item(this, method);
4290+
ast::TypeTraitItem(ref data) => {
4291+
this.resolve_type_parameter(&data.ty_param);
4292+
visit::walk_trait_item(this, trait_item);
42924293
}
42934294
}
42944295
}
@@ -4477,20 +4478,25 @@ impl<'a> Resolver<'a> {
44774478
fn resolve_type_parameters(&mut self,
44784479
type_parameters: &OwnedSlice<TyParam>) {
44794480
for type_parameter in type_parameters.iter() {
4480-
for bound in type_parameter.bounds.iter() {
4481-
self.resolve_type_parameter_bound(type_parameter.id, bound,
4482-
TraitBoundingTypeParameter);
4483-
}
4484-
match &type_parameter.unbound {
4485-
&Some(ref unbound) =>
4486-
self.resolve_type_parameter_bound(
4487-
type_parameter.id, unbound, TraitBoundingTypeParameter),
4488-
&None => {}
4489-
}
4490-
match type_parameter.default {
4491-
Some(ref ty) => self.resolve_type(&**ty),
4492-
None => {}
4493-
}
4481+
self.resolve_type_parameter(type_parameter);
4482+
}
4483+
}
4484+
4485+
fn resolve_type_parameter(&mut self,
4486+
type_parameter: &TyParam) {
4487+
for bound in type_parameter.bounds.iter() {
4488+
self.resolve_type_parameter_bound(type_parameter.id, bound,
4489+
TraitBoundingTypeParameter);
4490+
}
4491+
match &type_parameter.unbound {
4492+
&Some(ref unbound) =>
4493+
self.resolve_type_parameter_bound(
4494+
type_parameter.id, unbound, TraitBoundingTypeParameter),
4495+
&None => {}
4496+
}
4497+
match type_parameter.default {
4498+
Some(ref ty) => self.resolve_type(&**ty),
4499+
None => {}
44944500
}
44954501
}
44964502

@@ -4577,14 +4583,14 @@ impl<'a> Resolver<'a> {
45774583
self.resolve_error(trait_reference.path.span,
45784584
format!("`{}` is not a trait",
45794585
self.path_names_to_string(
4580-
&trait_reference.path)));
4586+
&trait_reference.path)));
45814587

45824588
// If it's a typedef, give a note
45834589
match def {
45844590
DefTy(..) => {
45854591
self.session.span_note(
4586-
trait_reference.path.span,
4587-
format!("`type` aliases cannot \
4592+
trait_reference.path.span,
4593+
format!("`type` aliases cannot \
45884594
be used for traits")
45894595
.as_slice());
45904596
}

src/librustc/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'v> Visitor<'v> for Annotator {
8686
}
8787
}
8888

89-
TypeTraitItem(ref typedef) => (typedef.id, &typedef.attrs),
89+
TypeTraitItem(ref typedef) => (typedef.ty_param.id, &typedef.attrs),
9090
};
9191
self.annotate(id, attrs, |v| visit::walk_trait_item(v, t));
9292
}

src/librustc/middle/typeck/collect.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn collect_trait_methods(ccx: &CrateCtxt,
298298
&*m.pe_fn_decl())
299299
}
300300
ast::TypeTraitItem(ref at) => {
301-
tcx.sess.span_bug(at.span,
301+
tcx.sess.span_bug(at.ty_param.span,
302302
"there shouldn't \
303303
be a type trait \
304304
item here")
@@ -315,9 +315,9 @@ fn collect_trait_methods(ccx: &CrateCtxt,
315315
ast::TypeTraitItem(ref ast_associated_type) => {
316316
let trait_did = local_def(trait_id);
317317
let associated_type = ty::AssociatedType {
318-
name: ast_associated_type.ident.name,
318+
name: ast_associated_type.ty_param.ident.name,
319319
vis: ast::Public,
320-
def_id: local_def(ast_associated_type.id),
320+
def_id: local_def(ast_associated_type.ty_param.id),
321321
container: TraitContainer(trait_did),
322322
};
323323

@@ -345,7 +345,7 @@ fn collect_trait_methods(ccx: &CrateCtxt,
345345
method.id))
346346
}
347347
ast::TypeTraitItem(ref typedef) => {
348-
ty::TypeTraitItemId(local_def(typedef.id))
348+
ty::TypeTraitItemId(local_def(typedef.ty_param.id))
349349
}
350350
}
351351
}).collect());
@@ -463,12 +463,12 @@ fn convert_associated_type(ccx: &CrateCtxt,
463463
.get_slice(subst::TypeSpace)
464464
.iter()
465465
.find(|def| {
466-
def.def_id == local_def(associated_type.id)
466+
def.def_id == local_def(associated_type.ty_param.id)
467467
});
468468
let type_parameter_def = match type_parameter_def {
469469
Some(type_parameter_def) => type_parameter_def,
470470
None => {
471-
ccx.tcx().sess.span_bug(associated_type.span,
471+
ccx.tcx().sess.span_bug(associated_type.ty_param.span,
472472
"`convert_associated_type()` didn't find \
473473
a type parameter ID corresponding to \
474474
this type")
@@ -477,18 +477,18 @@ fn convert_associated_type(ccx: &CrateCtxt,
477477
let param_type = ty::mk_param(ccx.tcx,
478478
subst::TypeSpace,
479479
type_parameter_def.index,
480-
local_def(associated_type.id));
481-
ccx.tcx.tcache.borrow_mut().insert(local_def(associated_type.id),
480+
local_def(associated_type.ty_param.id));
481+
ccx.tcx.tcache.borrow_mut().insert(local_def(associated_type.ty_param.id),
482482
Polytype {
483483
generics: ty::Generics::empty(),
484484
ty: param_type,
485485
});
486-
write_ty_to_tcx(ccx.tcx, associated_type.id, param_type);
486+
write_ty_to_tcx(ccx.tcx, associated_type.ty_param.id, param_type);
487487

488488
let associated_type = Rc::new(ty::AssociatedType {
489-
name: associated_type.ident.name,
489+
name: associated_type.ty_param.ident.name,
490490
vis: ast::Public,
491-
def_id: local_def(associated_type.id),
491+
def_id: local_def(associated_type.ty_param.id),
492492
container: TraitContainer(trait_def.trait_ref.def_id),
493493
});
494494
ccx.tcx
@@ -978,7 +978,7 @@ impl<'a,'tcx> AstConv<'tcx> for TraitMethodCtxt<'a,'tcx> {
978978
match *item {
979979
ast::RequiredMethod(_) | ast::ProvidedMethod(_) => {}
980980
ast::TypeTraitItem(ref item) => {
981-
if local_def(item.id) == associated_type_id {
981+
if local_def(item.ty_param.id) == associated_type_id {
982982
return ty::mk_param(self.tcx(),
983983
subst::TypeSpace,
984984
index,
@@ -1480,7 +1480,7 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
14801480
types.push(ty::mk_param(ccx.tcx,
14811481
subst::TypeSpace,
14821482
index,
1483-
local_def(trait_item.id)))
1483+
local_def(trait_item.ty_param.id)))
14841484
}
14851485
ast::RequiredMethod(_) | ast::ProvidedMethod(_) => {}
14861486
}
@@ -1630,11 +1630,11 @@ fn ty_of_trait_item(ccx: &CrateCtxt, trait_item: &ast::TraitItem)
16301630
"ty_of_trait_item() on provided method")
16311631
}
16321632
ast::TypeTraitItem(ref associated_type) => {
1633-
let parent = ccx.tcx.map.get_parent(associated_type.id);
1633+
let parent = ccx.tcx.map.get_parent(associated_type.ty_param.id);
16341634
let trait_def = match ccx.tcx.map.get(parent) {
16351635
ast_map::NodeItem(item) => trait_def_of_item(ccx, &*item),
16361636
_ => {
1637-
ccx.tcx.sess.span_bug(associated_type.span,
1637+
ccx.tcx.sess.span_bug(associated_type.ty_param.span,
16381638
"associated type's parent wasn't \
16391639
an item?!")
16401640
}
@@ -1680,8 +1680,8 @@ fn ty_generics_for_trait(ccx: &CrateCtxt,
16801680
let def = ty::TypeParameterDef {
16811681
space: subst::TypeSpace,
16821682
index: generics.types.len(subst::TypeSpace),
1683-
name: associated_type.ident.name,
1684-
def_id: local_def(associated_type.id),
1683+
name: associated_type.ty_param.ident.name,
1684+
def_id: local_def(associated_type.ty_param.id),
16851685
bounds: ty::ParamBounds {
16861686
builtin_bounds: ty::empty_builtin_bounds(),
16871687
trait_bounds: Vec::new(),
@@ -1690,7 +1690,7 @@ fn ty_generics_for_trait(ccx: &CrateCtxt,
16901690
associated_with: Some(local_def(trait_id)),
16911691
default: None,
16921692
};
1693-
ccx.tcx.ty_param_defs.borrow_mut().insert(associated_type.id,
1693+
ccx.tcx.ty_param_defs.borrow_mut().insert(associated_type.ty_param.id,
16941694
def.clone());
16951695
generics.types.push(subst::TypeSpace, def);
16961696
}
@@ -1810,9 +1810,9 @@ fn ensure_associated_types<'tcx,AC>(this: &AC, trait_id: ast::DefId)
18101810
ast::ProvidedMethod(_) => {}
18111811
ast::TypeTraitItem(ref associated_type) => {
18121812
let info = ty::AssociatedTypeInfo {
1813-
def_id: local_def(associated_type.id),
1813+
def_id: local_def(associated_type.ty_param.id),
18141814
index: index,
1815-
name: associated_type.ident.name,
1815+
name: associated_type.ty_param.ident.name,
18161816
};
18171817
result.push(info);
18181818
index += 1;

src/libsyntax/ast.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,8 @@ pub enum ImplItem {
861861

862862
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
863863
pub struct AssociatedType {
864-
pub id: NodeId,
865-
pub span: Span,
866-
pub ident: Ident,
867864
pub attrs: Vec<Attribute>,
865+
pub ty_param: TyParam,
868866
}
869867

870868
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]

src/libsyntax/ast_map/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,9 @@ impl<'ast> Map<'ast> {
405405
MethMac(_) => panic!("no path elem for {}", node),
406406
}
407407
}
408-
TypeTraitItem(ref m) => PathName(m.ident.name),
408+
TypeTraitItem(ref m) => {
409+
PathName(m.ty_param.ident.name)
410+
}
409411
},
410412
NodeVariant(v) => PathName(v.node.name.name),
411413
_ => panic!("no path elem for {}", node)
@@ -510,7 +512,7 @@ impl<'ast> Map<'ast> {
510512
match *trait_method {
511513
RequiredMethod(ref type_method) => type_method.span,
512514
ProvidedMethod(ref method) => method.span,
513-
TypeTraitItem(ref typedef) => typedef.span,
515+
TypeTraitItem(ref typedef) => typedef.ty_param.span,
514516
}
515517
}
516518
Some(NodeImplItem(ref impl_item)) => {
@@ -650,7 +652,7 @@ impl Named for TraitItem {
650652
match *self {
651653
RequiredMethod(ref tm) => tm.ident.name,
652654
ProvidedMethod(ref m) => m.name(),
653-
TypeTraitItem(ref at) => at.ident.name,
655+
TypeTraitItem(ref at) => at.ty_param.ident.name,
654656
}
655657
}
656658
}
@@ -783,7 +785,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
783785
self.insert(m.id, NodeTraitItem(tm));
784786
}
785787
TypeTraitItem(ref typ) => {
786-
self.insert(typ.id, NodeTraitItem(tm));
788+
self.insert(typ.ty_param.id, NodeTraitItem(tm));
787789
}
788790
}
789791
}
@@ -976,7 +978,7 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
976978
let trait_item_id = match *trait_item {
977979
ProvidedMethod(ref m) => m.id,
978980
RequiredMethod(ref m) => m.id,
979-
TypeTraitItem(ref ty) => ty.id,
981+
TypeTraitItem(ref ty) => ty.ty_param.id,
980982
};
981983

982984
collector.insert(trait_item_id, NodeTraitItem(trait_item));
@@ -1080,7 +1082,7 @@ fn node_id_to_string(map: &Map, id: NodeId) -> String {
10801082
}
10811083
TypeTraitItem(ref t) => {
10821084
format!("type item {} in {} (id={})",
1083-
token::get_ident(t.ident),
1085+
token::get_ident(t.ty_param.ident),
10841086
map.path_to_string(id),
10851087
id)
10861088
}

src/libsyntax/ast_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
525525
match *tm {
526526
ast::RequiredMethod(ref m) => self.operation.visit_id(m.id),
527527
ast::ProvidedMethod(ref m) => self.operation.visit_id(m.id),
528-
ast::TypeTraitItem(ref typ) => self.operation.visit_id(typ.id),
528+
ast::TypeTraitItem(ref typ) => self.operation.visit_id(typ.ty_param.id),
529529
}
530530
visit::walk_trait_item(self, tm);
531531
}

src/libsyntax/feature_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'v> Visitor<'v> for Context<'a> {
260260
ast::RequiredMethod(_) | ast::ProvidedMethod(_) => {}
261261
ast::TypeTraitItem(ref ti) => {
262262
self.gate_feature("associated_types",
263-
ti.span,
263+
ti.ty_param.span,
264264
"associated types are experimental")
265265
}
266266
}

0 commit comments

Comments
 (0)