Skip to content

Commit 0947e05

Browse files
authored
Rollup merge of #80250 - bugadani:resolver-cleanup, r=petrochenkov
Minor cleanups in LateResolver - Avoid calculating hash twice - Avoid creating a closure in every iteration of a loop - Reserve space for path in advance - Some readability changes
2 parents 8232109 + 6d71cc6 commit 0947e05

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

compiler/rustc_resolve/src/late.rs

+29-36
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_span::Span;
2929
use smallvec::{smallvec, SmallVec};
3030

3131
use rustc_span::source_map::{respan, Spanned};
32-
use std::collections::BTreeSet;
32+
use std::collections::{hash_map::Entry, BTreeSet};
3333
use std::mem::{replace, take};
3434
use tracing::debug;
3535

@@ -953,8 +953,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
953953
});
954954
};
955955

956-
for item in trait_items {
957-
this.with_trait_items(trait_items, |this| {
956+
this.with_trait_items(trait_items, |this| {
957+
for item in trait_items {
958958
match &item.kind {
959959
AssocItemKind::Const(_, ty, default) => {
960960
this.visit_ty(ty);
@@ -983,8 +983,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
983983
panic!("unexpanded macro in resolve!")
984984
}
985985
};
986-
});
987-
}
986+
}
987+
});
988988
});
989989
});
990990
}
@@ -1060,36 +1060,29 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10601060
continue;
10611061
}
10621062

1063-
let def_kind = match param.kind {
1064-
GenericParamKind::Type { .. } => DefKind::TyParam,
1065-
GenericParamKind::Const { .. } => DefKind::ConstParam,
1066-
_ => unreachable!(),
1067-
};
1068-
10691063
let ident = param.ident.normalize_to_macros_2_0();
10701064
debug!("with_generic_param_rib: {}", param.id);
10711065

1072-
if seen_bindings.contains_key(&ident) {
1073-
let span = seen_bindings.get(&ident).unwrap();
1074-
let err = ResolutionError::NameAlreadyUsedInParameterList(ident.name, *span);
1075-
self.report_error(param.ident.span, err);
1066+
match seen_bindings.entry(ident) {
1067+
Entry::Occupied(entry) => {
1068+
let span = *entry.get();
1069+
let err = ResolutionError::NameAlreadyUsedInParameterList(ident.name, span);
1070+
self.report_error(param.ident.span, err);
1071+
}
1072+
Entry::Vacant(entry) => {
1073+
entry.insert(param.ident.span);
1074+
}
10761075
}
1077-
seen_bindings.entry(ident).or_insert(param.ident.span);
10781076

10791077
// Plain insert (no renaming).
1080-
let res = Res::Def(def_kind, self.r.local_def_id(param.id).to_def_id());
1081-
1082-
match param.kind {
1083-
GenericParamKind::Type { .. } => {
1084-
function_type_rib.bindings.insert(ident, res);
1085-
self.r.record_partial_res(param.id, PartialRes::new(res));
1086-
}
1087-
GenericParamKind::Const { .. } => {
1088-
function_value_rib.bindings.insert(ident, res);
1089-
self.r.record_partial_res(param.id, PartialRes::new(res));
1090-
}
1078+
let (rib, def_kind) = match param.kind {
1079+
GenericParamKind::Type { .. } => (&mut function_type_rib, DefKind::TyParam),
1080+
GenericParamKind::Const { .. } => (&mut function_value_rib, DefKind::ConstParam),
10911081
_ => unreachable!(),
1092-
}
1082+
};
1083+
let res = Res::Def(def_kind, self.r.local_def_id(param.id).to_def_id());
1084+
self.r.record_partial_res(param.id, PartialRes::new(res));
1085+
rib.bindings.insert(ident, res);
10931086
}
10941087

10951088
self.ribs[ValueNS].push(function_value_rib);
@@ -1778,7 +1771,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
17781771
path
17791772
);
17801773
let ns = source.namespace();
1781-
let is_expected = &|res| source.is_expected(res);
17821774

17831775
let report_errors = |this: &mut Self, res: Option<Res>| {
17841776
if this.should_report_errs() {
@@ -1881,7 +1873,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18811873
crate_lint,
18821874
) {
18831875
Ok(Some(partial_res)) if partial_res.unresolved_segments() == 0 => {
1884-
if is_expected(partial_res.base_res()) || partial_res.base_res() == Res::Err {
1876+
if source.is_expected(partial_res.base_res()) || partial_res.base_res() == Res::Err
1877+
{
18851878
partial_res
18861879
} else {
18871880
report_errors(self, Some(partial_res.base_res()))
@@ -1898,11 +1891,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18981891
self.r.trait_map.insert(id, traits);
18991892
}
19001893

1901-
let mut std_path = vec![Segment::from_ident(Ident::with_dummy_span(sym::std))];
1902-
1903-
std_path.extend(path);
1904-
19051894
if self.r.primitive_type_table.primitive_types.contains_key(&path[0].ident.name) {
1895+
let mut std_path = Vec::with_capacity(1 + path.len());
1896+
1897+
std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std)));
1898+
std_path.extend(path);
19061899
if let PathResult::Module(_) | PathResult::NonModule(_) =
19071900
self.resolve_path(&std_path, Some(ns), false, span, CrateLint::No)
19081901
{
@@ -1983,7 +1976,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19831976
) -> Result<Option<PartialRes>, Spanned<ResolutionError<'a>>> {
19841977
let mut fin_res = None;
19851978

1986-
for (i, ns) in [primary_ns, TypeNS, ValueNS].iter().cloned().enumerate() {
1979+
for (i, &ns) in [primary_ns, TypeNS, ValueNS].iter().enumerate() {
19871980
if i == 0 || ns != primary_ns {
19881981
match self.resolve_qpath(id, qself, path, ns, span, crate_lint)? {
19891982
Some(partial_res)
@@ -1993,7 +1986,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19931986
}
19941987
partial_res => {
19951988
if fin_res.is_none() {
1996-
fin_res = partial_res
1989+
fin_res = partial_res;
19971990
}
19981991
}
19991992
}

0 commit comments

Comments
 (0)