1
Fork 0

Small resolve refactor

This commit is contained in:
Andre Bogus 2025-02-08 10:46:05 +01:00
parent d2f335d58e
commit 32955b91e8
2 changed files with 17 additions and 16 deletions

View file

@ -1500,7 +1500,7 @@ fn import_path_to_string(names: &[Ident], import_kind: &ImportKind<'_>, span: Sp
let global = !names.is_empty() && names[0].name == kw::PathRoot; let global = !names.is_empty() && names[0].name == kw::PathRoot;
if let Some(pos) = pos { if let Some(pos) = pos {
let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] }; let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] };
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>()) names_to_string(names.iter().map(|ident| ident.name))
} else { } else {
let names = if global { &names[1..] } else { names }; let names = if global { &names[1..] } else { names };
if names.is_empty() { if names.is_empty() {
@ -1508,7 +1508,7 @@ fn import_path_to_string(names: &[Ident], import_kind: &ImportKind<'_>, span: Sp
} else { } else {
format!( format!(
"{}::{}", "{}::{}",
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>()), names_to_string(names.iter().map(|ident| ident.name)),
import_kind_to_string(import_kind), import_kind_to_string(import_kind),
) )
} }

View file

@ -358,7 +358,7 @@ impl Segment {
} }
fn names_to_string(segments: &[Segment]) -> String { fn names_to_string(segments: &[Segment]) -> String {
names_to_string(&segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>()) names_to_string(segments.iter().map(|seg| seg.ident.name))
} }
} }
@ -2241,13 +2241,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
} }
} }
fn names_to_string(names: &[Symbol]) -> String { fn names_to_string(names: impl Iterator<Item = Symbol>) -> String {
let mut result = String::new(); let mut result = String::new();
for (i, name) in names.iter().filter(|name| **name != kw::PathRoot).enumerate() { for (i, name) in names.filter(|name| *name != kw::PathRoot).enumerate() {
if i > 0 { if i > 0 {
result.push_str("::"); result.push_str("::");
} }
if Ident::with_dummy_span(*name).is_raw_guess() { if Ident::with_dummy_span(name).is_raw_guess() {
result.push_str("r#"); result.push_str("r#");
} }
result.push_str(name.as_str()); result.push_str(name.as_str());
@ -2256,31 +2256,32 @@ fn names_to_string(names: &[Symbol]) -> String {
} }
fn path_names_to_string(path: &Path) -> String { fn path_names_to_string(path: &Path) -> String {
names_to_string(&path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>()) names_to_string(path.segments.iter().map(|seg| seg.ident.name))
} }
/// A somewhat inefficient routine to obtain the name of a module. /// A somewhat inefficient routine to obtain the name of a module.
fn module_to_string(module: Module<'_>) -> Option<String> { fn module_to_string(mut module: Module<'_>) -> Option<String> {
let mut names = Vec::new(); let mut names = Vec::new();
loop {
fn collect_mod(names: &mut Vec<Symbol>, module: Module<'_>) {
if let ModuleKind::Def(.., name) = module.kind { if let ModuleKind::Def(.., name) = module.kind {
if let Some(parent) = module.parent { if let Some(parent) = module.parent {
names.push(name); names.push(name);
collect_mod(names, parent); module = parent
} else {
break;
} }
} else { } else {
names.push(sym::opaque_module_name_placeholder); names.push(sym::opaque_module_name_placeholder);
collect_mod(names, module.parent.unwrap()); let Some(parent) = module.parent else {
return None;
};
module = parent;
} }
} }
collect_mod(&mut names, module);
if names.is_empty() { if names.is_empty() {
return None; return None;
} }
names.reverse(); Some(names_to_string(names.iter().rev().copied()))
Some(names_to_string(&names))
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]