Skip to content

Commit e426796

Browse files
committed
Support local_inner_macros
1 parent a5f2b16 commit e426796

File tree

10 files changed

+97
-19
lines changed

10 files changed

+97
-19
lines changed

crates/ra_hir/src/semantics/source_to_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl SourceToDefCtx<'_, '_> {
151151
let krate = self.file_to_def(file_id)?.krate;
152152
let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
153153
let ast_id = Some(AstId::new(src.file_id, file_ast_id));
154-
Some(MacroDefId { krate: Some(krate), ast_id, kind })
154+
Some(MacroDefId { krate: Some(krate), ast_id, kind, local_inner: false })
155155
}
156156

157157
pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {

crates/ra_hir_def/src/body/lower.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ impl ExprCollector<'_> {
430430
krate: Some(self.expander.module.krate),
431431
ast_id: Some(self.expander.ast_id(&e)),
432432
kind: MacroDefKind::Declarative,
433+
local_inner: false,
433434
};
434435
self.body.item_scope.define_legacy_macro(name, mac);
435436

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ impl DefCollector<'_> {
204204
ast_id: None,
205205
krate: Some(krate),
206206
kind: MacroDefKind::CustomDerive(expander),
207+
local_inner: false,
207208
};
208209

209210
self.define_proc_macro(name.clone(), macro_id);
@@ -941,6 +942,7 @@ impl ModCollector<'_, '_> {
941942
ast_id: Some(ast_id.ast_id),
942943
krate: Some(self.def_collector.def_map.krate),
943944
kind: MacroDefKind::Declarative,
945+
local_inner: mac.local_inner,
944946
};
945947
self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export);
946948
}

crates/ra_hir_def/src/nameres/raw.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ pub(super) struct MacroData {
188188
pub(super) path: ModPath,
189189
pub(super) name: Option<Name>,
190190
pub(super) export: bool,
191+
pub(super) local_inner: bool,
191192
pub(super) builtin: bool,
192193
}
193194

@@ -401,14 +402,28 @@ impl RawItemsCollector {
401402

402403
let name = m.name().map(|it| it.as_name());
403404
let ast_id = self.source_ast_id_map.ast_id(&m);
404-
// FIXME: cfg_attr
405-
let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export");
406405

407406
// FIXME: cfg_attr
408-
let builtin =
409-
m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "rustc_builtin_macro");
410-
411-
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export, builtin });
407+
let export = attrs.by_key("macro_export").exists();
408+
let local_inner =
409+
attrs.by_key("macro_export").tt_values().map(|it| &it.token_trees).flatten().any(
410+
|it| match it {
411+
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
412+
ident.text.contains("local_inner_macros")
413+
}
414+
_ => false,
415+
},
416+
);
417+
let builtin = attrs.by_key("rustc_builtin_macro").exists();
418+
419+
let m = self.raw_items.macros.alloc(MacroData {
420+
ast_id,
421+
path,
422+
name,
423+
export,
424+
local_inner,
425+
builtin,
426+
});
412427
self.push_item(current_module, attrs, RawItemKind::Macro(m));
413428
}
414429

crates/ra_hir_def/src/path/lower.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use hir_expand::{
99
hygiene::Hygiene,
1010
name::{name, AsName},
1111
};
12-
use ra_syntax::ast::{self, AstNode, TypeAscriptionOwner, TypeBoundsOwner};
12+
use ra_syntax::{
13+
ast::{self, AstNode, TypeAscriptionOwner, TypeBoundsOwner},
14+
T,
15+
};
1316

1417
use super::AssociatedTypeBinding;
1518
use crate::{
@@ -113,6 +116,20 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
113116
}
114117
segments.reverse();
115118
generic_args.reverse();
119+
120+
// handle local_inner_macros :
121+
// Basically, even in rustc it is quite hacky:
122+
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
123+
// We follow what it did anyway :)
124+
if segments.len() == 1 && kind == PathKind::Plain {
125+
let next = path.syntax().last_token().and_then(|it| it.next_token());
126+
if next.map_or(false, |it| it.kind() == T![!]) {
127+
if let Some(crate_id) = hygiene.local_inner_macros() {
128+
kind = PathKind::DollarCrate(crate_id);
129+
}
130+
}
131+
}
132+
116133
let mod_path = ModPath { kind, segments };
117134
return Some(Path { type_anchor, mod_path, generic_args });
118135

crates/ra_hir_expand/src/builtin_derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ macro_rules! register_builtin {
3838
_ => return None,
3939
};
4040

