Skip to content

Commit 2442317

Browse files
committed
Update parser with for syntax
1 parent c18a132 commit 2442317

28 files changed

+544
-405
lines changed

src/librustc/lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,8 +1690,8 @@ impl LintPass for Stability {
16901690
for t in supertraits.iter() {
16911691
match *t {
16921692
ast::TraitTyParamBound(ref t) => {
1693-
let id = ty::trait_ref_to_def_id(cx.tcx, t);
1694-
self.lint(cx, id, t.path.span);
1693+
let id = ty::trait_ref_to_def_id(cx.tcx, &t.trait_ref);
1694+
self.lint(cx, id, t.trait_ref.path.span);
16951695
}
16961696
_ => (/* pass */)
16971697
}

src/librustc/middle/privacy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
12911291
match *ty_param_bound {
12921292
ast::TraitTyParamBound(ref trait_ref) => {
12931293
if !self.tcx.sess.features.borrow().visible_private_types &&
1294-
self.path_is_private_type(trait_ref.ref_id) {
1294+
self.path_is_private_type(trait_ref.trait_ref.ref_id) {
12951295
self.tcx.sess.span_err(span,
12961296
"private type in exported type \
12971297
parameter bound");
@@ -1432,7 +1432,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
14321432
//
14331433
// Those in 2. are warned via walk_generics and this
14341434
// call here.
1435-
visit::walk_trait_ref_helper(self, tr)
1435+
self.visit_trait_ref(tr)
14361436
}
14371437
}
14381438
} else if trait_ref.is_none() && self_is_public_path {

src/librustc/middle/resolve.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ use syntax::ast::{ItemTrait, ItemTy, LOCAL_CRATE, Local, ItemConst};
3333
use syntax::ast::{MethodImplItem, Mod, Name, NamedField, NodeId};
3434
use syntax::ast::{Pat, PatEnum, PatIdent, PatLit};
3535
use syntax::ast::{PatRange, PatStruct, Path, PathListIdent, PathListMod};
36-
use syntax::ast::{PrimTy, Public, SelfExplicit, SelfStatic};
36+
use syntax::ast::{PolyTraitRef, PrimTy, Public, SelfExplicit, SelfStatic};
3737
use syntax::ast::{RegionTyParamBound, StmtDecl, StructField};
3838
use syntax::ast::{StructVariantKind, TraitRef, TraitTyParamBound};
3939
use syntax::ast::{TupleVariantKind, Ty, TyBool, TyChar, TyClosure, TyF32};
4040
use syntax::ast::{TyF64, TyFloat, TyI, TyI8, TyI16, TyI32, TyI64, TyInt};
41-
use syntax::ast::{TyParam, TyParamBound, TyPath, TyPtr, TyProc, TyQPath};
41+
use syntax::ast::{TyParam, TyParamBound, TyPath, TyPtr, TyPolyTraitRef, TyProc, TyQPath};
4242
use syntax::ast::{TyRptr, TyStr, TyU, TyU8, TyU16, TyU32, TyU64, TyUint};
4343
use syntax::ast::{TypeImplItem, UnnamedField};
4444
use syntax::ast::{Variant, ViewItem, ViewItemExternCrate};
@@ -607,6 +607,7 @@ enum TraitReferenceType {
607607
TraitImplementation, // impl SomeTrait for T { ... }
608608
TraitDerivation, // trait T : SomeTrait { ... }
609609
TraitBoundingTypeParameter, // fn f<T:SomeTrait>() { ... }
610+
TraitObject, // Box<for<'a> SomeTrait>
610611
}
611612

612613
impl NameBindings {
@@ -4244,11 +4245,11 @@ impl<'a> Resolver<'a> {
42444245
this.resolve_type_parameter_bounds(item.id, bounds,
42454246
TraitDerivation);
42464247

4247-
match unbound {
4248-
&Some(ast::TraitTyParamBound(ref tpb)) => {
4248+
match *unbound {
4249+
Some(ref tpb) => {
42494250
this.resolve_trait_reference(item.id, tpb, TraitDerivation);
42504251
}
4251-
_ => {}
4252+
None => {}
42524253
}
42534254

42544255
for trait_item in (*trait_items).iter() {
@@ -4495,7 +4496,7 @@ impl<'a> Resolver<'a> {
44954496
}
44964497
match &type_parameter.unbound {
44974498
&Some(ref unbound) =>
4498-
self.resolve_type_parameter_bound(
4499+
self.resolve_trait_reference(
44994500
type_parameter.id, unbound, TraitBoundingTypeParameter),
45004501
&None => {}
45014502
}
@@ -4521,12 +4522,19 @@ impl<'a> Resolver<'a> {
45214522
reference_type: TraitReferenceType) {
45224523
match *type_parameter_bound {
45234524
TraitTyParamBound(ref tref) => {
4524-
self.resolve_trait_reference(id, tref, reference_type)
4525+
self.resolve_poly_trait_reference(id, tref, reference_type)
45254526
}
45264527
RegionTyParamBound(..) => {}
45274528
}
45284529
}
45294530

4531+
fn resolve_poly_trait_reference(&mut self,
4532+
id: NodeId,
4533+
poly_trait_reference: &PolyTraitRef,
4534+
reference_type: TraitReferenceType) {
4535+
self.resolve_trait_reference(id, &poly_trait_reference.trait_ref, reference_type)
4536+
}
4537+
45304538
fn resolve_trait_reference(&mut self,
45314539
id: NodeId,
45324540
trait_reference: &TraitRef,
@@ -4538,6 +4546,7 @@ impl<'a> Resolver<'a> {
45384546
TraitBoundingTypeParameter => "bound type parameter with",
45394547
TraitImplementation => "implement",
45404548
TraitDerivation => "derive",
4549+
TraitObject => "reference",
45414550
};
45424551

45434552
let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str);
@@ -5044,6 +5053,13 @@ impl<'a> Resolver<'a> {
50445053
visit::walk_ty(self, ty);
50455054
}
50465055

5056+
TyPolyTraitRef(ref poly_trait_ref) => {
5057+
self.resolve_poly_trait_reference(
5058+
ty.id,
5059+
&**poly_trait_ref,
5060+
TraitObject);
5061+
visit::walk_ty(self, ty);
5062+
}
50475063
_ => {
50485064
// Just resolve embedded types.
50495065
visit::walk_ty(self, ty);

src/librustc/middle/resolve_lifetime.rs

Lines changed: 78 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use driver::session::Session;
2121
use middle::subst;
22+
use std::fmt;
2223
use syntax::ast;
2324
use syntax::codemap::Span;
2425
use syntax::owned_slice::OwnedSlice;
@@ -46,18 +47,12 @@ pub enum DefRegion {
4647
// that it corresponds to
4748
pub type NamedRegionMap = NodeMap<DefRegion>;
4849

49-
// Returns an instance of some type that implements std::fmt::Show
50-
fn lifetime_show(lt_name: &ast::Name) -> token::InternedString {
51-
token::get_name(*lt_name)
52-
}
53-
5450
struct LifetimeContext<'a> {
5551
sess: &'a Session,
5652
named_region_map: &'a mut NamedRegionMap,
5753
scope: Scope<'a>
5854
}
5955

60-
#[deriving(Show)]
6156
enum ScopeChain<'a> {
6257
/// EarlyScope(i, ['a, 'b, ...], s) extends s with early-bound
6358
/// lifetimes, assigning indexes 'a => i, 'b => i+1, ... etc.
@@ -88,50 +83,42 @@ pub fn krate(sess: &Session, krate: &ast::Crate) -> NamedRegionMap {
8883

8984
impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
9085
fn visit_item(&mut self, item: &ast::Item) {
91-
let lifetimes = match item.node {
92-
ast::ItemFn(..) | // fn lifetimes get added in visit_fn below
86+
match item.node {
87+
ast::ItemFn(..) => {
88+
// Fn lifetimes get added in visit_fn below:
89+
self.with(RootScope, |this| visit::walk_item(this, item));
90+
}
9391
ast::ItemMod(..) |
9492
ast::ItemMac(..) |
9593
ast::ItemForeignMod(..) |
96-
ast::ItemStatic(..) | ast::ItemConst(..) => {
97-
self.with(|_, f| f(RootScope), |v| visit::walk_item(v, item));
98-
return;
94+
ast::ItemStatic(..) |
95+
ast::ItemConst(..) => {
96+
// These sorts of items have no lifetime parameters at all.
97+
self.with(RootScope, |this| visit::walk_item(this, item));
9998
}
10099
ast::ItemTy(_, ref generics) |
101100
ast::ItemEnum(_, ref generics) |
102101
ast::ItemStruct(_, ref generics) |
103-
ast::ItemTrait(ref generics, _, _, _) => {
104-
self.with(|scope, f| {
105-
f(EarlyScope(subst::TypeSpace,
106-
&generics.lifetimes,
107-
scope))
108-
}, |v| v.check_lifetime_defs(&generics.lifetimes));
109-
&generics.lifetimes
110-
}
102+
ast::ItemTrait(ref generics, _, _, _) |
111103
ast::ItemImpl(ref generics, _, _, _) => {
112-
self.with(|scope, f| {
113-
f(EarlyScope(subst::TypeSpace,
114-
&generics.lifetimes,
115-
scope))
116-
}, |v| v.check_lifetime_defs(&generics.lifetimes));
117-
&generics.lifetimes
104+
// These kinds of items have only early bound lifetime parameters.
105+
let lifetimes = &generics.lifetimes;
106+
self.with(EarlyScope(subst::TypeSpace, lifetimes, &ROOT_SCOPE), |this| {
107+
this.check_lifetime_defs(lifetimes);
108+
visit::walk_item(this, item);
109+
});
118110
}
119-
};
120-
121-
self.with(|_, f| f(EarlyScope(subst::TypeSpace, lifetimes, &ROOT_SCOPE)), |v| {
122-
debug!("entering scope {}", v.scope);
123-
v.check_lifetime_defs(lifetimes);
124-
visit::walk_item(v, item);
125-
debug!("exiting scope {}", v.scope);
126-
});
111+
}
127112
}
128113

129114
fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
130115
b: &'v ast::Block, s: Span, n: ast::NodeId) {
131116
match fk {
132117
visit::FkItemFn(_, generics, _, _) |
133118
visit::FkMethod(_, generics, _) => {
134-
self.visit_fn_decl(n, generics, |v| visit::walk_fn(v, fk, fd, b, s))
119+
self.visit_early_late(
120+
subst::FnSpace, n, generics,
121+
|this| visit::walk_fn(this, fk, fd, b, s))
135122
}
136123
visit::FkFnBlock(..) => {
137124
visit::walk_fn(self, fk, fd, b, s)
@@ -146,22 +133,20 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
146133
_ => return visit::walk_ty(self, ty)
147134
};
148135

149-
self.with(|scope, f| f(LateScope(ty.id, lifetimes, scope)), |v| {
150-
v.check_lifetime_defs(lifetimes);
151-
debug!("pushing fn scope id={} due to type", ty.id);
152-
visit::walk_ty(v, ty);
153-
debug!("popping fn scope id={} due to type", ty.id);
136+
self.with(LateScope(ty.id, lifetimes, self.scope), |this| {
137+
this.check_lifetime_defs(lifetimes);
138+
visit::walk_ty(this, ty);
154139
});
155140
}
156141

157142
fn visit_ty_method(&mut self, m: &ast::TypeMethod) {
158-
self.visit_fn_decl(m.id, &m.generics, |v| visit::walk_ty_method(v, m))
143+
self.visit_early_late(
144+
subst::FnSpace, m.id, &m.generics,
145+
|this| visit::walk_ty_method(this, m))
159146
}
160147

161148
fn visit_block(&mut self, b: &ast::Block) {
162-
debug!("pushing block scope {}", b.id);
163-
self.with(|scope, f| f(BlockScope(b.id, scope)), |v| visit::walk_block(v, b));
164-
debug!("popping block scope {}", b.id);
149+
self.with(BlockScope(b.id, self.scope), |this| visit::walk_block(this, b));
165150
}
166151

167152
fn visit_lifetime_ref(&mut self, lifetime_ref: &ast::Lifetime) {
@@ -188,21 +173,24 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
188173
}
189174

190175
impl<'a> LifetimeContext<'a> {
191-
fn with(&mut self, wrap_scope: |Scope, |ScopeChain||, f: |&mut LifetimeContext|) {
192-
let LifetimeContext { sess, ref mut named_region_map, scope} = *self;
193-
wrap_scope(scope, |scope1| f(&mut LifetimeContext {
176+
fn with(&mut self, wrap_scope: ScopeChain, f: |&mut LifetimeContext|) {
177+
let LifetimeContext {sess, ref mut named_region_map, ..} = *self;
178+
let mut this = LifetimeContext {
194179
sess: sess,
195180
named_region_map: *named_region_map,
196-
scope: &scope1
197-
}))
181+
scope: &wrap_scope
182+
};
183+
debug!("entering scope {}", this.scope);
184+
f(&mut this);
185+
debug!("exiting scope {}", this.scope);
198186
}
199187

200188
fn visit_ty_param_bounds(&mut self,
201189
bounds: &OwnedSlice<ast::TyParamBound>) {
202190
for bound in bounds.iter() {
203191
match *bound {
204192
ast::TraitTyParamBound(ref trait_ref) => {
205-
self.visit_trait_ref(trait_ref);
193+
self.visit_poly_trait_ref(trait_ref);
206194
}
207195
ast::RegionTyParamBound(ref lifetime) => {
208196
self.visit_lifetime_ref(lifetime);
@@ -211,23 +199,27 @@ impl<'a> LifetimeContext<'a> {
211199
}
212200
}
213201

214-
fn visit_trait_ref(&mut self, trait_ref: &ast::TraitRef) {
215-
self.with(|scope, f| {
216-
f(LateScope(trait_ref.ref_id, &trait_ref.lifetimes, scope))
217-
}, |v| {
218-
v.check_lifetime_defs(&trait_ref.lifetimes);
219-
for lifetime in trait_ref.lifetimes.iter() {
220-
v.visit_lifetime_decl(lifetime);
202+
fn visit_poly_trait_ref(&mut self, trait_ref: &ast::PolyTraitRef) {
203+
let ref_id = trait_ref.trait_ref.ref_id;
204+
self.with(LateScope(ref_id, &trait_ref.bound_lifetimes, self.scope), |this| {
205+
this.check_lifetime_defs(&trait_ref.bound_lifetimes);
206+
for lifetime in trait_ref.bound_lifetimes.iter() {
207+
this.visit_lifetime_decl(lifetime);
221208
}
222-
v.visit_path(&trait_ref.path, trait_ref.ref_id);
209+
this.visit_trait_ref(&trait_ref.trait_ref)
223210
})
224211
}
225212

213+
fn visit_trait_ref(&mut self, trait_ref: &ast::TraitRef) {
214+
self.visit_path(&trait_ref.path, trait_ref.ref_id);
215+
}
216+
226217
/// Visits self by adding a scope and handling recursive walk over the contents with `walk`.
227-
fn visit_fn_decl(&mut self,
228-
n: ast::NodeId,
229-
generics: &ast::Generics,
230-
walk: |&mut LifetimeContext|) {
218+
fn visit_early_late(&mut self,
219+
early_space: subst::ParamSpace,
220+
binder_id: ast::NodeId,
221+
generics: &ast::Generics,
222+
walk: |&mut LifetimeContext|) {
231223
/*!
232224
* Handles visiting fns and methods. These are a bit
233225
* complicated because we must distinguish early- vs late-bound
@@ -248,33 +240,25 @@ impl<'a> LifetimeContext<'a> {
248240
* numbered sequentially, starting from the lowest index that
249241
* is already in scope (for a fn item, that will be 0, but for
250242
* a method it might not be). Late bound lifetimes are
251-
* resolved by name and associated with a binder id (`n`), so
243+
* resolved by name and associated with a binder id (`binder_id`), so
252244
* the ordering is not important there.
253245
*/
254246

255247
let referenced_idents = early_bound_lifetime_names(generics);
256-
debug!("pushing fn scope id={} due to fn item/method\
257-
referenced_idents={}",
258-
n,
259-
referenced_idents.iter().map(lifetime_show).collect::<Vec<token::InternedString>>());
260-
let lifetimes = &generics.lifetimes;
261-
if referenced_idents.is_empty() {
262-
self.with(|scope, f| f(LateScope(n, lifetimes, scope)), |v| {
263-
v.check_lifetime_defs(lifetimes);
264-
walk(v);
265-
});
266-
} else {
267-
let (early, late) = lifetimes.clone().partition(
268-
|l| referenced_idents.iter().any(|&i| i == l.lifetime.name));
269-
270-
self.with(|scope, f| f(EarlyScope(subst::FnSpace, &early, scope)), |v| {
271-
v.with(|scope1, f| f(LateScope(n, &late, scope1)), |v| {
272-
v.check_lifetime_defs(lifetimes);
273-
walk(v);
274-
});
248+
249+
debug!("visit_early_late: binder_id={} referenced_idents={}",
250+
binder_id,
251+
referenced_idents);
252+
253+
let (early, late) = generics.lifetimes.clone().partition(
254+
|l| referenced_idents.iter().any(|&i| i == l.lifetime.name));
255+
256+
self.with(EarlyScope(early_space, &early, self.scope), |this| {
257+
this.with(LateScope(binder_id, &late, this.scope), |this| {
258+
this.check_lifetime_defs(&generics.lifetimes);
259+
walk(this);
275260
});
276-
}
277-
debug!("popping fn scope id={} due to fn item/method", n);
261+
});
278262
}
279263

280264
fn resolve_lifetime_ref(&mut self, lifetime_ref: &ast::Lifetime) {
@@ -525,3 +509,14 @@ fn early_bound_lifetime_names(generics: &ast::Generics) -> Vec<ast::Name> {
525509
}
526510
}
527511
}
512+
513+
impl<'a> fmt::Show for ScopeChain<'a> {
514+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
515+
match *self {
516+
EarlyScope(space, defs, _) => write!(fmt, "EarlyScope({}, {})", space, defs),
517+
LateScope(id, defs, _) => write!(fmt, "LateScope({}, {})", id, defs),
518+
BlockScope(id, _) => write!(fmt, "BlockScope({})", id),
519+
RootScope => write!(fmt, "RootScope"),
520+
}
521+
}
522+
}

src/librustc/middle/save/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
710710
}
711711
};
712712

713+
let trait_ref = &trait_ref.trait_ref;
713714
match self.lookup_type_ref(trait_ref.ref_id) {
714715
Some(id) => {
715716
let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
@@ -1068,7 +1069,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
10681069
for bound in param.bounds.iter() {
10691070
match *bound {
10701071
ast::TraitTyParamBound(ref trait_ref) => {
1071-
self.process_trait_ref(trait_ref, None);
1072+
self.process_trait_ref(&trait_ref.trait_ref, None);
10721073
}
10731074
_ => {}
10741075
}

0 commit comments

Comments
 (0)