Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e14d84d

Browse files
committed
Skip out on single-segment immediate macro resolution when there are errors
1 parent d999d34 commit e14d84d

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

crates/hir-def/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ pub trait AsMacroCall {
10831083
&self,
10841084
db: &dyn ExpandDatabase,
10851085
krate: CrateId,
1086-
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
1086+
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
10871087
) -> Option<MacroCallId> {
10881088
self.as_call_id_with_errors(db, krate, resolver).ok()?.value
10891089
}
@@ -1092,7 +1092,7 @@ pub trait AsMacroCall {
10921092
&self,
10931093
db: &dyn ExpandDatabase,
10941094
krate: CrateId,
1095-
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
1095+
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
10961096
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>;
10971097
}
10981098

@@ -1101,7 +1101,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
11011101
&self,
11021102
db: &dyn ExpandDatabase,
11031103
krate: CrateId,
1104-
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
1104+
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
11051105
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
11061106
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
11071107
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
@@ -1112,12 +1112,13 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
11121112
return Ok(ExpandResult::only_err(ExpandError::other("malformed macro invocation")));
11131113
};
11141114

1115-
macro_call_as_call_id_(
1115+
macro_call_as_call_id_with_eager(
11161116
db,
11171117
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
11181118
expands_to,
11191119
krate,
11201120
resolver,
1121+
resolver,
11211122
)
11221123
}
11231124
}
@@ -1140,17 +1141,19 @@ fn macro_call_as_call_id(
11401141
call: &AstIdWithPath<ast::MacroCall>,
11411142
expand_to: ExpandTo,
11421143
krate: CrateId,
1143-
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
1144+
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
11441145
) -> Result<Option<MacroCallId>, UnresolvedMacro> {
1145-
macro_call_as_call_id_(db, call, expand_to, krate, resolver).map(|res| res.value)
1146+
macro_call_as_call_id_with_eager(db, call, expand_to, krate, resolver, resolver)
1147+
.map(|res| res.value)
11461148
}
11471149

1148-
fn macro_call_as_call_id_(
1150+
fn macro_call_as_call_id_with_eager(
11491151
db: &dyn ExpandDatabase,
11501152
call: &AstIdWithPath<ast::MacroCall>,
11511153
expand_to: ExpandTo,
11521154
krate: CrateId,
1153-
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
1155+
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
1156+
eager_resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
11541157
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
11551158
let def =
11561159
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
@@ -1159,7 +1162,7 @@ fn macro_call_as_call_id_(
11591162
MacroDefKind::BuiltInEager(..) => {
11601163
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
11611164
expand_eager_macro_input(db, krate, macro_call, def, &|path| {
1162-
resolver(path).filter(MacroDefId::is_fn_like)
1165+
eager_resolver(path).filter(MacroDefId::is_fn_like)
11631166
})
11641167
}
11651168
_ if def.is_fn_like() => ExpandResult {

crates/hir-def/src/nameres/collector.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{
3838
self, ExternCrate, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId, ItemTreeNode,
3939
MacroCall, MacroDef, MacroRules, Mod, ModItem, ModKind, TreeId,
4040
},
41-
macro_call_as_call_id, macro_id_to_def_id,
41+
macro_call_as_call_id, macro_call_as_call_id_with_eager, macro_id_to_def_id,
4242
nameres::{
4343
diagnostics::DefDiagnostic,
4444
mod_resolution::ModDir,
@@ -2187,7 +2187,7 @@ impl ModCollector<'_, '_> {
21872187
// scopes without eager expansion.
21882188

21892189
// Case 1: try to resolve macro calls with single-segment name and expand macro_rules
2190-
if let Ok(res) = macro_call_as_call_id(
2190+
if let Ok(res) = macro_call_as_call_id_with_eager(
21912191
db.upcast(),
21922192
&ast_id,
21932193
mac.expand_to,
@@ -2210,19 +2210,34 @@ impl ModCollector<'_, '_> {
22102210
.map(|it| macro_id_to_def_id(self.def_collector.db, it))
22112211
})
22122212
},
2213-
) {
2214-
// Legacy macros need to be expanded immediately, so that any macros they produce
2215-
// are in scope.
2216-
if let Some(val) = res {
2217-
self.def_collector.collect_macro_expansion(
2213+
|path| {
2214+
let resolved_res = self.def_collector.def_map.resolve_path_fp_with_macro(
2215+
db,
2216+
ResolveMode::Other,
22182217
self.module_id,
2219-
val,
2220-
self.macro_depth + 1,
2221-
container,
2218+
&path,
2219+
BuiltinShadowMode::Module,
2220+
Some(MacroSubNs::Bang),
22222221
);
2223-
}
2222+
resolved_res.resolved_def.take_macros().map(|it| macro_id_to_def_id(db, it))
2223+
},
2224+
) {
2225+
// FIXME: if there were errors, this mightve been in the eager expansion from an
2226+
// unresolved macro, so we need to push this into late macro resolution. see fixme above
2227+
if res.err.is_none() {
2228+
// Legacy macros need to be expanded immediately, so that any macros they produce
2229+
// are in scope.
2230+
if let Some(val) = res.value {
2231+
self.def_collector.collect_macro_expansion(
2232+
self.module_id,
2233+
val,
2234+
self.macro_depth + 1,
2235+
container,
2236+
);
2237+
}
22242238

2225-
return;
2239+
return;
2240+
}
22262241
}
22272242

22282243
// Case 2: resolve in module scope, expand during name resolution.

0 commit comments

Comments
 (0)