Skip to content

Commit 1bbf7a4

Browse files
committed
Add field expansion: Mark to NameBinding.
1 parent 83aac43 commit 1bbf7a4

File tree

4 files changed

+68
-43
lines changed

4 files changed

+68
-43
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,25 @@ use syntax::visit::{self, Visitor};
4545

4646
use syntax_pos::{Span, DUMMY_SP};
4747

48-
impl<'a> ToNameBinding<'a> for (Module<'a>, Span, ty::Visibility) {
48+
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
4949
fn to_name_binding(self) -> NameBinding<'a> {
50-
NameBinding { kind: NameBindingKind::Module(self.0), span: self.1, vis: self.2 }
50+
NameBinding {
51+
kind: NameBindingKind::Module(self.0),
52+
vis: self.1,
53+
span: self.2,
54+
expansion: self.3,
55+
}
5156
}
5257
}
5358

54-
impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
59+
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
5560
fn to_name_binding(self) -> NameBinding<'a> {
56-
NameBinding { kind: NameBindingKind::Def(self.0), span: self.1, vis: self.2 }
61+
NameBinding {
62+
kind: NameBindingKind::Def(self.0),
63+
vis: self.1,
64+
span: self.2,
65+
expansion: self.3,
66+
}
5767
}
5868
}
5969

@@ -148,8 +158,9 @@ impl<'b> Resolver<'b> {
148158
}
149159

150160
let subclass = ImportDirectiveSubclass::single(binding.name, source.name);
151-
let span = view_path.span;
152-
self.add_import_directive(module_path, subclass, span, item.id, vis);
161+
self.add_import_directive(
162+
module_path, subclass, view_path.span, item.id, vis, expansion,
163+
);
153164
}
154165
ViewPathList(_, ref source_items) => {
155166
// Make sure there's at most one `mod` import in the list.
@@ -196,17 +207,20 @@ impl<'b> Resolver<'b> {
196207
}
197208
};
198209
let subclass = ImportDirectiveSubclass::single(rename, name);
199-
let (span, id) = (source_item.span, source_item.node.id);
200-
self.add_import_directive(module_path, subclass, span, id, vis);
210+
let id = source_item.node.id;
211+
self.add_import_directive(
212+
module_path, subclass, source_item.span, id, vis, expansion,
213+
);
201214
}
202215
}
203216
ViewPathGlob(_) => {
204217
let subclass = GlobImport {
205218
is_prelude: is_prelude,
206219
max_vis: Cell::new(ty::Visibility::PrivateExternal),
207220
};
208-
let span = view_path.span;
209-
self.add_import_directive(module_path, subclass, span, item.id, vis);
221+
self.add_import_directive(
222+
module_path, subclass, view_path.span, item.id, vis, expansion,
223+
);
210224
}
211225
}
212226
}
@@ -217,7 +231,7 @@ impl<'b> Resolver<'b> {
217231
// n.b. we don't need to look at the path option here, because cstore already did
218232
let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id).unwrap();
219233
let module = self.get_extern_crate_root(crate_id);
220-
let binding = (module, sp, ty::Visibility::Public).to_name_binding();
234+
let binding = (module, ty::Visibility::Public, sp, expansion).to_name_binding();
221235
let binding = self.arenas.alloc_name_binding(binding);
222236
let directive = self.arenas.alloc_import_directive(ImportDirective {
223237
id: item.id,
@@ -227,6 +241,7 @@ impl<'b> Resolver<'b> {
227241
span: item.span,
228242
module_path: Vec::new(),
229243
vis: Cell::new(vis),
244+
expansion: expansion,
230245
});
231246
let imported_binding = self.import(binding, directive);
232247
self.define(parent, name, TypeNS, imported_binding);
@@ -245,7 +260,7 @@ impl<'b> Resolver<'b> {
245260
normal_ancestor_id: Some(item.id),
246261
..ModuleS::new(Some(parent), ModuleKind::Def(def, name))
247262
});
248-
self.define(parent, name, TypeNS, (module, sp, vis));
263+
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
249264
self.module_map.insert(item.id, module);
250265

