Skip to content

Commit 4d8191c

Browse files
committed
Record eager macros correctly in source maps
1 parent 4b7b319 commit 4d8191c

File tree

20 files changed

+171
-93
lines changed

20 files changed

+171
-93
lines changed

crates/hir-def/src/db.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,6 @@ pub trait DefDatabase:
158158
#[salsa::invoke_actual(TraitItems::trait_items_with_diagnostics_query)]
159159
fn trait_items_with_diagnostics(&self, tr: TraitId) -> (Arc<TraitItems>, DefDiagnostics);
160160

161-
// #[salsa::invoke_actual(Macro2Data::macro2_data_query)]
162-
// fn macro2_data(&self, makro: Macro2Id) -> Arc<Macro2Data>;
163-
164-
// #[salsa::invoke_actual(MacroRulesData::macro_rules_data_query)]
165-
// fn macro_rules_data(&self, makro: MacroRulesId) -> Arc<MacroRulesData>;
166-
167-
// #[salsa::invoke_actual(ProcMacroData::proc_macro_data_query)]
168-
// fn proc_macro_data(&self, makro: ProcMacroId) -> Arc<ProcMacroData>;
169-
170-
// #[salsa::invoke_actual(ExternCrateDeclData::extern_crate_decl_data_query)]
171-
// fn extern_crate_decl_data(&self, extern_crate: ExternCrateId) -> Arc<ExternCrateDeclData>;
172-
173161
#[salsa::tracked]
174162
fn variant_fields(&self, id: VariantId) -> Arc<VariantFields> {
175163
self.variant_fields_with_source_map(id).0

crates/hir-def/src/expr_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub struct ExpressionStoreSourceMap {
142142

143143
template_map: Option<Box<FormatTemplate>>,
144144

145-
expansions: FxHashMap<InFile<MacroCallPtr>, MacroFileId>,
145+
pub expansions: FxHashMap<InFile<MacroCallPtr>, MacroFileId>,
146146

147147
/// Diagnostics accumulated during lowering. These contain `AstPtr`s and so are stored in
148148
/// the source map (since they're just as volatile).

crates/hir-def/src/expr_store/expander.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::mem;
66
use base_db::Crate;
77
use cfg::CfgOptions;
88
use drop_bomb::DropBomb;
9+
use hir_expand::eager::EagerCallBackFn;
910
use hir_expand::{
1011
ExpandError, ExpandErrorKind, ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
1112
attrs::RawAttrs, mod_path::ModPath, span_map::SpanMap,
@@ -65,15 +66,19 @@ impl Expander {
6566
macro_call: ast::MacroCall,
6667
krate: Crate,
6768
resolver: impl Fn(&ModPath) -> Option<MacroId>,
69+
eager_callback: EagerCallBackFn<'_>,
6870
) -> Result<ExpandResult<Option<(Mark, Option<Parse<T>>)>>, UnresolvedMacro> {
6971
// FIXME: within_limit should support this, instead of us having to extract the error
7072
let mut unresolved_macro_err = None;
7173

7274
let result = self.within_limit(db, |this| {
7375
let macro_call = this.in_file(&macro_call);
74-
match macro_call.as_call_id_with_errors(db.upcast(), krate, |path| {
75-
resolver(path).map(|it| db.macro_def(it))
76-
}) {
76+
match macro_call.as_call_id_with_errors(
77+
db.upcast(),
78+
krate,
79+
|path| resolver(path).map(|it| db.macro_def(it)),
80+
eager_callback,
81+
) {
7782
Ok(call_id) => call_id,
7883
Err(resolve_err) => {
7984
unresolved_macro_err = Some(resolve_err);

crates/hir-def/src/expr_store/lower.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,18 @@ impl ExprCollector<'_> {
19261926
.0
19271927
.take_macros()
19281928
};
1929-
self.expander.enter_expand(self.db, mcall, self.module.krate(), resolver)
1929+
self.expander.enter_expand(
1930+
self.db,
1931+
mcall,
1932+
self.module.krate(),
1933+
resolver,
1934+
&mut |ptr, call| {
1935+
_ = self
1936+
.source_map
1937+
.expansions
1938+
.insert(ptr.map(|(it, _)| it), call.as_macro_file());
1939+
},
1940+
)
19301941
}
19311942
};
19321943

crates/hir-def/src/expr_store/tests/signatures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn ret_impl_trait() -> impl Trait {}
106106
T: Trait::<Item = A>,
107107
(): Default
108108
{...}
109-
const async unsafe extern ""C"" fn a() -> impl ::core::future::Future::<Output = ()> {...}
109+
const async unsafe extern "C" fn a() -> impl ::core::future::Future::<Output = ()> {...}
110110
fn ret_impl_trait() -> impl Trait {...}
111111
"#]],
112112
);

crates/hir-def/src/item_scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl ItemScope {
453453
)
454454
}
455455

456-
pub(crate) fn macro_invoc(&self, call: AstId<ast::MacroCall>) -> Option<MacroCallId> {
456+
pub fn macro_invoc(&self, call: AstId<ast::MacroCall>) -> Option<MacroCallId> {
457457
self.macro_invocations.get(&call).copied()
458458
}
459459

crates/hir-def/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,13 +1182,18 @@ impl ModuleDefId {
11821182
}
11831183
}
11841184

1185+
// FIXME: Replace this with a plain function, it only has one impl
11851186
/// A helper trait for converting to MacroCallId
11861187
trait AsMacroCall {
11871188
fn as_call_id_with_errors(
11881189
&self,
11891190
db: &dyn ExpandDatabase,
11901191
krate: Crate,
11911192
resolver: impl Fn(&ModPath) -> Option<MacroDefId> + Copy,
1193+
eager_callback: &mut dyn FnMut(
1194+
InFile<(syntax::AstPtr<ast::MacroCall>, span::FileAstId<ast::MacroCall>)>,
1195+
MacroCallId,
1196+
),
11921197
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>;
11931198
}
11941199

@@ -1198,6 +1203,10 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
11981203
db: &dyn ExpandDatabase,
11991204
krate: Crate,
12001205
resolver: impl Fn(&ModPath) -> Option<MacroDefId> + Copy,
1206+
eager_callback: &mut dyn FnMut(
1207+
InFile<(syntax::AstPtr<ast::MacroCall>, span::FileAstId<ast::MacroCall>)>,
1208+
MacroCallId,
1209+
),
12011210
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
12021211
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
12031212
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
@@ -1227,6 +1236,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
12271236
krate,
12281237
resolver,
12291238
resolver,
1239+
eager_callback,
12301240
)
12311241
}
12321242
}
@@ -1251,6 +1261,10 @@ fn macro_call_as_call_id(
12511261
expand_to: ExpandTo,
12521262
krate: Crate,
12531263
resolver: impl Fn(&ModPath) -> Option<MacroDefId> + Copy,
1264+
eager_callback: &mut dyn FnMut(
1265+
InFile<(syntax::AstPtr<ast::MacroCall>, span::FileAstId<ast::MacroCall>)>,
1266+
MacroCallId,
1267+
),
12541268
) -> Result<Option<MacroCallId>, UnresolvedMacro> {
12551269
macro_call_as_call_id_with_eager(
12561270
db,
@@ -1261,6 +1275,7 @@ fn macro_call_as_call_id(
12611275
krate,
12621276
resolver,
12631277
resolver,
1278+
eager_callback,
12641279
)
12651280
.map(|res| res.value)
12661281
}
@@ -1274,6 +1289,10 @@ fn macro_call_as_call_id_with_eager(
12741289
krate: Crate,
12751290
resolver: impl FnOnce(&ModPath) -> Option<MacroDefId>,
12761291
eager_resolver: impl Fn(&ModPath) -> Option<MacroDefId>,
1292+
eager_callback: &mut dyn FnMut(
1293+
InFile<(syntax::AstPtr<ast::MacroCall>, span::FileAstId<ast::MacroCall>)>,
1294+
MacroCallId,
1295+
),
12771296
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
12781297
let def = resolver(path).ok_or_else(|| UnresolvedMacro { path: path.clone() })?;
12791298

@@ -1286,6 +1305,7 @@ fn macro_call_as_call_id_with_eager(
12861305
def,
12871306
call_site,
12881307
&|path| eager_resolver(path).filter(MacroDefId::is_fn_like),
1308+
eager_callback,
12891309
),
12901310
_ if def.is_fn_like() => ExpandResult {
12911311
value: Some(def.make_call(

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,16 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
131131
for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
132132
let macro_call = InFile::new(source.file_id, &macro_call);
133133
let res = macro_call
134-
.as_call_id_with_errors(&db, krate, |path| {
135-
resolver
136-
.resolve_path_as_macro(&db, path, Some(MacroSubNs::Bang))
137-
.map(|(it, _)| db.macro_def(it))
138-
})
134+
.as_call_id_with_errors(
135+
&db,
136+
krate,
137+
|path| {
138+
resolver
139+
.resolve_path_as_macro(&db, path, Some(MacroSubNs::Bang))
140+
.map(|(it, _)| db.macro_def(it))
141+
},
142+
&mut |_, _| (),
143+
)
139144
.unwrap();
140145
let macro_call_id = res.value.unwrap();
141146
let macro_file = MacroFileId { macro_call_id };

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ impl<'a> AssocItemCollector<'a> {
265265
expand_to,
266266
self.module_id.krate(),
267267
resolver,
268+
&mut |ptr, call_id| {
269+
self.macro_calls.push((ptr.map(|(_, it)| it.upcast()), call_id))
270+
},
268271
) {
269272
Ok(Some(call_id)) => {
270273
self.macro_calls

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,12 @@ impl DefCollector<'_> {
12731273
*expand_to,
12741274
self.def_map.krate,
12751275
resolver_def_id,
1276+
&mut |_ptr, _call_id| {
1277+
// FIXME:
1278+
// self.def_map.modules[directive.module_id]
1279+
// .scope
1280+
// .add_macro_invoc(ptr.map(|(_, it)| it), call_id);
1281+
},
12761282
);
12771283
if let Ok(Some(call_id)) = call_id {
12781284
self.def_map.modules[directive.module_id]
@@ -1564,6 +1570,7 @@ impl DefCollector<'_> {
15641570
);
15651571
resolved_res.resolved_def.take_macros().map(|it| self.db.macro_def(it))
15661572
},
1573+
&mut |_, _| (),
15671574
);
15681575
if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
15691576
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
@@ -2498,6 +2505,12 @@ impl ModCollector<'_, '_> {
24982505
);
24992506
resolved_res.resolved_def.take_macros().map(|it| db.macro_def(it))
25002507
},
2508+
&mut |_ptr, _call_id| {
2509+
// FIXME:
2510+
// self.def_collector.def_map.modules[self.module_id]
2511+
// .scope
2512+
// .add_macro_invoc(ptr.map(|(_, it)| it), call_id);
2513+
},
25012514
) {
25022515
// FIXME: if there were errors, this might've been in the eager expansion from an
25032516
// unresolved macro, so we need to push this into late macro resolution. see fixme above

crates/hir-def/src/resolver.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131
BindingId, ExprId, LabelId,
3232
generics::{GenericParams, TypeOrConstParamData},
3333
},
34-
item_scope::{BUILTIN_SCOPE, BuiltinShadowMode, ImportOrExternCrate, ImportOrGlob},
34+
item_scope::{BUILTIN_SCOPE, BuiltinShadowMode, ImportOrExternCrate, ImportOrGlob, ItemScope},
3535
item_tree::ImportAlias,
3636
lang_item::LangItemTarget,
3737
nameres::{DefMap, LocalDefMap, MacroSubNs, ResolvePathResultPrefixInfo},
@@ -276,7 +276,7 @@ impl Resolver {
276276
) -> Option<Visibility> {
277277
match visibility {
278278
RawVisibility::Module(_, _) => {
279-
let (item_map, item_local_map, module) = self.item_scope();
279+
let (item_map, item_local_map, module) = self.item_scope_();
280280
item_map.resolve_visibility(
281281
item_local_map,
282282
db,
@@ -477,7 +477,7 @@ impl Resolver {
477477
path: &ModPath,
478478
expected_macro_kind: Option<MacroSubNs>,
479479
) -> Option<(MacroId, Option<ImportOrGlob>)> {
480-
let (item_map, item_local_map, module) = self.item_scope();
480+
let (item_map, item_local_map, module) = self.item_scope_();
481481
item_map
482482
.resolve_path(
483483
item_local_map,
@@ -667,16 +667,21 @@ impl Resolver {
667667
}
668668

669669
pub fn module(&self) -> ModuleId {
670-
let (def_map, _, local_id) = self.item_scope();
670+
let (def_map, _, local_id) = self.item_scope_();
671671
def_map.module_id(local_id)
672672
}
673673

674+
pub fn item_scope(&self) -> &ItemScope {
675+
let (def_map, _, local_id) = self.item_scope_();
676+
&def_map[local_id].scope
677+
}
678+
674679
pub fn krate(&self) -> Crate {
675680
self.module_scope.def_map.krate()
676681
}
677682

678683
pub fn def_map(&self) -> &DefMap {
679-
self.item_scope().0
684+
self.item_scope_().0
680685
}
681686

682687
pub fn generic_def(&self) -> Option<GenericDefId> {
@@ -931,7 +936,7 @@ impl Resolver {
931936
path: &ModPath,
932937
shadow: BuiltinShadowMode,
933938
) -> PerNs {
934-
let (item_map, item_local_map, module) = self.item_scope();
939+
let (item_map, item_local_map, module) = self.item_scope_();
935940
// This method resolves `path` just like import paths, so no expected macro subns is given.
936941
let (module_res, segment_index) =
937942
item_map.resolve_path(item_local_map, db, module, path, shadow, None);
@@ -942,7 +947,7 @@ impl Resolver {
942947
}
943948

944949
/// The innermost block scope that contains items or the module scope that contains this resolver.
945-
fn item_scope(&self) -> (&DefMap, &LocalDefMap, LocalModuleId) {
950+
fn item_scope_(&self) -> (&DefMap, &LocalDefMap, LocalModuleId) {
946951
self.scopes()
947952
.find_map(|scope| match scope {
948953
Scope::BlockScope(m) => Some((&*m.def_map, &*m.local_def_map, m.module_id)),

crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -784,13 +784,13 @@ fn include_str_expand(
784784
db: &dyn ExpandDatabase,
785785
arg_id: MacroCallId,
786786
tt: &tt::TopSubtree,
787-
span: Span,
787+
call_site: Span,
788788
) -> ExpandResult<tt::TopSubtree> {
789-
let (path, span) = match parse_string(tt) {
789+
let (path, input_span) = match parse_string(tt) {
790790
Ok(it) => it,
791791
Err(e) => {
792792
return ExpandResult::new(
793-
tt::TopSubtree::empty(DelimSpan { open: span, close: span }),
793+
tt::TopSubtree::empty(DelimSpan { open: call_site, close: call_site }),
794794
e,
795795
);
796796
}
@@ -800,17 +800,17 @@ fn include_str_expand(
800800
// it's unusual to `include_str!` a Rust file), but we can return an empty string.
801801
// Ideally, we'd be able to offer a precise expansion if the user asks for macro
802802
// expansion.
803-
let file_id = match relative_file(db, arg_id, path.as_str(), true, span) {
803+
let file_id = match relative_file(db, arg_id, path.as_str(), true, input_span) {
804804
Ok(file_id) => file_id,
805805
Err(_) => {
806-
return ExpandResult::ok(quote!(span =>""));
806+
return ExpandResult::ok(quote!(call_site =>""));
807807
}
808808
};
809809

810810
let text = db.file_text(file_id.file_id());
811811
let text = &*text.text(db);
812812

813-
ExpandResult::ok(quote!(span =>#text))
813+
ExpandResult::ok(quote!(call_site =>#text))
814814
}
815815

816816
fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &Symbol) -> Option<String> {

0 commit comments

Comments
 (0)