Skip to content

Commit 3ee9346

Browse files
committed
Collect nameres eager macros
1 parent b2ed56f commit 3ee9346

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

crates/hir-def/src/db.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub trait DefDatabase:
149149
fn enum_variants_with_diagnostics(
150150
&self,
151151
id: EnumId,
152-
) -> (Arc<EnumVariants>, Arc<ThinVec<InactiveEnumVariantCode>>);
152+
) -> (Arc<EnumVariants>, Option<Arc<ThinVec<InactiveEnumVariantCode>>>);
153153

154154
#[salsa::transparent]
155155
#[salsa::invoke_actual(ImplItems::impl_items_query)]

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ impl DefCollector<'_> {
12291229
No,
12301230
}
12311231

1232+
let mut eager_callback_buffer = vec![];
12321233
let mut res = ReachedFixedPoint::Yes;
12331234
// Retain unresolved macros after this round of resolution.
12341235
let mut retain = |directive: &MacroDirective| {
@@ -1261,11 +1262,8 @@ impl DefCollector<'_> {
12611262
*expand_to,
12621263
self.def_map.krate,
12631264
resolver_def_id,
1264-
&mut |_ptr, _call_id| {
1265-
// FIXME:
1266-
// self.def_map.modules[directive.module_id]
1267-
// .scope
1268-
// .add_macro_invoc(ptr.map(|(_, it)| it), call_id);
1265+
&mut |ptr, call_id| {
1266+
eager_callback_buffer.push((directive.module_id, ptr, call_id));
12691267
},
12701268
);
12711269
if let Ok(Some(call_id)) = call_id {
@@ -1492,6 +1490,10 @@ impl DefCollector<'_> {
14921490
macros.extend(mem::take(&mut self.unresolved_macros));
14931491
self.unresolved_macros = macros;
14941492

1493+
for (module_id, ptr, call_id) in eager_callback_buffer {
1494+
self.def_map.modules[module_id].scope.add_macro_invoc(ptr.map(|(_, it)| it), call_id);
1495+
}
1496+
14951497
for (module_id, depth, container, macro_call_id) in resolved {
14961498
self.collect_macro_expansion(module_id, macro_call_id, depth, container);
14971499
}
@@ -2419,6 +2421,7 @@ impl ModCollector<'_, '_> {
24192421
// new legacy macros that create textual scopes. We need a way to resolve names in textual
24202422
// scopes without eager expansion.
24212423

2424+
let mut eager_callback_buffer = vec![];
24222425
// Case 1: try to resolve macro calls with single-segment name and expand macro_rules
24232426
if let Ok(res) = macro_call_as_call_id_with_eager(
24242427
db.upcast(),
@@ -2460,13 +2463,13 @@ impl ModCollector<'_, '_> {
24602463
);
24612464
resolved_res.resolved_def.take_macros().map(|it| db.macro_def(it))
24622465
},
2463-
&mut |_ptr, _call_id| {
2464-
// FIXME:
2465-
// self.def_collector.def_map.modules[self.module_id]
2466-
// .scope
2467-
// .add_macro_invoc(ptr.map(|(_, it)| it), call_id);
2468-
},
2466+
&mut |ptr, call_id| eager_callback_buffer.push((ptr, call_id)),
24692467
) {
2468+
for (ptr, call_id) in eager_callback_buffer {
2469+
self.def_collector.def_map.modules[self.module_id]
2470+
.scope
2471+
.add_macro_invoc(ptr.map(|(_, it)| it), call_id);
2472+
}
24702473
// FIXME: if there were errors, this might've been in the eager expansion from an
24712474
// unresolved macro, so we need to push this into late macro resolution. see fixme above
24722475
if res.err.is_none() {
@@ -2629,7 +2632,7 @@ foo!(KABOOM);
26292632
// the release mode. That's why the argument is not an ra_fixture --
26302633
// otherwise injection highlighting gets stuck.
26312634
//
2632-
// We need to find a way to fail this faster.
2635+
// We need to find a way to fail this faster!
26332636
do_resolve(
26342637
r#"
26352638
macro_rules! foo {

crates/hir-def/src/signatures.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Item signature IR definitions
22
3+
use std::ops::Not as _;
4+
35
use bitflags::bitflags;
46
use cfg::{CfgExpr, CfgOptions};
57
use either::Either;
@@ -907,7 +909,7 @@ impl EnumVariants {
907909
pub(crate) fn enum_variants_query(
908910
db: &dyn DefDatabase,
909911
e: EnumId,
910-
) -> (Arc<EnumVariants>, Arc<ThinVec<InactiveEnumVariantCode>>) {
912+
) -> (Arc<EnumVariants>, Option<Arc<ThinVec<InactiveEnumVariantCode>>>) {
911913
let loc = e.lookup(db);
912914
let item_tree = loc.id.item_tree(db);
913915

@@ -937,7 +939,10 @@ impl EnumVariants {
937939
})
938940
.collect();
939941

940-
(Arc::new(EnumVariants { variants }), Arc::new(diagnostics))
942+
(
943+
Arc::new(EnumVariants { variants }),
944+
diagnostics.is_empty().not().then(|| Arc::new(diagnostics)),
945+
)
941946
}
942947

943948
pub fn variant(&self, name: &Name) -> Option<EnumVariantId> {

crates/hir/src/lib.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -691,18 +691,20 @@ impl Module {
691691
let (variants, diagnostics) = db.enum_variants_with_diagnostics(e.id);
692692
let file = e.id.lookup(db.upcast()).id.file_id();
693693
let ast_id_map = db.ast_id_map(file);
694-
for diag in diagnostics.iter() {
695-
acc.push(
696-
InactiveCode {
697-
node: InFile::new(
698-
file,
699-
ast_id_map.get(diag.ast_id).syntax_node_ptr(),
700-
),
701-
cfg: diag.cfg.clone(),
702-
opts: diag.opts.clone(),
703-
}
704-
.into(),
705-
);
694+
if let Some(diagnostics) = &diagnostics {
695+
for diag in diagnostics.iter() {
696+
acc.push(
697+
InactiveCode {
698+
node: InFile::new(
699+
file,
700+
ast_id_map.get(diag.ast_id).syntax_node_ptr(),
701+
),
702+
cfg: diag.cfg.clone(),
703+
opts: diag.opts.clone(),
704+
}
705+
.into(),
706+
);
707+
}
706708
}
707709
for &(v, _) in &variants.variants {
708710
let source_map = db.variant_fields_with_source_map(v.into()).1;

crates/ide-diagnostics/src/handlers/macro_error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ include!("foo/bar.rs");
123123

124124
#[test]
125125
fn good_out_dir_diagnostic() {
126+
// FIXME: The diagnostic here is duplicated for each eager expansion
126127
check_diagnostics(
127128
r#"
128129
#[rustc_builtin_macro]
@@ -134,6 +135,8 @@ macro_rules! concat { () => {} }
134135
135136
include!(concat!(env!("OUT_DIR"), "/out.rs"));
136137
//^^^^^^^^^ error: `OUT_DIR` not set, build scripts may have failed to run
138+
//^^^^^^^^^^^^^^^ error: `OUT_DIR` not set, build scripts may have failed to run
139+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: `OUT_DIR` not set, build scripts may have failed to run
137140
"#,
138141
);
139142
}

0 commit comments

Comments
 (0)