Skip to content

Commit 004df41

Browse files
committed
syntax: don't use TraitRef in QPath.
1 parent a817c69 commit 004df41

File tree

23 files changed

+143
-146
lines changed

23 files changed

+143
-146
lines changed

src/librustc/lint/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
717717
visit::walk_path(self, p);
718718
}
719719

720+
fn visit_qpath(&mut self, p: &ast::QPath, id: ast::NodeId) {
721+
run_lints!(self, check_qpath, p, id);
722+
visit::walk_qpath(self, p);
723+
}
724+
720725
fn visit_attribute(&mut self, attr: &ast::Attribute) {
721726
run_lints!(self, check_attribute, attr);
722727
}

src/librustc/lint/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub trait LintPass {
157157
fn check_explicit_self(&mut self, _: &Context, _: &ast::ExplicitSelf) { }
158158
fn check_mac(&mut self, _: &Context, _: &ast::Mac) { }
159159
fn check_path(&mut self, _: &Context, _: &ast::Path, _: ast::NodeId) { }
160+
fn check_qpath(&mut self, _: &Context, _: &ast::QPath, _: ast::NodeId) { }
160161
fn check_attribute(&mut self, _: &Context, _: &ast::Attribute) { }
161162

162163
/// Called when entering a syntax node that can have lint attributes such

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,9 +1241,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
12411241
}
12421242
rbml_w.end_tag();
12431243
}
1244-
if let Some(ref ast_trait_ref) = *opt_trait {
1245-
let trait_ref = ty::node_id_to_trait_ref(
1246-
tcx, ast_trait_ref.ref_id);
1244+
if opt_trait.is_some() {
1245+
let trait_ref = ty::impl_id_to_trait_ref(tcx, item.id);
12471246
encode_trait_ref(rbml_w, ecx, &*trait_ref, tag_item_trait_ref);
12481247
}
12491248
encode_path(rbml_w, path.clone());

src/librustc/middle/dead.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
306306
visit::walk_path(self, path);
307307
}
308308

309+
fn visit_qpath(&mut self, qpath: &ast::QPath, id: ast::NodeId) {
310+
self.lookup_and_handle_definition(&id);
311+
visit::walk_qpath(self, qpath);
312+
}
313+
309314
fn visit_item(&mut self, _: &ast::Item) {
310315
// Do not recurse into items. These items will be added to the
311316
// worklist and recursed into manually if necessary.

src/librustc/middle/ty.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ pub struct ctxt<'tcx> {
709709

710710
pub impl_trait_cache: RefCell<DefIdMap<Option<Rc<ty::TraitRef<'tcx>>>>>,
711711

712-
pub trait_refs: RefCell<NodeMap<Rc<TraitRef<'tcx>>>>,
712+
pub impl_trait_refs: RefCell<NodeMap<Rc<TraitRef<'tcx>>>>,
713713
pub trait_defs: RefCell<DefIdMap<Rc<TraitDef<'tcx>>>>,
714714

715715
/// Maps from the def-id of an item (trait/struct/enum/fn) to its
@@ -2449,7 +2449,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
24492449
region_maps: region_maps,
24502450
node_types: RefCell::new(FnvHashMap()),
24512451
item_substs: RefCell::new(NodeMap()),
2452-
trait_refs: RefCell::new(NodeMap()),
2452+
impl_trait_refs: RefCell::new(NodeMap()),
24532453
trait_defs: RefCell::new(DefIdMap()),
24542454
predicates: RefCell::new(DefIdMap()),
24552455
object_cast_map: RefCell::new(NodeMap()),
@@ -4174,12 +4174,12 @@ pub fn named_element_ty<'tcx>(cx: &ctxt<'tcx>,
41744174
}
41754175
}
41764176

