Skip to content

Commit 8c3e28e

Browse files
committed
Auto merge of #16094 - HKalbasi:rustc-tests-fixup, r=HKalbasi
Replace `doc_comments_and_attrs` with `collect_attrs`, 2nd round I didn't understand why the one in the semantics is different. Please tell me if it needs special action. r? `@Veykril`
2 parents 3aa6306 + 801c0ea commit 8c3e28e

File tree

7 files changed

+57
-55
lines changed

7 files changed

+57
-55
lines changed

crates/hir-def/src/child_by_source.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
//! node for a *child*, and get its hir.
66
77
use either::Either;
8-
use hir_expand::HirFileId;
9-
use syntax::ast::HasDocComments;
8+
use hir_expand::{attrs::collect_attrs, HirFileId};
109

1110
use crate::{
1211
db::DefDatabase,
@@ -118,8 +117,8 @@ impl ChildBySource for ItemScope {
118117
|(ast_id, calls)| {
119118
let adt = ast_id.to_node(db.upcast());
120119
calls.for_each(|(attr_id, call_id, calls)| {
121-
if let Some(Either::Left(attr)) =
122-
adt.doc_comments_and_attrs().nth(attr_id.ast_index())
120+
if let Some((_, Either::Left(attr))) =
121+
collect_attrs(&adt).nth(attr_id.ast_index())
123122
{
124123
res[keys::DERIVE_MACRO_CALL].insert(attr, (attr_id, call_id, calls.into()));
125124
}

crates/hir-expand/src/db.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use limit::Limit;
1010
use mbe::{syntax_node_to_token_tree, ValueResult};
1111
use rustc_hash::FxHashSet;
1212
use syntax::{
13-
ast::{self, HasAttrs, HasDocComments},
13+
ast::{self, HasAttrs},
1414
AstNode, Parse, SyntaxError, SyntaxNode, SyntaxToken, T,
1515
};
1616
use triomphe::Arc;
1717

1818
use crate::{
1919
ast_id_map::AstIdMap,
20-
attrs::RawAttrs,
20+
attrs::{collect_attrs, RawAttrs},
2121
builtin_attr_macro::pseudo_derive_attr_expansion,
2222
builtin_fn_macro::EagerExpander,
2323
fixup::{self, SyntaxFixupUndoInfo},
@@ -216,9 +216,9 @@ pub fn expand_speculative(
216216
// Attributes may have an input token tree, build the subtree and map for this as well
217217
// then try finding a token id for our token if it is inside this input subtree.
218218
let item = ast::Item::cast(speculative_args.clone())?;
219-
item.doc_comments_and_attrs()
219+
collect_attrs(&item)
220220
.nth(invoc_attr_index.ast_index())
221-
.and_then(Either::left)
221+
.and_then(|x| Either::left(x.1))
222222
}?;
223223
match attr.token_tree() {
224224
Some(token_tree) => {
@@ -479,10 +479,9 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
479479
MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => return None,
480480
MacroCallKind::Attr { invoc_attr_index, .. } => {
481481
cov_mark::hit!(attribute_macro_attr_censoring);
482-
ast::Item::cast(node.clone())?
483-
.doc_comments_and_attrs()
482+
collect_attrs(&ast::Item::cast(node.clone())?)
484483
.nth(invoc_attr_index.ast_index())
485-
.and_then(Either::left)
484+
.and_then(|x| Either::left(x.1))
486485
.map(|attr| attr.syntax().clone())
487486
.into_iter()
488487
.collect()

crates/hir-expand/src/lib.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod span;
2222
pub mod files;
2323
mod fixup;
2424

25+
use attrs::collect_attrs;
2526
use triomphe::Arc;
2627

2728
use std::{fmt, hash::Hash};
@@ -32,7 +33,7 @@ use base_db::{
3233
};
3334
use either::Either;
3435
use syntax::{
35-
ast::{self, AstNode, HasDocComments},
36+
ast::{self, AstNode},
3637
SyntaxNode, SyntaxToken, TextRange, TextSize,
3738
};
3839

@@ -438,9 +439,9 @@ impl MacroCallLoc {
438439
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
439440
// FIXME: handle `cfg_attr`
440441
ast_id.with_value(ast_id.to_node(db)).map(|it| {
441-
it.doc_comments_and_attrs()
442+
collect_attrs(&it)
442443
.nth(derive_attr_index.ast_index())
443-
.and_then(|it| match it {
444+
.and_then(|it| match it.1 {
444445
Either::Left(attr) => Some(attr.syntax().clone()),
445446
Either::Right(_) => None,
446447
})
@@ -451,9 +452,9 @@ impl MacroCallLoc {
451452
if self.def.is_attribute_derive() {
452453
// FIXME: handle `cfg_attr`
453454
ast_id.with_value(ast_id.to_node(db)).map(|it| {
454-
it.doc_comments_and_attrs()
455+
collect_attrs(&it)
455456
.nth(invoc_attr_index.ast_index())
456-
.and_then(|it| match it {
457+
.and_then(|it| match it.1 {
457458
Either::Left(attr) => Some(attr.syntax().clone()),
458459
Either::Right(_) => None,
459460
})
@@ -549,24 +550,24 @@ impl MacroCallKind {
549550
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
550551
// FIXME: should be the range of the macro name, not the whole derive
551552
// FIXME: handle `cfg_attr`
552-
ast_id
553-
.to_node(db)
554-
.doc_comments_and_attrs()
553+
collect_attrs(&ast_id.to_node(db))
555554
.nth(derive_attr_index.ast_index())
556555
.expect("missing derive")
556+
.1
557557
.expect_left("derive is a doc comment?")
558558
.syntax()
559559
.text_range()
560560
}
561561
// FIXME: handle `cfg_attr`
562-
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => ast_id
563-
.to_node(db)
564-
.doc_comments_and_attrs()
565-
.nth(invoc_attr_index.ast_index())
566-
.expect("missing attribute")
567-
.expect_left("attribute macro is a doc comment?")
568-
.syntax()
569-
.text_range(),
562+
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
563+
collect_attrs(&ast_id.to_node(db))
564+
.nth(invoc_attr_index.ast_index())
565+
.expect("missing attribute")
566+
.1
567+
.expect_left("attribute macro is a doc comment?")
568+
.syntax()
569+
.text_range()
570+
}
570571
};
571572

572573
FileRange { range, file_id }
@@ -737,11 +738,9 @@ impl ExpansionInfo {
737738
let attr_input_or_mac_def = def.or_else(|| match loc.kind {
738739
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
739740
// FIXME: handle `cfg_attr`
740-
let tt = ast_id
741-
.to_node(db)
742-
.doc_comments_and_attrs()
741+
let tt = collect_attrs(&ast_id.to_node(db))
743742
.nth(invoc_attr_index.ast_index())
744-
.and_then(Either::left)?
743+
.and_then(|x| Either::left(x.1))?
745744
.token_tree()?;
746745
Some(InFile::new(ast_id.file_id, tt))
747746
}

crates/hir/src/semantics.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ use hir_def::{
2020
AsMacroCall, DefWithBodyId, FunctionId, MacroId, TraitId, VariantId,
2121
};
2222
use hir_expand::{
23-
db::ExpandDatabase, files::InRealFile, name::AsName, ExpansionInfo, InMacroFile, MacroCallId,
24-
MacroFileId, MacroFileIdExt,
23+
attrs::collect_attrs, db::ExpandDatabase, files::InRealFile, name::AsName, ExpansionInfo,
24+
InMacroFile, MacroCallId, MacroFileId, MacroFileIdExt,
2525
};
2626
use itertools::Itertools;
2727
use rustc_hash::{FxHashMap, FxHashSet};
2828
use smallvec::{smallvec, SmallVec};
2929
use stdx::TupleExt;
3030
use syntax::{
3131
algo::skip_trivia_token,
32-
ast::{self, HasAttrs as _, HasDocComments, HasGenericParams, HasLoopBody, IsString as _},
32+
ast::{self, HasAttrs as _, HasGenericParams, HasLoopBody, IsString as _},
3333
match_ast, AstNode, AstToken, Direction, SyntaxKind, SyntaxNode, SyntaxNodePtr, SyntaxToken,
3434
TextRange, TextSize,
3535
};
@@ -673,11 +673,22 @@ impl<'db> SemanticsImpl<'db> {
673673
}
674674
_ => 0,
675675
};
676+
// FIXME: here, the attribute's text range is used to strip away all
677+
// entries from the start of the attribute "list" up the the invoking
678+
// attribute. But in
679+
// ```
680+
// mod foo {
681+
// #![inner]
682+
// }
683+
// ```
684+
// we don't wanna strip away stuff in the `mod foo {` range, that is
685+
// here if the id corresponds to an inner attribute we got strip all
686+
// text ranges of the outer ones, and then all of the inner ones up
687+
// to the invoking attribute so that the inbetween is ignored.
676688
let text_range = item.syntax().text_range();
677-
let start = item
678-
.doc_comments_and_attrs()
689+
let start = collect_attrs(&item)
679690
.nth(attr_id)
680-
.map(|attr| match attr {
691+
.map(|attr| match attr.1 {
681692
Either::Left(it) => it.syntax().text_range().start(),
682693
Either::Right(it) => it.syntax().text_range().start(),
683694
})

crates/ide-diagnostics/src/handlers/unresolved_extern_crate.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,6 @@ extern crate core;
4444
extern crate self as foo;
4545
struct Foo;
4646
use foo::Foo as Bar;
47-
"#,
48-
);
49-
}
50-
51-
#[test]
52-
fn regression_panic_with_inner_attribute_in_presence_of_unresolved_crate() {
53-
check_diagnostics(
54-
r#"
55-
//- /lib.rs
56-
#[macro_use] extern crate doesnotexist;
57-
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unresolved extern crate
58-
mod _test_inner {
59-
#![empty_attr]
60-
//^^^^^^^^^^^^^^ error: unresolved macro `empty_attr`
61-
}
6247
"#,
6348
);
6449
}

crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ macro_rules! m { () => {} } }
6767
6868
self::m!(); self::m2!();
6969
//^^ error: unresolved macro `self::m2!`
70+
"#,
71+
);
72+
}
73+
74+
#[test]
75+
fn regression_panic_with_inner_attribute_in_presence_of_unresolved_crate() {
76+
check_diagnostics(
77+
r#"
78+
mod _test_inner {
79+
#![empty_attr]
80+
//^^^^^^^^^^^^^^ error: unresolved macro `empty_attr`
81+
}
7082
"#,
7183
);
7284
}

crates/syntax/src/ast/traits.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ pub trait HasDocComments: HasAttrs {
7676
fn doc_comments(&self) -> DocCommentIter {
7777
DocCommentIter { iter: self.syntax().children_with_tokens() }
7878
}
79-
fn doc_comments_and_attrs(&self) -> AttrDocCommentIter {
80-
AttrDocCommentIter { iter: self.syntax().children_with_tokens() }
81-
}
8279
}
8380

8481
impl DocCommentIter {

0 commit comments

Comments
 (0)