Skip to content

Commit 6dc7dd5

Browse files
Rollup merge of #164074 - TaKO8Ki:resolve-borrow-impl-trait-ast, r=petrochenkov
Avoid cloning impl trait AST nodes in late resolution I have removed unnecessary AST clones by storing `&'ast` references. This is just a refactoring and does not change behavior. I did not use an LLM in this pull request.
2 parents 3c4fab2 + c446785 commit 6dc7dd5

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

‎compiler/rustc_resolve/src/late.rs‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ pub(crate) struct DiagMetadata<'ast> {
824824
current_impl_item: Option<&'ast AssocItem>,
825825

826826
/// When processing impl trait
827-
currently_processing_impl_trait: Option<(TraitRef, Ty)>,
827+
currently_processing_impl_trait: Option<(&'ast TraitRef, &'ast Ty)>,
828828

829829
/// Accumulate the errors due to missed lifetime elision,
830830
/// and report them all at once for each function.
@@ -857,7 +857,7 @@ struct LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
857857
lifetime_elision_candidates: Option<Vec<(LifetimeRes, LifetimeElisionCandidate)>>,
858858

859859
/// The trait that the current context can refer to.
860-
current_trait_ref: Option<(Module<'ra>, TraitRef)>,
860+
current_trait_ref: Option<(Module<'ra>, &'ast TraitRef)>,
861861

862862
/// Fields used to add information to diagnostic errors.
863863
diag_metadata: Box<DiagMetadata<'ast>>,
@@ -2004,8 +2004,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
20042004
// are we trying to use an anonymous lifetime
20052005
// on a non GAT associated trait type?
20062006
if !self.in_func_body
2007-
&& let Some((module, _)) = &self.current_trait_ref
2008-
&& let Some(ty) = &self.diag_metadata.current_self_type
2007+
&& let Some((module, _)) = self.current_trait_ref
2008+
&& let Some(ty) = self.diag_metadata.current_self_type
20092009
&& Some(true) == self.diag_metadata.in_non_gat_assoc_type
20102010
&& let crate::ModuleKind::Def(DefKind::Trait, trait_id, _, _) =
20112011
module.kind
@@ -3509,16 +3509,15 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
35093509
/// This is called to resolve a trait reference from an `impl` (i.e., `impl Trait for Foo`).
35103510
fn with_optional_trait_ref<T>(
35113511
&mut self,
3512-
opt_trait_ref: Option<&TraitRef>,
3512+
opt_trait_ref: Option<&'ast TraitRef>,
35133513
self_type: &'ast Ty,
35143514
f: impl FnOnce(&mut Self, Option<DefId>) -> T,
35153515
) -> T {
35163516
let mut new_val = None;
35173517
let mut new_id = None;
35183518
if let Some(trait_ref) = opt_trait_ref {
35193519
let path: Vec<_> = Segment::from_path(&trait_ref.path);
3520-
self.diag_metadata.currently_processing_impl_trait =
3521-
Some((trait_ref.clone(), self_type.clone()));
3520+
self.diag_metadata.currently_processing_impl_trait = Some((trait_ref, self_type));
35223521
let res = self.smart_resolve_path_fragment(
35233522
&None,
35243523
&path,
@@ -3530,7 +3529,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
35303529
self.diag_metadata.currently_processing_impl_trait = None;
35313530
if let Some(def_id) = res.expect_full_res().opt_def_id() {
35323531
new_id = Some(def_id);
3533-
new_val = Some((self.r.expect_module(def_id), trait_ref.clone()));
3532+
new_val = Some((self.r.expect_module(def_id), trait_ref));
35343533
}
35353534
}
35363535
let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
@@ -3917,7 +3916,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
39173916
let Some(decl) = decl else {
39183917
// We could not find the method: report an error.
39193918
let candidate = self.find_similarly_named_assoc_item(reported_ident.name, kind);
3920-
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
3919+
let path = &self.current_trait_ref.unwrap().1.path;
39213920
let path_names = path_names_to_string(path);
39223921
self.report_error(span, err(reported_ident, path_names, candidate));
39233922
feed_visibility(self, module.def_id());
@@ -3931,7 +3930,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
39313930
match seen_trait_items.entry(id_in_trait) {
39323931
Entry::Occupied(entry) => {
39333932
let trait_span = decl.parent_module.unwrap().span.shrink_to_lo();
3934-
let impl_span = self.current_trait_ref.as_ref().unwrap().1.path.span;
3933+
let impl_span = self.current_trait_ref.unwrap().1.path.span;
39353934
self.report_error(
39363935
span,
39373936
ResolutionError::TraitImplDuplicate {
@@ -3961,7 +3960,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
39613960
}
39623961

39633962
// The method kind does not correspond to what appeared in the trait, report.
3964-
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
3963+
let path = &self.current_trait_ref.unwrap().1.path;
39653964
let (code, kind) = match kind {
39663965
AssocItemKind::Const(..) => (E0323, "const"),
39673966
AssocItemKind::Fn(..) => (E0324, "method"),
@@ -3973,7 +3972,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
39733972
};
39743973
let trait_path = path_names_to_string(path);
39753974
let trait_span = decl.parent_module.unwrap().span.shrink_to_lo();
3976-
let impl_span = self.current_trait_ref.as_ref().unwrap().1.path.span;
3975+
let impl_span = self.current_trait_ref.unwrap().1.path.span;
39773976
self.report_error(
39783977
span,
39793978
ResolutionError::TraitImplMismatch {
@@ -5542,7 +5541,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
55425541

55435542
fn record_traits_in_scope(&mut self, node_id: NodeId, ident: Ident) {
55445543
let traits = self.r.traits_in_scope(
5545-
self.current_trait_ref.as_ref().map(|(module, _)| *module),
5544+
self.current_trait_ref.map(|(module, _)| module),
55465545
&self.parent_scope,
55475546
ident.span,
55485547
Some((ident.name, ValueNS)),

‎compiler/rustc_resolve/src/late/diagnostics.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,8 +1884,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
18841884
res: Option<Res>,
18851885
span: Span,
18861886
) {
1887-
if let Some((trait_ref, self_ty)) =
1888-
self.diag_metadata.currently_processing_impl_trait.clone()
1887+
if let Some((trait_ref, self_ty)) = self.diag_metadata.currently_processing_impl_trait
18891888
&& let TyKind::Path(_, self_ty_path) = &self_ty.kind
18901889
&& let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
18911890
self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None, source)
@@ -2921,15 +2920,15 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
29212920
ident: Symbol,
29222921
kind: &AssocItemKind,
29232922
) -> Option<Symbol> {
2924-
let (module, _) = self.current_trait_ref.as_ref()?;
2923+
let (module, _) = self.current_trait_ref?;
29252924
if ident == kw::Underscore {
29262925
// We do nothing for `_`.
29272926
return None;
29282927
}
29292928

29302929
let targets = self
29312930
.r
2932-
.resolutions(*module)
2931+
.resolutions(module)
29332932
.iter()
29342933
.filter_map(|(key, res)| {
29352934
res.borrow(self.r).best_decl().map(|binding| (key, binding.res()))

0 commit comments

Comments
 (0)