251266
// Descend into the module.
@@ -258,45 +273,45 @@ impl<'b> Resolver<'b> {
258273
ItemKind::Static(_, m, _) => {
259274
let mutbl = m == Mutability::Mutable;
260275
let def = Def::Static(self.definitions.local_def_id(item.id), mutbl);
261-
self.define(parent, name, ValueNS, (def, sp, vis));
276+
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
262277
}
263278
ItemKind::Const(..) => {
264279
let def = Def::Const(self.definitions.local_def_id(item.id));
265-
self.define(parent, name, ValueNS, (def, sp, vis));
280+
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
266281
}
267282
ItemKind::Fn(..) => {
268283
let def = Def::Fn(self.definitions.local_def_id(item.id));
269-
self.define(parent, name, ValueNS, (def, sp, vis));
284+
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
270285
}
271286

272287
// These items live in the type namespace.
273288
ItemKind::Ty(..) => {
274289
let def = Def::TyAlias(self.definitions.local_def_id(item.id));
275-
self.define(parent, name, TypeNS, (def, sp, vis));
290+
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
276291
}
277292

278293
ItemKind::Enum(ref enum_definition, _) => {
279294
let def = Def::Enum(self.definitions.local_def_id(item.id));
280295
let module = self.new_module(parent, ModuleKind::Def(def, name), true);
281-
self.define(parent, name, TypeNS, (module, sp, vis));
296+
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
282297

283298
for variant in &(*enum_definition).variants {
284-
self.build_reduced_graph_for_variant(variant, module, vis);
299+
self.build_reduced_graph_for_variant(variant, module, vis, expansion);
285300
}
286301
}
287302

288303
// These items live in both the type and value namespaces.
289304
ItemKind::Struct(ref struct_def, _) => {
290305
// Define a name in the type namespace.
291306
let def = Def::Struct(self.definitions.local_def_id(item.id));
292-
self.define(parent, name, TypeNS, (def, sp, vis));
307+
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
293308

294309
// If this is a tuple or unit struct, define a name
295310
// in the value namespace as well.
296311
if !struct_def.is_struct() {
297312
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
298313
CtorKind::from_ast(struct_def));
299-
self.define(parent, name, ValueNS, (ctor_def, sp, vis));
314+
self.define(parent, name, ValueNS, (ctor_def, vis, sp, expansion));
300315
}
301316

302317
// Record field names for error reporting.
@@ -310,7 +325,7 @@ impl<'b> Resolver<'b> {
310325

311326
ItemKind::Union(ref vdata, _) => {
312327
let def = Def::Union(self.definitions.local_def_id(item.id));
313-
self.define(parent, name, TypeNS, (def, sp, vis));
328+
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
314329

315330
// Record field names for error reporting.
316331
let field_names = vdata.fields().iter().filter_map(|field| {
@@ -329,7 +344,7 @@ impl<'b> Resolver<'b> {
329344
// Add all the items within to a new module.
330345
let module =
331346
self.new_module(parent, ModuleKind::Def(Def::Trait(def_id), name), true);
332-
self.define(parent, name, TypeNS, (module, sp, vis));
347+
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
333348
self.current_module = module;
334349
}
335350
ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
@@ -338,27 +353,28 @@ impl<'b> Resolver<'b> {
338353

339354
// Constructs the reduced graph for one variant. Variants exist in the
340355
// type and value namespaces.
341-
fn build_reduced_graph_for_variant(&mut self,
342-
variant: &Variant,
343-
parent: Module<'b>,
344-
vis: ty::Visibility) {
356+
fn build_reduced_graph_for_variant(
357+
&mut self, variant: &Variant, parent: Module<'b>, vis: ty::Visibility, expansion: Mark,
358+
) {
345359
let name = variant.node.name.name;
346360
let def_id = self.definitions.local_def_id(variant.node.data.id());
347361

348362
// Define a name in the type namespace.
349363
let def = Def::Variant(def_id);
350-
self.define(parent, name, TypeNS, (def, variant.span, vis));
364+
self.define(parent, name, TypeNS, (def, vis, variant.span, expansion));
351365

352366
// Define a constructor name in the value namespace.
353367
// Braced variants, unlike structs, generate unusable names in
354368
// value namespace, they are reserved for possible future use.
355369
let ctor_kind = CtorKind::from_ast(&variant.node.data);
356370
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
357-
self.define(parent, name, ValueNS, (ctor_def, variant.span, vis));
371+
self.define(parent, name, ValueNS, (ctor_def, vis, variant.span, expansion));
358372
}
359373

360374
/// Constructs the reduced graph for one foreign item.
361-
fn build_reduced_graph_for_foreign_item(&mut self, foreign_item: &ForeignItem) {
375+
fn build_reduced_graph_for_foreign_item(
376+
&mut self, foreign_item: &ForeignItem, expansion: Mark,
377+
) {
362378
let parent = self.current_module;
363379
let name = foreign_item.ident.name;
364380

@@ -371,7 +387,7 @@ impl<'b> Resolver<'b> {
371387
}
372388
};
373389
let vis = self.resolve_visibility(&foreign_item.vis);
374-
self.define(parent, name, ValueNS, (def, foreign_item.span, vis));
390+
self.define(parent, name, ValueNS, (def, vis, foreign_item.span, expansion));
375391
}
376392

377393
fn build_reduced_graph_for_block(&mut self, block: &Block) {
@@ -404,24 +420,24 @@ impl<'b> Resolver<'b> {
404420
match def {
405421
Def::Mod(..) | Def::Enum(..) => {
406422
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
407-
self.define(parent, name, TypeNS, (module, DUMMY_SP, vis));
423+
self.define(parent, name, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
408424
}
409425
Def::Variant(..) => {
410-
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
426+
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
411427
}
412428
Def::VariantCtor(..) => {
413-
self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
429+
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
414430
}
415431
Def::Fn(..) |
416432
Def::Static(..) |
417433
Def::Const(..) |
418434
Def::AssociatedConst(..) |
419435
Def::Method(..) => {
420-
self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
436+
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
421437
}
422438
Def::Trait(..) => {
423439
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
424-
self.define(parent, name, TypeNS, (module, DUMMY_SP, vis));
440+
self.define(parent, name, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
425441

426442
// If this is a trait, add all the trait item names to the trait info.
427443
let trait_item_def_ids = self.session.cstore.associated_item_def_ids(def_id);
@@ -433,27 +449,27 @@ impl<'b> Resolver<'b> {
433449
}
434450
}
435451
Def::TyAlias(..) | Def::AssociatedTy(..) => {
436-
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
452+
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
437453
}
438454
Def::Struct(..) => {
439-
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
455+
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
440456

441457
// Record field names for error reporting.
442458
let field_names = self.session.cstore.struct_field_names(def_id);
443459
self.insert_field_names(def_id, field_names);
444460
}
445461
Def::StructCtor(..) => {
446-
self.define(parent, name, ValueNS, (def, DUMMY_SP, vis));
462+
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
447463
}
448464
Def::Union(..) => {
449-
self.define(parent, name, TypeNS, (def, DUMMY_SP, vis));
465+
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
450466

451467
// Record field names for error reporting.
452468
let field_names = self.session.cstore.struct_field_names(def_id);
453469
self.insert_field_names(def_id, field_names);
454470
}
455471
Def::Macro(..) => {
456-
self.define(parent, name, MacroNS, (def, DUMMY_SP, vis));
472+
self.define(parent, name, MacroNS, (def, vis, DUMMY_SP, Mark::root()));
457473
}
458474
Def::Local(..) |
459475
Def::PrimTy(..) |
@@ -695,7 +711,7 @@ impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
695711
}
696712

697713
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
698-
self.resolver.build_reduced_graph_for_foreign_item(foreign_item);
714+
self.resolver.build_reduced_graph_for_foreign_item(foreign_item, self.expansion);
699715
visit::walk_foreign_item(self, foreign_item);
700716
}
701717

@@ -732,7 +748,7 @@ impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
732748
self.resolver.trait_item_map.insert((item.ident.name, def_id), is_static_method);
733749

734750
let vis = ty::Visibility::Public;
735-
self.resolver.define(parent, item.ident.name, ns, (def, item.span, vis));
751+
self.resolver.define(parent, item.ident.name, ns, (def, vis, item.span, self.expansion));
736752

737753
self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor
738754
visit::walk_trait_item(self, item);

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ impl<'a> fmt::Debug for ModuleS<'a> {
881881
#[derive(Clone, Debug)]
882882
pub struct NameBinding<'a> {
883883
kind: NameBindingKind<'a>,
884+
expansion: Mark,
884885
span: Span,
885886
vis: ty::Visibility,
886887
}
@@ -1304,6 +1305,7 @@ impl<'a> Resolver<'a> {
13041305
arenas: arenas,
13051306
dummy_binding: arenas.alloc_name_binding(NameBinding {
13061307
kind: NameBindingKind::Def(Def::Err),
1308+
expansion: Mark::root(),
13071309
span: DUMMY_SP,
13081310
vis: ty::Visibility::Public,
13091311
}),

src/librustc_resolve/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ impl<'a> base::Resolver for Resolver<'a> {
184184
kind: NameBindingKind::Def(Def::Macro(def_id)),
185185
span: DUMMY_SP,
186186
vis: ty::Visibility::PrivateExternal,
187+
expansion: Mark::root(),
187188
});
188189
self.builtin_macros.insert(ident.name, binding);
189190
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc::hir::def::*;
2626

2727
use syntax::ast::{Ident, NodeId, Name};
2828
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
29+
use syntax::ext::hygiene::Mark;
2930
use syntax::util::lev_distance::find_best_match_for_name;
3031
use syntax_pos::Span;
3132

@@ -70,6 +71,7 @@ pub struct ImportDirective<'a> {
7071
pub subclass: ImportDirectiveSubclass<'a>,
7172
pub span: Span,
7273
pub vis: Cell<ty::Visibility>,
74+
pub expansion: Mark,
7375
}
7476

7577
impl<'a> ImportDirective<'a> {
@@ -257,7 +259,8 @@ impl<'a> Resolver<'a> {
257259
subclass: ImportDirectiveSubclass<'a>,
258260
span: Span,
259261
id: NodeId,
260-
vis: ty::Visibility) {
262+
vis: ty::Visibility,
263+
expansion: Mark) {
261264
let current_module = self.current_module;
262265
let directive = self.arenas.alloc_import_directive(ImportDirective {
263266
parent: current_module,
@@ -267,6 +270,7 @@ impl<'a> Resolver<'a> {
267270
span: span,
268271
id: id,
269272
vis: Cell::new(vis),
273+
expansion: expansion,
270274
});
271275

272276
self.indeterminate_imports.push(directive);
@@ -310,6 +314,7 @@ impl<'a> Resolver<'a> {
310314
},
311315
span: directive.span,
312316
vis: vis,
317+
expansion: directive.expansion,
313318
}
314319
}
315320

@@ -336,6 +341,7 @@ impl<'a> Resolver<'a> {
336341
binding.vis
337342
},
338343
span: old_binding.span,
344+
expansion: Mark::root(),
339345
}));
340346
} else if !old_binding.vis.is_at_least(binding.vis, this) {
341347
// We are glob-importing the same item but with greater visibility.

0 commit comments

Comments
 (0)