From ea5cc8d07ac28a2110b894d154468c3fa46d7040 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 19 Dec 2020 01:09:48 +0100 Subject: More accurate `#[derive]` parsing This now allows full paths to the derive macro --- crates/hir_def/src/attr.rs | 46 ++++++++++++++++++++++++++++++--- crates/hir_def/src/nameres/collector.rs | 28 ++++++++++---------- crates/hir_def/src/path.rs | 12 ++------- crates/hir_expand/src/name.rs | 1 + 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 1b9c64ee5..18525406c 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -5,7 +5,7 @@ use std::{ops, sync::Arc}; use base_db::CrateId; use cfg::{CfgExpr, CfgOptions}; use either::Either; -use hir_expand::{hygiene::Hygiene, AstId, InFile}; +use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile}; use itertools::Itertools; use mbe::ast_to_token_tree; use syntax::{ @@ -19,7 +19,7 @@ use crate::{ db::DefDatabase, item_tree::{ItemTreeId, ItemTreeNode}, nameres::ModuleSource, - path::ModPath, + path::{ModPath, PathKind}, src::HasChildSource, AdtId, AttrDefId, Lookup, }; @@ -357,6 +357,46 @@ impl Attr { }; Some(Attr { path, input }) } + + /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths + /// to derive macros. + /// + /// Returns `None` when the attribute is not a well-formed `#[derive]` attribute. + pub(crate) fn parse_derive(&self) -> Option> { + if self.path.as_ident() != Some(&hir_expand::name![derive]) { + return None; + } + + match &self.input { + Some(AttrInput::TokenTree(args)) => { + let mut counter = 0; + let paths = args + .token_trees + .iter() + .group_by(move |tt| { + match tt { + tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => { + counter += 1; + } + _ => {} + } + counter + }) + .into_iter() + .map(|(_, tts)| { + let segments = tts.filter_map(|tt| match tt { + tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => Some(id.as_name()), + _ => None, + }); + ModPath::from_segments(PathKind::Plain, segments) + }) + .collect::>(); + + Some(paths.into_iter()) + } + _ => None, + } + } } #[derive(Debug, Clone, Copy)] @@ -384,7 +424,7 @@ impl<'a> AttrQuery<'a> { self.attrs().next().is_some() } - fn attrs(self) -> impl Iterator { + pub(crate) fn attrs(self) -> impl Iterator { let key = self.key; self.attrs .iter() diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 55228e480..a636ec77d 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -1289,20 +1289,20 @@ impl ModCollector<'_, '_> { } fn collect_derives(&mut self, attrs: &Attrs, ast_id: FileAstId) { - for derive_subtree in attrs.by_key("derive").tt_values() { - // for #[derive(Copy, Clone)], `derive_subtree` is the `(Copy, Clone)` subtree - for tt in &derive_subtree.token_trees { - let ident = match &tt { - tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident, - tt::TokenTree::Leaf(tt::Leaf::Punct(_)) => continue, // , is ok - _ => continue, // anything else would be an error (which we currently ignore) - }; - let path = ModPath::from_tt_ident(ident); - - let ast_id = AstIdWithPath::new(self.file_id, ast_id, path); - self.def_collector - .unexpanded_attribute_macros - .push(DeriveDirective { module_id: self.module_id, ast_id }); + for derive in attrs.by_key("derive").attrs() { + match derive.parse_derive() { + Some(derive_macros) => { + for path in derive_macros { + let ast_id = AstIdWithPath::new(self.file_id, ast_id, path); + self.def_collector + .unexpanded_attribute_macros + .push(DeriveDirective { module_id: self.module_id, ast_id }); + } + } + None => { + // FIXME: diagnose + log::debug!("malformed derive: {:?}", derive); + } } } } diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 00a69a8a6..e2bf85bbc 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs @@ -9,11 +9,8 @@ use std::{ use crate::{body::LowerCtx, type_ref::LifetimeRef}; use base_db::CrateId; -use hir_expand::{ - hygiene::Hygiene, - name::{AsName, Name}, -}; -use syntax::ast::{self}; +use hir_expand::{hygiene::Hygiene, name::Name}; +use syntax::ast; use crate::{ type_ref::{TypeBound, TypeRef}, @@ -56,11 +53,6 @@ impl ModPath { ModPath { kind, segments } } - /// Converts an `tt::Ident` into a single-identifier `Path`. - pub(crate) fn from_tt_ident(ident: &tt::Ident) -> ModPath { - ident.as_name().into() - } - /// Calls `cb` with all paths, represented by this use item. pub(crate) fn expand_use_item( item_src: InFile, diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index 77eeee3fe..2f44876a8 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs @@ -152,6 +152,7 @@ pub mod known { str, // Special names macro_rules, + derive, doc, cfg_attr, // Components of known path (value or mod name) -- cgit v1.2.3 From d5edd0e9281a7d13f81a652b55acc13d594d62b6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 19 Dec 2020 01:11:33 +0100 Subject: Test paths to derive macros --- crates/hir_def/src/nameres/tests/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs index 6fe2ee78a..bec1354bd 100644 --- a/crates/hir_def/src/nameres/tests/macros.rs +++ b/crates/hir_def/src/nameres/tests/macros.rs @@ -634,9 +634,9 @@ fn expand_derive() { let map = compute_crate_def_map( " //- /main.rs crate:main deps:core - use core::*; + use core::Copy; - #[derive(Copy, Clone)] + #[derive(Copy, core::Clone)] struct Foo; //- /core.rs crate:core -- cgit v1.2.3 From aa00d1acd68911735334cb1c7de91d347bb56808 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 19 Dec 2020 01:12:41 +0100 Subject: fixture -> ra_fixture --- crates/hir_def/src/nameres/tests.rs | 4 ++-- crates/hir_def/src/nameres/tests/macros.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index a4d1fb8f3..c459fa66d 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs @@ -13,8 +13,8 @@ use test_utils::mark; use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; -fn compute_crate_def_map(fixture: &str) -> Arc { - let db = TestDB::with_files(fixture); +fn compute_crate_def_map(ra_fixture: &str) -> Arc { + let db = TestDB::with_files(ra_fixture); let krate = db.crate_graph().iter().next().unwrap(); db.crate_def_map(krate) } diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs index bec1354bd..f9bf5bc72 100644 --- a/crates/hir_def/src/nameres/tests/macros.rs +++ b/crates/hir_def/src/nameres/tests/macros.rs @@ -632,7 +632,7 @@ pub struct bar; #[test] fn expand_derive() { let map = compute_crate_def_map( - " + r#" //- /main.rs crate:main deps:core use core::Copy; @@ -645,7 +645,7 @@ fn expand_derive() { #[rustc_builtin_macro] pub macro Clone {} - ", + "#, ); assert_eq!(map.modules[map.root].scope.impls().len(), 2); } -- cgit v1.2.3