Skip to content

Commit 75bbb93

Browse files
authored
fix(compiler): Properly handle types not imported (#1329)
1 parent fb947b5 commit 75bbb93

File tree

4 files changed

+211
-2
lines changed

4 files changed

+211
-2
lines changed

compiler/src/typed/type_utils.re

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ open Types;
22

33
let rec get_allocation_type = (env, ty) => {
44
switch (ty.desc) {
5-
| TTyConstr(path, _, _) => Env.find_type(path, env).type_allocation
5+
| TTyConstr(path, _, _) =>
6+
try(Env.find_type(path, env).type_allocation) {
7+
// Types not in the environment come from other modules and are nested in
8+
// types we do know about; we treat them as Managed Grain values.
9+
| Not_found => Managed
10+
}
611
| TTySubst(linked)
712
| TTyLink(linked) => get_allocation_type(env, linked)
813
| TTyVar(_)
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
exports › export12
2+
(module
3+
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
4+
(type $none_=>_i32 (func (result i32)))
5+
(type $none_=>_none (func))
6+
(import \"_grainEnv\" \"mem\" (memory $0 0))
7+
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
8+
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
9+
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
10+
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
11+
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
12+
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
13+
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1177 (mut i32)))
14+
(import \"GRAIN$MODULE$exposedType\" \"GRAIN$EXPORT$apply\" (global $apply_1175 (mut i32)))
15+
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
16+
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
17+
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
18+
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1177 (param i32 i32) (result i32)))
19+
(import \"GRAIN$MODULE$exposedType\" \"apply\" (func $apply_1175 (param i32 i32) (result i32)))
20+
(global $GRAIN$TABLE_SIZE i32 (i32.const 1))
21+
(elem $elem (global.get $relocBase_0) $lam_lambda_1176)
22+
(export \"memory\" (memory $0))
23+
(export \"_gmain\" (func $_gmain))
24+
(export \"_start\" (func $_start))
25+
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
26+
(func $lam_lambda_1176 (param $0 i32) (param $1 i32) (result i32)
27+
(local $2 i32)
28+
(local $3 i32)
29+
(local $4 i32)
30+
(local $5 i64)
31+
(local $6 f32)
32+
(local $7 f64)
33+
(local $8 i32)
34+
(return
35+
(block $cleanup_locals.5 (result i32)
36+
(local.set $2
37+
(block $compile_block.4 (result i32)
38+
(block $compile_store.3
39+
(local.set $8
40+
(tuple.extract 0
41+
(tuple.make
42+
(block $allocate_string.1 (result i32)
43+
(i32.store
44+
(local.tee $2
45+
(call $malloc_0
46+
(global.get $GRAIN$EXPORT$malloc_0)
47+
(i32.const 16)
48+
)
49+
)
50+
(i32.const 1)
51+
)
52+
(i32.store offset=4
53+
(local.get $2)
54+
(i32.const 2)
55+
)
56+
(i64.store offset=8
57+
(local.get $2)
58+
(i64.const 27503)
59+
)
60+
(local.get $2)
61+
)
62+
(call $decRef_0
63+
(global.get $GRAIN$EXPORT$decRef_0)
64+
(local.get $8)
65+
)
66+
)
67+
)
68+
)
69+
(block $do_backpatches.2
70+
)
71+
)
72+
(call $print_1177
73+
(call $incRef_0
74+
(global.get $GRAIN$EXPORT$incRef_0)
75+
(global.get $print_1177)
76+
)
77+
(call $incRef_0
78+
(global.get $GRAIN$EXPORT$incRef_0)
79+
(local.get $8)
80+
)
81+
)
82+
)
83+
)
84+
(drop
85+
(call $decRef_0
86+
(global.get $GRAIN$EXPORT$decRef_0)
87+
(local.get $0)
88+
)
89+
)
90+
(drop
91+
(call $decRef_0
92+
(global.get $GRAIN$EXPORT$decRef_0)
93+
(local.get $1)
94+
)
95+
)
96+
(drop
97+
(call $decRef_0
98+
(global.get $GRAIN$EXPORT$decRef_0)
99+
(local.get $8)
100+
)
101+
)
102+
(local.get $2)
103+
)
104+
)
105+
)
106+
(func $_gmain (result i32)
107+
(local $0 i32)
108+
(local $1 i32)
109+
(local $2 i32)
110+
(local $3 i64)
111+
(local $4 f32)
112+
(local $5 f64)
113+
(local $6 i32)
114+
(return
115+
(block $cleanup_locals.10 (result i32)
116+
(local.set $0
117+
(block $compile_block.9 (result i32)
118+
(block $compile_store.8
119+
(local.set $6
120+
(tuple.extract 0
121+
(tuple.make
122+
(block $allocate_closure.6 (result i32)
123+
(i32.store
124+
(local.tee $0
125+
(call $malloc_0
126+
(global.get $GRAIN$EXPORT$malloc_0)
127+
(i32.const 16)
128+
)
129+
)
130+
(i32.const 6)
131+
)
132+
(i32.store offset=4
133+
(local.get $0)
134+
(i32.const 2)
135+
)
136+
(i32.store offset=8
137+
(local.get $0)
138+
(i32.add
139+
(global.get $relocBase_0)
140+
(i32.const 0)
141+
)
142+
)
143+
(i32.store offset=12
144+
(local.get $0)
145+
(i32.const 0)
146+
)
147+
(local.get $0)
148+
)
149+
(call $decRef_0
150+
(global.get $GRAIN$EXPORT$decRef_0)
151+
(local.get $6)
152+
)
153+
)
154+
)
155+
)
156+
(block $do_backpatches.7
157+
(local.set $0
158+
(local.get $6)
159+
)
160+
)
161+
)
162+
(call $apply_1175
163+
(call $incRef_0
164+
(global.get $GRAIN$EXPORT$incRef_0)
165+
(global.get $apply_1175)
166+
)
167+
(call $incRef_0
168+
(global.get $GRAIN$EXPORT$incRef_0)
169+
(local.get $6)
170+
)
171+
)
172+
)
173+
)
174+
(drop
175+
(call $decRef_0
176+
(global.get $GRAIN$EXPORT$decRef_0)
177+
(local.get $6)
178+
)
179+
)
180+
(local.get $0)
181+
)
182+
)
183+
)
184+
(func $_start
185+
(drop
186+
(call $_gmain)
187+
)
188+
)
189+
;; custom section \"cmi\", size 291
190+
)

compiler/test/suites/exports.re

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,17 @@ describe("exports", ({test}) => {
7171
"Unbound value secret",
7272
);
7373
assertCompileError(
74-
"export10",
74+
"export11",
7575
"enum Foo { Bar }; export Bar",
7676
"Unbound type constructor",
7777
);
78+
assertSnapshot(
79+
"export12",
80+
{|
81+
import ExposedType from "exposedType"
82+
ExposedType.apply((arg) => print("ok"))
83+
|},
84+
);
7885

7986
assertSnapshot("let_rec_export", "export let rec foo = () => 5");
8087
assertHasExport(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Map from "map"
2+
3+
export let apply = (f: Map.Map<String, String> -> Void) => {
4+
let map = Map.make()
5+
Map.set("foo", "bar", map)
6+
f(map)
7+
}

0 commit comments

Comments
 (0)