Skip to content

Commit 13d4609

Browse files
committed
Merge remote-tracking branch 'upstream/master' into sync-from-rust
2 parents 54acf87 + 8c3e28e commit 13d4609

File tree

11 files changed

+95
-75
lines changed

11 files changed

+95
-75
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/ide/src/inlay_hints.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ fn ty_to_text_edit(
422422
Some(builder.finish())
423423
}
424424

425+
pub enum RangeLimit {
426+
Fixed(TextRange),
427+
NearestParent(TextSize),
428+
}
429+
425430
// Feature: Inlay Hints
426431
//
427432
// rust-analyzer shows additional information inline with the source code.
@@ -443,7 +448,7 @@ fn ty_to_text_edit(
443448
pub(crate) fn inlay_hints(
444449
db: &RootDatabase,
445450
file_id: FileId,
446-
range_limit: Option<TextRange>,
451+
range_limit: Option<RangeLimit>,
447452
config: &InlayHintsConfig,
448453
) -> Vec<InlayHint> {
449454
let _p = profile::span("inlay_hints");
@@ -458,13 +463,31 @@ pub(crate) fn inlay_hints(
458463

459464
let hints = |node| hints(&mut acc, &famous_defs, config, file_id, node);
460465
match range_limit {
461-
Some(range) => match file.covering_element(range) {
466+
Some(RangeLimit::Fixed(range)) => match file.covering_element(range) {
462467
NodeOrToken::Token(_) => return acc,
463468
NodeOrToken::Node(n) => n
464469
.descendants()
465470
.filter(|descendant| range.intersect(descendant.text_range()).is_some())
466471
.for_each(hints),
467472
},
473+
Some(RangeLimit::NearestParent(position)) => {
474+
match file.token_at_offset(position).left_biased() {
475+
Some(token) => {
476+
if let Some(parent_block) =
477+
token.parent_ancestors().find_map(ast::BlockExpr::cast)
478+
{
479+
parent_block.syntax().descendants().for_each(hints)
480+
} else if let Some(parent_item) =
481+
token.parent_ancestors().find_map(ast::Item::cast)
482+
{
483+
parent_item.syntax().descendants().for_each(hints)
484+
} else {
485+
return acc;
486+
}
487+
}
488+
None => return acc,
489+
}
490+
}
468491
None => file.descendants().for_each(hints),
469492
};
470493
}

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ mod tests {
177177
use syntax::{TextRange, TextSize};
178178
use test_utils::extract_annotations;
179179

180-
use crate::{fixture, inlay_hints::InlayHintsConfig, ClosureReturnTypeHints};
180+
use crate::{
181+
fixture,
182+
inlay_hints::{InlayHintsConfig, RangeLimit},
183+
ClosureReturnTypeHints,
184+
};
181185

182186
use crate::inlay_hints::tests::{
183187
check, check_edit, check_no_edit, check_with_config, DISABLED_CONFIG, TEST_CONFIG,
@@ -400,7 +404,7 @@ fn main() {
400404
.inlay_hints(
401405
&InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG },
402406
file_id,
403-
Some(TextRange::new(TextSize::from(500), TextSize::from(600))),
407+
Some(RangeLimit::Fixed(TextRange::new(TextSize::from(500), TextSize::from(600)))),
404408
)
405409
.unwrap();
406410
let actual =

crates/ide/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub use crate::{
9494
inlay_hints::{
9595
AdjustmentHints, AdjustmentHintsMode, ClosureReturnTypeHints, DiscriminantHints,
9696
InlayFieldsToResolve, InlayHint, InlayHintLabel, InlayHintLabelPart, InlayHintPosition,
97-
InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints,
97+
InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints, RangeLimit,
9898
},
9999
join_lines::JoinLinesConfig,
100100
markup::Markup,
@@ -397,7 +397,7 @@ impl Analysis {
397397
&self,
398398
config: &InlayHintsConfig,
399399
file_id: FileId,
400-
range: Option<TextRange>,
400+
range: Option<RangeLimit>,
401401
) -> Cancellable<Vec<InlayHint>> {
402402
self.with_db(|db| inlay_hints::inlay_hints(db, file_id, range, config))
403403
}

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use anyhow::Context;
1212

1313
use ide::{
1414
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FilePosition, FileRange,
15-
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, ReferenceCategory,
16-
Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit,
15+
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, RangeLimit,
16+
ReferenceCategory, Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit,
1717
};
1818
use ide_db::SymbolKind;
1919
use lsp_server::ErrorCode;
@@ -1409,7 +1409,7 @@ pub(crate) fn handle_inlay_hints(
14091409
let inlay_hints_config = snap.config.inlay_hints();
14101410
Ok(Some(
14111411
snap.analysis
1412-
.inlay_hints(&inlay_hints_config, file_id, Some(range))?
1412+
.inlay_hints(&inlay_hints_config, file_id, Some(RangeLimit::Fixed(range)))?
14131413
.into_iter()
14141414
.map(|it| {
14151415
to_proto::inlay_hint(
@@ -1440,22 +1440,13 @@ pub(crate) fn handle_inlay_hints_resolve(
14401440
anyhow::ensure!(snap.file_exists(file_id), "Invalid LSP resolve data");
14411441

14421442
let line_index = snap.file_line_index(file_id)?;
1443-
let range = from_proto::text_range(
1444-
&line_index,
1445-
lsp_types::Range { start: original_hint.position, end: original_hint.position },
1446-
)?;
1447-
let range_start = range.start();
1448-
let range_end = range.end();
1449-
let large_range = TextRange::new(
1450-
range_start.checked_sub(1.into()).unwrap_or(range_start),
1451-
range_end.checked_add(1.into()).unwrap_or(range_end),
1452-
);
1443+
let hint_position = from_proto::offset(&line_index, original_hint.position)?;
14531444
let mut forced_resolve_inlay_hints_config = snap.config.inlay_hints();
14541445
forced_resolve_inlay_hints_config.fields_to_resolve = InlayFieldsToResolve::empty();
14551446
let resolve_hints = snap.analysis.inlay_hints(
14561447
&forced_resolve_inlay_hints_config,
14571448
file_id,
1458-
Some(large_range),
1449+
Some(RangeLimit::NearestParent(hint_position)),
14591450
)?;
14601451

14611452
let mut resolved_hints = resolve_hints

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)