Skip to content

Commit a4196cd

Browse files
committed
resolve: Use vis: ty::Visibility instead of is_public: bool
1 parent 3bf9fc0 commit a4196cd

File tree

3 files changed

+127
-105
lines changed

3 files changed

+127
-105
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, Span) {
5353
}
5454
}
5555

56-
impl<'a> ToNameBinding<'a> for (Def, Span, DefModifiers) {
56+
impl<'a> ToNameBinding<'a> for (Def, Span, DefModifiers, ty::Visibility) {
5757
fn to_name_binding(self) -> NameBinding<'a> {
5858
let kind = NameBindingKind::Def(self.0);
59-
NameBinding { modifiers: self.2, kind: kind, span: Some(self.1) }
59+
NameBinding { modifiers: self.2, kind: kind, span: Some(self.1), vis: self.3 }
6060
}
6161
}
6262

@@ -105,12 +105,9 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
105105
let parent = *parent_ref;
106106
let name = item.name;
107107
let sp = item.span;
108-
let is_public = item.vis == hir::Public;
109-
let modifiers = if is_public {
110-
DefModifiers::PUBLIC
111-
} else {
112-
DefModifiers::empty()
113-
} | DefModifiers::IMPORTABLE;
108+
let modifiers = DefModifiers::IMPORTABLE;
109+
self.current_module = parent;
110+
let vis = self.resolve_visibility(&item.vis);
114111

115112
match item.node {
116113
ItemUse(ref view_path) => {
@@ -172,7 +169,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
172169
subclass,
173170
view_path.span,
174171
item.id,
175-
is_public,
172+
vis,
176173
is_prelude);
177174
}
178175
ViewPathList(_, ref source_items) => {
@@ -223,7 +220,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
223220
subclass,
224221
source_item.span,
225222
source_item.node.id(),
226-
is_public,
223+
vis,
227224
is_prelude);
228225
}
229226
}
@@ -233,7 +230,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
233230
GlobImport,
234231
view_path.span,
235232
item.id,
236-
is_public,
233+
vis,
237234
is_prelude);
238235
}
239236
}
@@ -249,7 +246,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
249246
};
250247
let parent_link = ModuleParentLink(parent, name);
251248
let def = Def::Mod(def_id);
252-
let module = self.new_extern_crate_module(parent_link, def, is_public, item.id);
249+
let module = self.new_extern_crate_module(parent_link, def, vis, item.id);
253250
self.define(parent, name, TypeNS, (module, sp));
254251

