internal: Don't unnecessarily clone ModPaths in early name res

This commit is contained in:
Lukas Wirth 2024-06-12 11:25:19 +02:00
parent 72dfbe95de
commit 855282fa53
12 changed files with 29 additions and 30 deletions

View file

@ -965,7 +965,7 @@ impl ExprCollector<'_> {
.resolve_path( .resolve_path(
self.db, self.db,
module, module,
&path, path,
crate::item_scope::BuiltinShadowMode::Other, crate::item_scope::BuiltinShadowMode::Other,
Some(MacroSubNs::Bang), Some(MacroSubNs::Bang),
) )

View file

@ -719,12 +719,12 @@ impl<'a> AssocItemCollector<'a> {
let MacroCall { ast_id, expand_to, ctxt, ref path } = item_tree[call]; let MacroCall { ast_id, expand_to, ctxt, ref path } = item_tree[call];
let module = self.expander.module.local_id; let module = self.expander.module.local_id;
let resolver = |path| { let resolver = |path: &_| {
self.def_map self.def_map
.resolve_path( .resolve_path(
self.db, self.db,
module, module,
&path, path,
crate::item_scope::BuiltinShadowMode::Other, crate::item_scope::BuiltinShadowMode::Other,
Some(MacroSubNs::Bang), Some(MacroSubNs::Bang),
) )

View file

@ -56,7 +56,7 @@ impl Expander {
&mut self, &mut self,
db: &dyn DefDatabase, db: &dyn DefDatabase,
macro_call: ast::MacroCall, macro_call: ast::MacroCall,
resolver: impl Fn(ModPath) -> Option<MacroId>, resolver: impl Fn(&ModPath) -> Option<MacroId>,
) -> Result<ExpandResult<Option<(Mark, Parse<T>)>>, UnresolvedMacro> { ) -> Result<ExpandResult<Option<(Mark, Parse<T>)>>, UnresolvedMacro> {
// FIXME: within_limit should support this, instead of us having to extract the error // FIXME: within_limit should support this, instead of us having to extract the error
let mut unresolved_macro_err = None; let mut unresolved_macro_err = None;

View file

@ -403,12 +403,12 @@ impl GenericParamsCollector {
let (def_map, expander) = &mut **exp; let (def_map, expander) = &mut **exp;
let module = expander.module.local_id; let module = expander.module.local_id;
let resolver = |path| { let resolver = |path: &_| {
def_map def_map
.resolve_path( .resolve_path(
db, db,
module, module,
&path, path,
crate::item_scope::BuiltinShadowMode::Other, crate::item_scope::BuiltinShadowMode::Other,
Some(MacroSubNs::Bang), Some(MacroSubNs::Bang),
) )

View file

@ -1370,7 +1370,7 @@ pub trait AsMacroCall {
&self, &self,
db: &dyn ExpandDatabase, db: &dyn ExpandDatabase,
krate: CrateId, krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy, resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Option<MacroCallId> { ) -> Option<MacroCallId> {
self.as_call_id_with_errors(db, krate, resolver).ok()?.value self.as_call_id_with_errors(db, krate, resolver).ok()?.value
} }
@ -1379,7 +1379,7 @@ pub trait AsMacroCall {
&self, &self,
db: &dyn ExpandDatabase, db: &dyn ExpandDatabase,
krate: CrateId, krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy, resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>; ) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>;
} }
@ -1388,7 +1388,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
&self, &self,
db: &dyn ExpandDatabase, db: &dyn ExpandDatabase,
krate: CrateId, krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy, resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> { ) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
let expands_to = hir_expand::ExpandTo::from_call_site(self.value); let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value)); let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
@ -1437,7 +1437,7 @@ fn macro_call_as_call_id(
call_site: SyntaxContextId, call_site: SyntaxContextId,
expand_to: ExpandTo, expand_to: ExpandTo,
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(db, call, call_site, expand_to, krate, resolver, resolver)
.map(|res| res.value) .map(|res| res.value)
@ -1449,11 +1449,10 @@ fn macro_call_as_call_id_with_eager(
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 = let def = resolver(&call.path).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
let res = match def.kind { let res = match def.kind {
MacroDefKind::BuiltInEager(..) => expand_eager_macro_input( MacroDefKind::BuiltInEager(..) => expand_eager_macro_input(

View file

@ -96,7 +96,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
let res = macro_call let res = macro_call
.as_call_id_with_errors(&db, krate, |path| { .as_call_id_with_errors(&db, krate, |path| {
resolver resolver
.resolve_path_as_macro(&db, &path, Some(MacroSubNs::Bang)) .resolve_path_as_macro(&db, path, Some(MacroSubNs::Bang))
.map(|(it, _)| db.macro_def(it)) .map(|(it, _)| db.macro_def(it))
}) })
.unwrap(); .unwrap();

View file

@ -137,10 +137,10 @@ pub(super) fn derive_macro_as_call_id(
derive_pos: u32, derive_pos: u32,
call_site: SyntaxContextId, call_site: SyntaxContextId,
krate: CrateId, krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>, resolver: impl Fn(&path::ModPath) -> Option<(MacroId, MacroDefId)>,
derive_macro_id: MacroCallId, derive_macro_id: MacroCallId,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> { ) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
let (macro_id, def_id) = resolver(item_attr.path.clone()) 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.clone() })?;
let call_id = def_id.make_call( let call_id = def_id.make_call(

View file

@ -1137,18 +1137,18 @@ impl DefCollector<'_> {
MacroSubNs::Attr MacroSubNs::Attr
} }
}; };
let resolver = |path| { let resolver = |path: &_| {
let resolved_res = self.def_map.resolve_path_fp_with_macro( let resolved_res = self.def_map.resolve_path_fp_with_macro(
self.db, self.db,
ResolveMode::Other, ResolveMode::Other,
directive.module_id, directive.module_id,
&path, path,
BuiltinShadowMode::Module, BuiltinShadowMode::Module,
Some(subns), Some(subns),
); );
resolved_res.resolved_def.take_macros().map(|it| (it, self.db.macro_def(it))) resolved_res.resolved_def.take_macros().map(|it| (it, self.db.macro_def(it)))
}; };
let resolver_def_id = |path| resolver(path).map(|(_, it)| it); let resolver_def_id = |path: &_| resolver(path).map(|(_, it)| it);
match &directive.kind { match &directive.kind {
MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => { MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => {
@ -1251,7 +1251,7 @@ impl DefCollector<'_> {
} }
} }
let def = match resolver_def_id(path.clone()) { let def = match resolver_def_id(path) {
Some(def) if def.is_attribute() => def, Some(def) if def.is_attribute() => def,
_ => return Resolved::No, _ => return Resolved::No,
}; };
@ -1439,7 +1439,7 @@ impl DefCollector<'_> {
self.db, self.db,
ResolveMode::Other, ResolveMode::Other,
directive.module_id, directive.module_id,
&path, path,
BuiltinShadowMode::Module, BuiltinShadowMode::Module,
Some(MacroSubNs::Bang), Some(MacroSubNs::Bang),
); );
@ -2339,7 +2339,7 @@ impl ModCollector<'_, '_> {
db, db,
ResolveMode::Other, ResolveMode::Other,
self.module_id, self.module_id,
&path, path,
BuiltinShadowMode::Module, BuiltinShadowMode::Module,
Some(MacroSubNs::Bang), Some(MacroSubNs::Bang),
); );

View file

@ -39,7 +39,7 @@ pub fn expand_eager_macro_input(
ast_id: AstId<ast::MacroCall>, ast_id: AstId<ast::MacroCall>,
def: MacroDefId, def: MacroDefId,
call_site: SyntaxContextId, call_site: SyntaxContextId,
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>, resolver: &dyn Fn(&ModPath) -> Option<MacroDefId>,
) -> ExpandResult<Option<MacroCallId>> { ) -> ExpandResult<Option<MacroCallId>> {
let expand_to = ExpandTo::from_call_site(macro_call); let expand_to = ExpandTo::from_call_site(macro_call);
@ -138,7 +138,7 @@ fn eager_macro_recur(
curr: InFile<SyntaxNode>, curr: InFile<SyntaxNode>,
krate: CrateId, krate: CrateId,
call_site: SyntaxContextId, call_site: SyntaxContextId,
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>, macro_resolver: &dyn Fn(&ModPath) -> Option<MacroDefId>,
) -> ExpandResult<Option<(SyntaxNode, TextSize)>> { ) -> ExpandResult<Option<(SyntaxNode, TextSize)>> {
let original = curr.value.clone_for_update(); let original = curr.value.clone_for_update();
@ -172,7 +172,7 @@ fn eager_macro_recur(
let def = match call.path().and_then(|path| { let def = match call.path().and_then(|path| {
ModPath::from_src(db, path, &mut |range| span_map.span_at(range.start()).ctx) ModPath::from_src(db, path, &mut |range| span_map.span_at(range.start()).ctx)
}) { }) {
Some(path) => match macro_resolver(path.clone()) { Some(path) => match macro_resolver(&path) {
Some(def) => def, Some(def) => def,
None => { None => {
error = error =

View file

@ -416,9 +416,9 @@ impl<'a> TyLoweringContext<'a> {
}; };
let ty = { let ty = {
let macro_call = macro_call.to_node(self.db.upcast()); let macro_call = macro_call.to_node(self.db.upcast());
let resolver = |path| { let resolver = |path: &_| {
self.resolver self.resolver
.resolve_path_as_macro(self.db.upcast(), &path, Some(MacroSubNs::Bang)) .resolve_path_as_macro(self.db.upcast(), path, Some(MacroSubNs::Bang))
.map(|(it, _)| it) .map(|(it, _)| it)
}; };
match expander.enter_expand::<ast::Type>(self.db.upcast(), macro_call, resolver) match expander.enter_expand::<ast::Type>(self.db.upcast(), macro_call, resolver)

View file

@ -430,7 +430,7 @@ impl<'db> SemanticsImpl<'db> {
let macro_call = InFile::new(file_id, actual_macro_call); let macro_call = InFile::new(file_id, actual_macro_call);
let krate = resolver.krate(); let krate = resolver.krate();
let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| { let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| {
resolver.resolve_path_as_macro_def(self.db.upcast(), &path, Some(MacroSubNs::Bang)) resolver.resolve_path_as_macro_def(self.db.upcast(), path, Some(MacroSubNs::Bang))
})?; })?;
hir_expand::db::expand_speculative( hir_expand::db::expand_speculative(
self.db.upcast(), self.db.upcast(),

View file

@ -826,7 +826,7 @@ impl SourceAnalyzer {
// FIXME: This causes us to parse, generally this is the wrong approach for resolving a // FIXME: This causes us to parse, generally this is the wrong approach for resolving a
// macro call to a macro call id! // macro call to a macro call id!
let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| { let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| {
self.resolver.resolve_path_as_macro_def(db.upcast(), &path, Some(MacroSubNs::Bang)) self.resolver.resolve_path_as_macro_def(db.upcast(), path, Some(MacroSubNs::Bang))
})?; })?;
// why the 64? // why the 64?
Some(macro_call_id.as_macro_file()).filter(|it| it.expansion_level(db.upcast()) < 64) Some(macro_call_id.as_macro_file()).filter(|it| it.expansion_level(db.upcast()) < 64)