mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
internal: Expand the derive attribute into a pseudo expansion
This commit is contained in:
parent
1fe3b2edd6
commit
7b89d5ede2
17 changed files with 178 additions and 155 deletions
|
@ -1,9 +1,11 @@
|
|||
//! Builtin attributes.
|
||||
|
||||
use itertools::Itertools;
|
||||
use syntax::ast;
|
||||
|
||||
use crate::{
|
||||
db::AstDatabase, name, AstId, CrateId, ExpandResult, MacroCallId, MacroDefId, MacroDefKind,
|
||||
db::AstDatabase, name, AstId, CrateId, ExpandResult, MacroCallId, MacroCallKind, MacroDefId,
|
||||
MacroDefKind,
|
||||
};
|
||||
|
||||
macro_rules! register_builtin {
|
||||
|
@ -53,7 +55,7 @@ register_builtin! {
|
|||
(bench, Bench) => dummy_attr_expand,
|
||||
(cfg_accessible, CfgAccessible) => dummy_attr_expand,
|
||||
(cfg_eval, CfgEval) => dummy_attr_expand,
|
||||
(derive, Derive) => dummy_attr_expand,
|
||||
(derive, Derive) => derive_attr_expand,
|
||||
(global_allocator, GlobalAllocator) => dummy_attr_expand,
|
||||
(test, Test) => dummy_attr_expand,
|
||||
(test_case, TestCase) => dummy_attr_expand
|
||||
|
@ -79,3 +81,76 @@ fn dummy_attr_expand(
|
|||
) -> ExpandResult<tt::Subtree> {
|
||||
ExpandResult::ok(tt.clone())
|
||||
}
|
||||
|
||||
fn derive_attr_expand(
|
||||
db: &dyn AstDatabase,
|
||||
id: MacroCallId,
|
||||
tt: &tt::Subtree,
|
||||
) -> ExpandResult<tt::Subtree> {
|
||||
// we generate a very specific expansion here, as we do not actually expand the `#[derive]` attribute
|
||||
// itself in name res, but we do want to expand it to something for the IDE layer, so that the input
|
||||
// derive attributes can be downmapped, and resolved
|
||||
// This is basically a hack, to get rid of hacks in the IDE layer that slowly accumulate more and more
|
||||
// in various places.
|
||||
|
||||
// we transform the token tree of `#[derive(Foo, bar::Bar)]` into
|
||||
// ```
|
||||
// #[Foo]
|
||||
// #[bar::Bar]
|
||||
// ();
|
||||
// ```
|
||||
// which allows fallback path resolution in hir::Semantics to properly identify our derives
|
||||
let loc = db.lookup_intern_macro_call(id);
|
||||
let derives = match &loc.kind {
|
||||
MacroCallKind::Attr { attr_args, .. } => &attr_args.0,
|
||||
_ => return ExpandResult::ok(tt.clone()),
|
||||
};
|
||||
|
||||
let mut token_trees = Vec::new();
|
||||
for (comma, group) in &derives
|
||||
.token_trees
|
||||
.iter()
|
||||
.filter_map(|tt| match tt {
|
||||
tt::TokenTree::Leaf(l) => Some(l),
|
||||
tt::TokenTree::Subtree(_) => None,
|
||||
})
|
||||
.group_by(|l| matches!(l, tt::Leaf::Punct(tt::Punct { char: ',', .. })))
|
||||
{
|
||||
if comma {
|
||||
continue;
|
||||
}
|
||||
let wrap = |leaf| tt::TokenTree::Leaf(tt::Leaf::Punct(leaf));
|
||||
token_trees.push(wrap(tt::Punct {
|
||||
char: '#',
|
||||
spacing: tt::Spacing::Alone,
|
||||
id: tt::TokenId::unspecified(),
|
||||
}));
|
||||
token_trees.push(wrap(tt::Punct {
|
||||
char: '[',
|
||||
spacing: tt::Spacing::Alone,
|
||||
id: tt::TokenId::unspecified(),
|
||||
}));
|
||||
token_trees.extend(group.cloned().map(tt::TokenTree::Leaf));
|
||||
token_trees.push(wrap(tt::Punct {
|
||||
char: ']',
|
||||
spacing: tt::Spacing::Alone,
|
||||
id: tt::TokenId::unspecified(),
|
||||
}));
|
||||
token_trees.push(wrap(tt::Punct {
|
||||
char: '(',
|
||||
spacing: tt::Spacing::Alone,
|
||||
id: tt::TokenId::unspecified(),
|
||||
}));
|
||||
token_trees.push(wrap(tt::Punct {
|
||||
char: ')',
|
||||
spacing: tt::Spacing::Alone,
|
||||
id: tt::TokenId::unspecified(),
|
||||
}));
|
||||
token_trees.push(wrap(tt::Punct {
|
||||
char: ';',
|
||||
spacing: tt::Spacing::Alone,
|
||||
id: tt::TokenId::unspecified(),
|
||||
}));
|
||||
}
|
||||
ExpandResult::ok(tt::Subtree { delimiter: tt.delimiter, token_trees })
|
||||
}
|
||||
|
|
|
@ -342,6 +342,7 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
|
|||
.map(|it| it.syntax().clone())
|
||||
.collect()
|
||||
}
|
||||
MacroCallKind::Attr { is_derive: true, .. } => return None,
|
||||
MacroCallKind::Attr { invoc_attr_index, .. } => {
|
||||
cov_mark::hit!(attribute_macro_attr_censoring);
|
||||
ast::Item::cast(node.clone())?
|
||||
|
|
|
@ -166,6 +166,7 @@ pub enum MacroCallKind {
|
|||
/// Outer attributes are counted first, then inner attributes. This does not support
|
||||
/// out-of-line modules, which may have attributes spread across 2 files!
|
||||
invoc_attr_index: u32,
|
||||
is_derive: bool,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -431,6 +432,7 @@ impl MacroCallKind {
|
|||
match self {
|
||||
MacroCallKind::FnLike { expand_to, .. } => *expand_to,
|
||||
MacroCallKind::Derive { .. } => ExpandTo::Items,
|
||||
MacroCallKind::Attr { is_derive: true, .. } => ExpandTo::Statements,
|
||||
MacroCallKind::Attr { .. } => ExpandTo::Items, // is this always correct?
|
||||
}
|
||||
}
|
||||
|
@ -497,7 +499,7 @@ impl ExpansionInfo {
|
|||
|
||||
let token_range = token.value.text_range();
|
||||
match &loc.kind {
|
||||
MacroCallKind::Attr { attr_args, invoc_attr_index, .. } => {
|
||||
MacroCallKind::Attr { attr_args, invoc_attr_index, is_derive, .. } => {
|
||||
let attr = item
|
||||
.doc_comments_and_attrs()
|
||||
.nth(*invoc_attr_index as usize)
|
||||
|
@ -511,9 +513,13 @@ impl ExpansionInfo {
|
|||
let relative_range =
|
||||
token.value.text_range().checked_sub(attr_input_start)?;
|
||||
// shift by the item's tree's max id
|
||||
let token_id = self
|
||||
.macro_arg_shift
|
||||
.shift(attr_args.1.token_by_range(relative_range)?);
|
||||
let token_id = attr_args.1.token_by_range(relative_range)?;
|
||||
let token_id = if *is_derive {
|
||||
// we do not shift for `#[derive]`, as we only need to downmap the derive attribute tokens
|
||||
token_id
|
||||
} else {
|
||||
self.macro_arg_shift.shift(token_id)
|
||||
};
|
||||
Some(token_id)
|
||||
}
|
||||
_ => None,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue