Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 344a42e

Browse files
committed
resolve: Move get_traits_in_module_containing_item to Resolver
1 parent e674422 commit 344a42e

File tree

2 files changed

+93
-81
lines changed

2 files changed

+93
-81
lines changed

src/librustc_resolve/late.rs

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use RibKind::*;
99

1010
use crate::{path_names_to_string, BindingError, CrateLint, LexicalScopeBinding};
11-
use crate::{Module, ModuleOrUniformRoot, NameBindingKind, ParentScope, PathResult};
11+
use crate::{Module, ModuleOrUniformRoot, ParentScope, PathResult};
1212
use crate::{ResolutionError, Resolver, Segment, UseError};
1313

1414
use rustc_ast::ptr::P;
@@ -24,7 +24,6 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
2424
use rustc_hir::TraitCandidate;
2525
use rustc_middle::{bug, span_bug};
2626
use rustc_session::lint;
27-
use rustc_span::def_id::LocalDefId;
2827
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2928
use rustc_span::Span;
3029
use smallvec::{smallvec, SmallVec};
@@ -2342,95 +2341,31 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
23422341
ident.span = ident.span.normalize_to_macros_2_0();
23432342
let mut search_module = self.parent_scope.module;
23442343
loop {
2345-
self.get_traits_in_module_containing_item(ident, ns, search_module, &mut found_traits);
2344+
self.r.get_traits_in_module_containing_item(
2345+
ident,
2346+
ns,
2347+
search_module,
2348+
&mut found_traits,
2349+
&self.parent_scope,
2350+
);
23462351
search_module =
23472352
unwrap_or!(self.r.hygienic_lexical_parent(search_module, &mut ident.span), break);
23482353
}
23492354

23502355
if let Some(prelude) = self.r.prelude {
23512356
if !search_module.no_implicit_prelude {
2352-
self.get_traits_in_module_containing_item(ident, ns, prelude, &mut found_traits);
2357+
self.r.get_traits_in_module_containing_item(
2358+
ident,
2359+
ns,
2360+
prelude,
2361+
&mut found_traits,
2362+
&self.parent_scope,
2363+
);
23532364
}
23542365
}
23552366

23562367
found_traits
23572368
}
2358-
2359-
fn get_traits_in_module_containing_item(
2360-
&mut self,
2361-
ident: Ident,
2362-
ns: Namespace,
2363-
module: Module<'a>,
2364-
found_traits: &mut Vec<TraitCandidate>,
2365-
) {
2366-
assert!(ns == TypeNS || ns == ValueNS);
2367-
let mut traits = module.traits.borrow_mut();
2368-
if traits.is_none() {
2369-
let mut collected_traits = Vec::new();
2370-
module.for_each_child(self.r, |_, name, ns, binding| {
2371-
if ns != TypeNS {
2372-
return;
2373-
}
2374-
match binding.res() {
2375-
Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
2376-
collected_traits.push((name, binding))
2377-
}
2378-
_ => (),
2379-
}
2380-
});
2381-
*traits = Some(collected_traits.into_boxed_slice());
2382-
}
2383-
2384-
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
2385-
// Traits have pseudo-modules that can be used to search for the given ident.
2386-
if let Some(module) = binding.module() {
2387-
let mut ident = ident;
2388-
if ident.span.glob_adjust(module.expansion, binding.span).is_none() {
2389-
continue;
2390-
}
2391-
if self
2392-
.r
2393-
.resolve_ident_in_module_unadjusted(
2394-
ModuleOrUniformRoot::Module(module),
2395-
ident,
2396-
ns,
2397-
&self.parent_scope,
2398-
false,
2399-
module.span,
2400-
)
2401-
.is_ok()
2402-
{
2403-
let import_ids = self.find_transitive_imports(&binding.kind, trait_name);
2404-
let trait_def_id = module.def_id().unwrap();
2405-
found_traits.push(TraitCandidate { def_id: trait_def_id, import_ids });
2406-
}
2407-
} else if let Res::Def(DefKind::TraitAlias, _) = binding.res() {
2408-
// For now, just treat all trait aliases as possible candidates, since we don't
2409-
// know if the ident is somewhere in the transitive bounds.
2410-
let import_ids = self.find_transitive_imports(&binding.kind, trait_name);
2411-
let trait_def_id = binding.res().def_id();
2412-
found_traits.push(TraitCandidate { def_id: trait_def_id, import_ids });
2413-
} else {
2414-
bug!("candidate is not trait or trait alias?")
2415-
}
2416-
}
2417-
}
2418-
2419-
fn find_transitive_imports(
2420-
&mut self,
2421-
mut kind: &NameBindingKind<'_>,
2422-
trait_name: Ident,
2423-
) -> SmallVec<[LocalDefId; 1]> {
2424-
let mut import_ids = smallvec![];
2425-
while let NameBindingKind::Import { import, binding, .. } = kind {
2426-
let id = self.r.local_def_id(import.id);
2427-
self.r.maybe_unused_trait_imports.insert(id);
2428-
self.r.add_to_glob_map(&import, trait_name);
2429-
import_ids.push(id);
2430-
kind = &binding.kind;
2431-
}
2432-
import_ids
2433-
}
24342369
}
24352370

