Repository: gleam-lang/gleam (rust)

Issue:
1.7.0-rc bug: Private types can be used by other modules
```
src
├── bug.gleam
└── wibble
    └── wobble.gleam
```
Inside wobble.gleam:
```gleam
type Wobble
```
Inside bug.gleam:
```gleam
import wibble/wobble

type Wibble {
  Wibble(wobble.Wobble)
}
```

Pull request description:
Fix private type bug
Fixes #4119 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 01c1c067b..b8d8ca471 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,11 @@
 - Function captures are now formatted like regular function calls.
   ([Giacomo Cavalieri](https://github.com/giacomocavalieri))
 
+### Bug Fixes
+
+- Fixed a bug where private types would be allowed to be used in other modules.
+  ([Surya Rose](https://github.com/GearsDatapacks))
+
 ## 1.7.0-rc2 - 2024-12-30
 
 ### Compiler
diff --git a/compiler-core/src/type_/environment.rs b/compiler-core/src/type_/environment.rs
index 8b4f4f2bd..d9109672b 100644
--- a/compiler-core/src/type_/environment.rs
+++ b/compiler-core/src/type_/environment.rs
@@ -350,7 +350,7 @@ impl Environment<'_> {
         module_alias: &Option<(EcoString, SrcSpan)>,
         name: &EcoString,
     ) -> Result<&TypeConstructor, UnknownTypeConstructorError> {
-        let t = match module_alias {
+        match module_alias {
             None => self
                 .module_types
                 .get(name)
@@ -369,19 +369,16 @@ impl Environment<'_> {
                 })?;
                 let _ = self.unused_modules.remove(module_name);
                 let _ = self.unused_module_aliases.remove(module_name);
-                module
-                    .types
-                    .get(name)
-                    .ok_or_else(|| UnknownTypeConstructorError::ModuleType {
+                module.get_public_type(name).ok_or_else(|| {
+                    UnknownTypeConstructorError::ModuleType {
                         name: name.clone(),
                         module_name: module.name.clone(),
                         type_constructors: module.public_type_names(),
                         imported_type_as_value: false,
-                    })
+                    }
+                })
             }
-        }?;
-
-        Ok(t)
+        }
     }
 
     fn unknown_type_hint(&self, type_name: &EcoString) -> UnknownTypeHint {


Test patch:
diff --git a/compiler-core/src/type_/snapshots/gleam_core__type___tests__private_types_not_available_in_other_modules.snap b/compiler-core/src/type_/snapshots/gleam_core__type___tests__private_types_not_available_in_other_modules.snap
new file mode 100644
index 000000000..d08b63633
--- /dev/null
+++ b/compiler-core/src/type_/snapshots/gleam_core__type___tests__private_types_not_available_in_other_modules.snap
@@ -0,0 +1,25 @@
+---
+source: compiler-core/src/type_/tests.rs
+expression: "\nimport wibble\n\ntype Wibble {\n  Wibble(wibble.Wibble)\n}\n"
+---
+----- SOURCE CODE
+-- wibble.gleam
+type Wibble
+
+-- main.gleam
+
+import wibble
+
+type Wibble {
+  Wibble(wibble.Wibble)
+}
+
+
+----- ERROR
+error: Unknown module type
+  ┌─ /src/one/two.gleam:5:10
+  │
+5 │   Wibble(wibble.Wibble)
+  │          ^^^^^^^^^^^^^
+
+The module `wibble` does not have a `Wibble` type.
diff --git a/compiler-core/src/type_/tests.rs b/compiler-core/src/type_/tests.rs
index ea1ce080d..bd3fa10f7 100644
--- a/compiler-core/src/type_/tests.rs
+++ b/compiler-core/src/type_/tests.rs
@@ -3025,3 +3025,17 @@ pub fn main() {
         vec![("main", "fn() -> Int")]
     );
 }
+
+#[test]
+fn private_types_not_available_in_other_modules() {
+    assert_with_module_error!(
+        ("wibble", "type Wibble"),
+        "
+import wibble
+
+type Wibble {
+  Wibble(wibble.Wibble)
+}
+"
+    );
+}