Skip to content

Commit

Permalink
fix(compiler): Properly handle types not imported (grain-lang#1329)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer authored Jun 13, 2022
1 parent fb947b5 commit 75bbb93
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 2 deletions.
7 changes: 6 additions & 1 deletion compiler/src/typed/type_utils.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ open Types;

let rec get_allocation_type = (env, ty) => {
switch (ty.desc) {
| TTyConstr(path, _, _) => Env.find_type(path, env).type_allocation
| TTyConstr(path, _, _) =>
try(Env.find_type(path, env).type_allocation) {
// Types not in the environment come from other modules and are nested in
// types we do know about; we treat them as Managed Grain values.
| Not_found => Managed
}
| TTySubst(linked)
| TTyLink(linked) => get_allocation_type(env, linked)
| TTyVar(_)
Expand Down
190 changes: 190 additions & 0 deletions compiler/test/__snapshots__/exports.d19c8510.0.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
exports › export12
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_i32 (func (result i32)))
(type $none_=>_none (func))
(import \"_grainEnv\" \"mem\" (memory $0 0))
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
(import \"GRAIN$MODULE$pervasives\" \"GRAIN$EXPORT$print\" (global $print_1177 (mut i32)))
(import \"GRAIN$MODULE$exposedType\" \"GRAIN$EXPORT$apply\" (global $apply_1175 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$pervasives\" \"print\" (func $print_1177 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$exposedType\" \"apply\" (func $apply_1175 (param i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 1))
(elem $elem (global.get $relocBase_0) $lam_lambda_1176)
(export \"memory\" (memory $0))
(export \"_gmain\" (func $_gmain))
(export \"_start\" (func $_start))
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
(func $lam_lambda_1176 (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local $5 i64)
(local $6 f32)
(local $7 f64)
(local $8 i32)
(return
(block $cleanup_locals.5 (result i32)
(local.set $2
(block $compile_block.4 (result i32)
(block $compile_store.3
(local.set $8
(tuple.extract 0
(tuple.make
(block $allocate_string.1 (result i32)
(i32.store
(local.tee $2
(call $malloc_0
(global.get $GRAIN$EXPORT$malloc_0)
(i32.const 16)
)
)
(i32.const 1)
)
(i32.store offset=4
(local.get $2)
(i32.const 2)
)
(i64.store offset=8
(local.get $2)
(i64.const 27503)
)
(local.get $2)
)
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $8)
)
)
)
)
(block $do_backpatches.2
)
)
(call $print_1177
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $print_1177)
)
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(local.get $8)
)
)
)
)
(drop
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $0)
)
)
(drop
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $1)
)
)
(drop
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $8)
)
)
(local.get $2)
)
)
)
(func $_gmain (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i64)
(local $4 f32)
(local $5 f64)
(local $6 i32)
(return
(block $cleanup_locals.10 (result i32)
(local.set $0
(block $compile_block.9 (result i32)
(block $compile_store.8
(local.set $6
(tuple.extract 0
(tuple.make
(block $allocate_closure.6 (result i32)
(i32.store
(local.tee $0
(call $malloc_0
(global.get $GRAIN$EXPORT$malloc_0)
(i32.const 16)
)
)
(i32.const 6)
)
(i32.store offset=4
(local.get $0)
(i32.const 2)
)
(i32.store offset=8
(local.get $0)
(i32.add
(global.get $relocBase_0)
(i32.const 0)
)
)
(i32.store offset=12
(local.get $0)
(i32.const 0)
)
(local.get $0)
)
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $6)
)
)
)
)
(block $do_backpatches.7
(local.set $0
(local.get $6)
)
)
)
(call $apply_1175
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(global.get $apply_1175)
)
(call $incRef_0
(global.get $GRAIN$EXPORT$incRef_0)
(local.get $6)
)
)
)
)
(drop
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(local.get $6)
)
)
(local.get $0)
)
)
)
(func $_start
(drop
(call $_gmain)
)
)
;; custom section \"cmi\", size 291
)
9 changes: 8 additions & 1 deletion compiler/test/suites/exports.re
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ describe("exports", ({test}) => {
"Unbound value secret",
);
assertCompileError(
"export10",
"export11",
"enum Foo { Bar }; export Bar",
"Unbound type constructor",
);
assertSnapshot(
"export12",
{|
import ExposedType from "exposedType"
ExposedType.apply((arg) => print("ok"))
|},
);

assertSnapshot("let_rec_export", "export let rec foo = () => 5");
assertHasExport(
Expand Down
7 changes: 7 additions & 0 deletions compiler/test/test-libs/exposedType.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Map from "map"

export let apply = (f: Map.Map<String, String> -> Void) => {
let map = Map.make()
Map.set("foo", "bar", map)
f(map)
}

0 comments on commit 75bbb93

Please sign in to comment.