41-
Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind) })
41+
Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind),local_inner:false })
4242
}
4343
};
4444
}

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ pub fn find_builtin_macro(
7373
krate: Some(krate),
7474
ast_id: Some(ast_id),
7575
kind: MacroDefKind::BuiltIn(kind),
76+
local_inner: false,
7677
}),
7778
Either::Right(kind) => Some(MacroDefId {
7879
krate: Some(krate),
7980
ast_id: Some(ast_id),
8081
kind: MacroDefKind::BuiltInEager(kind),
82+
local_inner: false,
8183
}),
8284
}
8385
}
@@ -406,6 +408,7 @@ mod tests {
406408
krate: Some(CrateId(0)),
407409
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
408410
kind: MacroDefKind::BuiltIn(expander),
411+
local_inner: false,
409412
};
410413

411414
let loc = MacroCallLoc {
@@ -425,6 +428,7 @@ mod tests {
425428
krate: Some(CrateId(0)),
426429
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
427430
kind: MacroDefKind::BuiltInEager(expander),
431+
local_inner: false,
428432
};
429433

430434
let args = macro_calls[1].token_tree().unwrap();

crates/ra_hir_expand/src/hygiene.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,34 @@ use crate::{
1616
pub struct Hygiene {
1717
// This is what `$crate` expands to
1818
def_crate: Option<CrateId>,
19+
20+
// Indiciate this is a local inner macro
21+
local_inner: bool,
1922
}
2023

2124
impl Hygiene {
2225
pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene {
23-
let def_crate = match file_id.0 {
24-
HirFileIdRepr::FileId(_) => None,
26+
let (def_crate, local_inner) = match file_id.0 {
27+
HirFileIdRepr::FileId(_) => (None, false),
2528
HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id {
2629
MacroCallId::LazyMacro(id) => {
2730
let loc = db.lookup_intern_macro(id);
2831
match loc.def.kind {
29-
MacroDefKind::Declarative => loc.def.krate,
30-
MacroDefKind::BuiltIn(_) => None,
31-
MacroDefKind::BuiltInDerive(_) => None,
32-
MacroDefKind::BuiltInEager(_) => None,
33-
MacroDefKind::CustomDerive(_) => None,
32+
MacroDefKind::Declarative => (loc.def.krate, loc.def.local_inner),
33+
MacroDefKind::BuiltIn(_) => (None, false),
34+
MacroDefKind::BuiltInDerive(_) => (None, false),
35+
MacroDefKind::BuiltInEager(_) => (None, false),
36+
MacroDefKind::CustomDerive(_) => (None, false),
3437
}
3538
}
36-
MacroCallId::EagerMacro(_id) => None,
39+
MacroCallId::EagerMacro(_id) => (None, false),
3740
},
3841
};
39-
Hygiene { def_crate }
42+
Hygiene { def_crate, local_inner }
4043
}
4144

4245
pub fn new_unhygienic() -> Hygiene {
43-
Hygiene { def_crate: None }
46+
Hygiene { def_crate: None, local_inner: false }
4447
}
4548

4649
// FIXME: this should just return name
@@ -52,4 +55,12 @@ impl Hygiene {
5255
}
5356
Either::Left(name_ref.as_name())
5457
}
58+
59+
pub fn local_inner_macros(&self) -> Option<CrateId> {
60+
if self.local_inner {
61+
self.def_crate
62+
} else {
63+
None
64+
}
65+
}
5566
}

crates/ra_hir_expand/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ pub struct MacroDefId {
204204
pub krate: Option<CrateId>,
205205
pub ast_id: Option<AstId<ast::MacroCall>>,
206206
pub kind: MacroDefKind,
207+
208+
pub local_inner: bool,
207209
}
208210

209211
impl MacroDefId {

crates/ra_hir_ty/src/tests/macros.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,32 @@ fn main() {
387387
);
388388
}
389389

390+
#[test]
391+
fn infer_local_inner_macros() {
392+
let (db, pos) = TestDB::with_position(
393+
r#"
394+
//- /main.rs crate:main deps:foo
395+
fn test() {
396+
let x = foo::foo!(1);
397+
x<|>;
398+
}
399+
400+
//- /lib.rs crate:foo
401+
#[macro_export(local_inner_macros)]
402+
macro_rules! foo {
403+
(1) => { bar!() };
404+
}
405+
406+
#[macro_export]
407+
macro_rules! bar {
408+
() => { 42 }
409+
}
410+
411+
"#,
412+
);
413+
assert_eq!("i32", type_at_pos(&db, pos));
414+
}
415+
390416
#[test]
391417
fn infer_builtin_macros_line() {
392418
assert_snapshot!(

0 commit comments

Comments
 (0)