Skip to content

Commit 1a31896

Browse files
committed
Auto merge of rust-lang#14353 - lowr:fix/inline-call-bad-ast, r=Veykril
fix: don't replace `SyntaxToken` with `SyntaxNode` Fixes rust-lang#14339 When we inline method calls, we replace the `self` parameter with a local variable `this`. We have been replacing the `self` **tokens** with `NameRef` **nodes**, which makes the AST malformed. This leads to crash when we apply path transformation after the replacement (which only takes place when the method is generic and such scenario was not tested).
2 parents 05f95cb + 01bf072 commit 1a31896

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

crates/ide-assists/src/handlers/inline_call.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,10 @@ fn inline(
363363
.collect();
364364

365365
if function.self_param(sema.db).is_some() {
366-
let this = || make::name_ref("this").syntax().clone_for_update();
366+
let this = || make::name_ref("this").syntax().clone_for_update().first_token().unwrap();
367367
if let Some(self_local) = params[0].2.as_local(sema.db) {
368368
usages_for_locals(self_local)
369-
.flat_map(|FileReference { name, range, .. }| match name {
369+
.filter_map(|FileReference { name, range, .. }| match name {
370370
ast::NameLike::NameRef(_) => Some(body.syntax().covering_element(range)),
371371
_ => None,
372372
})
@@ -680,6 +680,42 @@ impl Foo {
680680
}
681681
}
682682
683+
fn main() {
684+
let x = {
685+
let ref this = Foo(3);
686+
Foo(this.0 + 2)
687+
};
688+
}
689+
"#,
690+
);
691+
}
692+
693+
#[test]
694+
fn generic_method_by_ref() {
695+
check_assist(
696+
inline_call,
697+
r#"
698+
struct Foo(u32);
699+
700+
impl Foo {
701+
fn add<T>(&self, a: u32) -> Self {
702+
Foo(self.0 + a)
703+
}
704+
}
705+
706+
fn main() {
707+
let x = Foo(3).add$0::<usize>(2);
708+
}
709+
"#,
710+
r#"
711+
struct Foo(u32);
712+
713+
impl Foo {
714+
fn add<T>(&self, a: u32) -> Self {
715+
Foo(self.0 + a)
716+
}
717+
}
718+
683719
fn main() {
684720
let x = {
685721
let ref this = Foo(3);

0 commit comments

Comments
 (0)