Skip to content

Commit 32955b9

Browse files
committed
Small resolve refactor
1 parent d2f335d commit 32955b9

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

Diff for: compiler/rustc_resolve/src/imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1500,15 +1500,15 @@ fn import_path_to_string(names: &[Ident], import_kind: &ImportKind<'_>, span: Sp
15001500
let global = !names.is_empty() && names[0].name == kw::PathRoot;
15011501
if let Some(pos) = pos {
15021502
let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] };
1503-
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>())
1503+
names_to_string(names.iter().map(|ident| ident.name))
15041504
} else {
15051505
let names = if global { &names[1..] } else { names };
15061506
if names.is_empty() {
15071507
import_kind_to_string(import_kind)
15081508
} else {
15091509
format!(
15101510
"{}::{}",
1511-
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>()),
1511+
names_to_string(names.iter().map(|ident| ident.name)),
15121512
import_kind_to_string(import_kind),
15131513
)
15141514
}

Diff for: compiler/rustc_resolve/src/lib.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl Segment {
358358
}
359359

360360
fn names_to_string(segments: &[Segment]) -> String {
361-
names_to_string(&segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>())
361+
names_to_string(segments.iter().map(|seg| seg.ident.name))
362362
}
363363
}
364364

@@ -2241,13 +2241,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22412241
}
22422242
}
22432243

2244-
fn names_to_string(names: &[Symbol]) -> String {
2244+
fn names_to_string(names: impl Iterator<Item = Symbol>) -> String {
22452245
let mut result = String::new();
2246-
for (i, name) in names.iter().filter(|name| **name != kw::PathRoot).enumerate() {
2246+
for (i, name) in names.filter(|name| *name != kw::PathRoot).enumerate() {
22472247
if i > 0 {
22482248
result.push_str("::");
22492249
}
2250-
if Ident::with_dummy_span(*name).is_raw_guess() {
2250+
if Ident::with_dummy_span(name).is_raw_guess() {
22512251
result.push_str("r#");
22522252
}
22532253
result.push_str(name.as_str());
@@ -2256,31 +2256,32 @@ fn names_to_string(names: &[Symbol]) -> String {
22562256
}
22572257

22582258
fn path_names_to_string(path: &Path) -> String {
2259-
names_to_string(&path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>())
2259+
names_to_string(path.segments.iter().map(|seg| seg.ident.name))
22602260
}
22612261

22622262
/// A somewhat inefficient routine to obtain the name of a module.
2263-
fn module_to_string(module: Module<'_>) -> Option<String> {
2263+
fn module_to_string(mut module: Module<'_>) -> Option<String> {
22642264
let mut names = Vec::new();
2265-
2266-
fn collect_mod(names: &mut Vec<Symbol>, module: Module<'_>) {
2265+
loop {
22672266
if let ModuleKind::Def(.., name) = module.kind {
22682267
if let Some(parent) = module.parent {
22692268
names.push(name);
2270-
collect_mod(names, parent);
2269+
module = parent
2270+
} else {
2271+
break;
22712272
}
22722273
} else {
22732274
names.push(sym::opaque_module_name_placeholder);
2274-
collect_mod(names, module.parent.unwrap());
2275+
let Some(parent) = module.parent else {
2276+
return None;
2277+
};
2278+
module = parent;
22752279
}
22762280
}
2277-
collect_mod(&mut names, module);
2278-
22792281
if names.is_empty() {
22802282
return None;
22812283
}
2282-
names.reverse();
2283-
Some(names_to_string(&names))
2284+
Some(names_to_string(names.iter().rev().copied()))
22842285
}
22852286

22862287
#[derive(Copy, Clone, Debug)]

0 commit comments

Comments
 (0)