Skip to content

Commit

Permalink
Rollup merge of #72750 - marmeladema:remove-as-local-node-id, r=petro…
Browse files Browse the repository at this point in the history
…chenkov

Remove remaining calls to `as_local_node_id`

Split out from #72552

cc #50928
  • Loading branch information
JohnTitor authored May 30, 2020
2 parents 990195f + 2f3dd7b commit c20a97d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 43 deletions.
11 changes: 7 additions & 4 deletions src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,12 +1321,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
.get_partial_res(bound_pred.bounded_ty.id)
.map(|d| d.base_res())
{
if let Some(node_id) =
self.resolver.definitions().as_local_node_id(def_id)
{
if let Some(def_id) = def_id.as_local() {
for param in &generics.params {
if let GenericParamKind::Type { .. } = param.kind {
if node_id == param.id {
if def_id
== self
.resolver
.definitions()
.local_def_id(param.id)
{
add_bounds
.entry(param.id)
.or_default()
Expand Down
11 changes: 0 additions & 11 deletions src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,6 @@ impl Definitions {
})
}

#[inline]
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
if let Some(def_id) = def_id.as_local() {
let node_id = self.def_id_to_node_id[def_id];
if node_id != ast::DUMMY_NODE_ID {
return Some(node_id);
}
}
None
}

#[inline]
pub fn as_local_hir_id(&self, def_id: LocalDefId) -> hir::HirId {
self.local_def_id_to_hir_id(def_id)
Expand Down
20 changes: 13 additions & 7 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_errors::{struct_span_err, Applicability};
use rustc_expand::base::SyntaxExtension;
use rustc_expand::expand::AstFragment;
use rustc_hir::def::{self, *};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_metadata::creader::LoadedMacro;
use rustc_middle::bug;
use rustc_middle::hir::exports::Export;
Expand Down Expand Up @@ -1150,31 +1150,37 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// Mark the given macro as unused unless its name starts with `_`.
// Macro uses will remove items from this set, and the remaining
// items will be reported as `unused_macros`.
fn insert_unused_macro(&mut self, ident: Ident, node_id: NodeId, span: Span) {
fn insert_unused_macro(
&mut self,
ident: Ident,
def_id: LocalDefId,
node_id: NodeId,
span: Span,
) {
if !ident.as_str().starts_with('_') {
self.r.unused_macros.insert(node_id, span);
self.r.unused_macros.insert(def_id, (node_id, span));
}
}

fn define_macro(&mut self, item: &ast::Item) -> MacroRulesScope<'a> {
let parent_scope = self.parent_scope;
let expansion = parent_scope.expansion;
let def_id = self.r.definitions.local_def_id(item.id);
let (ext, ident, span, macro_rules) = match &item.kind {
ItemKind::MacroDef(def) => {
let ext = Lrc::new(self.r.compile_macro(item, self.r.session.edition()));
(ext, item.ident, item.span, def.macro_rules)
}
ItemKind::Fn(..) => match Self::proc_macro_stub(item) {
Some((macro_kind, ident, span)) => {
self.r.proc_macro_stubs.insert(item.id);
self.r.proc_macro_stubs.insert(def_id);
(self.r.dummy_ext(macro_kind), ident, span, false)
}
None => return parent_scope.macro_rules,
},
_ => unreachable!(),
};

let def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id.to_def_id());
self.r.macro_map.insert(def_id.to_def_id(), ext);
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
Expand All @@ -1196,7 +1202,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.define(module, ident, MacroNS, (res, vis, span, expansion, IsMacroExport));
} else {
self.r.check_reserved_macro_name(ident, res);
self.insert_unused_macro(ident, item.id, span);
self.insert_unused_macro(ident, def_id, item.id, span);
}
MacroRulesScope::Binding(self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
parent_macro_rules_scope: parent_scope.macro_rules,
Expand All @@ -1214,7 +1220,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
_ => self.resolve_visibility(&item.vis),
};
if vis != ty::Visibility::Public {
self.insert_unused_macro(ident, item.id, span);
self.insert_unused_macro(ident, def_id, item.id, span);
}
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
self.parent_scope.macro_rules
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,11 +1621,10 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
let report_errors = |this: &mut Self, res: Option<Res>| {
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res);
let def_id = this.parent_scope.module.normal_ancestor_id;
let node_id = this.r.definitions.as_local_node_id(def_id).unwrap();
let better = res.is_some();
let suggestion =
if res.is_none() { this.report_missing_type_error(path) } else { None };
this.r.use_injections.push(UseError { err, candidates, node_id, better, suggestion });
this.r.use_injections.push(UseError { err, candidates, def_id, better, suggestion });
PartialRes::new(Res::Err)
};

Expand Down
40 changes: 25 additions & 15 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use rustc_ast::ast::{self, FloatTy, IntTy, NodeId, UintTy};
use rustc_ast::ast::{Crate, CRATE_NODE_ID};
use rustc_ast::ast::{ItemKind, Path};
use rustc_ast::attr;
use rustc_ast::node_id::{NodeMap, NodeSet};
use rustc_ast::node_id::NodeMap;
use rustc_ast::unwrap_or;
use rustc_ast::visit::{self, Visitor};
use rustc_ast_pretty::pprust;
Expand Down Expand Up @@ -253,21 +253,31 @@ impl<'a> From<&'a ast::PathSegment> for Segment {
}
}

struct UsePlacementFinder {
target_module: NodeId,
struct UsePlacementFinder<'d> {
definitions: &'d Definitions,
target_module: LocalDefId,
span: Option<Span>,
found_use: bool,
}

impl UsePlacementFinder {
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, bool) {
let mut finder = UsePlacementFinder { target_module, span: None, found_use: false };
visit::walk_crate(&mut finder, krate);
(finder.span, finder.found_use)
impl<'d> UsePlacementFinder<'d> {
fn check(
definitions: &'d Definitions,
krate: &Crate,
target_module: DefId,
) -> (Option<Span>, bool) {
if let Some(target_module) = target_module.as_local() {
let mut finder =
UsePlacementFinder { definitions, target_module, span: None, found_use: false };
visit::walk_crate(&mut finder, krate);
(finder.span, finder.found_use)
} else {
(None, false)
}
}
}

impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
impl<'tcx, 'd> Visitor<'tcx> for UsePlacementFinder<'d> {
fn visit_mod(
&mut self,
module: &'tcx ast::Mod,
Expand All @@ -278,7 +288,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
if self.span.is_some() {
return;
}
if node_id != self.target_module {
if self.definitions.local_def_id(node_id) != self.target_module {
visit::walk_mod(self, module);
return;
}
Expand Down Expand Up @@ -611,7 +621,7 @@ struct UseError<'a> {
/// Attach `use` statements for these candidates.
candidates: Vec<ImportSuggestion>,
/// The `NodeId` of the module to place the use-statements in.
node_id: NodeId,
def_id: DefId,
/// Whether the diagnostic should state that it's "better".
better: bool,
/// Extra free form suggestion. Currently used to suggest new type parameter.
Expand Down Expand Up @@ -926,8 +936,8 @@ pub struct Resolver<'a> {
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>,
ast_transform_scopes: FxHashMap<ExpnId, Module<'a>>,
unused_macros: NodeMap<Span>,
proc_macro_stubs: NodeSet,
unused_macros: FxHashMap<LocalDefId, (NodeId, Span)>,
proc_macro_stubs: FxHashSet<LocalDefId>,
/// Traces collected during macro resolution and validated when it's complete.
single_segment_macro_resolutions:
Vec<(Ident, MacroKind, ParentScope<'a>, Option<&'a NameBinding<'a>>)>,
Expand Down Expand Up @@ -2567,10 +2577,10 @@ impl<'a> Resolver<'a> {
}

fn report_with_use_injections(&mut self, krate: &Crate) {
for UseError { mut err, candidates, node_id, better, suggestion } in
for UseError { mut err, candidates, def_id, better, suggestion } in
self.use_injections.drain(..)
{
let (span, found_use) = UsePlacementFinder::check(krate, node_id);
let (span, found_use) = UsePlacementFinder::check(&self.definitions, krate, def_id);
if !candidates.is_empty() {
diagnostics::show_candidates(&mut err, span, &candidates, better, found_use);
} else if let Some((span, msg, sugg, appl)) = suggestion {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl<'a> base::Resolver for Resolver<'a> {
}

fn check_unused_macros(&mut self) {
for (&node_id, &span) in self.unused_macros.iter() {
for (_, &(node_id, span)) in self.unused_macros.iter() {
self.lint_buffer.buffer_lint(UNUSED_MACROS, node_id, span, "unused macro definition");
}
}
Expand Down Expand Up @@ -416,9 +416,9 @@ impl<'a> Resolver<'a> {

match res {
Res::Def(DefKind::Macro(_), def_id) => {
if let Some(node_id) = self.definitions.as_local_node_id(def_id) {
self.unused_macros.remove(&node_id);
if self.proc_macro_stubs.contains(&node_id) {
if let Some(def_id) = def_id.as_local() {
self.unused_macros.remove(&def_id);
if self.proc_macro_stubs.contains(&def_id) {
self.session.span_err(
path.span,
"can't use a procedural macro from the same crate that defines it",
Expand Down

0 comments on commit c20a97d

Please sign in to comment.