255252
self.build_reduced_graph_for_external_crate(module);
@@ -259,7 +256,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
259256
ItemMod(..) => {
260257
let parent_link = ModuleParentLink(parent, name);
261258
let def = Def::Mod(self.ast_map.local_def_id(item.id));
262-
let module = self.new_module(parent_link, Some(def), false, is_public);
259+
let module = self.new_module(parent_link, Some(def), false, vis);
263260
self.define(parent, name, TypeNS, (module, sp));
264261
parent.module_children.borrow_mut().insert(item.id, module);
265262
*parent_ref = module;
@@ -271,33 +268,32 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
271268
ItemStatic(_, m, _) => {
272269
let mutbl = m == hir::MutMutable;
273270
let def = Def::Static(self.ast_map.local_def_id(item.id), mutbl);
274-
self.define(parent, name, ValueNS, (def, sp, modifiers));
271+
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
275272
}
276273
ItemConst(_, _) => {
277274
let def = Def::Const(self.ast_map.local_def_id(item.id));
278-
self.define(parent, name, ValueNS, (def, sp, modifiers));
275+
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
279276
}
280277
ItemFn(_, _, _, _, _, _) => {
281278
let def = Def::Fn(self.ast_map.local_def_id(item.id));
282-
self.define(parent, name, ValueNS, (def, sp, modifiers));
279+
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
283280
}
284281

285282
// These items live in the type namespace.
286283
ItemTy(..) => {
287284
let def = Def::TyAlias(self.ast_map.local_def_id(item.id));
288-
self.define(parent, name, TypeNS, (def, sp, modifiers));
285+
self.define(parent, name, TypeNS, (def, sp, modifiers, vis));
289286
}
290287

291288
ItemEnum(ref enum_definition, _) => {
292289
let parent_link = ModuleParentLink(parent, name);
293290
let def = Def::Enum(self.ast_map.local_def_id(item.id));
294-
let module = self.new_module(parent_link, Some(def), false, is_public);
291+
let module = self.new_module(parent_link, Some(def), false, vis);
295292
self.define(parent, name, TypeNS, (module, sp));
296293

297-
let variant_modifiers = if is_public {
298-
DefModifiers::empty()
299-
} else {
300-
DefModifiers::PRIVATE_VARIANT
294+
let variant_modifiers = match vis {
295+
ty::Visibility::Public => DefModifiers::empty(),
296+
_ => DefModifiers::PRIVATE_VARIANT,
301297
};
302298
for variant in &(*enum_definition).variants {
303299
let item_def_id = self.ast_map.local_def_id(item.id);
@@ -310,20 +306,20 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
310306
ItemStruct(ref struct_def, _) => {
311307
// Define a name in the type namespace.
312308
let def = Def::Struct(self.ast_map.local_def_id(item.id));
313-
self.define(parent, name, TypeNS, (def, sp, modifiers));
309+
self.define(parent, name, TypeNS, (def, sp, modifiers, vis));
314310

315311
// If this is a newtype or unit-like struct, define a name
316312
// in the value namespace as well
317313
if !struct_def.is_struct() {
318314
let def = Def::Struct(self.ast_map.local_def_id(struct_def.id()));
319-
self.define(parent, name, ValueNS, (def, sp, modifiers));
315+
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
320316
}
321317

322318
// Record the def ID and fields of this struct.
323-
let field_names = struct_def.fields()
324-
.iter()
325-
.map(|f| f.name)
326-
.collect();
319+
let field_names = struct_def.fields().iter().map(|field| {
320+
self.resolve_visibility(&field.vis);
321+
field.name
322+
}).collect();
327323
let item_def_id = self.ast_map.local_def_id(item.id);
328324
self.structs.insert(item_def_id, field_names);
329325
}
@@ -336,7 +332,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
336332
// Add all the items within to a new module.
337333
let parent_link = ModuleParentLink(parent, name);
338334
let def = Def::Trait(def_id);
339-
let module_parent = self.new_module(parent_link, Some(def), false, is_public);
335+
let module_parent = self.new_module(parent_link, Some(def), false, vis);
340336
self.define(parent, name, TypeNS, (module_parent, sp));
341337

342338
// Add the names of all the items to the trait info.
@@ -348,8 +344,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
348344
hir::TypeTraitItem(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),
349345
};
350346

351-
let modifiers = DefModifiers::PUBLIC; // NB: not DefModifiers::IMPORTABLE
352-
self.define(module_parent, item.name, ns, (def, item.span, modifiers));
347+
let modifiers = DefModifiers::empty(); // NB: not DefModifiers::IMPORTABLE
348+
self.define(module_parent, item.name, ns, (def, item.span, modifiers, vis));
353349

354350
self.trait_item_map.insert((item.name, def_id), item_def_id);
355351
}
@@ -373,24 +369,20 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
373369

374370
// Variants are always treated as importable to allow them to be glob used.
375371
// All variants are defined in both type and value namespaces as future-proofing.
376-
let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE | variant_modifiers;
372+
let modifiers = DefModifiers::IMPORTABLE | variant_modifiers;
377373
let def = Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id()));
374+
let vis = ty::Visibility::Public;
378375

379-
self.define(parent, name, ValueNS, (def, variant.span, modifiers));
380-
self.define(parent, name, TypeNS, (def, variant.span, modifiers));
376+
self.define(parent, name, ValueNS, (def, variant.span, modifiers, vis));
377+
self.define(parent, name, TypeNS, (def, variant.span, modifiers, vis));
381378
}
382379

