Skip to content

Commit 50cd622

Browse files
giacomocavalierilpil
authored andcommitted
use correct type for extracted nested use
1 parent 4a40121 commit 50cd622

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
extract a `use` expression.
99
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
1010

11+
- Fixed a bug where the "Extract function" code action would generate a function
12+
with the wrong type when used on a use expression.
13+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
14+
1115
## v1.13.0-rc1 - 2025-09-29
1216

1317
### Compiler

compiler-core/src/language_server/code_action.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8635,13 +8635,23 @@ impl<'a> ExtractFunction<'a> {
86358635
end,
86368636
)
86378637
}
8638-
ExtractedValue::Expression(expression) => self.extract_code_in_tail_position(
8639-
expression.location(),
8640-
expression.location(),
8641-
expression.type_(),
8642-
extracted.parameters,
8643-
end,
8644-
),
8638+
ExtractedValue::Expression(expression) => {
8639+
let expression_type = match expression {
8640+
TypedExpr::Fn {
8641+
type_,
8642+
kind: FunctionLiteralKind::Use { .. },
8643+
..
8644+
} => type_.fn_types().expect("use callback to be a function").1,
8645+
_ => expression.type_(),
8646+
};
8647+
self.extract_code_in_tail_position(
8648+
expression.location(),
8649+
expression.location(),
8650+
expression_type,
8651+
extracted.parameters,
8652+
end,
8653+
)
8654+
}
86458655
ExtractedValue::Statements {
86468656
location,
86478657
position: StatementPosition::NotTail,

compiler-core/src/language_server/tests/action.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10571,3 +10571,22 @@ fn wibble(f: fn() -> Int) -> Int { f() }
1057110571
);
1057210572
}
1057310573

10574+
#[test]
10575+
fn extract_use_in_tail_position_2() {
10576+
assert_code_action!(
10577+
EXTRACT_FUNCTION,
10578+
r#"
10579+
pub fn main() {
10580+
use <- wibble
10581+
use <- wobble
10582+
123
10583+
}
10584+
10585+
fn wibble(f: fn() -> Float) -> Float { f() }
10586+
fn wobble(f: fn() -> Int) -> Float { 1.1 }
10587+
"#,
10588+
find_position_of("use")
10589+
.nth_occurrence(2)
10590+
.select_until(find_position_of("wobble"))
10591+
);
10592+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\npub fn main() {\n use <- wibble\n use <- wobble\n 123\n}\n\nfn wibble(f: fn() -> Float) -> Float { f() }\nfn wobble(f: fn() -> Int) -> Float { 1.1 }\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
pub fn main() {
8+
use <- wibble
9+
use <- wobble
10+
▔▔▔▔▔▔▔↑
11+
123
12+
}
13+
14+
fn wibble(f: fn() -> Float) -> Float { f() }
15+
fn wobble(f: fn() -> Int) -> Float { 1.1 }
16+
17+
18+
----- AFTER ACTION
19+
20+
pub fn main() {
21+
use <- wibble
22+
function()
23+
}
24+
25+
fn function() -> Float {
26+
use <- wobble
27+
123
28+
}
29+
30+
fn wibble(f: fn() -> Float) -> Float { f() }
31+
fn wobble(f: fn() -> Int) -> Float { 1.1 }

0 commit comments

Comments
 (0)