about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-01-07 18:06:33 +0000
committerGitHub <noreply@github.com>2022-01-07 18:06:33 +0000
commit41a0e95d6125c7d15aad4daa48123afd5d410af7 (patch)
treef073e50d50bbdf8eb14ce27f3c813c2d5069adc8
parent40009e07d002bf676b4b32e90a858aed37ea4cc2 (diff)
parent81163b8cd4a03ac0d37807e02de20ad2605e26dd (diff)
downloadrust-41a0e95d6125c7d15aad4daa48123afd5d410af7.tar.gz
rust-41a0e95d6125c7d15aad4daa48123afd5d410af7.zip
Merge #11230
11230: fix: Fix attribute stripping ignoring doc comments r=Veykril a=Veykril

Follow up to https://github.com/rust-analyzer/rust-analyzer/pull/11225#pullrequestreview-846779237


Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
-rw-r--r--crates/hir/src/lib.rs13
-rw-r--r--crates/hir_expand/src/db.rs11
-rw-r--r--crates/hir_expand/src/hygiene.rs9
-rw-r--r--crates/hir_expand/src/lib.rs19
-rw-r--r--crates/syntax/src/ast/node_ext.rs1
5 files changed, 35 insertions, 18 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 3b28b14668e..b0ff7972757 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -68,7 +68,7 @@ use once_cell::unsync::Lazy;
 use rustc_hash::FxHashSet;
 use stdx::{format_to, impl_from};
 use syntax::{
-    ast::{self, HasAttrs as _, HasName},
+    ast::{self, HasAttrs as _, HasDocComments, HasName},
     AstNode, AstPtr, SmolStr, SyntaxKind, SyntaxNodePtr,
 };
 use tt::{Ident, Leaf, Literal, TokenTree};
@@ -612,10 +612,13 @@ impl Module {
                         }
                         MacroCallKind::Attr { ast_id, invoc_attr_index, attr_name, .. } => {
                             let node = ast_id.to_node(db.upcast());
-                            let attr =
-                                node.attrs().nth((*invoc_attr_index) as usize).unwrap_or_else(
-                                    || panic!("cannot find attribute #{}", invoc_attr_index),
-                                );
+                            let attr = node
+                                .doc_comments_and_attrs()
+                                .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))),
                                 Some(attr_name.clone()),
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index c88b5639f06..c39825b591c 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -3,12 +3,13 @@
 use std::sync::Arc;
 
 use base_db::{salsa, SourceDatabase};
+use either::Either;
 use limit::Limit;
 use mbe::{syntax_node_to_token_tree, ExpandError, ExpandResult};
 use rustc_hash::FxHashSet;
 use syntax::{
     algo::diff,
-    ast::{self, HasAttrs},
+    ast::{self, HasAttrs, HasDocComments},
     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
             // 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 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() {
                 Some(token_tree) => {
                     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, .. } => {
                 cov_mark::hit!(attribute_macro_attr_censoring);
                 ast::Item::cast(node.clone())?
-                    .attrs()
+                    .doc_comments_and_attrs()
                     .nth(invoc_attr_index as usize)
+                    .and_then(Either::right)
                     .map(|attr| attr.syntax().clone())
                     .into_iter()
                     .collect()
diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs
index e74c7052174..d7dc0443b21 100644
--- a/crates/hir_expand/src/hygiene.rs
+++ b/crates/hir_expand/src/hygiene.rs
@@ -9,7 +9,7 @@ use db::TokenExpander;
 use either::Either;
 use mbe::Origin;
 use syntax::{
-    ast::{self, HasAttrs},
+    ast::{self, HasDocComments},
     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 {
         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))
         }
         _ => None,
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 3e2e1ba630c..5f190b87277 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -25,7 +25,7 @@ use std::{hash::Hash, iter, sync::Arc};
 use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
 use syntax::{
     algo::{self, skip_trivia_token},
-    ast::{self, AstNode, HasAttrs},
+    ast::{self, AstNode, HasDocComments},
     Direction, SyntaxNode, SyntaxToken,
 };
 
@@ -201,8 +201,9 @@ impl HirFileId {
                     MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
                         let tt = ast_id
                             .to_node(db)
-                            .attrs()
-                            .nth(invoc_attr_index as usize)?
+                            .doc_comments_and_attrs()
+                            .nth(invoc_attr_index as usize)
+                            .and_then(Either::right)?
                             .token_tree()?;
                         Some(InFile::new(ast_id.file_id, tt))
                     }
@@ -429,8 +430,11 @@ impl ExpansionInfo {
 
             let token_range = token.value.text_range();
             match &loc.kind {
-                MacroCallKind::Attr { attr_args, invoc_attr_index, .. } => {
-                    let attr = item.attrs().nth(*invoc_attr_index as usize)?;
+                MacroCallKind::Attr { attr_args: (_, map), invoc_attr_index, .. } => {
+                    let attr = item
+                        .doc_comments_and_attrs()
+                        .nth(*invoc_attr_index as usize)
+                        .and_then(Either::right)?;
                     match attr.token_tree() {
                         Some(token_tree)
                             if token_tree.syntax().text_range().contains_range(token_range) =>
@@ -440,9 +444,8 @@ 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 =
+                                self.macro_arg_shift.shift(map.token_by_range(relative_range)?);
                             Some(token_id)
                         }
                         _ => None,
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index 705aa5edac4..470e9a04b32 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -772,6 +772,7 @@ impl ast::HasLoopBody for ast::ForExpr {
 }
 
 impl ast::HasAttrs for ast::AnyHasDocComments {}
+impl ast::HasDocComments for ast::Item {}
 
 impl From<ast::Adt> for ast::Item {
     fn from(it: ast::Adt) -> Self {