383380
/// Constructs the reduced graph for one foreign item.
384381
fn build_reduced_graph_for_foreign_item(&mut self,
385382
foreign_item: &ForeignItem,
386383
parent: Module<'b>) {
387384
let name = foreign_item.name;
388-
let is_public = foreign_item.vis == hir::Public;
389-
let modifiers = if is_public {
390-
DefModifiers::PUBLIC
391-
} else {
392-
DefModifiers::empty()
393-
} | DefModifiers::IMPORTABLE;
385+
let modifiers = DefModifiers::IMPORTABLE;
394386

395387
let def = match foreign_item.node {
396388
ForeignItemFn(..) => {
@@ -400,7 +392,9 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
400392
Def::Static(self.ast_map.local_def_id(foreign_item.id), m)
401393
}
402394
};
403-
self.define(parent, name, ValueNS, (def, foreign_item.span, modifiers));
395+
self.current_module = parent;
396+
let vis = self.resolve_visibility(&foreign_item.vis);
397+
self.define(parent, name, ValueNS, (def, foreign_item.span, modifiers, vis));
404398
}
405399

406400
fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &mut Module<'b>) {
@@ -412,7 +406,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
412406
block_id);
413407

414408
let parent_link = BlockParentLink(parent, block_id);
415-
let new_module = self.new_module(parent_link, None, false, false);
409+
let new_module = self.new_module(parent_link, None, false, parent.vis);
416410
parent.module_children.borrow_mut().insert(block_id, new_module);
417411
*parent = new_module;
418412
}
@@ -434,32 +428,27 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
434428
}
435429

436430
let name = xcdef.name;
437-
let is_public = xcdef.vis == ty::Visibility::Public || parent.is_trait();
438-
439-
let mut modifiers = DefModifiers::empty();
440-
if is_public {
441-
modifiers = modifiers | DefModifiers::PUBLIC;
442-
}
443-
if parent.is_normal() {
444-
modifiers = modifiers | DefModifiers::IMPORTABLE;
445-
}
431+
let vis = if parent.is_trait() { ty::Visibility::Public } else { xcdef.vis };
432+
let modifiers = match parent.is_normal() {
433+
true => DefModifiers::IMPORTABLE,
434+
false => DefModifiers::empty(),
435+
};
446436

447437
match def {
448438
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {
449-
debug!("(building reduced graph for external crate) building module {} {}",
450-
name,
451-
is_public);
439+
debug!("(building reduced graph for external crate) building module {} {:?}",
440+
name, vis);
452441
let parent_link = ModuleParentLink(parent, name);
453-
let module = self.new_module(parent_link, Some(def), true, is_public);
442+
let module = self.new_module(parent_link, Some(def), true, vis);
454443
self.try_define(parent, name, TypeNS, (module, DUMMY_SP));
455444
}
456445
Def::Variant(_, variant_id) => {
457446
debug!("(building reduced graph for external crate) building variant {}", name);
458447
// Variants are always treated as importable to allow them to be glob used.
459448
// All variants are defined in both type and value namespaces as future-proofing.
460-
let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE;
461-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers));
462-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers));
449+
let modifiers = DefModifiers::IMPORTABLE;
450+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
451+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
463452
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
464453
// Not adding fields for variants as they are not accessed with a self receiver
465454
self.structs.insert(variant_id, Vec::new());
@@ -472,7 +461,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
472461
Def::Method(..) => {
473462
debug!("(building reduced graph for external crate) building value (fn/static) {}",
474463
name);
475-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers));
464+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
476465
}
477466
Def::Trait(def_id) => {
478467
debug!("(building reduced graph for external crate) building type {}", name);
@@ -493,21 +482,21 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
493482
}
494483

495484
let parent_link = ModuleParentLink(parent, name);
496-
let module = self.new_module(parent_link, Some(def), true, is_public);
485+
let module = self.new_module(parent_link, Some(def), true, vis);
497486
self.try_define(parent, name, TypeNS, (module, DUMMY_SP));
498487
}
499488
Def::TyAlias(..) | Def::AssociatedTy(..) => {
500489
debug!("(building reduced graph for external crate) building type {}", name);
501-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers));
490+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
502491
}
503492
Def::Struct(def_id)
504493
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
505494
debug!("(building reduced graph for external crate) building type and value for {}",
506495
name);
507-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers));
496+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
508497
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
509498
let def = Def::Struct(ctor_def_id);
510-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers));
499+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
511500
}
512501

513502
// Record the def ID and fields of this struct.

0 commit comments

Comments
 (0)