Skip to content
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

Small resolve refactor #136736

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,15 +1500,15 @@ fn import_path_to_string(names: &[Ident], import_kind: &ImportKind<'_>, span: Sp
let global = !names.is_empty() && names[0].name == kw::PathRoot;
if let Some(pos) = pos {
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 {
let names = if global { &names[1..] } else { names };
if names.is_empty() {
import_kind_to_string(import_kind)
} else {
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),
)
}
Expand Down
29 changes: 15 additions & 14 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl Segment {
}

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))
}
}

Expand Down Expand Up @@ -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();
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 {
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(name.as_str());
Expand All @@ -2256,31 +2256,32 @@ fn names_to_string(names: &[Symbol]) -> 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.
fn module_to_string(module: Module<'_>) -> Option<String> {
fn module_to_string(mut module: Module<'_>) -> Option<String> {
let mut names = Vec::new();

fn collect_mod(names: &mut Vec<Symbol>, module: Module<'_>) {
loop {
if let ModuleKind::Def(.., name) = module.kind {
if let Some(parent) = module.parent {
names.push(name);
collect_mod(names, parent);
module = parent
} else {
break;
}
} else {
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() {
return None;
}
names.reverse();
Some(names_to_string(&names))
Some(names_to_string(names.iter().rev().copied()))
}

#[derive(Copy, Clone, Debug)]
Expand Down
Loading