Skip to content

Commit 3555e7b

Browse files
scristoballpil
authored andcommitted
avoid qualify code action on shadowed modules
1 parent cdde47f commit 3555e7b

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,8 @@
411411
- Only print the user-friendly LSP message when stdin is a terminal to avoid
412412
emitting redundant logs.
413413
([Ariel Parker](https://github.com/arielherself))
414+
415+
- Fixed a bug where the language server would wrongly override local variables
416+
if they were shadowing an unqualified module function.
417+
([Samuel Cristobal](https://github.com/scristobal))
418+

compiler-core/src/language_server/code_action.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,11 @@ impl<'ast> ast::visit::Visit<'ast> for UnqualifiedToQualifiedImportSecondPass<'a
22212221
layer,
22222222
..
22232223
} = &self.unqualified_constructor;
2224-
if layer.is_value() && wanted_constructor.used_name() == name {
2224+
2225+
if layer.is_value()
2226+
&& wanted_constructor.used_name() == name
2227+
&& !constructor.is_local_variable()
2228+
{
22252229
self.add_module_qualifier(*location);
22262230
}
22272231
ast::visit::visit_typed_expr_var(self, location, constructor, name);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,6 +3582,37 @@ pub fn main() {
35823582
.select_until(find_position_of("(1)")),
35833583
);
35843584
}
3585+
3586+
#[test]
3587+
fn test_unqualified_to_qualified_import_variable_shadowing() {
3588+
let src = r#"
3589+
3590+
import wibble.{wobble}
3591+
3592+
pub fn example() {
3593+
echo wobble
3594+
3595+
let wobble = 1
3596+
3597+
echo wobble
3598+
3599+
let _ = fn(wobble) {
3600+
echo wobble
3601+
}
3602+
3603+
todo
3604+
}
3605+
"#;
3606+
3607+
assert_code_action!(
3608+
"Qualify wobble as wibble.wobble",
3609+
TestProject::for_source(src).add_hex_module("wibble", "pub fn wobble() { todo }"),
3610+
find_position_of("wob")
3611+
.nth_occurrence(2)
3612+
.select_until(find_position_of("ble").nth_occurrence(3))
3613+
);
3614+
}
3615+
35853616
/* TODO: implement qualified unused location
35863617
#[test]
35873618
fn test_remove_unused_qualified_action() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\n\nimport wibble.{wobble}\n\npub fn example() {\n echo wobble\n\n let wobble = 1\n\n echo wobble\n\n let _ = fn(wobble) {\n echo wobble\n }\n\n todo\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
8+
import wibble.{wobble}
9+
10+
pub fn example() {
11+
echo wobble
12+
▔▔▔↑
13+
14+
let wobble = 1
15+
16+
echo wobble
17+
18+
let _ = fn(wobble) {
19+
echo wobble
20+
}
21+
22+
todo
23+
}
24+
25+
26+
----- AFTER ACTION
27+
28+
29+
import wibble.{}
30+
31+
pub fn example() {
32+
echo wibble.wobble
33+
34+
let wobble = 1
35+
36+
echo wobble
37+
38+
let _ = fn(wobble) {
39+
echo wobble
40+
}
41+
42+
todo
43+
}

0 commit comments

Comments
 (0)