Skip to content

Commit 2c978dc

Browse files
committed
resolve: Refactor away DefModifiers
1 parent ae33aa7 commit 2c978dc

File tree

4 files changed

+56
-75
lines changed

4 files changed

+56
-75
lines changed

src/librustc_resolve/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ crate-type = ["dylib"]
1212
log = { path = "../liblog" }
1313
syntax = { path = "../libsyntax" }
1414
rustc = { path = "../librustc" }
15-
rustc_bitflags = { path = "../librustc_bitflags" }
1615
arena = { path = "../libarena" }

src/librustc_resolve/build_reduced_graph.rs

+18-30
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//! Here we build the "reduced graph": the graph of the module tree without
1414
//! any imports resolved.
1515
16-
use DefModifiers;
1716
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
1817
use Module;
1918
use Namespace::{self, TypeNS, ValueNS};
@@ -53,10 +52,9 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, Span) {
5352
}
5453
}
5554

56-
impl<'a> ToNameBinding<'a> for (Def, Span, DefModifiers, ty::Visibility) {
55+
impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
5756
fn to_name_binding(self) -> NameBinding<'a> {
58-
let kind = NameBindingKind::Def(self.0);
59-
NameBinding { modifiers: self.2, kind: kind, span: Some(self.1), vis: self.3 }
57+
NameBinding { kind: NameBindingKind::Def(self.0), span: Some(self.1), vis: self.2 }
6058
}
6159
}
6260

@@ -105,7 +103,6 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
105103
let parent = *parent_ref;
106104
let name = item.name;
107105
let sp = item.span;
108-
let modifiers = DefModifiers::IMPORTABLE;
109106
self.current_module = parent;
110107
let vis = self.resolve_visibility(&item.vis);
111108

@@ -268,21 +265,21 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
268265
ItemStatic(_, m, _) => {
269266
let mutbl = m == hir::MutMutable;
270267
let def = Def::Static(self.ast_map.local_def_id(item.id), mutbl);
271-
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
268+
self.define(parent, name, ValueNS, (def, sp, vis));
272269
}
273270
ItemConst(_, _) => {
274271
let def = Def::Const(self.ast_map.local_def_id(item.id));
275-
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
272+
self.define(parent, name, ValueNS, (def, sp, vis));
276273
}
277274
ItemFn(_, _, _, _, _, _) => {
278275
let def = Def::Fn(self.ast_map.local_def_id(item.id));
279-
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
276+
self.define(parent, name, ValueNS, (def, sp, vis));
280277
}
281278

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

288285
ItemEnum(ref enum_definition, _) => {
@@ -301,13 +298,13 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
301298
ItemStruct(ref struct_def, _) => {
302299
// Define a name in the type namespace.
303300
let def = Def::Struct(self.ast_map.local_def_id(item.id));
304-
self.define(parent, name, TypeNS, (def, sp, modifiers, vis));
301+
self.define(parent, name, TypeNS, (def, sp, vis));
305302

306303
// If this is a newtype or unit-like struct, define a name
307304
// in the value namespace as well
308305
if !struct_def.is_struct() {
309306
let def = Def::Struct(self.ast_map.local_def_id(struct_def.id()));
310-
self.define(parent, name, ValueNS, (def, sp, modifiers, vis));
307+
self.define(parent, name, ValueNS, (def, sp, vis));
311308
}
312309

313310
// Record the def ID and fields of this struct.
@@ -339,8 +336,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
339336
hir::TypeTraitItem(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),
340337
};
341338

342-
let modifiers = DefModifiers::empty(); // NB: not DefModifiers::IMPORTABLE
343-
self.define(module_parent, item.name, ns, (def, item.span, modifiers, vis));
339+
self.define(module_parent, item.name, ns, (def, item.span, vis));
344340

345341
self.trait_item_map.insert((item.name, def_id), item_def_id);
346342
}
@@ -363,19 +359,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
363359

364360
// Variants are always treated as importable to allow them to be glob used.
365361
// All variants are defined in both type and value namespaces as future-proofing.
366-
let modifiers = DefModifiers::IMPORTABLE;
367362
let def = Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id()));
368-
369-
self.define(parent, name, ValueNS, (def, variant.span, modifiers, parent.vis));
370-
self.define(parent, name, TypeNS, (def, variant.span, modifiers, parent.vis));
363+
self.define(parent, name, ValueNS, (def, variant.span, parent.vis));
364+
self.define(parent, name, TypeNS, (def, variant.span, parent.vis));
371365
}
372366

