Skip to content

Commit c46a638

Browse files
committed
Fix unused module for discarded alias collision
1 parent de199aa commit c46a638

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

compiler-core/src/reference.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,10 @@ impl ReferenceTracker {
748748
/// that remembers which module it came from.
749749
///
750750
fn register_module_reference_by_module_name(&mut self, name: EcoString) {
751-
let target = match self.module_name_to_node.get(&name) {
752-
Some(target) => *target,
753-
None => self.get_or_create_node(name, EntityLayer::Module),
751+
// If the module has no node then it was imported with a discarded
752+
// alias, meaning there's no import for the reference to point at.
753+
let Some(target) = self.module_name_to_node.get(&name).copied() else {
754+
return;
754755
};
755756
_ = self.graph.add_edge(self.current_node, target, ());
756757
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "\n import wibble/wobble\n import wobble.{two} as _wobble\n pub fn main() { two }\n "
4+
---
5+
----- SOURCE CODE
6+
-- wibble/wobble.gleam
7+
pub const one = 1
8+
9+
-- wobble.gleam
10+
pub const two = 2
11+
12+
-- main.gleam
13+
14+
import wibble/wobble
15+
import wobble.{two} as _wobble
16+
pub fn main() { two }
17+
18+
19+
----- WARNING
20+
warning: Unused imported module
21+
┌─ /src/warning/wrn.gleam:2:13
22+
23+
2import wibble/wobble
24+
^^^^^^^^^^^^^^^^^^^^ This imported module is never used
25+
26+
Hint: You can safely remove it.

compiler-core/src/type_/tests/warnings.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,19 @@ fn unused_module_alias_not_confused_with_another_module_name() {
11891189
);
11901190
}
11911191

1192+
#[test]
1193+
fn unused_module_not_marked_as_used_by_discard_aliased_import_with_same_name() {
1194+
assert_warning!(
1195+
("thepackage", "wibble/wobble", "pub const one = 1"),
1196+
("thepackage", "wobble", "pub const two = 2"),
1197+
r#"
1198+
import wibble/wobble
1199+
import wobble.{two} as _wobble
1200+
pub fn main() { two }
1201+
"#
1202+
);
1203+
}
1204+
11921205
#[test]
11931206
fn result_in_case_discarded() {
11941207
assert_warning!(

0 commit comments

Comments
 (0)