4177-
pub fn node_id_to_trait_ref<'tcx>(cx: &ctxt<'tcx>, id: ast::NodeId)
4177+
pub fn impl_id_to_trait_ref<'tcx>(cx: &ctxt<'tcx>, id: ast::NodeId)
41784178
-> Rc<ty::TraitRef<'tcx>> {
4179-
match cx.trait_refs.borrow().get(&id) {
4179+
match cx.impl_trait_refs.borrow().get(&id) {
41804180
Some(ty) => ty.clone(),
41814181
None => cx.sess.bug(
4182-
&format!("node_id_to_trait_ref: no trait ref for node `{}`",
4182+
&format!("impl_id_to_trait_ref: no trait ref for impl `{}`",
41834183
cx.map.node_to_string(id)))
41844184
}
41854185
}
@@ -5116,25 +5116,19 @@ pub fn impl_trait_ref<'tcx>(cx: &ctxt<'tcx>, id: ast::DefId)
51165116
memoized(&cx.impl_trait_cache, id, |id: ast::DefId| {
51175117
if id.krate == ast::LOCAL_CRATE {
51185118
debug!("(impl_trait_ref) searching for trait impl {:?}", id);
5119-
match cx.map.find(id.node) {
5120-
Some(ast_map::NodeItem(item)) => {
5121-
match item.node {
5122-
ast::ItemImpl(_, _, _, ref opt_trait, _, _) => {
5123-
match opt_trait {
5124-
&Some(ref t) => {
5125-
let trait_ref = ty::node_id_to_trait_ref(cx, t.ref_id);
5126-
Some(trait_ref)
5127-
}
5128-
&None => None
5129-
}
5130-
}
5119+
if let Some(ast_map::NodeItem(item)) = cx.map.find(id.node) {
5120+
if let ast::ItemImpl(_, _, _, ref opt_trait, _, _) = item.node {
5121+
opt_trait.as_ref().map(|_| {
5122+
ty::impl_id_to_trait_ref(cx, id.node)
5123+
})
5124+
} else {
5125+
None
51315126
ast::ItemDefaultImpl(_, ref ast_trait_ref) => {
51325127
Some(ty::node_id_to_trait_ref(cx, ast_trait_ref.ref_id))
51335128
}
5134-
_ => None
5135-
}
51365129
}
5137-
_ => None
5130+
} else {
5131+
None
51385132
}
51395133
} else {
51405134
csearch::get_impl_trait(cx, id)

src/librustc_back/svh.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ mod svh_visitor {
194194
SawVariant,
195195
SawExplicitSelf,
196196
SawPath,
197+
SawQPath,
197198
SawOptLifetimeRef,
198199
SawBlock,
199200
SawPat,
@@ -485,6 +486,10 @@ mod svh_visitor {
485486
SawPath.hash(self.st); visit::walk_path(self, path)
486487
}
487488

489+
fn visit_qpath(&mut self, qpath: &QPath, _: ast::NodeId) {
490+
SawQPath.hash(self.st); visit::walk_qpath(self, qpath)
491+
}
492+
488493
fn visit_block(&mut self, b: &Block) {
489494
SawBlock.hash(self.st); visit::walk_block(self, b)
490495
}

src/librustc_privacy/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
10211021
self.check_path(path.span, id, path.segments.last().unwrap().identifier);
10221022
visit::walk_path(self, path);
10231023
}
1024+
1025+
fn visit_qpath(&mut self, qpath: &ast::QPath, id: ast::NodeId) {
1026+
self.check_path(qpath.trait_path.span, id, qpath.item_path.identifier);
1027+
visit::walk_qpath(self, qpath);
1028+
}
10241029
}
10251030

10261031
////////////////////////////////////////////////////////////////////////////////

src/librustc_resolve/lib.rs

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,8 +2875,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
28752875
this.resolve_type_parameters(&generics.ty_params);
28762876
this.resolve_where_clause(&generics.where_clause);
28772877

2878-
this.resolve_type_parameter_bounds(item.id, bounds,
2879-
TraitDerivation);
2878+
this.resolve_type_parameter_bounds(bounds, TraitDerivation);
28802879

28812880
for trait_item in &(*trait_items) {
28822881
// Create a new rib for the trait_item-specific type
@@ -3141,8 +3140,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
31413140
type_parameter: &TyParam) {
31423141
self.check_if_primitive_type_name(type_parameter.ident.name, type_parameter.span);
31433142
for bound in &*type_parameter.bounds {
3144-
self.resolve_type_parameter_bound(type_parameter.id, bound,
3145-
TraitBoundingTypeParameter);
3143+
self.resolve_type_parameter_bound(bound, TraitBoundingTypeParameter);
31463144
}
31473145
match type_parameter.default {
31483146
Some(ref ty) => self.resolve_type(&**ty),
@@ -3151,41 +3149,33 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
31513149
}
31523150

31533151
fn resolve_type_parameter_bounds(&mut self,
3154-
id: NodeId,
31553152
type_parameter_bounds: &OwnedSlice<TyParamBound>,
31563153
reference_type: TraitReferenceType) {
31573154
for type_parameter_bound in &**type_parameter_bounds {
3158-
self.resolve_type_parameter_bound(id, type_parameter_bound,
3159-
reference_type);
3155+
self.resolve_type_parameter_bound(type_parameter_bound, reference_type);
31603156
}
31613157
}
31623158

31633159
fn resolve_type_parameter_bound(&mut self,
3164-
id: NodeId,
31653160
type_parameter_bound: &TyParamBound,
31663161
reference_type: TraitReferenceType) {
31673162
match *type_parameter_bound {
31683163
TraitTyParamBound(ref tref, _) => {
3169-
self.resolve_poly_trait_reference(id, tref, reference_type)
3164+
self.resolve_trait_reference(tref.trait_ref.ref_id,
3165+
&tref.trait_ref.path,
3166+
reference_type)
31703167
}
31713168
RegionTyParamBound(..) => {}
31723169
}
31733170
}
31743171

3175-
fn resolve_poly_trait_reference(&mut self,
3176-
id: NodeId,
3177-
poly_trait_reference: &PolyTraitRef,
3178-
reference_type: TraitReferenceType) {
3179-
self.resolve_trait_reference(id, &poly_trait_reference.trait_ref, reference_type)
3180-
}
3181-
31823172
fn resolve_trait_reference(&mut self,
31833173
id: NodeId,
3184-
trait_reference: &TraitRef,
3174+
trait_path: &Path,
31853175
reference_type: TraitReferenceType) {
3186-
match self.resolve_path(id, &trait_reference.path, TypeNS, true) {
3176+
match self.resolve_path(id, trait_path, TypeNS, true) {
31873177
None => {
3188-
let path_str = self.path_names_to_string(&trait_reference.path);
3178+
let path_str = self.path_names_to_string(trait_path);
31893179
let usage_str = match reference_type {
31903180
TraitBoundingTypeParameter => "bound type parameter with",
31913181
TraitImplementation => "implement",
@@ -3195,26 +3185,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
31953185
};
31963186

31973187
let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str);
3198-
self.resolve_error(trait_reference.path.span, &msg[..]);
3188+
self.resolve_error(trait_path.span, &msg[..]);
31993189
}
32003190
Some(def) => {
32013191
match def {
32023192
(DefTrait(_), _) => {
32033193
debug!("(resolving trait) found trait def: {:?}", def);
3204-
self.record_def(trait_reference.ref_id, def);
3194+
self.record_def(id, def);
32053195
}
32063196
(def, _) => {
3207-
self.resolve_error(trait_reference.path.span,
3208-
&format!("`{}` is not a trait",
3209-
self.path_names_to_string(
3210-
&trait_reference.path)));
3197+
self.resolve_error(trait_path.span,
3198+
&format!("`{}` is not a trait",
3199+
self.path_names_to_string(trait_path)));
32113200

32123201
// If it's a typedef, give a note
32133202
if let DefTy(..) = def {
3214-
self.session.span_note(
3215-
trait_reference.path.span,
3216-
&format!("`type` aliases cannot be used for traits")
3217-
);
3203+
self.session.span_note(trait_path.span,
3204+
&format!("`type` aliases cannot be used for traits"));
32183205
}
32193206
}
32203207
}
@@ -3229,8 +3216,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
32293216
self.resolve_type(&*bound_pred.bounded_ty);
32303217

32313218
for bound in &*bound_pred.bounds {
3232-
self.resolve_type_parameter_bound(bound_pred.bounded_ty.id, bound,
3233-
TraitBoundingTypeParameter);
3219+
self.resolve_type_parameter_bound(bound, TraitBoundingTypeParameter);
32343220
}
32353221
}
32363222
&ast::WherePredicate::RegionPredicate(_) => {}
@@ -3303,14 +3289,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33033289
result
33043290
}
33053291

3306-
fn with_optional_trait_ref<T, F>(&mut self, id: NodeId,
3292+
fn with_optional_trait_ref<T, F>(&mut self,
33073293
opt_trait_ref: &Option<TraitRef>,
33083294
f: F) -> T where
33093295
F: FnOnce(&mut Resolver) -> T,
33103296
{
33113297
let new_val = match *opt_trait_ref {
33123298
Some(ref trait_ref) => {
3313-
self.resolve_trait_reference(id, trait_ref, TraitImplementation);
3299+
self.resolve_trait_reference(trait_ref.ref_id,
3300+
&trait_ref.path,
3301+
TraitImplementation);
33143302

33153303
match self.def_map.borrow().get(&trait_ref.ref_id) {
33163304
Some(def) => {
@@ -3345,7 +3333,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33453333
this.resolve_where_clause(&generics.where_clause);
33463334

33473335
// Resolve the trait reference, if necessary.
3348-
this.with_optional_trait_ref(id, opt_trait_reference, |this| {
3336+
this.with_optional_trait_ref(opt_trait_reference, |this| {
33493337
// Resolve the self type.
33503338
this.resolve_type(self_type);
33513339

@@ -3630,13 +3618,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
36303618

36313619
TyObjectSum(ref ty, ref bound_vec) => {
36323620
self.resolve_type(&**ty);
3633-
self.resolve_type_parameter_bounds(ty.id, bound_vec,
3634-
TraitBoundingTypeParameter);
3621+
self.resolve_type_parameter_bounds(bound_vec, TraitBoundingTypeParameter);
36353622
}
36363623

36373624
TyQPath(ref qpath) => {
36383625
self.resolve_type(&*qpath.self_type);
3639-
self.resolve_trait_reference(ty.id, &*qpath.trait_ref, TraitQPath);
3626+
self.resolve_trait_reference(ty.id, &qpath.trait_path, TraitQPath);
36403627
for ty in qpath.item_path.parameters.types() {
36413628
self.resolve_type(&**ty);
36423629
}
@@ -3646,10 +3633,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
36463633
}
36473634

36483635
TyPolyTraitRef(ref bounds) => {
3649-
self.resolve_type_parameter_bounds(
3650-
ty.id,
3651-
bounds,
3652-
TraitObject);
3636+
self.resolve_type_parameter_bounds(bounds, TraitObject);
36533637
visit::walk_ty(self, ty);
36543638
}
36553639
_ => {
@@ -4439,8 +4423,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
44394423
ExprPath(ref path) => path,
44404424
ExprQPath(ref qpath) => {
44414425
self.resolve_type(&*qpath.self_type);
4442-
self.resolve_trait_reference(expr.id, &*qpath.trait_ref, TraitQPath);
4443-
path_from_qpath = qpath.trait_ref.path.clone();
4426+
4427+
// Just make sure the trait is valid, don't record a def.
4428+
self.resolve_trait_reference(expr.id, &qpath.trait_path, TraitQPath);
4429+
self.def_map.borrow_mut().remove(&expr.id);
4430+
4431+
path_from_qpath = qpath.trait_path.clone();
44444432
path_from_qpath.segments.push(qpath.item_path.clone());
44454433
&path_from_qpath
44464434
}

src/librustc_trans/save/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,10 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
13401340
visit::walk_path(self, path);
13411341
}
13421342
ast::ExprQPath(ref qpath) => {
1343-
let mut path = qpath.trait_ref.path.clone();
1343+
let mut path = qpath.trait_path.clone();
13441344
path.segments.push(qpath.item_path.clone());
13451345
self.process_path(ex.id, ex.span, &path, None);
1346-
visit::walk_qpath(self, ex.span, &**qpath);
1346+
visit::walk_qpath(self, &**qpath);
13471347
}
13481348
ast::ExprStruct(ref path, ref fields, ref base) =>
13491349
self.process_struct_lit(ex, path, fields, base),

0 commit comments

Comments
 (0)