Skip to content

Commit 2796851

Browse files
committed
Auto merge of #14998 - Veykril:eager-mapping, r=Veykril
internal: Lazy eager macros This PR makes eager macros less eager. We now only eagerly expand the input of them, while the actual expansion of the macro itself now happens like other lazy macros. This change allows unifying a lot of macro handling between the two now, most of the special casing now happens for `include!` specifically as it is a very unique macro (by having two inputs that come from differing files). Fixes #14841 Fixes #14996
2 parents 9973b11 + a02b9b2 commit 2796851

File tree

13 files changed

+355
-276
lines changed

13 files changed

+355
-276
lines changed

crates/hir-def/src/expander.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ impl Expander {
113113
call_id: MacroCallId,
114114
error: Option<ExpandError>,
115115
) -> ExpandResult<Option<InFile<Parse<SyntaxNode>>>> {
116-
let file_id = call_id.as_file();
117-
let ExpandResult { value, err } = db.parse_or_expand_with_err(file_id);
116+
let macro_file = call_id.as_macro_file();
117+
let ExpandResult { value, err } = db.parse_macro_expansion(macro_file);
118118

119-
ExpandResult { value: Some(InFile::new(file_id, value)), err: error.or(err) }
119+
ExpandResult { value: Some(InFile::new(macro_file.into(), value.0)), err: error.or(err) }
120120
}
121121

122122
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
@@ -179,8 +179,8 @@ impl Expander {
179179
} else if self.recursion_limit.check(self.recursion_depth as usize + 1).is_err() {
180180
self.recursion_depth = u32::MAX;
181181
cov_mark::hit!(your_stack_belongs_to_me);
182-
return ExpandResult::only_err(ExpandError::Other(
183-
"reached recursion limit during macro expansion".into(),
182+
return ExpandResult::only_err(ExpandError::other(
183+
"reached recursion limit during macro expansion",
184184
));
185185
}
186186

crates/hir-def/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use hir_expand::{
7171
builtin_derive_macro::BuiltinDeriveExpander,
7272
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
7373
db::ExpandDatabase,
74-
eager::expand_eager_macro,
74+
eager::expand_eager_macro_input,
7575
hygiene::Hygiene,
7676
proc_macro::ProcMacroExpander,
7777
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
@@ -865,7 +865,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
865865
let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
866866

867867
let Some(path) = path else {
868-
return Ok(ExpandResult::only_err(ExpandError::Other("malformed macro invocation".into())));
868+
return Ok(ExpandResult::only_err(ExpandError::other("malformed macro invocation")));
869869
};
870870

871871
macro_call_as_call_id_(
@@ -913,7 +913,7 @@ fn macro_call_as_call_id_(
913913

914914
let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
915915
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
916-
expand_eager_macro(db, krate, macro_call, def, &resolver)?
916+
expand_eager_macro_input(db, krate, macro_call, def, &resolver)?
917917
} else {
918918
ExpandResult {
919919
value: Some(def.as_lazy_macro(

crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn main() { env!("TEST_ENV_VAR"); }
7979
#[rustc_builtin_macro]
8080
macro_rules! env {() => {}}
8181
82-
fn main() { "__RA_UNIMPLEMENTED__"; }
82+
fn main() { "UNRESOLVED_ENV_VAR"; }
8383
"##]],
8484
);
8585
}
@@ -442,10 +442,6 @@ macro_rules! surprise {
442442
() => { "s" };
443443
}
444444
445-
macro_rules! stuff {
446-
($string:expr) => { concat!($string) };
447-
}
448-
449445
fn main() { concat!(surprise!()); }
450446
"##,
451447
expect![[r##"
@@ -456,10 +452,6 @@ macro_rules! surprise {
456452
() => { "s" };
457453
}
458454
459-
macro_rules! stuff {
460-
($string:expr) => { concat!($string) };
461-
}
462-
463455
fn main() { "s"; }
464456
"##]],
465457
);

crates/hir-expand/src/builtin_derive_macro.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
194194
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems);
195195
let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
196196
debug!("derive node didn't parse");
197-
ExpandError::Other("invalid item definition".into())
197+
ExpandError::other("invalid item definition")
198198
})?;
199199
let item = macro_items.items().next().ok_or_else(|| {
200200
debug!("no module item parsed");
201-
ExpandError::Other("no item found".into())
201+
ExpandError::other("no item found")
202202
})?;
203203
let adt = ast::Adt::cast(item.syntax().clone()).ok_or_else(|| {
204204
debug!("expected adt, found: {:?}", item);
205-
ExpandError::Other("expected struct, enum or union".into())
205+
ExpandError::other("expected struct, enum or union")
206206
})?;
207207
let (name, generic_param_list, shape) = match &adt {
208208
ast::Adt::Struct(it) => (
@@ -305,7 +305,7 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
305305
fn name_to_token(token_map: &TokenMap, name: Option<ast::Name>) -> Result<tt::Ident, ExpandError> {
306306
let name = name.ok_or_else(|| {
307307
debug!("parsed item has no name");
308-
ExpandError::Other("missing name".into())
308+
ExpandError::other("missing name")
309309
})?;
310310
let name_token_id =
311311
token_map.token_by_range(name.syntax().text_range()).unwrap_or_else(TokenId::unspecified);

0 commit comments

Comments
 (0)