diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a2ce7ff7..12d48eb44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -501,3 +501,7 @@ - Fixed a bug where running `gleam docs build` on non-Erlang target projects with a warm cache would not produce the module pages. ([Matt Champagne](https://github.com/han-tyumi)) + +- Fixed a bug where an incorrect `package-interface.json` would be generated for + certain type aliases. + ([Surya Rose](https://github.com/GearsDatapacks)) diff --git a/compiler-core/src/package_interface.rs b/compiler-core/src/package_interface.rs index 9f13ff5a8..56dbaf264 100644 --- a/compiler-core/src/package_interface.rs +++ b/compiler-core/src/package_interface.rs @@ -440,7 +440,7 @@ impl ModuleInterface { label: arg.label.clone(), // We share the same id_map between each step so that the // incremental ids assigned are consisten with each other - type_: from_type_helper(arg.type_.as_ref(), &mut id_map), + type_: from_type_with_ids(arg.type_.as_ref(), &mut id_map), }) .collect(), }) @@ -456,13 +456,19 @@ impl ModuleInterface { .iter() .filter(|(_, v)| v.publicity.is_public()) { + let mut id_map = IdMap::new(); + + for typed_parameter in alias.parameters.iter() { + id_map.add_type_variable_id(typed_parameter.as_ref()); + } + let _ = type_aliases.insert( name.clone(), TypeAliasInterface { documentation: alias.documentation.clone(), deprecation: DeprecationInterface::from_deprecation(&alias.deprecation), parameters: alias.arity, - alias: TypeInterface::from_type(&alias.type_), + alias: from_type_with_ids(&alias.type_, &mut id_map), }, ); } @@ -507,10 +513,10 @@ impl ModuleInterface { label: reverse_field_map .get(&(index as u32)) .map(|label| (*label).clone()), - type_: from_type_helper(type_, &mut id_map), + type_: from_type_with_ids(type_, &mut id_map), }) .collect(), - return_: from_type_helper(return_type, &mut id_map), + return_: from_type_with_ids(return_type, &mut id_map), }, ); } @@ -552,7 +558,7 @@ impl ModuleInterface { impl TypeInterface { fn from_type(type_: &Type) -> TypeInterface { - from_type_helper(type_, &mut IdMap::new()) + from_type_with_ids(type_, &mut IdMap::new()) } } @@ -560,20 +566,20 @@ impl TypeInterface { /// the type variables' ids that appear in the type are mapped to an incremental /// number and consistent with each other (that is, two types variables that /// have the same id will also have the same incremental number in the end). -fn from_type_helper(type_: &Type, id_map: &mut IdMap) -> TypeInterface { +fn from_type_with_ids(type_: &Type, id_map: &mut IdMap) -> TypeInterface { match type_ { Type::Fn { arguments, return_ } => TypeInterface::Fn { parameters: arguments .iter() - .map(|argument| from_type_helper(argument.as_ref(), id_map)) + .map(|argument| from_type_with_ids(argument.as_ref(), id_map)) .collect(), - return_: Box::new(from_type_helper(return_, id_map)), + return_: Box::new(from_type_with_ids(return_, id_map)), }, Type::Tuple { elements } => TypeInterface::Tuple { elements: elements .iter() - .map(|element| from_type_helper(element.as_ref(), id_map)) + .map(|element| from_type_with_ids(element.as_ref(), id_map)) .collect(), }, @@ -583,7 +589,7 @@ fn from_type_helper(type_: &Type, id_map: &mut IdMap) -> TypeInterface { .expect("borrow type after inference") .deref() { - TypeVar::Link { type_ } => from_type_helper(type_, id_map), + TypeVar::Link { type_ } => from_type_with_ids(type_, id_map), // Since package serialisation happens after inference there // should be no unbound type variables. // TODO: This branch should be `unreachable!()` but because of @@ -613,7 +619,7 @@ fn from_type_helper(type_: &Type, id_map: &mut IdMap) -> TypeInterface { module: module.clone(), parameters: arguments .iter() - .map(|argument| from_type_helper(argument.as_ref(), id_map)) + .map(|argument| from_type_with_ids(argument.as_ref(), id_map)) .collect(), }, } diff --git a/compiler-core/src/package_interface/snapshots/gleam_core__package_interface__tests__type_alias_with_two_parameters.snap b/compiler-core/src/package_interface/snapshots/gleam_core__package_interface__tests__type_alias_with_two_parameters.snap new file mode 100644 index 000000000..3d6e0a5d7 --- /dev/null +++ b/compiler-core/src/package_interface/snapshots/gleam_core__package_interface__tests__type_alias_with_two_parameters.snap @@ -0,0 +1,40 @@ +--- +source: compiler-core/src/package_interface/tests.rs +expression: "pub type Wibble(a, b) = Result(a, b)" +--- +{ + "name": "my_package", + "version": "11.10.9-1.wibble+build", + "gleam-version-constraint": "1.0.0", + "modules": { + "my/module": { + "documentation": [], + "type-aliases": { + "Wibble": { + "documentation": null, + "deprecation": null, + "parameters": 2, + "alias": { + "kind": "named", + "name": "Result", + "package": "", + "module": "gleam", + "parameters": [ + { + "kind": "variable", + "id": 0 + }, + { + "kind": "variable", + "id": 1 + } + ] + } + } + }, + "types": {}, + "constants": {}, + "functions": {} + } + } +} diff --git a/compiler-core/src/package_interface/snapshots/gleam_core__package_interface__tests__type_alias_with_two_parameters_in_reverse.snap b/compiler-core/src/package_interface/snapshots/gleam_core__package_interface__tests__type_alias_with_two_parameters_in_reverse.snap new file mode 100644 index 000000000..c07c67151 --- /dev/null +++ b/compiler-core/src/package_interface/snapshots/gleam_core__package_interface__tests__type_alias_with_two_parameters_in_reverse.snap @@ -0,0 +1,40 @@ +--- +source: compiler-core/src/package_interface/tests.rs +expression: "pub type Wibble(a, b) = Result(b, a)" +--- +{ + "name": "my_package", + "version": "11.10.9-1.wibble+build", + "gleam-version-constraint": "1.0.0", + "modules": { + "my/module": { + "documentation": [], + "type-aliases": { + "Wibble": { + "documentation": null, + "deprecation": null, + "parameters": 2, + "alias": { + "kind": "named", + "name": "Result", + "package": "", + "module": "gleam", + "parameters": [ + { + "kind": "variable", + "id": 1 + }, + { + "kind": "variable", + "id": 0 + } + ] + } + } + }, + "types": {}, + "constants": {}, + "functions": {} + } + } +} diff --git a/compiler-core/src/package_interface/tests.rs b/compiler-core/src/package_interface/tests.rs index 5db49e339..0bb223c20 100644 --- a/compiler-core/src/package_interface/tests.rs +++ b/compiler-core/src/package_interface/tests.rs @@ -338,3 +338,15 @@ pub type Wibble { "# ); } + +// https://github.com/gleam-lang/gleam/issues/5957 +#[test] +pub fn type_alias_with_two_parameters() { + assert_package_interface!("pub type Wibble(a, b) = Result(a, b)"); +} + +// https://github.com/gleam-lang/gleam/issues/5957 +#[test] +pub fn type_alias_with_two_parameters_in_reverse() { + assert_package_interface!("pub type Wibble(a, b) = Result(b, a)"); +}