Skip to content

Commit f267937

Browse files
GearsDatapackslpil
authored andcommitted
Fix renaming variables in record updates
1 parent 5d777af commit f267937

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,7 @@
272272

273273
- Fixed a bug where `echo .. as ..` message will be omitted in browser target.
274274
([Andrey Kozhev](https://github.com/ankddev))
275+
276+
- Fixed a bug where renaming a variable used in a record update would produce
277+
invalid code in certain situations.
278+
([Surya Rose](https://github.com/GearsDatapacks))

compiler-core/src/language_server/reference.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,16 @@ impl<'ast> Visit<'ast> for FindVariableReferences {
400400
location: definition_location,
401401
..
402402
} if definition_location == self.definition_location => {
403-
self.references.push(VariableReference {
404-
location: *location,
405-
kind: VariableReferenceKind::Variable,
406-
})
403+
if !self
404+
.references
405+
.iter()
406+
.any(|reference| reference.location == *location)
407+
{
408+
self.references.push(VariableReference {
409+
location: *location,
410+
kind: VariableReferenceKind::Variable,
411+
})
412+
}
407413
}
408414
_ => {}
409415
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,3 +1550,20 @@ pub const something = 10
15501550
find_position_of("something")
15511551
);
15521552
}
1553+
1554+
#[test]
1555+
fn rename_variable_used_in_record_update() {
1556+
assert_rename!(
1557+
"
1558+
type Wibble {
1559+
Wibble(a: Int, b: Int, c: Int)
1560+
}
1561+
1562+
fn wibble(wibble: Wibble) {
1563+
Wibble(..wibble, c: 1)
1564+
}
1565+
",
1566+
"value",
1567+
find_position_of("wibble:")
1568+
);
1569+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
source: compiler-core/src/language_server/tests/rename.rs
3+
assertion_line: 1556
4+
expression: "\ntype Wibble {\n Wibble(a: Int, b: Int, c: Int)\n}\n\nfn wibble(wibble: Wibble) {\n Wibble(..wibble, c: 1)\n}\n"
5+
snapshot_kind: text
6+
---
7+
----- BEFORE RENAME
8+
-- app.gleam
9+
10+
type Wibble {
11+
Wibble(a: Int, b: Int, c: Int)
12+
}
13+
14+
fn wibble(wibble: Wibble) {
15+
↑▔▔▔▔▔
16+
Wibble(..wibble, c: 1)
17+
}
18+
19+
20+
----- AFTER RENAME
21+
-- app.gleam
22+
23+
type Wibble {
24+
Wibble(a: Int, b: Int, c: Int)
25+
}
26+
27+
fn wibble(value: Wibble) {
28+
Wibble(..value, c: 1)
29+
}

0 commit comments

Comments
 (0)