-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Resolve: refactor define
into define_local
and define_extern
#143884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LorrensP-2158466
wants to merge
10
commits into
rust-lang:master
Choose a base branch
from
LorrensP-2158466:resolve-split-define
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−56
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7f600c3
split up define into define_extern and define_local
LorrensP-2158466 46f991a
cleanup old stuff
LorrensP-2158466 74ade71
address review
LorrensP-2158466 1035597
check that vis is extern aswell
LorrensP-2158466 0d19d32
split `for_each_child` into immutable and mutable versions
LorrensP-2158466 c9c1d6f
some cleanup/rename
LorrensP-2158466 158bd59
account for `(non)_glob_binding` change
LorrensP-2158466 d35d5ec
spell check
LorrensP-2158466 05925fb
update define_extern
LorrensP-2158466 a9d94be
address review
LorrensP-2158466 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,9 +23,9 @@ use rustc_hir::def::{self, *}; | |||||
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId}; | ||||||
use rustc_index::bit_set::DenseBitSet; | ||||||
use rustc_metadata::creader::LoadedMacro; | ||||||
use rustc_middle::bug; | ||||||
use rustc_middle::metadata::ModChild; | ||||||
use rustc_middle::ty::{Feed, Visibility}; | ||||||
use rustc_middle::{bug, span_bug}; | ||||||
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind}; | ||||||
use rustc_span::{Ident, Span, Symbol, kw, sym}; | ||||||
use thin_vec::ThinVec; | ||||||
|
@@ -46,30 +46,59 @@ type Res = def::Res<NodeId>; | |||||
impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | ||||||
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined; | ||||||
/// otherwise, reports an error. | ||||||
pub(crate) fn define_binding( | ||||||
pub(crate) fn define_binding_local( | ||||||
&mut self, | ||||||
parent: Module<'ra>, | ||||||
ident: Ident, | ||||||
ns: Namespace, | ||||||
binding: NameBinding<'ra>, | ||||||
) { | ||||||
if let Err(old_binding) = self.try_define(parent, ident, ns, binding, false) { | ||||||
if let Err(old_binding) = self.try_define_local(parent, ident, ns, binding, false) { | ||||||
self.report_conflict(parent, ident, ns, old_binding, binding); | ||||||
} | ||||||
} | ||||||
|
||||||
fn define( | ||||||
fn define_local( | ||||||
&mut self, | ||||||
parent: Module<'ra>, | ||||||
ident: Ident, | ||||||
ns: Namespace, | ||||||
res: Res, | ||||||
vis: Visibility<impl Into<DefId>>, | ||||||
vis: Visibility, // Implicitly local | ||||||
span: Span, | ||||||
expn_id: LocalExpnId, | ||||||
) { | ||||||
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id); | ||||||
self.define_binding(parent, ident, ns, binding) | ||||||
self.define_binding_local(parent, ident, ns, binding); | ||||||
} | ||||||
|
||||||
fn define_extern( | ||||||
&self, | ||||||
parent: Module<'ra>, | ||||||
ident: Ident, | ||||||
ns: Namespace, | ||||||
res: Res, | ||||||
vis: Visibility<DefId>, | ||||||
span: Span, | ||||||
expn_id: LocalExpnId, | ||||||
) { | ||||||
assert!(!parent.is_local()); | ||||||
assert!(res.opt_def_id().is_none_or(|def_id| !def_id.is_local())); | ||||||
vis.map_id(|def_id| { | ||||||
assert!(!def_id.is_local()); | ||||||
}); | ||||||
let binding = self.arenas.new_res_binding(res, vis, span, expn_id); | ||||||
// Even if underscore names cannot be looked up, we still need to add them to modules, | ||||||
// because they can be fetched by glob imports from those modules, and bring traits | ||||||
// into scope both directly and through glob imports. | ||||||
let key = BindingKey::new_disambiguated(ident, ns, || { | ||||||
parent.underscore_disambiguator.update(|d| d + 1); | ||||||
parent.underscore_disambiguator.get() | ||||||
}); | ||||||
let resolution = &mut *self.resolution(parent, key).borrow_mut(); | ||||||
if resolution.non_glob_binding.replace(binding).is_some() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
span_bug!(span, "an external binding was already defined"); | ||||||
} | ||||||
} | ||||||
|
||||||
/// Walks up the tree of definitions starting at `def_id`, | ||||||
|
@@ -192,7 +221,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |||||
visitor.parent_scope.macro_rules | ||||||
} | ||||||
|
||||||
pub(crate) fn build_reduced_graph_external(&mut self, module: Module<'ra>) { | ||||||
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) { | ||||||
for child in self.tcx.module_children(module.def_id()) { | ||||||
let parent_scope = ParentScope::module(module, self); | ||||||
self.build_reduced_graph_for_external_crate_res(child, parent_scope) | ||||||
|
@@ -201,7 +230,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |||||
|
||||||
/// Builds the reduced graph for a single item in an external crate. | ||||||
fn build_reduced_graph_for_external_crate_res( | ||||||
&mut self, | ||||||
&self, | ||||||
child: &ModChild, | ||||||
parent_scope: ParentScope<'ra>, | ||||||
) { | ||||||
|
@@ -232,7 +261,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |||||
_, | ||||||
) | ||||||
| Res::PrimTy(..) | ||||||
| Res::ToolMod => self.define(parent, ident, TypeNS, res, vis, span, expansion), | ||||||
| Res::ToolMod => self.define_extern(parent, ident, TypeNS, res, vis, span, expansion), | ||||||
Res::Def( | ||||||
DefKind::Fn | ||||||
| DefKind::AssocFn | ||||||
|
@@ -241,9 +270,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |||||
| DefKind::AssocConst | ||||||
| DefKind::Ctor(..), | ||||||
_, | ||||||
) => self.define(parent, ident, ValueNS, res, vis, span, expansion), | ||||||
) => self.define_extern(parent, ident, ValueNS, res, vis, span, expansion), | ||||||
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => { | ||||||
self.define(parent, ident, MacroNS, res, vis, span, expansion) | ||||||
self.define_extern(parent, ident, MacroNS, res, vis, span, expansion) | ||||||
} | ||||||
Res::Def( | ||||||
DefKind::TyParam | ||||||
|
@@ -709,7 +738,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
let expansion = parent_scope.expansion; | ||||||
|
||||||
// Define a name in the type namespace if it is not anonymous. | ||||||
self.r.define(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion); | ||||||
self.r.define_local(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion); | ||||||
self.r.feed_visibility(feed, adt_vis); | ||||||
let def_id = feed.key(); | ||||||
|
||||||
|
@@ -761,7 +790,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
} | ||||||
|
||||||
ItemKind::Mod(_, ident, ref mod_kind) => { | ||||||
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion); | ||||||
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion); | ||||||
|
||||||
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind { | ||||||
self.r.mods_with_parse_errors.insert(def_id); | ||||||
|
@@ -780,10 +809,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
ItemKind::Const(box ConstItem { ident, .. }) | ||||||
| ItemKind::Delegation(box Delegation { ident, .. }) | ||||||
| ItemKind::Static(box StaticItem { ident, .. }) => { | ||||||
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion); | ||||||
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion); | ||||||
} | ||||||
ItemKind::Fn(box Fn { ident, .. }) => { | ||||||
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion); | ||||||
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion); | ||||||
|
||||||
// Functions introducing procedural macros reserve a slot | ||||||
// in the macro namespace as well (see #52225). | ||||||
|
@@ -792,11 +821,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
|
||||||
// These items live in the type namespace. | ||||||
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => { | ||||||
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion); | ||||||
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion); | ||||||
} | ||||||
|
||||||
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => { | ||||||
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion); | ||||||
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion); | ||||||
|
||||||
self.parent_scope.module = self.r.new_local_module( | ||||||
Some(parent), | ||||||
|
@@ -848,7 +877,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
let feed = self.r.feed(ctor_node_id); | ||||||
let ctor_def_id = feed.key(); | ||||||
let ctor_res = self.res(ctor_def_id); | ||||||
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion); | ||||||
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion); | ||||||
self.r.feed_visibility(feed, ctor_vis); | ||||||
// We need the field visibility spans also for the constructor for E0603. | ||||||
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields()); | ||||||
|
@@ -972,7 +1001,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
); | ||||||
} | ||||||
} | ||||||
self.r.define_binding(parent, ident, TypeNS, imported_binding); | ||||||
self.r.define_binding_local(parent, ident, TypeNS, imported_binding); | ||||||
} | ||||||
|
||||||
/// Constructs the reduced graph for one foreign item. | ||||||
|
@@ -989,7 +1018,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
let parent = self.parent_scope.module; | ||||||
let expansion = self.parent_scope.expansion; | ||||||
let vis = self.resolve_visibility(&item.vis); | ||||||
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion); | ||||||
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion); | ||||||
self.r.feed_visibility(feed, vis); | ||||||
} | ||||||
|
||||||
|
@@ -1072,7 +1101,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
if let Some(span) = import_all { | ||||||
let import = macro_use_import(self, span, false); | ||||||
self.r.potentially_unused_imports.push(import); | ||||||
module.for_each_child(self, |this, ident, ns, binding| { | ||||||
module.for_each_child_mut(self, |this, ident, ns, binding| { | ||||||
if ns == MacroNS { | ||||||
let import = if this.r.is_accessible_from(binding.vis, this.parent_scope.module) | ||||||
{ | ||||||
|
@@ -1237,7 +1266,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
}); | ||||||
self.r.import_use_map.insert(import, Used::Other); | ||||||
let import_binding = self.r.import(binding, import); | ||||||
self.r.define_binding(self.r.graph_root, ident, MacroNS, import_binding); | ||||||
self.r.define_binding_local(self.r.graph_root, ident, MacroNS, import_binding); | ||||||
} else { | ||||||
self.r.check_reserved_macro_name(ident, res); | ||||||
self.insert_unused_macro(ident, def_id, item.id); | ||||||
|
@@ -1265,7 +1294,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
if !vis.is_public() { | ||||||
self.insert_unused_macro(ident, def_id, item.id); | ||||||
} | ||||||
self.r.define(module, ident, MacroNS, res, vis, span, expansion); | ||||||
self.r.define_local(module, ident, MacroNS, res, vis, span, expansion); | ||||||
self.r.feed_visibility(feed, vis); | ||||||
self.parent_scope.macro_rules | ||||||
} | ||||||
|
@@ -1401,7 +1430,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
if ctxt == AssocCtxt::Trait { | ||||||
let parent = self.parent_scope.module; | ||||||
let expansion = self.parent_scope.expansion; | ||||||
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion); | ||||||
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion); | ||||||
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob) | ||||||
&& ident.name != kw::Underscore | ||||||
{ | ||||||
|
@@ -1489,7 +1518,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
let feed = self.r.feed(variant.id); | ||||||
let def_id = feed.key(); | ||||||
let vis = self.resolve_visibility(&variant.vis); | ||||||
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id); | ||||||
self.r.define_local(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id); | ||||||
self.r.feed_visibility(feed, vis); | ||||||
|
||||||
// If the variant is marked as non_exhaustive then lower the visibility to within the crate. | ||||||
|
@@ -1505,7 +1534,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> { | |||||
let feed = self.r.feed(ctor_node_id); | ||||||
let ctor_def_id = feed.key(); | ||||||
let ctor_res = self.res(ctor_def_id); | ||||||
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id); | ||||||
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id); | ||||||
self.r.feed_visibility(feed, ctor_vis); | ||||||
} | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is probably not very useful.