internal: Don't unnecessarily clone ModPaths out of interning wrappers

This commit is contained in:
Lukas Wirth 2024-06-12 11:43:36 +02:00
parent 855282fa53
commit ba543f7d76
4 changed files with 41 additions and 19 deletions

View file

@ -628,7 +628,7 @@ impl<'a> AssocItemCollector<'a> {
'attrs: for attr in &*attrs { 'attrs: for attr in &*attrs {
let ast_id = let ast_id =
AstId::new(self.expander.current_file_id(), item.ast_id(item_tree).upcast()); 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( match self.def_map.resolve_attr_macro(
self.db, self.db,

View file

@ -56,6 +56,7 @@ pub mod find_path;
pub mod import_map; pub mod import_map;
pub mod visibility; pub mod visibility;
use intern::Interned;
pub use rustc_abi as layout; pub use rustc_abi as layout;
use triomphe::Arc; use triomphe::Arc;
@ -1408,7 +1409,8 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
macro_call_as_call_id_with_eager( macro_call_as_call_id_with_eager(
db, db,
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path), ast_id,
&path,
call_site.ctx, call_site.ctx,
expands_to, expands_to,
krate, krate,
@ -1422,11 +1424,15 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
struct AstIdWithPath<T: AstIdNode> { struct AstIdWithPath<T: AstIdNode> {
ast_id: AstId<T>, ast_id: AstId<T>,
path: path::ModPath, path: Interned<path::ModPath>,
} }
impl<T: AstIdNode> AstIdWithPath<T> { impl<T: AstIdNode> AstIdWithPath<T> {
fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> { fn new(
file_id: HirFileId,
ast_id: FileAstId<T>,
path: Interned<path::ModPath>,
) -> AstIdWithPath<T> {
AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path } AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
} }
} }
@ -1439,27 +1445,37 @@ fn macro_call_as_call_id(
krate: CrateId, krate: CrateId,
resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy, resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Result<Option<MacroCallId>, UnresolvedMacro> { ) -> Result<Option<MacroCallId>, UnresolvedMacro> {
macro_call_as_call_id_with_eager(db, call, call_site, expand_to, krate, resolver, resolver) macro_call_as_call_id_with_eager(
.map(|res| res.value) 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( fn macro_call_as_call_id_with_eager(
db: &dyn ExpandDatabase, db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>, ast_id: AstId<ast::MacroCall>,
path: &path::ModPath,
call_site: SyntaxContextId, call_site: SyntaxContextId,
expand_to: ExpandTo, expand_to: ExpandTo,
krate: CrateId, krate: CrateId,
resolver: impl FnOnce(&path::ModPath) -> Option<MacroDefId>, resolver: impl FnOnce(&path::ModPath) -> Option<MacroDefId>,
eager_resolver: impl Fn(&path::ModPath) -> Option<MacroDefId>, eager_resolver: impl Fn(&path::ModPath) -> Option<MacroDefId>,
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> { ) -> Result<ExpandResult<Option<MacroCallId>>, 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 { let res = match def.kind {
MacroDefKind::BuiltInEager(..) => expand_eager_macro_input( MacroDefKind::BuiltInEager(..) => expand_eager_macro_input(
db, db,
krate, krate,
&call.ast_id.to_node(db), &ast_id.to_node(db),
call.ast_id, ast_id,
def, def,
call_site, call_site,
&|path| eager_resolver(path).filter(MacroDefId::is_fn_like), &|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( value: Some(def.make_call(
db, db,
krate, krate,
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to, eager: None }, MacroCallKind::FnLike { ast_id, expand_to, eager: None },
call_site, call_site,
)), )),
err: None, err: None,
}, },
_ => return Err(UnresolvedMacro { path: call.path.clone() }), _ => return Err(UnresolvedMacro { path: path.clone() }),
}; };
Ok(res) Ok(res)
} }

View file

@ -59,7 +59,7 @@ impl DefMap {
return Ok(ResolvedAttr::Other); 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( 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> { ) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
let (macro_id, def_id) = resolver(&item_attr.path) let (macro_id, def_id) = resolver(&item_attr.path)
.filter(|(_, def_id)| def_id.is_derive()) .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( let call_id = def_id.make_call(
db.upcast(), db.upcast(),
krate, krate,

View file

@ -17,6 +17,7 @@ use hir_expand::{
proc_macro::CustomProcMacroExpander, proc_macro::CustomProcMacroExpander,
ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
}; };
use intern::Interned;
use itertools::{izip, Itertools}; use itertools::{izip, Itertools};
use la_arena::Idx; use la_arena::Idx;
use limit::Limit; use limit::Limit;
@ -1300,7 +1301,11 @@ impl DefCollector<'_> {
let call_id = call_id(); let call_id = call_id();
let mut len = 0; let mut len = 0;
for (idx, (path, call_site)) in derive_macros.enumerate() { 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 { self.unresolved_macros.push(MacroDirective {
module_id: directive.module_id, module_id: directive.module_id,
depth: directive.depth + 1, depth: directive.depth + 1,
@ -1473,7 +1478,7 @@ impl DefCollector<'_> {
derive_index: *derive_pos as u32, derive_index: *derive_pos as u32,
derive_macro_id: *derive_macro_id, 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 // These are diagnosed by `reseed_with_unresolved_attribute`, as that function consumes them
@ -2108,7 +2113,7 @@ impl ModCollector<'_, '_> {
let ast_id = AstIdWithPath::new( let ast_id = AstIdWithPath::new(
self.file_id(), self.file_id(),
mod_item.ast_id(self.item_tree), mod_item.ast_id(self.item_tree),
attr.path.as_ref().clone(), attr.path.clone(),
); );
self.def_collector.unresolved_macros.push(MacroDirective { self.def_collector.unresolved_macros.push(MacroDirective {
module_id: self.module_id, module_id: self.module_id,
@ -2302,7 +2307,7 @@ impl ModCollector<'_, '_> {
&MacroCall { ref path, ast_id, expand_to, ctxt }: &MacroCall, &MacroCall { ref path, ast_id, expand_to, ctxt }: &MacroCall,
container: ItemContainerId, 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; let db = self.def_collector.db;
// FIXME: Immediately expanding in "Case 1" is insufficient since "Case 2" may also define // 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 // 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( if let Ok(res) = macro_call_as_call_id_with_eager(
db.upcast(), db.upcast(),
&ast_id, ast_id.ast_id,
&ast_id.path,
ctxt, ctxt,
expand_to, expand_to,
self.def_collector.def_map.krate, self.def_collector.def_map.krate,