Skip to content

Commit 8505642

Browse files
bors[bot]Veykril
andauthored
Merge #9193
9193: Implement dummy expansions for builtin attributes r=jonas-schievink a=Veykril Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 5f592f4 + 1e51b13 commit 8505642

File tree

10 files changed

+128
-11
lines changed

10 files changed

+128
-11
lines changed

crates/hir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ impl MacroDef {
13441344
MacroDefKind::Declarative(_) => MacroKind::Declarative,
13451345
MacroDefKind::BuiltIn(_, _) | MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
13461346
MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
1347+
MacroDefKind::BuiltInAttr(_, _) => MacroKind::Attr,
13471348
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::CustomDerive, _) => {
13481349
MacroKind::Derive
13491350
}

crates/hir_def/src/nameres/collector.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use base_db::{CrateId, Edition, FileId, ProcMacroId};
99
use cfg::{CfgExpr, CfgOptions};
1010
use hir_expand::{
1111
ast_id_map::FileAstId,
12+
builtin_attr::find_builtin_attr,
1213
builtin_derive::find_builtin_derive,
1314
builtin_macro::find_builtin_macro,
1415
name::{name, AsName, Name},
@@ -1836,7 +1837,8 @@ impl ModCollector<'_, '_> {
18361837
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
18371838
if attrs.by_key("rustc_builtin_macro").exists() {
18381839
let macro_id = find_builtin_macro(&mac.name, krate, ast_id)
1839-
.or_else(|| find_builtin_derive(&mac.name, krate, ast_id));
1840+
.or_else(|| find_builtin_derive(&mac.name, krate, ast_id))
1841+
.or_else(|| find_builtin_attr(&mac.name, krate, ast_id));
18401842

18411843
match macro_id {
18421844
Some(macro_id) => {

crates/hir_expand/src/builtin_attr.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//! Builtin derives.
2+
3+
use syntax::ast;
4+
5+
use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind};
6+
7+
macro_rules! register_builtin {
8+
( $(($name:ident, $variant:ident) => $expand:ident),* ) => {
9+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10+
pub enum BuiltinAttrExpander {
11+
$($variant),*
12+
}
13+
14+
impl BuiltinAttrExpander {
15+
pub fn expand(
16+
&self,
17+
db: &dyn AstDatabase,
18+
id: MacroCallId,
19+
tt: &tt::Subtree,
20+
) -> Result<tt::Subtree, mbe::ExpandError> {
21+
let expander = match *self {
22+
$( BuiltinAttrExpander::$variant => $expand, )*
23+
};
24+
expander(db, id, tt)
25+
}
26+
27+
fn find_by_name(name: &name::Name) -> Option<Self> {
28+
match name {
29+
$( id if id == &name::name![$name] => Some(BuiltinAttrExpander::$variant), )*
30+
_ => None,
31+
}
32+
}
33+
}
34+
35+
};
36+
}
37+
38+
register_builtin! {
39+
(bench, Bench) => dummy_attr_expand,
40+
(cfg_accessible, CfgAccessible) => dummy_attr_expand,
41+
(cfg_eval, CfgEval) => dummy_attr_expand,
42+
(derive, Derive) => dummy_attr_expand,
43+
(global_allocator, GlobalAllocator) => dummy_attr_expand,
44+
(test, Test) => dummy_attr_expand,
45+
(test_case, TestCase) => dummy_attr_expand
46+
}
47+
48+
pub fn find_builtin_attr(
49+
ident: &name::Name,
50+
krate: CrateId,
51+
ast_id: AstId<ast::Macro>,
52+
) -> Option<MacroDefId> {
53+
let expander = BuiltinAttrExpander::find_by_name(ident)?;
54+
Some(MacroDefId {
55+
krate,
56+
kind: MacroDefKind::BuiltInAttr(expander, ast_id),
57+
local_inner: false,
58+
})
59+
}
60+
61+
fn dummy_attr_expand(
62+
_db: &dyn AstDatabase,
63+
_id: MacroCallId,
64+
tt: &tt::Subtree,
65+
) -> Result<tt::Subtree, mbe::ExpandError> {
66+
Ok(tt.clone())
67+
}

crates/hir_expand/src/db.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use syntax::{
1212
};
1313

1414
use crate::{
15-
ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinDeriveExpander,
16-
BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallKind, MacroCallLoc,
17-
MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
15+
ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinAttrExpander,
16+
BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId,
17+
MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
1818
};
1919

2020
/// Total limit on the number of tokens produced by any macro invocation.
@@ -31,6 +31,8 @@ pub enum TokenExpander {
3131
MacroDef { mac: mbe::MacroDef, def_site_token_map: mbe::TokenMap },
3232
/// Stuff like `line!` and `file!`.
3333
Builtin(BuiltinFnLikeExpander),
34+
/// `global_allocator` and such.
35+
BuiltinAttr(BuiltinAttrExpander),
3436
/// `derive(Copy)` and such.
3537
BuiltinDerive(BuiltinDeriveExpander),
3638
/// The thing we love the most here in rust-analyzer -- procedural macros.
@@ -49,6 +51,7 @@ impl TokenExpander {
4951
TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
5052
TokenExpander::Builtin(it) => it.expand(db, id, tt),
5153
// FIXME switch these to ExpandResult as well
54+
TokenExpander::BuiltinAttr(it) => it.expand(db, id, tt).into(),
5255
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
5356
TokenExpander::ProcMacro(_) => {
5457
// We store the result in salsa db to prevent non-deterministic behavior in
@@ -64,6 +67,7 @@ impl TokenExpander {
6467
TokenExpander::MacroRules { mac, .. } => mac.map_id_down(id),
6568
TokenExpander::MacroDef { mac, .. } => mac.map_id_down(id),
6669
TokenExpander::Builtin(..)
70+
| TokenExpander::BuiltinAttr(..)
6771
| TokenExpander::BuiltinDerive(..)
6872
| TokenExpander::ProcMacro(..) => id,
6973
}
@@ -74,6 +78,7 @@ impl TokenExpander {
7478
TokenExpander::MacroRules { mac, .. } => mac.map_id_up(id),
7579
TokenExpander::MacroDef { mac, .. } => mac.map_id_up(id),
7680
TokenExpander::Builtin(..)
81+
| TokenExpander::BuiltinAttr(..)
7782
| TokenExpander::BuiltinDerive(..)
7883
| TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call),
7984
}
@@ -299,6 +304,9 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>>
299304
}
300305
},
301306
MacroDefKind::BuiltIn(expander, _) => Some(Arc::new(TokenExpander::Builtin(expander))),
307+
MacroDefKind::BuiltInAttr(expander, _) => {
308+
Some(Arc::new(TokenExpander::BuiltinAttr(expander)))
309+
}
302310
MacroDefKind::BuiltInDerive(expander, _) => {
303311
Some(Arc::new(TokenExpander::BuiltinDerive(expander)))
304312
}

crates/hir_expand/src/eager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ fn eager_macro_recur(
224224
}
225225
MacroDefKind::Declarative(_)
226226
| MacroDefKind::BuiltIn(..)
227+
| MacroDefKind::BuiltInAttr(..)
227228
| MacroDefKind::BuiltInDerive(..)
228229
| MacroDefKind::ProcMacro(..) => {
229230
let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);

crates/hir_expand/src/hygiene.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl HygieneFrame {
192192
(info, Some(loc.def.krate), loc.def.local_inner)
193193
}
194194
MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false),
195+
MacroDefKind::BuiltInAttr(..) => (info, None, false),
195196
MacroDefKind::BuiltInDerive(..) => (info, None, false),
196197
MacroDefKind::BuiltInEager(..) => (info, None, false),
197198
MacroDefKind::ProcMacro(..) => (info, None, false),

crates/hir_expand/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod db;
88
pub mod ast_id_map;
99
pub mod name;
1010
pub mod hygiene;
11+
pub mod builtin_attr;
1112
pub mod builtin_derive;
1213
pub mod builtin_macro;
1314
pub mod proc_macro;
@@ -32,6 +33,7 @@ use syntax::{
3233
};
3334

3435
use crate::ast_id_map::FileAstId;
36+
use crate::builtin_attr::BuiltinAttrExpander;
3537
use crate::builtin_derive::BuiltinDeriveExpander;
3638
use crate::builtin_macro::{BuiltinFnLikeExpander, EagerExpander};
3739
use crate::proc_macro::ProcMacroExpander;
@@ -206,6 +208,7 @@ impl MacroDefId {
206208
let id = match &self.kind {
207209
MacroDefKind::Declarative(id) => id,
208210
MacroDefKind::BuiltIn(_, id) => id,
211+
MacroDefKind::BuiltInAttr(_, id) => id,
209212
MacroDefKind::BuiltInDerive(_, id) => id,
210213
MacroDefKind::BuiltInEager(_, id) => id,
211214
MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
@@ -223,6 +226,7 @@ pub enum MacroDefKind {
223226
Declarative(AstId<ast::Macro>),
224227
BuiltIn(BuiltinFnLikeExpander, AstId<ast::Macro>),
225228
// FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
229+
BuiltInAttr(BuiltinAttrExpander, AstId<ast::Macro>),
226230
BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
227231
BuiltInEager(EagerExpander, AstId<ast::Macro>),
228232
ProcMacro(ProcMacroExpander, ProcMacroKind, AstId<ast::Fn>),

crates/hir_expand/src/name.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ pub mod known {
160160
str,
161161
// Special names
162162
macro_rules,
163-
derive,
164163
doc,
165164
cfg,
166165
cfg_attr,
@@ -240,6 +239,14 @@ pub mod known {
240239
PartialOrd,
241240
Eq,
242241
PartialEq,
242+
// Builtin attributes
243+
bench,
244+
cfg_accessible,
245+
cfg_eval,
246+
derive,
247+
global_allocator,
248+
test,
249+
test_case,
243250
// Safe intrinsics
244251
abort,
245252
size_of,

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,32 @@ fn main() { let _ = crate::$0 }
656656
);
657657
}
658658

659+
#[test]
660+
fn does_not_complete_non_fn_macros() {
661+
check(
662+
r#"
663+
mod m {
664+
#[rustc_builtin_macro]
665+
pub macro Clone {}
666+
}
667+
668+
fn f() {m::$0}
669+
"#,
670+
expect![[r#""#]],
671+
);
672+
check(
673+
r#"
674+
mod m {
675+
#[rustc_builtin_macro]
676+
pub macro bench {}
677+
}
678+
679+
fn f() {m::$0}
680+
"#,
681+
expect![[r#""#]],
682+
);
683+
}
684+
659685
#[test]
660686
fn completes_in_assoc_item_list() {
661687
check(

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,14 +481,14 @@ impl S {
481481
);
482482
check(
483483
r#"
484-
mod m {
485-
#[rustc_builtin_macro]
486-
pub macro Clone {}
487-
}
484+
#[rustc_builtin_macro]
485+
pub macro bench {}
488486
489-
fn f() {m::$0}
487+
fn f() {$0}
490488
"#,
491-
expect![[r#""#]],
489+
expect![[r#"
490+
fn f() fn()
491+
"#]],
492492
);
493493
}
494494

0 commit comments

Comments
 (0)