Skip to content

Commit 41a0e95

Browse files
bors[bot]Veykril
andauthored
Merge #11230
11230: fix: Fix attribute stripping ignoring doc comments r=Veykril a=Veykril Follow up to #11225 (review) Co-authored-by: Lukas Wirth <[email protected]>
2 parents 40009e0 + 81163b8 commit 41a0e95

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

crates/hir/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use once_cell::unsync::Lazy;
6868
use rustc_hash::FxHashSet;
6969
use stdx::{format_to, impl_from};
7070
use syntax::{
71-
ast::{self, HasAttrs as _, HasName},
71+
ast::{self, HasAttrs as _, HasDocComments, HasName},
7272
AstNode, AstPtr, SmolStr, SyntaxKind, SyntaxNodePtr,
7373
};
7474
use tt::{Ident, Leaf, Literal, TokenTree};
@@ -612,10 +612,13 @@ impl Module {
612612
}
613613
MacroCallKind::Attr { ast_id, invoc_attr_index, attr_name, .. } => {
614614
let node = ast_id.to_node(db.upcast());
615-
let attr =
616-
node.attrs().nth((*invoc_attr_index) as usize).unwrap_or_else(
617-
|| panic!("cannot find attribute #{}", invoc_attr_index),
618-
);
615+
let attr = node
616+
.doc_comments_and_attrs()
617+
.nth((*invoc_attr_index) as usize)
618+
.and_then(Either::right)
619+
.unwrap_or_else(|| {
620+
panic!("cannot find attribute #{}", invoc_attr_index)
621+
});
619622
(
620623
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
621624
Some(attr_name.clone()),

crates/hir_expand/src/db.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
use std::sync::Arc;
44

55
use base_db::{salsa, SourceDatabase};
6+
use either::Either;
67
use limit::Limit;
78
use mbe::{syntax_node_to_token_tree, ExpandError, ExpandResult};
89
use rustc_hash::FxHashSet;
910
use syntax::{
1011
algo::diff,
11-
ast::{self, HasAttrs},
12+
ast::{self, HasAttrs, HasDocComments},
1213
AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken, T,
1314
};
1415

@@ -153,7 +154,10 @@ pub fn expand_speculative(
153154
// Attributes may have an input token tree, build the subtree and map for this as well
154155
// then try finding a token id for our token if it is inside this input subtree.
155156
let item = ast::Item::cast(speculative_args.clone())?;
156-
let attr = item.attrs().nth(invoc_attr_index as usize)?;
157+
let attr = item
158+
.doc_comments_and_attrs()
159+
.nth(invoc_attr_index as usize)
160+
.and_then(Either::right)?;
157161
match attr.token_tree() {
158162
Some(token_tree) => {
159163
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
328332
MacroCallKind::Attr { invoc_attr_index, .. } => {
329333
cov_mark::hit!(attribute_macro_attr_censoring);
330334
ast::Item::cast(node.clone())?
331-
.attrs()
335+
.doc_comments_and_attrs()
332336
.nth(invoc_attr_index as usize)
337+
.and_then(Either::right)
333338
.map(|attr| attr.syntax().clone())
334339
.into_iter()
335340
.collect()

crates/hir_expand/src/hygiene.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use db::TokenExpander;
99
use either::Either;
1010
use mbe::Origin;
1111
use syntax::{
12-
ast::{self, HasAttrs},
12+
ast::{self, HasDocComments},
1313
AstNode, SyntaxKind, SyntaxNode, TextRange, TextSize,
1414
};
1515

@@ -187,7 +187,12 @@ fn make_hygiene_info(
187187
});
188188
let attr_input_or_mac_def = def.or_else(|| match loc.kind {
189189
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
190-
let tt = ast_id.to_node(db).attrs().nth(invoc_attr_index as usize)?.token_tree()?;
190+
let tt = ast_id
191+
.to_node(db)
192+
.doc_comments_and_attrs()
193+
.nth(invoc_attr_index as usize)
194+
.and_then(Either::right)?
195+
.token_tree()?;
191196
Some(InFile::new(ast_id.file_id, tt))
192197
}
193198
_ => None,

crates/hir_expand/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::{hash::Hash, iter, sync::Arc};
2525
use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
2626
use syntax::{
2727
algo::{self, skip_trivia_token},
28-
ast::{self, AstNode, HasAttrs},
28+
ast::{self, AstNode, HasDocComments},
2929
Direction, SyntaxNode, SyntaxToken,
3030
};
3131

@@ -201,8 +201,9 @@ impl HirFileId {
201201
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
202202
let tt = ast_id
203203
.to_node(db)
204-
.attrs()
205-
.nth(invoc_attr_index as usize)?
204+
.doc_comments_and_attrs()
205+
.nth(invoc_attr_index as usize)
206+
.and_then(Either::right)?
206207
.token_tree()?;
207208
Some(InFile::new(ast_id.file_id, tt))
208209
}
@@ -429,8 +430,11 @@ impl ExpansionInfo {
429430

430431
let token_range = token.value.text_range();
431432
match &loc.kind {
432-
MacroCallKind::Attr { attr_args, invoc_attr_index, .. } => {
433-
let attr = item.attrs().nth(*invoc_attr_index as usize)?;
433+
MacroCallKind::Attr { attr_args: (_, map), invoc_attr_index, .. } => {
434+
let attr = item
435+
.doc_comments_and_attrs()
436+
.nth(*invoc_attr_index as usize)
437+
.and_then(Either::right)?;
434438
match attr.token_tree() {
435439
Some(token_tree)
436440
if token_tree.syntax().text_range().contains_range(token_range) =>
@@ -440,9 +444,8 @@ impl ExpansionInfo {
440444
let relative_range =
441445
token.value.text_range().checked_sub(attr_input_start)?;
442446
// shift by the item's tree's max id
443-
let token_id = self
444-
.macro_arg_shift
445-
.shift(attr_args.1.token_by_range(relative_range)?);
447+
let token_id =
448+
self.macro_arg_shift.shift(map.token_by_range(relative_range)?);
446449
Some(token_id)
447450
}
448451
_ => None,

crates/syntax/src/ast/node_ext.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ impl ast::HasLoopBody for ast::ForExpr {
772772
}
773773

774774
impl ast::HasAttrs for ast::AnyHasDocComments {}
775+
impl ast::HasDocComments for ast::Item {}
775776

776777
impl From<ast::Adt> for ast::Item {
777778
fn from(it: ast::Adt) -> Self {

0 commit comments

Comments
 (0)