373367
/// Constructs the reduced graph for one foreign item.
374368
fn build_reduced_graph_for_foreign_item(&mut self,
375369
foreign_item: &ForeignItem,
376370
parent: Module<'b>) {
377371
let name = foreign_item.name;
378-
let modifiers = DefModifiers::IMPORTABLE;
379372

380373
let def = match foreign_item.node {
381374
ForeignItemFn(..) => {
@@ -387,7 +380,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
387380
};
388381
self.current_module = parent;
389382
let vis = self.resolve_visibility(&foreign_item.vis);
390-
self.define(parent, name, ValueNS, (def, foreign_item.span, modifiers, vis));
383+
self.define(parent, name, ValueNS, (def, foreign_item.span, vis));
391384
}
392385

393386
fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &mut Module<'b>) {
@@ -422,10 +415,6 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
422415

423416
let name = xcdef.name;
424417
let vis = if parent.is_trait() { ty::Visibility::Public } else { xcdef.vis };
425-
let modifiers = match parent.is_normal() {
426-
true => DefModifiers::IMPORTABLE,
427-
false => DefModifiers::empty(),
428-
};
429418

430419
match def {
431420
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {
@@ -439,9 +428,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
439428
debug!("(building reduced graph for external crate) building variant {}", name);
440429
// Variants are always treated as importable to allow them to be glob used.
441430
// All variants are defined in both type and value namespaces as future-proofing.
442-
let modifiers = DefModifiers::IMPORTABLE;
443-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
444-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
431+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
432+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
445433
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
446434
// Not adding fields for variants as they are not accessed with a self receiver
447435
self.structs.insert(variant_id, Vec::new());
@@ -454,7 +442,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
454442
Def::Method(..) => {
455443
debug!("(building reduced graph for external crate) building value (fn/static) {}",
456444
name);
457-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
445+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
458446
}
459447
Def::Trait(def_id) => {
460448
debug!("(building reduced graph for external crate) building type {}", name);
@@ -480,16 +468,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
480468
}
481469
Def::TyAlias(..) | Def::AssociatedTy(..) => {
482470
debug!("(building reduced graph for external crate) building type {}", name);
483-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
471+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
484472
}
485473
Def::Struct(def_id)
486474
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
487475
debug!("(building reduced graph for external crate) building type and value for {}",
488476
name);
489-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers, vis));
477+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
490478
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
491479
let def = Def::Struct(ctor_def_id);
492-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers, vis));
480+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
493481
}
494482

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

src/librustc_resolve/lib.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ extern crate log;
2929
extern crate syntax;
3030
extern crate arena;
3131
#[macro_use]
32-
#[no_link]
33-
extern crate rustc_bitflags;
34-
#[macro_use]
3532
extern crate rustc;
3633

3734
use self::PatternBindingMode::*;
@@ -915,18 +912,9 @@ impl<'a> fmt::Debug for ModuleS<'a> {
915912
}
916913
}
917914

918-
bitflags! {
919-
#[derive(Debug)]
920-
flags DefModifiers: u8 {
921-
const IMPORTABLE = 1 << 1,
922-
const GLOB_IMPORTED = 1 << 3,
923-
}
924-
}
925-
926915
// Records a possibly-private value, type, or module definition.
927916
#[derive(Clone, Debug)]
928917
pub struct NameBinding<'a> {
929-
modifiers: DefModifiers,
930918
kind: NameBindingKind<'a>,
931919
span: Option<Span>,
932920
vis: ty::Visibility,
@@ -938,7 +926,7 @@ enum NameBindingKind<'a> {
938926
Module(Module<'a>),
939927
Import {
940928
binding: &'a NameBinding<'a>,
941-
id: NodeId,
929+
directive: &'a ImportDirective<'a>,
942930
// Some(error) if using this imported name causes the import to be a privacy error
943931
privacy_error: Option<Box<PrivacyError<'a>>>,
944932
},
@@ -950,7 +938,6 @@ struct PrivacyError<'a>(Span, Name, &'a NameBinding<'a>);
950938
impl<'a> NameBinding<'a> {
951939
fn create_from_module(module: Module<'a>, span: Option<Span>) -> Self {
952940
NameBinding {
953-
modifiers: DefModifiers::IMPORTABLE,
954941
kind: NameBindingKind::Module(module),
955942
span: span,
956943
vis: module.vis,
@@ -973,10 +960,6 @@ impl<'a> NameBinding<'a> {
973960
}
974961
}
975962

976-
fn defined_with(&self, modifiers: DefModifiers) -> bool {
977-
self.modifiers.contains(modifiers)
978-
}
979-
980963
fn is_pseudo_public(&self) -> bool {
981964
self.pseudo_vis() == ty::Visibility::Public
982965
}
@@ -1003,6 +986,20 @@ impl<'a> NameBinding<'a> {
1003986
_ => false,
1004987
}
1005988
}
989+
990+
fn is_glob_import(&self) -> bool {
991+
match self.kind {
992+
NameBindingKind::Import { directive, .. } => directive.is_glob(),
993+
_ => false,
994+
}
995+
}
996+
997+
fn is_importable(&self) -> bool {
998+
match self.def().unwrap() {
999+
Def::AssociatedConst(..) | Def::Method(..) | Def::AssociatedTy(..) => false,
1000+
_ => true,
1001+
}
1002+
}
10061003
}
10071004

10081005
/// Interns the names of the primitive types.
@@ -1228,27 +1225,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12281225
self.used_crates.insert(krate);
12291226
}
12301227

1231-
let (import_id, privacy_error) = match binding.kind {
1232-
NameBindingKind::Import { id, ref privacy_error, .. } => (id, privacy_error),
1228+
let (directive, privacy_error) = match binding.kind {
1229+
NameBindingKind::Import { directive, ref privacy_error, .. } =>
1230+
(directive, privacy_error),
12331231
_ => return,
12341232
};
12351233

1236-
self.used_imports.insert((import_id, ns));
1234+
self.used_imports.insert((directive.id, ns));
12371235
if let Some(error) = privacy_error.as_ref() {
12381236
self.privacy_errors.push((**error).clone());
12391237
}
12401238

12411239
if !self.make_glob_map {
12421240
return;
12431241
}
1244-
if self.glob_map.contains_key(&import_id) {
1245-
self.glob_map.get_mut(&import_id).unwrap().insert(name);
1242+
if self.glob_map.contains_key(&directive.id) {
1243+
self.glob_map.get_mut(&directive.id).unwrap().insert(name);
12461244
return;
12471245
}
12481246

12491247
let mut new_set = FnvHashSet();
12501248
new_set.insert(name);
1251-
self.glob_map.insert(import_id, new_set);
1249+
self.glob_map.insert(directive.id, new_set);
12521250
}
12531251

12541252
fn get_trait_name(&self, did: DefId) -> Name {

0 commit comments

Comments
 (0)