Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Reexport Erlang types for externally annotated types

author
Gears
committer
Louis Pilfold
date (Oct 27, 2025, 4:31 PM UTC) commit d19d5f82 parent 741b9135 change-id tnzkxwwv
+87 -3
+21 -3
compiler-core/src/erlang.rs
··· 334 334 constructors, 335 335 typed_parameters, 336 336 opaque, 337 + external_erlang, 337 338 .. 338 339 }) => { 339 340 // Erlang doesn't allow phantom type variables in type definitions but gleam does ··· 373 374 ); 374 375 // Type definitions 375 376 let definition = if constructors.is_empty() { 376 - let constructors = 377 - std::iter::once("any()".to_doc()).chain(phantom_vars_constructor); 378 - join(constructors, break_(" |", " | ")) 377 + if let Some((module, external_type, _location)) = external_erlang { 378 + let printer = TypePrinter::new(module_name); 379 + docvec![ 380 + module, 381 + ":", 382 + external_type, 383 + "(", 384 + join( 385 + typed_parameters 386 + .iter() 387 + .map(|parameter| printer.print(parameter)), 388 + ", ".to_doc() 389 + ), 390 + ")" 391 + ] 392 + } else { 393 + let constructors = 394 + std::iter::once("any()".to_doc()).chain(phantom_vars_constructor); 395 + join(constructors, break_(" |", " | ")) 396 + } 379 397 } else { 380 398 let constructors = constructors 381 399 .iter()
+23
compiler-core/src/erlang/tests/custom_types.rs
··· 4 4 fn phantom() { 5 5 assert_erl!("pub type Map(k, v)"); 6 6 } 7 + 8 + #[test] 9 + fn annotated_external_type() { 10 + assert_erl!( 11 + r#" 12 + @external(erlang, "gleam_stdlib", "dict") 13 + pub type Dict(key, value) 14 + "# 15 + ); 16 + } 17 + 18 + #[test] 19 + fn annotated_external_type_used_in_function() { 20 + assert_erl!( 21 + r#" 22 + @external(erlang, "gleam_stdlib", "dict") 23 + pub type Dict(key, value) 24 + 25 + @external(erlang, "maps", "get") 26 + pub fn get(dict: Dict(key, value), key: key) -> Result(value, Nil) 27 + "# 28 + ); 29 + }
+17
compiler-core/src/erlang/tests/snapshots/gleam_core__erlang__tests__custom_types__annotated_external_type.snap
··· 1 + --- 2 + source: compiler-core/src/erlang/tests/custom_types.rs 3 + expression: "\n@external(erlang, \"gleam_stdlib\", \"dict\")\npub type Dict(key, value)\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + @external(erlang, "gleam_stdlib", "dict") 8 + pub type Dict(key, value) 9 + 10 + 11 + ----- COMPILED ERLANG 12 + -module(my@mod). 13 + -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). 14 + -define(FILEPATH, "project/test/my/mod.gleam"). 15 + -export_type([dict/2]). 16 + 17 + -type dict(I, J) :: gleam_stdlib:dict(I, J).
+26
compiler-core/src/erlang/tests/snapshots/gleam_core__erlang__tests__custom_types__annotated_external_type_used_in_function.snap
··· 1 + --- 2 + source: compiler-core/src/erlang/tests/custom_types.rs 3 + expression: "\n@external(erlang, \"gleam_stdlib\", \"dict\")\npub type Dict(key, value)\n\n@external(erlang, \"maps\", \"get\")\npub fn get(dict: Dict(key, value), key: key) -> Result(value, Nil)\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + @external(erlang, "gleam_stdlib", "dict") 8 + pub type Dict(key, value) 9 + 10 + @external(erlang, "maps", "get") 11 + pub fn get(dict: Dict(key, value), key: key) -> Result(value, Nil) 12 + 13 + 14 + ----- COMPILED ERLANG 15 + -module(my@mod). 16 + -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). 17 + -define(FILEPATH, "project/test/my/mod.gleam"). 18 + -export([get/2]). 19 + -export_type([dict/2]). 20 + 21 + -type dict(I, J) :: gleam_stdlib:dict(I, J). 22 + 23 + -file("project/test/my/mod.gleam", 6). 24 + -spec get(dict(K, L), K) -> {ok, L} | {error, nil}. 25 + get(Dict, Key) -> 26 + maps:get(Dict, Key).