fix: Fix attribute stripping ignoring doc comments

This commit is contained in:
Lukas Wirth 2022-01-07 18:51:10 +01:00
parent 87735e5e9e
commit 81163b8cd4
5 changed files with 35 additions and 18 deletions

View file

@ -68,7 +68,7 @@ use once_cell::unsync::Lazy;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use stdx::{format_to, impl_from}; use stdx::{format_to, impl_from};
use syntax::{ use syntax::{
ast::{self, HasAttrs as _, HasName}, ast::{self, HasAttrs as _, HasDocComments, HasName},
AstNode, AstPtr, SmolStr, SyntaxKind, SyntaxNodePtr, AstNode, AstPtr, SmolStr, SyntaxKind, SyntaxNodePtr,
}; };
use tt::{Ident, Leaf, Literal, TokenTree}; use tt::{Ident, Leaf, Literal, TokenTree};
@ -612,10 +612,13 @@ impl Module {
} }
MacroCallKind::Attr { ast_id, invoc_attr_index, attr_name, .. } => { MacroCallKind::Attr { ast_id, invoc_attr_index, attr_name, .. } => {
let node = ast_id.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
let attr = let attr = node
node.attrs().nth((*invoc_attr_index) as usize).unwrap_or_else( .doc_comments_and_attrs()
|| panic!("cannot find attribute #{}", invoc_attr_index), .nth((*invoc_attr_index) as usize)
); .and_then(Either::right)
.unwrap_or_else(|| {
panic!("cannot find attribute #{}", invoc_attr_index)
});
( (
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))), ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
Some(attr_name.clone()), Some(attr_name.clone()),

View file

@ -3,12 +3,13 @@
use std::sync::Arc; use std::sync::Arc;
use base_db::{salsa, SourceDatabase}; use base_db::{salsa, SourceDatabase};
use either::Either;
use limit::Limit; use limit::Limit;
use mbe::{syntax_node_to_token_tree, ExpandError, ExpandResult}; use mbe::{syntax_node_to_token_tree, ExpandError, ExpandResult};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::{ use syntax::{
algo::diff, algo::diff,
ast::{self, HasAttrs}, ast::{self, HasAttrs, HasDocComments},
AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken, T, AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken, T,
}; };
@ -153,7 +154,10 @@ pub fn expand_speculative(
// Attributes may have an input token tree, build the subtree and map for this as well // Attributes may have an input token tree, build the subtree and map for this as well
// then try finding a token id for our token if it is inside this input subtree. // then try finding a token id for our token if it is inside this input subtree.
let item = ast::Item::cast(speculative_args.clone())?; let item = ast::Item::cast(speculative_args.clone())?;
let attr = item.attrs().nth(invoc_attr_index as usize)?; let attr = item
.doc_comments_and_attrs()
.nth(invoc_attr_index as usize)
.and_then(Either::right)?;
match attr.token_tree() { match attr.token_tree() {
Some(token_tree) => { Some(token_tree) => {
let (mut tree, map) = syntax_node_to_token_tree(attr.token_tree()?.syntax()); let (mut tree, map) = syntax_node_to_token_tree(attr.token_tree()?.syntax());
@ -328,8 +332,9 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
MacroCallKind::Attr { invoc_attr_index, .. } => { MacroCallKind::Attr { invoc_attr_index, .. } => {
cov_mark::hit!(attribute_macro_attr_censoring); cov_mark::hit!(attribute_macro_attr_censoring);
ast::Item::cast(node.clone())? ast::Item::cast(node.clone())?
.attrs() .doc_comments_and_attrs()
.nth(invoc_attr_index as usize) .nth(invoc_attr_index as usize)
.and_then(Either::right)
.map(|attr| attr.syntax().clone()) .map(|attr| attr.syntax().clone())
.into_iter() .into_iter()
.collect() .collect()

View file

@ -9,7 +9,7 @@ use db::TokenExpander;
use either::Either; use either::Either;
use mbe::Origin; use mbe::Origin;
use syntax::{ use syntax::{
ast::{self, HasAttrs}, ast::{self, HasDocComments},
AstNode, SyntaxKind, SyntaxNode, TextRange, TextSize, AstNode, SyntaxKind, SyntaxNode, TextRange, TextSize,
}; };
@ -187,7 +187,12 @@ fn make_hygiene_info(
}); });
let attr_input_or_mac_def = def.or_else(|| match loc.kind { let attr_input_or_mac_def = def.or_else(|| match loc.kind {
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => { MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
let tt = ast_id.to_node(db).attrs().nth(invoc_attr_index as usize)?.token_tree()?; let tt = ast_id
.to_node(db)
.doc_comments_and_attrs()
.nth(invoc_attr_index as usize)
.and_then(Either::right)?
.token_tree()?;
Some(InFile::new(ast_id.file_id, tt)) Some(InFile::new(ast_id.file_id, tt))
} }
_ => None, _ => None,

View file

@ -25,7 +25,7 @@ use std::{hash::Hash, iter, sync::Arc};
use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange}; use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
use syntax::{ use syntax::{
algo::{self, skip_trivia_token}, algo::{self, skip_trivia_token},
ast::{self, AstNode, HasAttrs}, ast::{self, AstNode, HasDocComments},
Direction, SyntaxNode, SyntaxToken, Direction, SyntaxNode, SyntaxToken,
}; };
@ -201,8 +201,9 @@ impl HirFileId {
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => { MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
let tt = ast_id let tt = ast_id
.to_node(db) .to_node(db)
.attrs() .doc_comments_and_attrs()
.nth(invoc_attr_index as usize)? .nth(invoc_attr_index as usize)
.and_then(Either::right)?
.token_tree()?; .token_tree()?;
Some(InFile::new(ast_id.file_id, tt)) Some(InFile::new(ast_id.file_id, tt))
} }
@ -429,8 +430,11 @@ impl ExpansionInfo {
let token_range = token.value.text_range(); let token_range = token.value.text_range();
match &loc.kind { match &loc.kind {
MacroCallKind::Attr { attr_args, invoc_attr_index, .. } => { MacroCallKind::Attr { attr_args: (_, map), invoc_attr_index, .. } => {
let attr = item.attrs().nth(*invoc_attr_index as usize)?; let attr = item
.doc_comments_and_attrs()
.nth(*invoc_attr_index as usize)
.and_then(Either::right)?;
match attr.token_tree() { match attr.token_tree() {
Some(token_tree) Some(token_tree)
if token_tree.syntax().text_range().contains_range(token_range) => if token_tree.syntax().text_range().contains_range(token_range) =>
@ -440,9 +444,8 @@ impl ExpansionInfo {
let relative_range = let relative_range =
token.value.text_range().checked_sub(attr_input_start)?; token.value.text_range().checked_sub(attr_input_start)?;
// shift by the item's tree's max id // shift by the item's tree's max id
let token_id = self let token_id =
.macro_arg_shift self.macro_arg_shift.shift(map.token_by_range(relative_range)?);
.shift(attr_args.1.token_by_range(relative_range)?);
Some(token_id) Some(token_id)
} }
_ => None, _ => None,

View file

@ -772,6 +772,7 @@ impl ast::HasLoopBody for ast::ForExpr {
} }
impl ast::HasAttrs for ast::AnyHasDocComments {} impl ast::HasAttrs for ast::AnyHasDocComments {}
impl ast::HasDocComments for ast::Item {}
impl From<ast::Adt> for ast::Item { impl From<ast::Adt> for ast::Item {
fn from(it: ast::Adt) -> Self { fn from(it: ast::Adt) -> Self {