From ba543f7d763e846a90ccd9905fec85779592c8a2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 12 Jun 2024 11:43:36 +0200 Subject: [PATCH] internal: Don't unnecessarily clone ModPaths out of interning wrappers --- crates/hir-def/src/data.rs | 2 +- crates/hir-def/src/lib.rs | 38 +++++++++++++------ crates/hir-def/src/nameres/attr_resolution.rs | 4 +- crates/hir-def/src/nameres/collector.rs | 16 +++++--- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/crates/hir-def/src/data.rs b/crates/hir-def/src/data.rs index ded2fd243b..4338163672 100644 --- a/crates/hir-def/src/data.rs +++ b/crates/hir-def/src/data.rs @@ -628,7 +628,7 @@ impl<'a> AssocItemCollector<'a> { 'attrs: for attr in &*attrs { let ast_id = AstId::new(self.expander.current_file_id(), item.ast_id(item_tree).upcast()); - let ast_id_with_path = AstIdWithPath { path: (*attr.path).clone(), ast_id }; + let ast_id_with_path = AstIdWithPath { path: attr.path.clone(), ast_id }; match self.def_map.resolve_attr_macro( self.db, diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs index e3d5e752e7..2a8fbe3d2c 100644 --- a/crates/hir-def/src/lib.rs +++ b/crates/hir-def/src/lib.rs @@ -56,6 +56,7 @@ pub mod find_path; pub mod import_map; pub mod visibility; +use intern::Interned; pub use rustc_abi as layout; use triomphe::Arc; @@ -1408,7 +1409,8 @@ impl AsMacroCall for InFile<&ast::MacroCall> { macro_call_as_call_id_with_eager( db, - &AstIdWithPath::new(ast_id.file_id, ast_id.value, path), + ast_id, + &path, call_site.ctx, expands_to, krate, @@ -1422,11 +1424,15 @@ impl AsMacroCall for InFile<&ast::MacroCall> { #[derive(Clone, Debug, Eq, PartialEq)] struct AstIdWithPath { ast_id: AstId, - path: path::ModPath, + path: Interned, } impl AstIdWithPath { - fn new(file_id: HirFileId, ast_id: FileAstId, path: path::ModPath) -> AstIdWithPath { + fn new( + file_id: HirFileId, + ast_id: FileAstId, + path: Interned, + ) -> AstIdWithPath { AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path } } } @@ -1439,27 +1445,37 @@ fn macro_call_as_call_id( krate: CrateId, resolver: impl Fn(&path::ModPath) -> Option + Copy, ) -> Result, UnresolvedMacro> { - macro_call_as_call_id_with_eager(db, call, call_site, expand_to, krate, resolver, resolver) - .map(|res| res.value) + macro_call_as_call_id_with_eager( + db, + call.ast_id, + &call.path, + call_site, + expand_to, + krate, + resolver, + resolver, + ) + .map(|res| res.value) } fn macro_call_as_call_id_with_eager( db: &dyn ExpandDatabase, - call: &AstIdWithPath, + ast_id: AstId, + path: &path::ModPath, call_site: SyntaxContextId, expand_to: ExpandTo, krate: CrateId, resolver: impl FnOnce(&path::ModPath) -> Option, eager_resolver: impl Fn(&path::ModPath) -> Option, ) -> Result>, UnresolvedMacro> { - let def = resolver(&call.path).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?; + let def = resolver(path).ok_or_else(|| UnresolvedMacro { path: path.clone() })?; let res = match def.kind { MacroDefKind::BuiltInEager(..) => expand_eager_macro_input( db, krate, - &call.ast_id.to_node(db), - call.ast_id, + &ast_id.to_node(db), + ast_id, def, call_site, &|path| eager_resolver(path).filter(MacroDefId::is_fn_like), @@ -1468,12 +1484,12 @@ fn macro_call_as_call_id_with_eager( value: Some(def.make_call( db, krate, - MacroCallKind::FnLike { ast_id: call.ast_id, expand_to, eager: None }, + MacroCallKind::FnLike { ast_id, expand_to, eager: None }, call_site, )), err: None, }, - _ => return Err(UnresolvedMacro { path: call.path.clone() }), + _ => return Err(UnresolvedMacro { path: path.clone() }), }; Ok(res) } diff --git a/crates/hir-def/src/nameres/attr_resolution.rs b/crates/hir-def/src/nameres/attr_resolution.rs index 460f1f1407..f842027d64 100644 --- a/crates/hir-def/src/nameres/attr_resolution.rs +++ b/crates/hir-def/src/nameres/attr_resolution.rs @@ -59,7 +59,7 @@ impl DefMap { return Ok(ResolvedAttr::Other); } } - None => return Err(UnresolvedMacro { path: ast_id.path }), + None => return Err(UnresolvedMacro { path: ast_id.path.as_ref().clone() }), }; Ok(ResolvedAttr::Macro(attr_macro_as_call_id( @@ -142,7 +142,7 @@ pub(super) fn derive_macro_as_call_id( ) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> { let (macro_id, def_id) = resolver(&item_attr.path) .filter(|(_, def_id)| def_id.is_derive()) - .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; + .ok_or_else(|| UnresolvedMacro { path: item_attr.path.as_ref().clone() })?; let call_id = def_id.make_call( db.upcast(), krate, diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index 4be35590e1..216fbeaa40 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -17,6 +17,7 @@ use hir_expand::{ proc_macro::CustomProcMacroExpander, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; +use intern::Interned; use itertools::{izip, Itertools}; use la_arena::Idx; use limit::Limit; @@ -1300,7 +1301,11 @@ impl DefCollector<'_> { let call_id = call_id(); let mut len = 0; for (idx, (path, call_site)) in derive_macros.enumerate() { - let ast_id = AstIdWithPath::new(file_id, ast_id.value, path); + let ast_id = AstIdWithPath::new( + file_id, + ast_id.value, + Interned::new(path), + ); self.unresolved_macros.push(MacroDirective { module_id: directive.module_id, depth: directive.depth + 1, @@ -1473,7 +1478,7 @@ impl DefCollector<'_> { derive_index: *derive_pos as u32, derive_macro_id: *derive_macro_id, }, - ast_id.path.clone(), + ast_id.path.as_ref().clone(), )); } // These are diagnosed by `reseed_with_unresolved_attribute`, as that function consumes them @@ -2108,7 +2113,7 @@ impl ModCollector<'_, '_> { let ast_id = AstIdWithPath::new( self.file_id(), mod_item.ast_id(self.item_tree), - attr.path.as_ref().clone(), + attr.path.clone(), ); self.def_collector.unresolved_macros.push(MacroDirective { module_id: self.module_id, @@ -2302,7 +2307,7 @@ impl ModCollector<'_, '_> { &MacroCall { ref path, ast_id, expand_to, ctxt }: &MacroCall, container: ItemContainerId, ) { - let ast_id = AstIdWithPath::new(self.file_id(), ast_id, ModPath::clone(path)); + let ast_id = AstIdWithPath::new(self.file_id(), ast_id, path.clone()); let db = self.def_collector.db; // FIXME: Immediately expanding in "Case 1" is insufficient since "Case 2" may also define @@ -2312,7 +2317,8 @@ impl ModCollector<'_, '_> { // Case 1: try to resolve macro calls with single-segment name and expand macro_rules if let Ok(res) = macro_call_as_call_id_with_eager( db.upcast(), - &ast_id, + ast_id.ast_id, + &ast_id.path, ctxt, expand_to, self.def_collector.def_map.krate,