Skip to content

Commit fc331fe

Browse files
bors[bot]Veykril
andauthored
Merge #11282
11282: fix: Properly cache files in Semantics when ascending macros r=Veykril a=Veykril Fixes #11280 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents b4c3148 + f1cb5ed commit fc331fe

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

crates/hir/src/has_source.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use crate::{
1616

1717
pub trait HasSource {
1818
type Ast;
19+
/// Fetches the definition's source node.
20+
/// Using [`crate::Semantics::source`] is preferred when working with [`crate::Semantics`],
21+
/// as that caches the parsed file in the semantics' cache.
1922
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>>;
2023
}
2124

crates/hir/src/semantics.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod source_to_def;
44

5-
use std::{cell::RefCell, fmt};
5+
use std::{cell::RefCell, fmt, iter};
66

77
use base_db::{FileId, FileRange};
88
use either::Either;
@@ -443,17 +443,15 @@ impl<'db> SemanticsImpl<'db> {
443443
fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
444444
let sa = self.analyze_no_infer(macro_call.syntax());
445445
let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?;
446-
let node = self.db.parse_or_expand(file_id)?;
447-
self.cache(node.clone(), file_id);
446+
let node = self.parse_or_expand(file_id)?;
448447
Some(node)
449448
}
450449

451450
fn expand_attr_macro(&self, item: &ast::Item) -> Option<SyntaxNode> {
452451
let src = self.find_file(item.syntax()).with_value(item.clone());
453452
let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(src))?;
454453
let file_id = macro_call_id.as_file();
455-
let node = self.db.parse_or_expand(file_id)?;
456-
self.cache(node.clone(), file_id);
454+
let node = self.parse_or_expand(file_id)?;
457455
Some(node)
458456
}
459457

@@ -750,10 +748,9 @@ impl<'db> SemanticsImpl<'db> {
750748
}
751749

752750
fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange {
753-
let root = self.db.parse_or_expand(src.file_id).unwrap();
754-
let node = src.value.to_node(&root);
755-
self.cache(root, src.file_id);
756-
src.with_value(&node).original_file_range(self.db.upcast())
751+
let root = self.parse_or_expand(src.file_id).unwrap();
752+
let node = src.map(|it| it.to_node(&root));
753+
node.as_ref().original_file_range(self.db.upcast())
757754
}
758755

759756
fn token_ancestors_with_macros(
@@ -768,7 +765,17 @@ impl<'db> SemanticsImpl<'db> {
768765
node: SyntaxNode,
769766
) -> impl Iterator<Item = SyntaxNode> + Clone + '_ {
770767
let node = self.find_file(&node);
771-
node.ancestors_with_macros(self.db.upcast()).map(|it| it.value)
768+
let db = self.db.upcast();
769+
iter::successors(Some(node.cloned()), move |&InFile { file_id, ref value }| {
770+
match value.parent() {
771+
Some(parent) => Some(InFile::new(file_id, parent)),
772+
None => {
773+
self.cache(value.clone(), file_id);
774+
file_id.call_node(db)
775+
}
776+
}
777+
})
778+
.map(|it| it.value)
772779
}
773780

774781
fn ancestors_at_offset_with_macros(

crates/hir_expand/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,7 @@ impl<'a> InFile<&'a SyntaxNode> {
636636
) -> impl Iterator<Item = InFile<SyntaxNode>> + Clone + '_ {
637637
iter::successors(Some(self.cloned()), move |node| match node.value.parent() {
638638
Some(parent) => Some(node.with_value(parent)),
639-
None => {
640-
let parent_node = node.file_id.call_node(db)?;
641-
Some(parent_node)
642-
}
639+
None => node.file_id.call_node(db),
643640
})
644641
}
645642

crates/ide_completion/src/completions/trait_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! }
3232
//! ```
3333
34-
use hir::{self, HasAttrs, HasSource};
34+
use hir::{self, HasAttrs};
3535
use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
3636
use syntax::{
3737
ast::{self, edit_in_place::AttrsOwnerEdit},
@@ -151,7 +151,7 @@ fn add_function_impl(
151151

152152
let range = replacement_range(ctx, fn_def_node);
153153

154-
if let Some(source) = func.source(ctx.db) {
154+
if let Some(source) = ctx.sema.source(func) {
155155
let assoc_item = ast::AssocItem::Fn(source.value);
156156
if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
157157
let transformed_fn = match transformed_item {
@@ -189,7 +189,7 @@ fn get_transformed_assoc_item(
189189
target_scope,
190190
source_scope,
191191
trait_,
192-
impl_def.source(ctx.db)?.value,
192+
ctx.sema.source(impl_def)?.value,
193193
);
194194

195195
transform.apply(assoc_item.syntax());
@@ -227,7 +227,7 @@ fn add_const_impl(
227227
let const_name = const_.name(ctx.db).map(|n| n.to_smol_str());
228228

229229
if let Some(const_name) = const_name {
230-
if let Some(source) = const_.source(ctx.db) {
230+
if let Some(source) = ctx.sema.source(const_) {
231231
let assoc_item = ast::AssocItem::Const(source.value);
232232
if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
233233
let transformed_const = match transformed_item {

crates/ide_completion/src/render/macro_.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Renderer for macro invocations.
22
33
use either::Either;
4-
use hir::{db::HirDatabase, Documentation, HasSource};
5-
use ide_db::SymbolKind;
4+
use hir::{Documentation, HasSource, InFile, Semantics};
5+
use ide_db::{RootDatabase, SymbolKind};
66
use syntax::{
77
display::{fn_as_proc_macro_label, macro_label},
88
SmolStr,
@@ -30,8 +30,6 @@ fn render(
3030
macro_: hir::MacroDef,
3131
import_to_add: Option<ImportEdit>,
3232
) -> CompletionItem {
33-
let db = completion.db;
34-
3533
let source_range = if completion.is_immediately_after_macro_bang() {
3634
cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token);
3735
completion.token.parent().map_or_else(|| ctx.source_range(), |it| it.text_range())
@@ -54,7 +52,7 @@ fn render(
5452
label(&ctx, needs_bang, bra, ket, &name),
5553
);
5654
item.set_deprecated(ctx.is_deprecated(macro_))
57-
.set_detail(detail(db, macro_))
55+
.set_detail(detail(&completion.sema, macro_))
5856
.set_documentation(docs);
5957

6058
if let Some(import_to_add) = import_to_add {
@@ -104,9 +102,11 @@ fn banged_name(name: &str) -> SmolStr {
104102
SmolStr::from_iter([name, "!"])
105103
}
106104

107-
fn detail(db: &dyn HirDatabase, macro_: hir::MacroDef) -> Option<String> {
105+
fn detail(sema: &Semantics<RootDatabase>, macro_: hir::MacroDef) -> Option<String> {
108106
// FIXME: This is parsing the file!
109-
let detail = match macro_.source(db)?.value {
107+
let InFile { file_id, value } = macro_.source(sema.db)?;
108+
let _ = sema.parse_or_expand(file_id);
109+
let detail = match value {
110110
Either::Left(node) => macro_label(&node),
111111
Either::Right(node) => fn_as_proc_macro_label(&node),
112112
};

0 commit comments

Comments
 (0)