Skip to content

Commit 2113d92

Browse files
giacomocavalierilpil
authored andcommitted
fix crash in "pattern match on variable code action"
1 parent 195c44a commit 2113d92

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
the final step of a pipeline.
2626
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
2727

28+
- Fixed a bug where the "pattern match on variable" code action would crash when
29+
used on a variable followed by a case expression.
30+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
31+
2832
## v1.13.0-rc1 - 2025-09-29
2933

3034
### Compiler

compiler-core/src/language_server/code_action.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4923,6 +4923,13 @@ impl<'ast, IO> ast::visit::Visit<'ast> for PatternMatchOnValue<'ast, IO> {
49234923
}
49244924

49254925
fn visit_typed_assignment(&mut self, assignment: &'ast TypedAssignment) {
4926+
// If we're not inside the assignment there's no point in exploring its
4927+
// ast further.
4928+
let assignment_range = self.edits.src_span_to_lsp_range(assignment.location);
4929+
if !within(self.params.range, assignment_range) {
4930+
return;
4931+
}
4932+
49264933
ast::visit::visit_typed_assignment(self, assignment);
49274934
if let Some((name, _, ref type_)) = self.pattern_variable_under_cursor {
49284935
self.selected_value = Some(PatternMatchedValue::LetVariable {
@@ -4934,6 +4941,13 @@ impl<'ast, IO> ast::visit::Visit<'ast> for PatternMatchOnValue<'ast, IO> {
49344941
}
49354942

49364943
fn visit_typed_clause(&mut self, clause: &'ast ast::TypedClause) {
4944+
// If we're not inside the clause there's no point in exploring its
4945+
// ast further.
4946+
let clause_range = self.edits.src_span_to_lsp_range(clause.location);
4947+
if !within(self.params.range, clause_range) {
4948+
return;
4949+
}
4950+
49374951
for pattern in clause.pattern.iter() {
49384952
self.visit_typed_pattern(pattern);
49394953
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7538,6 +7538,33 @@ fn map(list: List(a), fun: fn(a) -> b) { todo }
75387538
);
75397539
}
75407540

7541+
#[test]
7542+
// https://github.com/gleam-lang/gleam/issues/5042
7543+
fn pattern_match_on_variable_crashes() {
7544+
assert_code_action!(
7545+
PATTERN_MATCH_ON_VARIABLE,
7546+
r#"
7547+
pub type Wibble {
7548+
Wibble(Wobble)
7549+
}
7550+
7551+
pub type Wobble {
7552+
Wobble
7553+
Wubble
7554+
}
7555+
7556+
pub fn main() {
7557+
let Wibble(wobble) = todo
7558+
7559+
case todo {
7560+
_ -> todo
7561+
}
7562+
}
7563+
"#,
7564+
find_position_of("wobble").to_selection()
7565+
);
7566+
}
7567+
75417568
#[test]
75427569
fn generate_function_works_with_invalid_call() {
75437570
assert_code_action!(
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\npub type Wibble {\n Wibble(Wobble)\n}\n\npub type Wobble {\n Wobble\n Wubble\n}\n\npub fn main() {\n let Wibble(wobble) = todo\n\n case todo {\n _ -> todo\n }\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
pub type Wibble {
8+
Wibble(Wobble)
9+
}
10+
11+
pub type Wobble {
12+
Wobble
13+
Wubble
14+
}
15+
16+
pub fn main() {
17+
let Wibble(wobble) = todo
18+
19+
20+
case todo {
21+
_ -> todo
22+
}
23+
}
24+
25+
26+
----- AFTER ACTION
27+
28+
pub type Wibble {
29+
Wibble(Wobble)
30+
}
31+
32+
pub type Wobble {
33+
Wobble
34+
Wubble
35+
}
36+
37+
pub fn main() {
38+
let Wibble(wobble) = todo
39+
case wobble {
40+
Wobble -> todo
41+
Wubble -> todo
42+
}
43+
44+
case todo {
45+
_ -> todo
46+
}
47+
}

0 commit comments

Comments
 (0)