Skip to content

Commit b3e1882

Browse files
authored
fix(compiler): Fix compilation of functions annotated with alias (#1293)
1 parent ceb8dac commit b3e1882

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

compiler/src/middle_end/linearize.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ let rec transl_anf_statement =
14861486
let (argsty, retty) =
14871487
get_fn_allocation_type(env, desc.tvd_desc.ctyp_type);
14881488
let retty =
1489-
if (returns_void(desc.tvd_desc.ctyp_type)) {
1489+
if (returns_void(env, desc.tvd_desc.ctyp_type)) {
14901490
[];
14911491
} else {
14921492
[retty];

compiler/src/typed/ctype.re

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,17 +3246,8 @@ let nondep_extension_constructor = (env, id, ext) =>
32463246
switch (args) {
32473247
| TConstrSingleton => ReprValue(WasmI32)
32483248
| TConstrTuple(args) =>
3249-
ReprFunction(
3250-
List.map(
3251-
arg =>
3252-
Type_utils.wasm_repr_of_allocation_type(
3253-
Type_utils.get_allocation_type(env, arg),
3254-
),
3255-
args,
3256-
),
3257-
[WasmI32],
3258-
Indirect,
3259-
)
3249+
// All native Grain function args are Managed (i32) types.
3250+
ReprFunction(List.map(_ => WasmI32, args), [WasmI32], Indirect)
32603251
};
32613252

32623253
clear_hash();

compiler/src/typed/type_utils.re

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ let rec get_fn_allocation_type = (env, ty) => {
2222
List.map(get_allocation_type(env), args),
2323
get_allocation_type(env, ret),
2424
)
25-
| TTyConstr(_)
25+
| TTyConstr(path, args, _) =>
26+
let (ty_args, ty, _) = Env.find_type_expansion(path, env);
27+
get_fn_allocation_type(env, Ctype.apply(env, ty_args, ty, args));
2628
| TTyVar(_)
2729
| TTyTuple(_)
2830
| TTyRecord(_)
@@ -61,12 +63,14 @@ let rec is_void = ty => {
6163
};
6264
};
6365

64-
let rec returns_void = ty => {
66+
let rec returns_void = (env, ty) => {
6567
switch (ty.desc) {
6668
| TTySubst(linked)
67-
| TTyLink(linked) => returns_void(linked)
69+
| TTyLink(linked) => returns_void(env, linked)
6870
| TTyArrow(args, ret, _) => is_void(ret)
69-
| TTyConstr(_)
71+
| TTyConstr(path, args, _) =>
72+
let (ty_args, ty, _) = Env.find_type_expansion(path, env);
73+
returns_void(env, Ctype.apply(env, ty_args, ty, args));
7074
| TTyVar(_)
7175
| TTyTuple(_)
7276
| TTyRecord(_)
@@ -92,7 +96,7 @@ let repr_of_type = (env, ty) =>
9296
let (args, ret) = get_fn_allocation_type(env, ty);
9397
let args = List.map(wasm_repr_of_allocation_type, args);
9498
let rets =
95-
if (returns_void(ty)) {
99+
if (returns_void(env, ty)) {
96100
[];
97101
} else {
98102
[wasm_repr_of_allocation_type(ret)];

compiler/test/suites/types.re

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ describe("aliased types", ({test, testSkip}) => {
184184
|},
185185
"expression was expected of type %Aliases.Foo = Number",
186186
);
187+
assertRun(
188+
"regression_annotated_type_func_1",
189+
{|
190+
type AddPrinter = (Number, Number) -> Void
191+
export let add: AddPrinter = (x, y) => print(x + y)
192+
add(4, 4)
193+
|},
194+
"8\n",
195+
);
196+
assertRun(
197+
"regression_annotated_type_func_2",
198+
{|
199+
type AddPrinter<a> = (a, a) -> Void
200+
export let add: AddPrinter<Number> = (x, y) => print(x + y)
201+
add(4, 4)
202+
|},
203+
"8\n",
204+
);
187205
});
188206

189207
describe("abstract types", ({test}) => {

0 commit comments

Comments
 (0)