diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir_def/src/attr.rs | 37 | ||||
-rw-r--r-- | crates/hir_def/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 18 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/path_resolution.rs | 13 | ||||
-rw-r--r-- | crates/hir_expand/src/builtin_derive.rs | 8 | ||||
-rw-r--r-- | crates/hir_expand/src/db.rs | 7 | ||||
-rw-r--r-- | crates/hir_expand/src/input.rs | 95 | ||||
-rw-r--r-- | crates/hir_expand/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/hir_expand/src/proc_macro.rs | 102 |
10 files changed, 149 insertions, 143 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index d9df7564d..dd99c8d15 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -9,7 +9,7 @@ use std::{ | |||
9 | use base_db::CrateId; | 9 | use base_db::CrateId; |
10 | use cfg::{CfgExpr, CfgOptions}; | 10 | use cfg::{CfgExpr, CfgOptions}; |
11 | use either::Either; | 11 | use either::Either; |
12 | use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile}; | 12 | use hir_expand::{hygiene::Hygiene, name::AsName, AstId, AttrId, InFile}; |
13 | use itertools::Itertools; | 13 | use itertools::Itertools; |
14 | use la_arena::ArenaMap; | 14 | use la_arena::ArenaMap; |
15 | use mbe::ast_to_token_tree; | 15 | use mbe::ast_to_token_tree; |
@@ -98,13 +98,16 @@ impl RawAttrs { | |||
98 | pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { | 98 | pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { |
99 | let entries = collect_attrs(owner) | 99 | let entries = collect_attrs(owner) |
100 | .enumerate() | 100 | .enumerate() |
101 | .flat_map(|(i, attr)| match attr { | 101 | .flat_map(|(i, attr)| { |
102 | Either::Left(attr) => Attr::from_src(attr, hygiene, i as u32), | 102 | let index = AttrId(i as u32); |
103 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { | 103 | match attr { |
104 | index: i as u32, | 104 | Either::Left(attr) => Attr::from_src(attr, hygiene, index), |
105 | input: Some(AttrInput::Literal(SmolStr::new(doc))), | 105 | Either::Right(comment) => comment.doc_comment().map(|doc| Attr { |
106 | path: Interned::new(ModPath::from(hir_expand::name!(doc))), | 106 | id: index, |
107 | }), | 107 | input: Some(AttrInput::Literal(SmolStr::new(doc))), |
108 | path: Interned::new(ModPath::from(hir_expand::name!(doc))), | ||
109 | }), | ||
110 | } | ||
108 | }) | 111 | }) |
109 | .collect::<Arc<_>>(); | 112 | .collect::<Arc<_>>(); |
110 | 113 | ||
@@ -161,7 +164,7 @@ impl RawAttrs { | |||
161 | let cfg = parts.next().unwrap(); | 164 | let cfg = parts.next().unwrap(); |
162 | let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; | 165 | let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; |
163 | let cfg = CfgExpr::parse(&cfg); | 166 | let cfg = CfgExpr::parse(&cfg); |
164 | let index = attr.index; | 167 | let index = attr.id; |
165 | let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { | 168 | let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { |
166 | let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; | 169 | let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; |
167 | let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; | 170 | let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; |
@@ -468,7 +471,7 @@ impl AttrsWithOwner { | |||
468 | ) -> Option<(Documentation, DocsRangeMap)> { | 471 | ) -> Option<(Documentation, DocsRangeMap)> { |
469 | // FIXME: code duplication in `docs` above | 472 | // FIXME: code duplication in `docs` above |
470 | let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { | 473 | let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { |
471 | AttrInput::Literal(s) => Some((s, attr.index)), | 474 | AttrInput::Literal(s) => Some((s, attr.id)), |
472 | AttrInput::TokenTree(_) => None, | 475 | AttrInput::TokenTree(_) => None, |
473 | }); | 476 | }); |
474 | let indent = docs | 477 | let indent = docs |
@@ -560,8 +563,8 @@ impl AttrSourceMap { | |||
560 | /// the attribute represented by `Attr`. | 563 | /// the attribute represented by `Attr`. |
561 | pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> { | 564 | pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> { |
562 | self.attrs | 565 | self.attrs |
563 | .get(attr.index as usize) | 566 | .get(attr.id.0 as usize) |
564 | .unwrap_or_else(|| panic!("cannot find `Attr` at index {}", attr.index)) | 567 | .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.id)) |
565 | .as_ref() | 568 | .as_ref() |
566 | } | 569 | } |
567 | } | 570 | } |
@@ -572,7 +575,7 @@ pub struct DocsRangeMap { | |||
572 | // (docstring-line-range, attr_index, attr-string-range) | 575 | // (docstring-line-range, attr_index, attr-string-range) |
573 | // a mapping from the text range of a line of the [`Documentation`] to the attribute index and | 576 | // a mapping from the text range of a line of the [`Documentation`] to the attribute index and |
574 | // the original (untrimmed) syntax doc line | 577 | // the original (untrimmed) syntax doc line |
575 | mapping: Vec<(TextRange, u32, TextRange)>, | 578 | mapping: Vec<(TextRange, AttrId, TextRange)>, |
576 | } | 579 | } |
577 | 580 | ||
578 | impl DocsRangeMap { | 581 | impl DocsRangeMap { |
@@ -585,7 +588,7 @@ impl DocsRangeMap { | |||
585 | 588 | ||
586 | let relative_range = range - line_docs_range.start(); | 589 | let relative_range = range - line_docs_range.start(); |
587 | 590 | ||
588 | let &InFile { file_id, value: ref source } = &self.source[idx as usize]; | 591 | let &InFile { file_id, value: ref source } = &self.source[idx.0 as usize]; |
589 | match source { | 592 | match source { |
590 | Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here | 593 | Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here |
591 | // as well as for whats done in syntax highlight doc injection | 594 | // as well as for whats done in syntax highlight doc injection |
@@ -606,7 +609,7 @@ impl DocsRangeMap { | |||
606 | 609 | ||
607 | #[derive(Debug, Clone, PartialEq, Eq)] | 610 | #[derive(Debug, Clone, PartialEq, Eq)] |
608 | pub struct Attr { | 611 | pub struct Attr { |
609 | index: u32, | 612 | pub(crate) id: AttrId, |
610 | pub(crate) path: Interned<ModPath>, | 613 | pub(crate) path: Interned<ModPath>, |
611 | pub(crate) input: Option<AttrInput>, | 614 | pub(crate) input: Option<AttrInput>, |
612 | } | 615 | } |
@@ -620,7 +623,7 @@ pub enum AttrInput { | |||
620 | } | 623 | } |
621 | 624 | ||
622 | impl Attr { | 625 | impl Attr { |
623 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> { | 626 | fn from_src(ast: ast::Attr, hygiene: &Hygiene, id: AttrId) -> Option<Attr> { |
624 | let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); | 627 | let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); |
625 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { | 628 | let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { |
626 | let value = match lit.kind() { | 629 | let value = match lit.kind() { |
@@ -633,7 +636,7 @@ impl Attr { | |||
633 | } else { | 636 | } else { |
634 | None | 637 | None |
635 | }; | 638 | }; |
636 | Some(Attr { index, path, input }) | 639 | Some(Attr { id, path, input }) |
637 | } | 640 | } |
638 | 641 | ||
639 | /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths | 642 | /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index b72884925..d69116d51 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -62,7 +62,7 @@ use hir_expand::{ | |||
62 | ast_id_map::FileAstId, | 62 | ast_id_map::FileAstId, |
63 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, | 63 | eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, |
64 | hygiene::Hygiene, | 64 | hygiene::Hygiene, |
65 | AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 65 | AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
66 | }; | 66 | }; |
67 | use la_arena::Idx; | 67 | use la_arena::Idx; |
68 | use nameres::DefMap; | 68 | use nameres::DefMap; |
@@ -699,6 +699,7 @@ fn macro_call_as_call_id( | |||
699 | 699 | ||
700 | fn derive_macro_as_call_id( | 700 | fn derive_macro_as_call_id( |
701 | item_attr: &AstIdWithPath<ast::Item>, | 701 | item_attr: &AstIdWithPath<ast::Item>, |
702 | derive_attr: AttrId, | ||
702 | db: &dyn db::DefDatabase, | 703 | db: &dyn db::DefDatabase, |
703 | krate: CrateId, | 704 | krate: CrateId, |
704 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 705 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
@@ -712,6 +713,7 @@ fn derive_macro_as_call_id( | |||
712 | MacroCallKind::Derive { | 713 | MacroCallKind::Derive { |
713 | ast_id: item_attr.ast_id, | 714 | ast_id: item_attr.ast_id, |
714 | derive_name: last_segment.to_string(), | 715 | derive_name: last_segment.to_string(), |
716 | derive_attr, | ||
715 | }, | 717 | }, |
716 | ) | 718 | ) |
717 | .into(); | 719 | .into(); |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index d966fc239..9e181751c 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -617,7 +617,7 @@ mod diagnostics { | |||
617 | let node = ast_id.to_node(db.upcast()); | 617 | let node = ast_id.to_node(db.upcast()); |
618 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) | 618 | (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) |
619 | } | 619 | } |
620 | MacroCallKind::Derive { ast_id, derive_name } => { | 620 | MacroCallKind::Derive { ast_id, derive_name, .. } => { |
621 | let node = ast_id.to_node(db.upcast()); | 621 | let node = ast_id.to_node(db.upcast()); |
622 | 622 | ||
623 | // Compute the precise location of the macro name's token in the derive | 623 | // Compute the precise location of the macro name's token in the derive |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index f431da3f2..d13d7be27 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -13,7 +13,7 @@ use hir_expand::{ | |||
13 | builtin_macro::find_builtin_macro, | 13 | builtin_macro::find_builtin_macro, |
14 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
15 | proc_macro::ProcMacroExpander, | 15 | proc_macro::ProcMacroExpander, |
16 | HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 16 | AttrId, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, |
17 | }; | 17 | }; |
18 | use hir_expand::{InFile, MacroCallLoc}; | 18 | use hir_expand::{InFile, MacroCallLoc}; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 19 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -216,7 +216,7 @@ struct MacroDirective { | |||
216 | #[derive(Clone, Debug, Eq, PartialEq)] | 216 | #[derive(Clone, Debug, Eq, PartialEq)] |
217 | enum MacroDirectiveKind { | 217 | enum MacroDirectiveKind { |
218 | FnLike { ast_id: AstIdWithPath<ast::MacroCall> }, | 218 | FnLike { ast_id: AstIdWithPath<ast::MacroCall> }, |
219 | Derive { ast_id: AstIdWithPath<ast::Item> }, | 219 | Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId }, |
220 | } | 220 | } |
221 | 221 | ||
222 | struct DefData<'a> { | 222 | struct DefData<'a> { |
@@ -831,10 +831,14 @@ impl DefCollector<'_> { | |||
831 | Err(UnresolvedMacro) | Ok(Err(_)) => {} | 831 | Err(UnresolvedMacro) | Ok(Err(_)) => {} |
832 | } | 832 | } |
833 | } | 833 | } |
834 | MacroDirectiveKind::Derive { ast_id } => { | 834 | MacroDirectiveKind::Derive { ast_id, derive_attr } => { |
835 | match derive_macro_as_call_id(ast_id, self.db, self.def_map.krate, |path| { | 835 | match derive_macro_as_call_id( |
836 | self.resolve_derive_macro(directive.module_id, &path) | 836 | ast_id, |
837 | }) { | 837 | *derive_attr, |
838 | self.db, | ||
839 | self.def_map.krate, | ||
840 | |path| self.resolve_derive_macro(directive.module_id, &path), | ||
841 | ) { | ||
838 | Ok(call_id) => { | 842 | Ok(call_id) => { |
839 | resolved.push((directive.module_id, call_id, directive.depth)); | 843 | resolved.push((directive.module_id, call_id, directive.depth)); |
840 | res = ReachedFixedPoint::No; | 844 | res = ReachedFixedPoint::No; |
@@ -1368,7 +1372,7 @@ impl ModCollector<'_, '_> { | |||
1368 | self.def_collector.unexpanded_macros.push(MacroDirective { | 1372 | self.def_collector.unexpanded_macros.push(MacroDirective { |
1369 | module_id: self.module_id, | 1373 | module_id: self.module_id, |
1370 | depth: self.macro_depth + 1, | 1374 | depth: self.macro_depth + 1, |
1371 | kind: MacroDirectiveKind::Derive { ast_id }, | 1375 | kind: MacroDirectiveKind::Derive { ast_id, derive_attr: derive.id }, |
1372 | }); | 1376 | }); |
1373 | } | 1377 | } |
1374 | } | 1378 | } |
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 60471937c..a9cf651d2 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs | |||
@@ -384,15 +384,10 @@ impl DefMap { | |||
384 | } | 384 | } |
385 | } | 385 | } |
386 | }; | 386 | }; |
387 | // Give precedence to names in outer `DefMap`s over the extern prelude; only check prelude | 387 | let from_extern_prelude = self |
388 | // from the crate DefMap. | 388 | .extern_prelude |
389 | let from_extern_prelude = match self.block { | 389 | .get(name) |
390 | Some(_) => PerNs::none(), | 390 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)); |
391 | None => self | ||
392 | .extern_prelude | ||
393 | .get(name) | ||
394 | .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)), | ||
395 | }; | ||
396 | 391 | ||
397 | let from_prelude = self.resolve_in_prelude(db, name); | 392 | let from_prelude = self.resolve_in_prelude(db, name); |
398 | 393 | ||
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index 392079ed4..537c03028 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs | |||
@@ -269,7 +269,7 @@ mod tests { | |||
269 | use expect_test::{expect, Expect}; | 269 | use expect_test::{expect, Expect}; |
270 | use name::AsName; | 270 | use name::AsName; |
271 | 271 | ||
272 | use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; | 272 | use crate::{test_db::TestDB, AstId, AttrId, MacroCallId, MacroCallKind, MacroCallLoc}; |
273 | 273 | ||
274 | use super::*; | 274 | use super::*; |
275 | 275 | ||
@@ -317,7 +317,11 @@ $0 | |||
317 | local_inner: false, | 317 | local_inner: false, |
318 | }, | 318 | }, |
319 | krate: CrateId(0), | 319 | krate: CrateId(0), |
320 | kind: MacroCallKind::Derive { ast_id, derive_name: name.to_string() }, | 320 | kind: MacroCallKind::Derive { |
321 | ast_id, | ||
322 | derive_name: name.to_string(), | ||
323 | derive_attr: AttrId(0), | ||
324 | }, | ||
321 | }; | 325 | }; |
322 | 326 | ||
323 | let id: MacroCallId = db.intern_macro(loc).into(); | 327 | let id: MacroCallId = db.intern_macro(loc).into(); |
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 10fe60821..95dc12744 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 | ||
16 | use crate::{ | 16 | use crate::{ |
17 | ast_id_map::AstIdMap, hygiene::HygieneFrame, BuiltinDeriveExpander, BuiltinFnLikeExpander, | 17 | ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinDeriveExpander, |
18 | EagerCallLoc, EagerMacroId, HirFileId, HirFileIdRepr, LazyMacroId, MacroCallId, MacroCallLoc, | 18 | BuiltinFnLikeExpander, EagerCallLoc, EagerMacroId, HirFileId, HirFileIdRepr, LazyMacroId, |
19 | MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander, | 19 | MacroCallId, MacroCallLoc, 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,6 +191,7 @@ 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); | ||
194 | Some(arg.green()) | 195 | Some(arg.green()) |
195 | } | 196 | } |
196 | 197 | ||
diff --git a/crates/hir_expand/src/input.rs b/crates/hir_expand/src/input.rs new file mode 100644 index 000000000..d1f22aba4 --- /dev/null +++ b/crates/hir_expand/src/input.rs | |||
@@ -0,0 +1,95 @@ | |||
1 | //! Macro input conditioning. | ||
2 | |||
3 | use syntax::{ | ||
4 | ast::{self, AttrsOwner}, | ||
5 | AstNode, SyntaxNode, | ||
6 | }; | ||
7 | |||
8 | use crate::{ | ||
9 | db::AstDatabase, | ||
10 | name::{name, AsName}, | ||
11 | AttrId, LazyMacroId, MacroCallKind, MacroCallLoc, | ||
12 | }; | ||
13 | |||
14 | pub(crate) fn process_macro_input( | ||
15 | db: &dyn AstDatabase, | ||
16 | node: SyntaxNode, | ||
17 | id: LazyMacroId, | ||
18 | ) -> SyntaxNode { | ||
19 | let loc: MacroCallLoc = db.lookup_intern_macro(id); | ||
20 | |||
21 | match loc.kind { | ||
22 | MacroCallKind::FnLike { .. } => node, | ||
23 | MacroCallKind::Derive { derive_attr, .. } => { | ||
24 | let item = match ast::Item::cast(node.clone()) { | ||
25 | Some(item) => item, | ||
26 | None => return node, | ||
27 | }; | ||
28 | |||
29 | remove_derives_up_to(item, derive_attr).syntax().clone() | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | |||
34 | /// Removes `#[derive]` attributes from `item`, up to `attr`. | ||
35 | fn remove_derives_up_to(item: ast::Item, attr: AttrId) -> ast::Item { | ||
36 | let item = item.clone_for_update(); | ||
37 | let idx = attr.0 as usize; | ||
38 | for attr in item.attrs().take(idx + 1) { | ||
39 | if let Some(name) = | ||
40 | attr.path().and_then(|path| path.as_single_segment()).and_then(|seg| seg.name_ref()) | ||
41 | { | ||
42 | if name.as_name() == name![derive] { | ||
43 | attr.syntax().detach(); | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | item | ||
48 | } | ||
49 | |||
50 | #[cfg(test)] | ||
51 | mod tests { | ||
52 | use base_db::fixture::WithFixture; | ||
53 | use base_db::SourceDatabase; | ||
54 | use expect_test::{expect, Expect}; | ||
55 | |||
56 | use crate::test_db::TestDB; | ||
57 | |||
58 | use super::*; | ||
59 | |||
60 | fn test_remove_derives_up_to(attr: AttrId, ra_fixture: &str, expect: Expect) { | ||
61 | let (db, file_id) = TestDB::with_single_file(&ra_fixture); | ||
62 | let parsed = db.parse(file_id); | ||
63 | |||
64 | let mut items: Vec<_> = | ||
65 | parsed.syntax_node().descendants().filter_map(ast::Item::cast).collect(); | ||
66 | assert_eq!(items.len(), 1); | ||
67 | |||
68 | let item = remove_derives_up_to(items.pop().unwrap(), attr); | ||
69 | expect.assert_eq(&item.to_string()); | ||
70 | } | ||
71 | |||
72 | #[test] | ||
73 | fn remove_derive() { | ||
74 | test_remove_derives_up_to( | ||
75 | AttrId(2), | ||
76 | r#" | ||
77 | #[allow(unused)] | ||
78 | #[derive(Copy)] | ||
79 | #[derive(Hello)] | ||
80 | #[derive(Clone)] | ||
81 | struct A { | ||
82 | bar: u32 | ||
83 | } | ||
84 | "#, | ||
85 | expect![[r#" | ||
86 | #[allow(unused)] | ||
87 | |||
88 | |||
89 | #[derive(Clone)] | ||
90 | struct A { | ||
91 | bar: u32 | ||
92 | }"#]], | ||
93 | ); | ||
94 | } | ||
95 | } | ||
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index a179102f0..7349fdfe4 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -14,6 +14,7 @@ pub mod builtin_macro; | |||
14 | pub mod proc_macro; | 14 | pub mod proc_macro; |
15 | pub mod quote; | 15 | pub mod quote; |
16 | pub mod eager; | 16 | pub mod eager; |
17 | mod input; | ||
17 | 18 | ||
18 | use either::Either; | 19 | use either::Either; |
19 | pub use mbe::{ExpandError, ExpandResult}; | 20 | pub use mbe::{ExpandError, ExpandResult}; |
@@ -291,9 +292,12 @@ pub struct MacroCallLoc { | |||
291 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 292 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
292 | pub enum MacroCallKind { | 293 | pub enum MacroCallKind { |
293 | FnLike { ast_id: AstId<ast::MacroCall> }, | 294 | FnLike { ast_id: AstId<ast::MacroCall> }, |
294 | Derive { ast_id: AstId<ast::Item>, derive_name: String }, | 295 | Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId }, |
295 | } | 296 | } |
296 | 297 | ||
298 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
299 | pub struct AttrId(pub u32); | ||
300 | |||
297 | impl MacroCallKind { | 301 | impl MacroCallKind { |
298 | fn file_id(&self) -> HirFileId { | 302 | fn file_id(&self) -> HirFileId { |
299 | match self { | 303 | match self { |
diff --git a/crates/hir_expand/src/proc_macro.rs b/crates/hir_expand/src/proc_macro.rs index 75e950816..d5643393a 100644 --- a/crates/hir_expand/src/proc_macro.rs +++ b/crates/hir_expand/src/proc_macro.rs | |||
@@ -2,7 +2,6 @@ | |||
2 | 2 | ||
3 | use crate::db::AstDatabase; | 3 | use crate::db::AstDatabase; |
4 | use base_db::{CrateId, ProcMacroId}; | 4 | use base_db::{CrateId, ProcMacroId}; |
5 | use tt::buffer::{Cursor, TokenBuffer}; | ||
6 | 5 | ||
7 | #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] | 6 | #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] |
8 | pub struct ProcMacroExpander { | 7 | pub struct ProcMacroExpander { |
@@ -44,9 +43,6 @@ impl ProcMacroExpander { | |||
44 | .clone() | 43 | .clone() |
45 | .ok_or_else(|| err!("No derive macro found."))?; | 44 | .ok_or_else(|| err!("No derive macro found."))?; |
46 | 45 | ||
47 | let tt = remove_derive_attrs(tt) | ||
48 | .ok_or_else(|| err!("Fail to remove derive for custom derive"))?; | ||
49 | |||
50 | // Proc macros have access to the environment variables of the invoking crate. | 46 | // Proc macros have access to the environment variables of the invoking crate. |
51 | let env = &krate_graph[calling_crate].env; | 47 | let env = &krate_graph[calling_crate].env; |
52 | 48 | ||
@@ -56,101 +52,3 @@ impl ProcMacroExpander { | |||
56 | } | 52 | } |
57 | } | 53 | } |
58 | } | 54 | } |
59 | |||
60 | fn 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 | |||
70 | fn 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 | |||
80 | fn 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 | |||
90 | fn 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)] | ||
117 | mod 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#" | ||
139 | SUBTREE $ | ||
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 | } | ||