Skip to content

Commit 2cb9207

Browse files
committed
resolve: Move some code around
Avoid using dummy spans for some external items with available spans
1 parent 3ee614b commit 2cb9207

8 files changed

+121
-102
lines changed

src/librustc_resolve/build_reduced_graph.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ impl<'a> Resolver<'a> {
642642
// This is only a guess, two equivalent idents may incorrectly get different gensyms here.
643643
let ident = ident.gensym_if_underscore();
644644
let expansion = ExpnId::root(); // FIXME(jseyfried) intercrate hygiene
645+
// Record primary definitions.
645646
match res {
646647
Res::Def(kind @ DefKind::Mod, def_id)
647648
| Res::Def(kind @ DefKind::Enum, def_id)
@@ -651,53 +652,52 @@ impl<'a> Resolver<'a> {
651652
def_id,
652653
expansion,
653654
span);
654-
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
655+
self.define(parent, ident, TypeNS, (module, vis, span, expansion));
655656
}
656-
Res::Def(DefKind::Variant, _)
657+
Res::Def(DefKind::Struct, _)
658+
| Res::Def(DefKind::Union, _)
659+
| Res::Def(DefKind::Variant, _)
657660
| Res::Def(DefKind::TyAlias, _)
658661
| Res::Def(DefKind::ForeignTy, _)
659662
| Res::Def(DefKind::OpaqueTy, _)
660663
| Res::Def(DefKind::TraitAlias, _)
661664
| Res::Def(DefKind::AssocTy, _)
662-
| Res::Def(DefKind::AssocExistential, _)
665+
| Res::Def(DefKind::AssocOpaqueTy, _)
663666
| Res::PrimTy(..)
664-
| Res::ToolMod => {
665-
self.define(parent, ident, TypeNS, (res, vis, DUMMY_SP, expansion));
666-
}
667+
| Res::ToolMod =>
668+
self.define(parent, ident, TypeNS, (res, vis, span, expansion)),
667669
Res::Def(DefKind::Fn, _)
670+
| Res::Def(DefKind::Method, _)
668671
| Res::Def(DefKind::Static, _)
669672
| Res::Def(DefKind::Const, _)
670673
| Res::Def(DefKind::AssocConst, _)
671-
| Res::Def(DefKind::Ctor(CtorOf::Variant, ..), _) => {
672-
self.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
673-
}
674-
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), def_id) => {
675-
self.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
676-
677-
if let Some(struct_def_id) =
678-
self.cstore.def_key(def_id).parent
679-
.map(|index| DefId { krate: def_id.krate, index: index }) {
680-
self.struct_constructors.insert(struct_def_id, (res, vis));
681-
}
674+
| Res::Def(DefKind::Ctor(..), _) =>
675+
self.define(parent, ident, ValueNS, (res, vis, span, expansion)),
676+
Res::Def(DefKind::Macro(..), _)
677+
| Res::NonMacroAttr(..) =>
678+
self.define(parent, ident, MacroNS, (res, vis, span, expansion)),
679+
Res::Def(DefKind::TyParam, _) | Res::Def(DefKind::ConstParam, _)
680+
| Res::Local(..) | Res::SelfTy(..) | Res::SelfCtor(..) | Res::Err =>
681+
bug!("unexpected resolution: {:?}", res)
682+
}
683+
// Record some extra data for better diagnostics.
684+
match res {
685+
Res::Def(DefKind::Struct, def_id) | Res::Def(DefKind::Union, def_id) => {
686+
let field_names = self.cstore.struct_field_names_untracked(def_id);
687+
self.insert_field_names(def_id, field_names);
682688
}
683689
Res::Def(DefKind::Method, def_id) => {
684-
self.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
685-
686690
if self.cstore.associated_item_cloned_untracked(def_id).method_has_self_argument {
687691
self.has_self.insert(def_id);
688692
}
689693
}
690-
Res::Def(DefKind::Struct, def_id) | Res::Def(DefKind::Union, def_id) => {
691-
self.define(parent, ident, TypeNS, (res, vis, DUMMY_SP, expansion));
692-
693-
// Record field names for error reporting.
694-
let field_names = self.cstore.struct_field_names_untracked(def_id);
695-
self.insert_field_names(def_id, field_names);
696-
}
697-
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
698-
self.define(parent, ident, MacroNS, (res, vis, DUMMY_SP, expansion));
694+
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), def_id) => {
695+
let parent = self.cstore.def_key(def_id).parent;
696+
if let Some(struct_def_id) = parent.map(|index| DefId { index, ..def_id }) {
697+
self.struct_constructors.insert(struct_def_id, (res, vis));
698+
}
699699
}
700-
_ => bug!("unexpected resolution: {:?}", res)
700+
_ => {}
701701
}
702702
}
703703