24362371
impl<'a> Resolver<'a> {

src/librustc_resolve/lib.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ use rustc_index::vec::IndexVec;
4343
use rustc_metadata::creader::{CStore, CrateLoader};
4444
use rustc_middle::hir::exports::ExportMap;
4545
use rustc_middle::middle::cstore::{CrateStore, MetadataLoaderDyn};
46-
use rustc_middle::span_bug;
4746
use rustc_middle::ty::query::Providers;
4847
use rustc_middle::ty::{self, DefIdTree, ResolverOutputs};
48+
use rustc_middle::{bug, span_bug};
4949
use rustc_session::lint;
5050
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
5151
use rustc_session::Session;
@@ -54,6 +54,7 @@ use rustc_span::source_map::Spanned;
5454
use rustc_span::symbol::{kw, sym, Ident, Symbol};
5555
use rustc_span::{Span, DUMMY_SP};
5656

57+
use smallvec::{smallvec, SmallVec};
5758
use std::cell::{Cell, RefCell};
5859
use std::collections::BTreeSet;
5960
use std::{cmp, fmt, iter, ptr};
@@ -1430,6 +1431,82 @@ impl<'a> Resolver<'a> {
14301431
self.crate_loader.postprocess(krate);
14311432
}
14321433

1434+
fn get_traits_in_module_containing_item(
1435+
&mut self,
1436+
ident: Ident,
1437+
ns: Namespace,
1438+
module: Module<'a>,
1439+
found_traits: &mut Vec<TraitCandidate>,
1440+
parent_scope: &ParentScope<'a>,
1441+
) {
1442+
assert!(ns == TypeNS || ns == ValueNS);
1443+
let mut traits = module.traits.borrow_mut();
1444+
if traits.is_none() {
1445+
let mut collected_traits = Vec::new();
1446+
module.for_each_child(self, |_, name, ns, binding| {
1447+
if ns != TypeNS {
1448+
return;
1449+
}
1450+
match binding.res() {
1451+
Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
1452+
collected_traits.push((name, binding))
1453+
}
1454+
_ => (),
1455+
}
1456+
});
1457+
*traits = Some(collected_traits.into_boxed_slice());
1458+
}
1459+
1460+
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
1461+
// Traits have pseudo-modules that can be used to search for the given ident.
1462+
if let Some(module) = binding.module() {
1463+
let mut ident = ident;
1464+
if ident.span.glob_adjust(module.expansion, binding.span).is_none() {
1465+
continue;
1466+
}
1467+
if self
1468+
.resolve_ident_in_module_unadjusted(
1469+
ModuleOrUniformRoot::Module(module),
1470+
ident,
1471+
ns,
1472+
parent_scope,
1473+
false,
1474+
module.span,
1475+
)
1476+
.is_ok()
1477+
{
1478+
let import_ids = self.find_transitive_imports(&binding.kind, trait_name);
1479+
let trait_def_id = module.def_id().unwrap();
1480+
found_traits.push(TraitCandidate { def_id: trait_def_id, import_ids });
1481+
}
1482+
} else if let Res::Def(DefKind::TraitAlias, _) = binding.res() {
1483+
// For now, just treat all trait aliases as possible candidates, since we don't
1484+
// know if the ident is somewhere in the transitive bounds.
1485+
let import_ids = self.find_transitive_imports(&binding.kind, trait_name);
1486+
let trait_def_id = binding.res().def_id();
1487+
found_traits.push(TraitCandidate { def_id: trait_def_id, import_ids });
1488+
} else {
1489+
bug!("candidate is not trait or trait alias?")
1490+
}
1491+
}
1492+
}
1493+
1494+
fn find_transitive_imports(
1495+
&mut self,
1496+
mut kind: &NameBindingKind<'_>,
1497+
trait_name: Ident,
1498+
) -> SmallVec<[LocalDefId; 1]> {
1499+
let mut import_ids = smallvec![];
1500+
while let NameBindingKind::Import { import, binding, .. } = kind {
1501+
let id = self.local_def_id(import.id);
1502+
self.maybe_unused_trait_imports.insert(id);
1503+
self.add_to_glob_map(&import, trait_name);
1504+
import_ids.push(id);
1505+
kind = &binding.kind;
1506+
}
1507+
import_ids
1508+
}
1509+
14331510
fn new_module(
14341511
&self,
14351512
parent: Module<'a>,

0 commit comments

Comments
 (0)