|
1 | 1 | //! Builtin attributes. |
2 | 2 |
|
3 | | -use ::tt::Span; |
| 3 | +use base_db::{ |
| 4 | + span::{SyntaxContextId, ROOT_ERASED_FILE_AST_ID}, |
| 5 | + FileId, |
| 6 | +}; |
| 7 | +use syntax::{TextRange, TextSize}; |
4 | 8 |
|
5 | 9 | use crate::{db::ExpandDatabase, name, tt, ExpandResult, MacroCallId, MacroCallKind}; |
6 | 10 |
|
7 | 11 | macro_rules! register_builtin { |
8 | | - ( $(($name:ident, $variant:ident) => $expand:ident),* ) => { |
| 12 | + ($expand_fn:ident: $(($name:ident, $variant:ident) => $expand:ident),* ) => { |
9 | 13 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
10 | 14 | pub enum BuiltinAttrExpander { |
11 | 15 | $($variant),* |
12 | 16 | } |
13 | 17 |
|
14 | 18 | impl BuiltinAttrExpander { |
15 | | - pub fn expand( |
| 19 | + pub fn $expand_fn( |
16 | 20 | &self, |
17 | 21 | db: &dyn ExpandDatabase, |
18 | 22 | id: MacroCallId, |
@@ -47,7 +51,7 @@ impl BuiltinAttrExpander { |
47 | 51 | } |
48 | 52 | } |
49 | 53 |
|
50 | | -register_builtin! { |
| 54 | +register_builtin! { expand: |
51 | 55 | (bench, Bench) => dummy_attr_expand, |
52 | 56 | (cfg_accessible, CfgAccessible) => dummy_attr_expand, |
53 | 57 | (cfg_eval, CfgEval) => dummy_attr_expand, |
@@ -99,21 +103,31 @@ fn derive_attr_expand( |
99 | 103 | ) -> ExpandResult<tt::Subtree> { |
100 | 104 | let loc = db.lookup_intern_macro_call(id); |
101 | 105 | let derives = match &loc.kind { |
102 | | - MacroCallKind::Attr { attr_args, .. } if loc.def.is_attribute_derive() => attr_args, |
103 | | - _ => return ExpandResult::ok(tt::Subtree::empty()), |
| 106 | + MacroCallKind::Attr { attr_args: Some(attr_args), .. } if loc.def.is_attribute_derive() => { |
| 107 | + attr_args |
| 108 | + } |
| 109 | + _ => return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan::DUMMY)), |
104 | 110 | }; |
105 | | - pseudo_derive_attr_expansion(tt, derives) |
| 111 | + pseudo_derive_attr_expansion(tt, derives, loc.call_site) |
106 | 112 | } |
107 | 113 |
|
108 | 114 | pub fn pseudo_derive_attr_expansion( |
109 | 115 | tt: &tt::Subtree, |
110 | 116 | args: &tt::Subtree, |
| 117 | + call_site: SyntaxContextId, |
111 | 118 | ) -> ExpandResult<tt::Subtree> { |
112 | 119 | let mk_leaf = |char| { |
113 | 120 | tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { |
114 | 121 | char, |
115 | 122 | spacing: tt::Spacing::Alone, |
116 | | - span: tt::SpanData::DUMMY, |
| 123 | + span: tt::SpanData { |
| 124 | + range: TextRange::empty(TextSize::new(0)), |
| 125 | + anchor: base_db::span::SpanAnchor { |
| 126 | + file_id: FileId::BOGUS, |
| 127 | + ast_id: ROOT_ERASED_FILE_AST_ID, |
| 128 | + }, |
| 129 | + ctx: call_site, |
| 130 | + }, |
117 | 131 | })) |
118 | 132 | }; |
119 | 133 |
|
|
0 commit comments