@@ -837,7 +837,7 @@ impl<'a> Resolver<'a> {
837837
if let Some(span) = import_all {
838838
let directive = macro_use_directive(span);
839839
self.potentially_unused_imports.push(directive);
840-
module.for_each_child(self, |this, ident, ns, binding| if ns == MacroNS {
840+
self.for_each_child(module, |this, ident, ns, binding| if ns == MacroNS {
841841
let imported_binding = this.import(binding, directive);
842842
this.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
843843
});

src/librustc_resolve/diagnostics.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,23 @@ fn add_typo_suggestion(
6565
false
6666
}
6767

68-
fn add_module_candidates<'a>(
69-
resolver: &mut Resolver<'a>,
70-
module: Module<'a>,
71-
names: &mut Vec<TypoSuggestion>,
72-
filter_fn: &impl Fn(Res) -> bool,
73-
) {
74-
for (&(ident, _), resolution) in resolver.resolutions(module).borrow().iter() {
75-
if let Some(binding) = resolution.borrow().binding {
76-
let res = binding.res();
77-
if filter_fn(res) {
78-
names.push(TypoSuggestion::from_res(ident.name, res));
68+
impl<'a> Resolver<'a> {
69+
fn add_module_candidates(
70+
&mut self,
71+
module: Module<'a>,
72+
names: &mut Vec<TypoSuggestion>,
73+
filter_fn: &impl Fn(Res) -> bool,
74+
) {
75+
for (&(ident, _), resolution) in self.resolutions(module).borrow().iter() {
76+
if let Some(binding) = resolution.borrow().binding {
77+
let res = binding.res();
78+
if filter_fn(res) {
79+
names.push(TypoSuggestion::from_res(ident.name, res));
80+
}
7981
}
8082
}
8183
}
82-
}
8384

84-
impl<'a> Resolver<'a> {
8585
/// Handles error reporting for `smart_resolve_path_fragment` function.
8686
/// Creates base error and amends it with one short label and possibly some longer helps/notes.
8787
pub(crate) fn smart_resolve_report_errors(
@@ -596,10 +596,10 @@ impl<'a> Resolver<'a> {
596596
Scope::CrateRoot => {
597597
let root_ident = Ident::new(kw::PathRoot, ident.span);
598598
let root_module = this.resolve_crate_root(root_ident);
599-
add_module_candidates(this, root_module, &mut suggestions, filter_fn);
599+
this.add_module_candidates(root_module, &mut suggestions, filter_fn);
600600
}
601601
Scope::Module(module) => {
602-
add_module_candidates(this, module, &mut suggestions, filter_fn);
602+
this.add_module_candidates(module, &mut suggestions, filter_fn);
603603
}
604604
Scope::MacroUsePrelude => {
605605
suggestions.extend(this.macro_use_prelude.iter().filter_map(|(name, binding)| {
@@ -647,7 +647,7 @@ impl<'a> Resolver<'a> {
647647
Scope::StdLibPrelude => {
648648
if let Some(prelude) = this.prelude {
649649
let mut tmp_suggestions = Vec::new();
650-
add_module_candidates(this, prelude, &mut tmp_suggestions, filter_fn);
650+
this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn);
651651
suggestions.extend(tmp_suggestions.into_iter().filter(|s| {
652652
use_prelude || this.is_builtin_macro(s.res.opt_def_id())
653653
}));
@@ -709,7 +709,7 @@ impl<'a> Resolver<'a> {
709709
// Items in scope
710710
if let RibKind::ModuleRibKind(module) = rib.kind {
711711
// Items from this module
712-
add_module_candidates(self, module, &mut names, &filter_fn);
712+
self.add_module_candidates(module, &mut names, &filter_fn);
713713

714714
if let ModuleKind::Block(..) = module.kind {
715715
// We can see through blocks
@@ -737,7 +737,7 @@ impl<'a> Resolver<'a> {
737737
}));
738738

739739
if let Some(prelude) = self.prelude {
740-
add_module_candidates(self, prelude, &mut names, &filter_fn);
740+
self.add_module_candidates(prelude, &mut names, &filter_fn);
741741
}
742742
}
743743
break;
@@ -759,7 +759,7 @@ impl<'a> Resolver<'a> {
759759
mod_path, Some(TypeNS), false, span, CrateLint::No
760760
) {
761761
if let ModuleOrUniformRoot::Module(module) = module {
762-
add_module_candidates(self, module, &mut names, &filter_fn);
762+
self.add_module_candidates(module, &mut names, &filter_fn);
763763
}
764764
}
765765
}
@@ -799,7 +799,7 @@ impl<'a> Resolver<'a> {
799799
in_module_is_extern)) = worklist.pop() {
800800
// We have to visit module children in deterministic order to avoid
801801
// instabilities in reported imports (#43552).
802-
in_module.for_each_child_stable(self, |this, ident, ns, name_binding| {
802+
self.for_each_child_stable(in_module, |this, ident, ns, name_binding| {
803803
// avoid imports entirely
804804
if name_binding.is_import() && !name_binding.is_extern_crate() { return; }
805805
// avoid non-importable candidates as well
@@ -911,7 +911,7 @@ impl<'a> Resolver<'a> {
911911
// abort if the module is already found
912912
if result.is_some() { break; }
913913

914-
in_module.for_each_child_stable(self, |_, ident, _, name_binding| {
914+
self.for_each_child_stable(in_module, |_, ident, _, name_binding| {
915915
// abort if the module is already found or if name_binding is private external
916916
if result.is_some() || !name_binding.vis.is_visible_locally() {
917917
return
@@ -943,7 +943,7 @@ impl<'a> Resolver<'a> {
943943
fn collect_enum_variants(&mut self, def_id: DefId) -> Option<Vec<Path>> {
944944
self.find_module(def_id).map(|(enum_module, enum_import_suggestion)| {
945945
let mut variants = Vec::new();
946-
enum_module.for_each_child_stable(self, |_, ident, _, name_binding| {
946+
self.for_each_child_stable(enum_module, |_, ident, _, name_binding| {
947947
if let Res::Def(DefKind::Variant, _) = name_binding.res() {
948948
let mut segms = enum_import_suggestion.path.segments.clone();
949949
segms.push(ast::PathSegment::from_ident(ident));

src/librustc_resolve/lib.rs

+38-22
Original file line numberDiff line numberDiff line change
@@ -1221,25 +1221,6 @@ impl<'a> ModuleData<'a> {
12211221
}
12221222
}
12231223

1224-
fn for_each_child<F>(&'a self, resolver: &mut Resolver<'a>, mut f: F)
1225-
where F: FnMut(&mut Resolver<'a>, Ident, Namespace, &'a NameBinding<'a>)
1226-
{
1227-
for (&(ident, ns), name_resolution) in resolver.resolutions(self).borrow().iter() {
1228-
name_resolution.borrow().binding.map(|binding| f(resolver, ident, ns, binding));
1229-
}
1230-
}
1231-
1232-
fn for_each_child_stable<F>(&'a self, resolver: &mut Resolver<'a>, mut f: F)
1233-
where F: FnMut(&mut Resolver<'a>, Ident, Namespace, &'a NameBinding<'a>)
1234-
{
1235-
let resolutions = resolver.resolutions(self).borrow();
1236-
let mut resolutions = resolutions.iter().collect::<Vec<_>>();
1237-
resolutions.sort_by_cached_key(|&(&(ident, ns), _)| (ident.as_str(), ns));
1238-
for &(&(ident, ns), &resolution) in resolutions.iter() {
1239-
resolution.borrow().binding.map(|binding| f(resolver, ident, ns, binding));
1240-
}
1241-
}
1242-
12431224
fn res(&self) -> Option<Res> {
12441225
match self.kind {
12451226
ModuleKind::Def(kind, def_id, _) => Some(Res::Def(kind, def_id)),
@@ -1904,9 +1885,7 @@ impl<'a> Resolver<'a> {
19041885
seg.id = self.session.next_node_id();
19051886
seg
19061887
}
1907-
}
19081888

1909-
impl<'a> Resolver<'a> {
19101889
pub fn new(session: &'a Session,
19111890
cstore: &'a CStore,
19121891
krate: &Crate,
@@ -2116,6 +2095,43 @@ impl<'a> Resolver<'a> {
21162095
self.arenas.alloc_module(module)
21172096
}
21182097

2098+
fn resolutions(&mut self, module: Module<'a>) -> &'a Resolutions<'a> {
2099+
if module.populate_on_access.get() {
2100+
module.populate_on_access.set(false);
2101+
let def_id = module.def_id().expect("unpopulated module without a def-id");
2102+
for child in self.cstore.item_children_untracked(def_id, self.session) {
2103+
let child = child.map_id(|_| panic!("unexpected id"));
2104+
self.build_reduced_graph_for_external_crate_res(module, child);
2105+
}
2106+
}
2107+
&module.lazy_resolutions
2108+
}
2109+
2110+
fn resolution(&mut self, module: Module<'a>, ident: Ident, ns: Namespace)
2111+
-> &'a RefCell<NameResolution<'a>> {
2112+
*self.resolutions(module).borrow_mut().entry((ident.modern(), ns))
2113+
.or_insert_with(|| self.arenas.alloc_name_resolution())
2114+
}
2115+
2116+
fn for_each_child<F>(&mut self, module: Module<'a>, mut f: F)
2117+
where F: FnMut(&mut Resolver<'a>, Ident, Namespace, &'a NameBinding<'a>)
2118+
{
2119+
for (&(ident, ns), name_resolution) in self.resolutions(module).borrow().iter() {
2120+
name_resolution.borrow().binding.map(|binding| f(self, ident, ns, binding));
2121+
}
2122+
}
2123+
2124+
fn for_each_child_stable<F>(&mut self, module: Module<'a>, mut f: F)
2125+
where F: FnMut(&mut Resolver<'a>, Ident, Namespace, &'a NameBinding<'a>)
2126+
{
2127+
let resolutions = self.resolutions(module).borrow();
2128+
let mut resolutions = resolutions.iter().collect::<Vec<_>>();
2129+
resolutions.sort_by_cached_key(|&(&(ident, ns), _)| (ident.as_str(), ns));
2130+
for &(&(ident, ns), &resolution) in resolutions.iter() {
2131+
resolution.borrow().binding.map(|binding| f(self, ident, ns, binding));
2132+
}
2133+
}
2134+
21192135
fn record_use(&mut self, ident: Ident, ns: Namespace,
21202136
used_binding: &'a NameBinding<'a>, is_lexical_scope: bool) {
21212137
if let Some((b2, kind)) = used_binding.ambiguity {
@@ -4531,7 +4547,7 @@ impl<'a> Resolver<'a> {
45314547
let mut traits = module.traits.borrow_mut();
45324548
if traits.is_none() {
45334549
let mut collected_traits = Vec::new();
4534-
module.for_each_child(self, |_, name, ns, binding| {
4550+
self.for_each_child(module, |_, name, ns, binding| {
45354551
if ns != TypeNS { return }
45364552
match binding.res() {
45374553
Res::Def(DefKind::Trait, _) |

src/librustc_resolve/resolve_imports.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{CrateLint, Module, ModuleOrUniformRoot, PerNS, ScopeSet, ParentScope
55
use crate::Determinacy::{self, *};
66
use crate::Namespace::{self, TypeNS, MacroNS};
77
use crate::{NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyError};
8-
use crate::{Resolutions, Resolver, Segment};
8+
use crate::{Resolver, Segment};
99
use crate::{names_to_string, module_to_string};
1010
use crate::{resolve_error, ResolutionError};
1111
use crate::ModuleKind;
@@ -36,7 +36,7 @@ use syntax_pos::{MultiSpan, Span};
3636

3737
use log::*;
3838

39-
use std::cell::{Cell, RefCell};
39+
use std::cell::Cell;
4040
use std::{mem, ptr};
4141

4242
type Res = def::Res<NodeId>;
@@ -156,24 +156,6 @@ impl<'a> NameResolution<'a> {
156156
}
157157

158158
impl<'a> Resolver<'a> {
159-
crate fn resolutions(&mut self, module: Module<'a>) -> &'a Resolutions<'a> {
160-
if module.populate_on_access.get() {
161-
module.populate_on_access.set(false);
162-
let def_id = module.def_id().expect("unpopulated module without a def-id");
163-
for child in self.cstore.item_children_untracked(def_id, self.session) {
164-
let child = child.map_id(|_| panic!("unexpected id"));
165-
self.build_reduced_graph_for_external_crate_res(module, child);
166-
}
167-
}
168-
&module.lazy_resolutions
169-
}
170-
171-
fn resolution(&mut self, module: Module<'a>, ident: Ident, ns: Namespace)
172-
-> &'a RefCell<NameResolution<'a>> {
173-
*self.resolutions(module).borrow_mut().entry((ident.modern(), ns))
174-
.or_insert_with(|| self.arenas.alloc_name_resolution())
175-
}
176-
177159
crate fn resolve_ident_in_module_unadjusted(
178160
&mut self,
179161
module: ModuleOrUniformRoot<'a>,

src/test/ui/imports/extern-prelude-extern-crate-restricted-shadowing.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ error[E0659]: `Vec` is ambiguous (macro-expanded name vs less macro-expanded nam
1313
LL | Vec::panic!();
1414
| ^^^ ambiguous name
1515
|
16-
= note: `Vec` could refer to a struct from prelude
17-
note: `Vec` could also refer to the extern crate imported here
16+
note: `Vec` could refer to the extern crate imported here
1817
--> $DIR/extern-prelude-extern-crate-restricted-shadowing.rs:5:9
1918
|
2019
LL | extern crate std as Vec;
2120
| ^^^^^^^^^^^^^^^^^^^^^^^^
2221
...
2322
LL | define_vec!();
2423
| -------------- in this macro invocation
24+
note: `Vec` could also refer to the struct defined here
25+
--> $SRC_DIR/libstd/prelude/v1.rs:LL:COL
26+
|
27+
LL | pub use crate::vec::Vec;
28+
| ^^^^^^^^^^^^^^^
2529

2630
error: aborting due to 2 previous errors
2731

0 commit comments

Comments
 (0)