Skip to content

Commit d07f4e4

Browse files
bors[bot]Veykril
andauthored
Merge #9893
9893: fix: Don't use uncached syntax nodes in `generate_function` for sema lookups r=Veykril a=Veykril Fixes the crash in the comment here #9382 (comment) Couldn't make out a repro test for this unfortunately still not idea about the original issue Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents c1a7169 + 37ad9cb commit d07f4e4

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ to_def_impls![
884884
(crate::Local, ast::IdentPat, bind_pat_to_def),
885885
(crate::Local, ast::SelfParam, self_param_to_def),
886886
(crate::Label, ast::Label, label_to_def),
887+
(crate::Adt, ast::Adt, adt_to_def),
887888
];
888889

889890
fn find_root(node: &SyntaxNode) -> SyntaxNode {

crates/hir/src/semantics/source_to_def.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ use hir_def::{
9191
dyn_map::DynMap,
9292
expr::{LabelId, PatId},
9393
keys::{self, Key},
94-
ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId,
95-
ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
96-
UnionId, VariantId,
94+
AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId,
95+
GenericDefId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
96+
TypeParamId, UnionId, VariantId,
9797
};
9898
use hir_expand::{name::AsName, AstId, MacroCallId, MacroDefId, MacroDefKind};
9999
use rustc_hash::FxHashMap;
@@ -201,6 +201,18 @@ impl SourceToDefCtx<'_, '_> {
201201
) -> Option<EnumVariantId> {
202202
self.to_def(src, keys::VARIANT)
203203
}
204+
pub(super) fn adt_to_def(
205+
&mut self,
206+
InFile { file_id, value }: InFile<ast::Adt>,
207+
) -> Option<AdtId> {
208+
match value {
209+
ast::Adt::Enum(it) => self.enum_to_def(InFile::new(file_id, it)).map(AdtId::EnumId),
210+
ast::Adt::Struct(it) => {
211+
self.struct_to_def(InFile::new(file_id, it)).map(AdtId::StructId)
212+
}
213+
ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
214+
}
215+
}
204216
pub(super) fn bind_pat_to_def(
205217
&mut self,
206218
src: InFile<ast::IdentPat>,

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{HasSource, HirDisplay, InFile, Module, TypeInfo};
1+
use hir::{HasSource, HirDisplay, Module, TypeInfo};
22
use ide_db::{base_db::FileId, helpers::SnippetCap};
33
use rustc_hash::{FxHashMap, FxHashSet};
44
use stdx::to_lower_snake_case;
@@ -106,31 +106,28 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
106106

107107
fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
108108
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
109-
let fn_name: ast::NameRef = ast::NameRef::cast(
110-
call.syntax().children().find(|child| child.kind() == SyntaxKind::NAME_REF)?,
111-
)?;
112-
let ty = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;
109+
let fn_name = call.name_ref()?;
110+
let adt = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;
113111

114-
let current_module =
115-
ctx.sema.scope(ctx.find_node_at_offset::<ast::MethodCallExpr>()?.syntax()).module()?;
116-
let target_module = ty.module(ctx.sema.db);
112+
let current_module = ctx.sema.scope(call.syntax()).module()?;
113+
let target_module = adt.module(ctx.sema.db);
117114

118115
if current_module.krate() != target_module.krate() {
119116
return None;
120117
}
121118

122-
let (impl_, file) = match ty {
123-
hir::Adt::Struct(strukt) => get_impl(strukt.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
124-
hir::Adt::Enum(en) => get_impl(en.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
125-
hir::Adt::Union(union) => get_impl(union.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
126-
}?;
119+
let range = adt.source(ctx.sema.db)?.syntax().original_file_range(ctx.sema.db);
120+
let file = ctx.sema.parse(range.file_id);
121+
let adt_source =
122+
ctx.sema.find_node_at_offset_with_macros(file.syntax(), range.range.start())?;
123+
let impl_ = find_struct_impl(ctx, &adt_source, fn_name.text().as_str())?;
127124

128125
let function_builder = FunctionBuilder::from_method_call(
129126
ctx,
130127
&call,
131128
&fn_name,
132129
&impl_,
133-
file,
130+
range.file_id,
134131
target_module,
135132
current_module,
136133
)?;
@@ -145,7 +142,7 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
145142
builder.edit_file(function_template.file);
146143
let mut new_fn = function_template.to_string(ctx.config.snippet_cap);
147144
if impl_.is_none() {
148-
new_fn = format!("\nimpl {} {{\n{}\n}}", ty.name(ctx.sema.db), new_fn,);
145+
new_fn = format!("\nimpl {} {{\n{}\n}}", adt.name(ctx.sema.db), new_fn,);
149146
}
150147
match ctx.config.snippet_cap {
151148
Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
@@ -155,18 +152,6 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
155152
)
156153
}
157154

158-
fn get_impl(
159-
adt: InFile<&SyntaxNode>,
160-
fn_name: &ast::NameRef,
161-
ctx: &AssistContext,
162-
) -> Option<(Option<ast::Impl>, FileId)> {
163-
let file = adt.file_id.original_file(ctx.sema.db);
164-
let adt = adt.value;
165-
let adt = ast::Adt::cast(adt.clone())?;
166-
let r = find_struct_impl(ctx, &adt, fn_name.text().as_str())?;
167-
Some((r, file))
168-
}
169-
170155
struct FunctionTemplate {
171156
insert_offset: TextSize,
172157
leading_ws: String,

crates/ide_assists/src/utils.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod gen_trait_fn_body;
55

66
use std::ops;
77

8-
use hir::{Adt, HasSource};
8+
use hir::HasSource;
99
use ide_db::{helpers::SnippetCap, path_transform::PathTransform, RootDatabase};
1010
use itertools::Itertools;
1111
use stdx::format_to;
@@ -290,19 +290,13 @@ pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
290290
// FIXME: this partially overlaps with `find_impl_block_*`
291291
pub(crate) fn find_struct_impl(
292292
ctx: &AssistContext,
293-
strukt: &ast::Adt,
293+
adt: &ast::Adt,
294294
name: &str,
295295
) -> Option<Option<ast::Impl>> {
296296
let db = ctx.db();
297-
let module = strukt.syntax().ancestors().find(|node| {
298-
ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind())
299-
})?;
300-
301-
let struct_def = match strukt {
302-
ast::Adt::Enum(e) => Adt::Enum(ctx.sema.to_def(e)?),
303-
ast::Adt::Struct(s) => Adt::Struct(ctx.sema.to_def(s)?),
304-
ast::Adt::Union(u) => Adt::Union(ctx.sema.to_def(u)?),
305-
};
297+
let module = adt.syntax().parent()?;
298+
299+
let struct_def = ctx.sema.to_def(adt)?;
306300

307301
let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
308302
let blk = ctx.sema.to_def(&impl_blk)?;

0 commit comments

Comments
 (0)