aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-04-10 19:30:28 +0100
committerJonas Schievink <[email protected]>2021-04-10 19:30:28 +0100
commit44b04ebe43534f749bc3a8431449bc019cc9c3b2 (patch)
tree37f9e13ceb4552d68f0ecafefa8fb54a5f5bdae4
parent050dc93e00c9b4be3982c82fa1babd31c3b13b7b (diff)
Revert "Rewrite `#[derive]` removal to be based on AST"
This reverts commit 7e78aebc8fbbb4043d62949681e4d700f1a2ec46.
-rw-r--r--crates/hir_expand/src/db.rs7
-rw-r--r--crates/hir_expand/src/input.rs87
-rw-r--r--crates/hir_expand/src/lib.rs1
-rw-r--r--crates/hir_expand/src/proc_macro.rs102
4 files changed, 105 insertions, 92 deletions
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 95dc12744..10fe60821 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -14,9 +14,9 @@ use syntax::{
14}; 14};
15 15
16use crate::{ 16use crate::{
17 ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinDeriveExpander, 17 ast_id_map::AstIdMap, hygiene::HygieneFrame, BuiltinDeriveExpander, BuiltinFnLikeExpander,
18 BuiltinFnLikeExpander, EagerCallLoc, EagerMacroId, HirFileId, HirFileIdRepr, LazyMacroId, 18 EagerCallLoc, EagerMacroId, HirFileId, HirFileIdRepr, LazyMacroId, MacroCallId, MacroCallLoc,
19 MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander, 19 MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
20}; 20};
21 21
22/// Total limit on the number of tokens produced by any macro invocation. 22/// Total limit on the number of tokens produced by any macro invocation.
@@ -191,7 +191,6 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
191 }; 191 };
192 let loc = db.lookup_intern_macro(id); 192 let loc = db.lookup_intern_macro(id);
193 let arg = loc.kind.arg(db)?; 193 let arg = loc.kind.arg(db)?;
194 let arg = process_macro_input(db, arg, id);
195 Some(arg.green()) 194 Some(arg.green())
196} 195}
197 196
diff --git a/crates/hir_expand/src/input.rs b/crates/hir_expand/src/input.rs
deleted file mode 100644
index 8f652cd7c..000000000
--- a/crates/hir_expand/src/input.rs
+++ /dev/null
@@ -1,87 +0,0 @@
1//! Macro input conditioning.
2
3use syntax::{
4 ast::{self, AttrsOwner},
5 AstNode, SyntaxNode,
6};
7
8use crate::{db::AstDatabase, name::AsName, AttrId, LazyMacroId, MacroCallKind, MacroCallLoc};
9
10pub fn process_macro_input(db: &dyn AstDatabase, node: SyntaxNode, id: LazyMacroId) -> SyntaxNode {
11 let loc: MacroCallLoc = db.lookup_intern_macro(id);
12
13 match loc.kind {
14 MacroCallKind::FnLike { .. } => node,
15 MacroCallKind::Derive { derive_attr, .. } => {
16 let item = match ast::Item::cast(node.clone()) {
17 Some(item) => item,
18 None => return node,
19 };
20
21 remove_derives_up_to(item, derive_attr).syntax().clone()
22 }
23 }
24}
25
26/// Removes `#[derive]` attributes from `item`, up to `attr`.
27fn remove_derives_up_to(item: ast::Item, attr: AttrId) -> ast::Item {
28 let item = item.clone_for_update();
29 let idx = attr.0 as usize;
30 for attr in item.attrs().take(idx + 1) {
31 if let Some(name) =
32 attr.path().and_then(|path| path.as_single_segment()).and_then(|seg| seg.name_ref())
33 {
34 if name.as_name().to_string() == "derive" {
35 attr.syntax().detach();
36 }
37 }
38 }
39 item
40}
41
42#[cfg(test)]
43mod tests {
44 use base_db::fixture::WithFixture;
45 use base_db::SourceDatabase;
46 use expect_test::{expect, Expect};
47
48 use crate::test_db::TestDB;
49
50 use super::*;
51
52 fn test_remove_derives_up_to(attr: AttrId, ra_fixture: &str, expect: Expect) {
53 let (db, file_id) = TestDB::with_single_file(&ra_fixture);
54 let parsed = db.parse(file_id);
55
56 let mut items: Vec<_> =
57 parsed.syntax_node().descendants().filter_map(ast::Item::cast).collect();
58 assert_eq!(items.len(), 1);
59
60 let item = remove_derives_up_to(items.pop().unwrap(), attr);
61 expect.assert_eq(&item.to_string());
62 }
63
64 #[test]
65 fn remove_derive() {
66 test_remove_derives_up_to(
67 AttrId(2),
68 r#"
69#[allow(unused)]
70#[derive(Copy)]
71#[derive(Hello)]
72#[derive(Clone)]
73struct A {
74 bar: u32
75}
76 "#,
77 expect![[r#"
78#[allow(unused)]
79
80
81#[derive(Clone)]
82struct A {
83 bar: u32
84}"#]],
85 );
86 }
87}
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 7349fdfe4..a0e6aec62 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -14,7 +14,6 @@ pub mod builtin_macro;
14pub mod proc_macro; 14pub mod proc_macro;
15pub mod quote; 15pub mod quote;
16pub mod eager; 16pub mod eager;
17mod input;
18 17
19use either::Either; 18use either::Either;
20pub use mbe::{ExpandError, ExpandResult}; 19pub use mbe::{ExpandError, ExpandResult};
diff --git a/crates/hir_expand/src/proc_macro.rs b/crates/hir_expand/src/proc_macro.rs
index d5643393a..75e950816 100644
--- a/crates/hir_expand/src/proc_macro.rs
+++ b/crates/hir_expand/src/proc_macro.rs
@@ -2,6 +2,7 @@
2 2
3use crate::db::AstDatabase; 3use crate::db::AstDatabase;
4use base_db::{CrateId, ProcMacroId}; 4use base_db::{CrateId, ProcMacroId};
5use tt::buffer::{Cursor, TokenBuffer};
5 6
6#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] 7#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
7pub struct ProcMacroExpander { 8pub struct ProcMacroExpander {
@@ -43,6 +44,9 @@ impl ProcMacroExpander {
43 .clone() 44 .clone()
44 .ok_or_else(|| err!("No derive macro found."))?; 45 .ok_or_else(|| err!("No derive macro found."))?;
45 46
47 let tt = remove_derive_attrs(tt)
48 .ok_or_else(|| err!("Fail to remove derive for custom derive"))?;
49
46 // Proc macros have access to the environment variables of the invoking crate. 50 // Proc macros have access to the environment variables of the invoking crate.
47 let env = &krate_graph[calling_crate].env; 51 let env = &krate_graph[calling_crate].env;
48 52
@@ -52,3 +56,101 @@ impl ProcMacroExpander {
52 } 56 }
53 } 57 }
54} 58}
59
60fn eat_punct(cursor: &mut Cursor, c: char) -> bool {
61 if let Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(punct), _)) = cursor.token_tree() {
62 if punct.char == c {
63 *cursor = cursor.bump();
64 return true;
65 }
66 }
67 false
68}
69
70fn eat_subtree(cursor: &mut Cursor, kind: tt::DelimiterKind) -> bool {
71 if let Some(tt::buffer::TokenTreeRef::Subtree(subtree, _)) = cursor.token_tree() {
72 if Some(kind) == subtree.delimiter_kind() {
73 *cursor = cursor.bump_subtree();
74 return true;
75 }
76 }
77 false
78}
79
80fn eat_ident(cursor: &mut Cursor, t: &str) -> bool {
81 if let Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Ident(ident), _)) = cursor.token_tree() {
82 if t == ident.text.as_str() {
83 *cursor = cursor.bump();
84 return true;
85 }
86 }
87 false
88}
89
90fn remove_derive_attrs(tt: &tt::Subtree) -> Option<tt::Subtree> {
91 let buffer = TokenBuffer::from_tokens(&tt.token_trees);
92 let mut p = buffer.begin();
93 let mut result = tt::Subtree::default();
94
95 while !p.eof() {
96 let curr = p;
97
98 if eat_punct(&mut p, '#') {
99 eat_punct(&mut p, '!');
100 let parent = p;
101 if eat_subtree(&mut p, tt::DelimiterKind::Bracket) {
102 if eat_ident(&mut p, "derive") {
103 p = parent.bump();
104 continue;
105 }
106 }
107 }
108
109 result.token_trees.push(curr.token_tree()?.cloned());
110 p = curr.bump();
111 }
112
113 Some(result)
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119 use test_utils::assert_eq_text;
120
121 #[test]
122 fn test_remove_derive_attrs() {
123 let tt = mbe::parse_to_token_tree(
124 r#"
125 #[allow(unused)]
126 #[derive(Copy)]
127 #[derive(Hello)]
128 struct A {
129 bar: u32
130 }
131"#,
132 )
133 .unwrap()
134 .0;
135 let result = format!("{:#?}", remove_derive_attrs(&tt).unwrap());
136
137 assert_eq_text!(
138 r#"
139SUBTREE $
140 PUNCH # [alone] 0
141 SUBTREE [] 1
142 IDENT allow 2
143 SUBTREE () 3
144 IDENT unused 4
145 IDENT struct 15
146 IDENT A 16
147 SUBTREE {} 17
148 IDENT bar 18
149 PUNCH : [alone] 19
150 IDENT u32 20
151"#
152 .trim(),
153 &result
154 );
155 }
156}