Skip to content

Commit 4675410

Browse files
bors[bot]cynecxVeykril
authored
Merge #10477 #10482
10477: parser: fix parsing of macro call inside generic args r=Veykril a=cynecx 10482: fix: fix `inline_call` trying to use an uncached syntax node in Semantics r=Veykril a=Veykril Fixes #10475 bors r+ Co-authored-by: cynecx <[email protected]> Co-authored-by: Lukas Wirth <[email protected]>
3 parents 5191fb7 + 07cd19d + 12465a8 commit 4675410

File tree

7 files changed

+65
-8
lines changed

7 files changed

+65
-8
lines changed

crates/hir/src/semantics.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ use crate::{
2525
db::HirDatabase,
2626
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
2727
source_analyzer::{resolve_hir_path, SourceAnalyzer},
28-
Access, AssocItem, Callable, ConstParam, Crate, Field, Function, HirFileId, Impl, InFile,
29-
Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait, Type,
30-
TypeAlias, TypeParam, VariantDef,
28+
Access, AssocItem, Callable, ConstParam, Crate, Field, Function, HasSource, HirFileId, Impl,
29+
InFile, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait,
30+
Type, TypeAlias, TypeParam, VariantDef,
3131
};
3232

3333
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -190,6 +190,14 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
190190
self.imp.descend_node_into_attributes(node)
191191
}
192192

193+
/// Search for a definition's source and cache its syntax tree
194+
pub fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
195+
where
196+
Def::Ast: AstNode,
197+
{
198+
self.imp.source(def)
199+
}
200+
193201
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
194202
self.imp.find_file(syntax_node.clone()).file_id
195203
}
@@ -845,6 +853,15 @@ impl<'db> SemanticsImpl<'db> {
845853
SemanticsScope { db: self.db, file_id, resolver }
846854
}
847855

856+
fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
857+
where
858+
Def::Ast: AstNode,
859+
{
860+
let res = def.source(self.db)?;
861+
self.cache(find_root(res.value.syntax()), res.file_id);
862+
Some(res)
863+
}
864+
848865
fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
849866
self.analyze_impl(node, None)
850867
}

crates/ide_assists/src/handlers/inline_call.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ast::make;
22
use either::Either;
3-
use hir::{db::HirDatabase, HasSource, PathResolution, Semantics, TypeInfo};
3+
use hir::{db::HirDatabase, PathResolution, Semantics, TypeInfo};
44
use ide_db::{
55
base_db::{FileId, FileRange},
66
defs::Definition,
@@ -194,7 +194,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
194194
}
195195
};
196196

197-
let fn_source = function.source(ctx.db())?;
197+
let fn_source = ctx.sema.source(function)?;
198198
let fn_body = fn_source.value.body()?;
199199
let param_list = fn_source.value.param_list()?;
200200

crates/ide_assists/src/handlers/replace_let_with_if_let.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
3838
// ```
3939
pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4040
let let_kw = ctx.find_token_syntax_at_offset(T![let])?;
41-
let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?;
41+
let let_stmt = let_kw.parent().and_then(ast::LetStmt::cast)?;
4242
let init = let_stmt.initializer()?;
4343
let original_pat = let_stmt.pat()?;
4444

crates/ide_completion/src/completions/trait_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn get_transformed_assoc_item(
185185
let assoc_item = assoc_item.clone_for_update();
186186
let trait_ = impl_def.trait_(ctx.db)?;
187187
let source_scope = &ctx.sema.scope_for_def(trait_);
188-
let target_scope = &ctx.sema.scope(impl_def.source(ctx.db)?.syntax().value);
188+
let target_scope = &ctx.sema.scope(ctx.sema.source(impl_def)?.syntax().value);
189189
let transform = PathTransform::trait_impl(
190190
target_scope,
191191
source_scope,

crates/parser/src/grammar/generic_args.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ fn generic_arg(p: &mut Parser) {
3232
k if k.is_literal() => const_arg(p),
3333
// test associated_type_bounds
3434
// fn print_all<T: Iterator<Item, Item::Item, Item::<true>, Item: Display, Item<'a> = Item>>(printables: T) {}
35-
IDENT if [T![<], T![=], T![:]].contains(&p.nth(1)) => {
35+
36+
// test macro_inside_generic_arg
37+
// type A = Foo<syn::Token![_]>;
38+
IDENT if [T![<], T![=], T![:]].contains(&p.nth(1)) && !p.nth_at(1, T![::]) => {
3639
let m = p.start();
3740
name_ref(p);
3841
opt_generic_arg_list(p, false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type A = Foo<syn::Token![_]>;

0 commit comments

Comments
 (0)