|
| 1 | +diff --git a/bin/main.ml b/bin/main.ml |
| 2 | +index 8230ab2..b772a3c 100644 |
| 3 | +--- a/bin/main.ml |
| 4 | ++++ b/bin/main.ml |
| 5 | +@@ -195,6 +195,8 @@ let check_file face json path = |
| 6 | + resolve_refs := List.rev resolve_ctx.references; |
| 7 | + (match Affinescript.Typecheck.check_program |
| 8 | + ~import_types:type_ctx.Affinescript.Typecheck.name_types |
| 9 | ++ ~import_type_env:type_ctx.Affinescript.Typecheck.type_env |
| 10 | ++ ~import_constructor_env:type_ctx.Affinescript.Typecheck.constructor_env |
| 11 | + resolve_ctx.symbols prog with |
| 12 | + | Error e -> |
| 13 | + add (Affinescript.Json_output.of_type_error e) |
| 14 | +@@ -242,6 +244,8 @@ let check_file face json path = |
| 15 | + | Ok (resolve_ctx, type_ctx) -> |
| 16 | + (match Affinescript.Typecheck.check_program |
| 17 | + ~import_types:type_ctx.Affinescript.Typecheck.name_types |
| 18 | ++ ~import_type_env:type_ctx.Affinescript.Typecheck.type_env |
| 19 | ++ ~import_constructor_env:type_ctx.Affinescript.Typecheck.constructor_env |
| 20 | + resolve_ctx.symbols prog with |
| 21 | + | Error e -> |
| 22 | + Format.eprintf "@[<v>%s@]@." |
| 23 | +@@ -505,6 +509,8 @@ let compile_file face json wasm_gc vscode_ext vscode_adapter vscode_no_lc |
| 24 | + | Ok (resolve_ctx, import_type_ctx) -> |
| 25 | + (match Affinescript.Typecheck.check_program |
| 26 | + ~import_types:import_type_ctx.Affinescript.Typecheck.name_types |
| 27 | ++ ~import_type_env:import_type_ctx.Affinescript.Typecheck.type_env |
| 28 | ++ ~import_constructor_env:import_type_ctx.Affinescript.Typecheck.constructor_env |
| 29 | + resolve_ctx.symbols prog with |
| 30 | + | Error e -> |
| 31 | + add (Affinescript.Json_output.of_type_error e) |
| 32 | +@@ -732,6 +738,8 @@ let compile_file face json wasm_gc vscode_ext vscode_adapter vscode_no_lc |
| 33 | + | Ok (resolve_ctx, import_type_ctx) -> |
| 34 | + (match Affinescript.Typecheck.check_program |
| 35 | + ~import_types:import_type_ctx.Affinescript.Typecheck.name_types |
| 36 | ++ ~import_type_env:import_type_ctx.Affinescript.Typecheck.type_env |
| 37 | ++ ~import_constructor_env:import_type_ctx.Affinescript.Typecheck.constructor_env |
| 38 | + resolve_ctx.symbols prog with |
| 39 | + | Error e -> |
| 40 | + Format.eprintf "@[<v>%s@]@." |
| 41 | +diff --git a/lib/resolve.ml b/lib/resolve.ml |
| 42 | +index 65a6b48..65a9206 100644 |
| 43 | +--- a/lib/resolve.ml |
| 44 | ++++ b/lib/resolve.ml |
| 45 | +@@ -654,6 +654,20 @@ let import_specific_items |
| 46 | + Error (UndefinedVariable item.ii_name, item.ii_name.span) |
| 47 | + ) (Ok ()) items |
| 48 | + |
| 49 | ++(** Thread imported struct/alias/enum type definitions and value constructors |
| 50 | ++ from a resolved source module into the destination type-check context, so |
| 51 | ++ that field access on an imported struct resolves (companion to the scheme |
| 52 | ++ imports above — a struct needs its TRecord definition, not just a |
| 53 | ++ name_types scheme). *) |
| 54 | ++let import_type_defs |
| 55 | ++ (dest : Typecheck.context) (source : Typecheck.context) : unit = |
| 56 | ++ Hashtbl.iter (fun name ty -> |
| 57 | ++ Hashtbl.replace dest.Typecheck.type_env name ty |
| 58 | ++ ) source.Typecheck.type_env; |
| 59 | ++ Hashtbl.iter (fun name ty -> |
| 60 | ++ Hashtbl.replace dest.Typecheck.constructor_env name ty |
| 61 | ++ ) source.Typecheck.constructor_env |
| 62 | ++ |
| 63 | + (** Resolve imports in a program using module loader *) |
| 64 | + let rec resolve_and_typecheck_module |
| 65 | + (loader : Module_loader.t) |
| 66 | +@@ -698,7 +712,10 @@ let rec resolve_and_typecheck_module |
| 67 | + imported modules must check the same way top-level programs do.) *) |
| 68 | + match |
| 69 | + Typecheck.check_program |
| 70 | +- ~import_types:type_ctx.Typecheck.name_types symbols prog |
| 71 | ++ ~import_types:type_ctx.Typecheck.name_types |
| 72 | ++ ~import_type_env:type_ctx.Typecheck.type_env |
| 73 | ++ ~import_constructor_env:type_ctx.Typecheck.constructor_env |
| 74 | ++ symbols prog |
| 75 | + with |
| 76 | + | Ok final_ctx -> Ok (symbols, final_ctx) |
| 77 | + | Error type_err -> |
| 78 | +@@ -729,6 +746,7 @@ and resolve_imports_with_loader |
| 79 | + mod_type_ctx.Typecheck.var_types |
| 80 | + mod_type_ctx.Typecheck.name_types |
| 81 | + alias_str; |
| 82 | ++ import_type_defs type_ctx mod_type_ctx; |
| 83 | + Ok () |
| 84 | + | Error e -> Error e |
| 85 | + end |
| 86 | +@@ -747,13 +765,15 @@ and resolve_imports_with_loader |
| 87 | + (* Resolve and type-check the module *) |
| 88 | + begin match resolve_and_typecheck_module loader loaded_mod with |
| 89 | + | Ok (mod_symbols, mod_type_ctx) -> |
| 90 | +- import_specific_items ctx.symbols |
| 91 | ++ let* () = import_specific_items ctx.symbols |
| 92 | + type_ctx.Typecheck.var_types |
| 93 | + type_ctx.Typecheck.name_types |
| 94 | + mod_symbols |
| 95 | + mod_type_ctx.Typecheck.var_types |
| 96 | + mod_type_ctx.Typecheck.name_types |
| 97 | +- items |
| 98 | ++ items in |
| 99 | ++ import_type_defs type_ctx mod_type_ctx; |
| 100 | ++ Ok () |
| 101 | + | Error e -> Error e |
| 102 | + end |
| 103 | + | Error (Module_loader.ModuleNotFound _) -> |
| 104 | +@@ -785,6 +805,7 @@ and resolve_imports_with_loader |
| 105 | + sym) |
| 106 | + | _ -> () |
| 107 | + ) mod_symbols.all_symbols; |
| 108 | ++ import_type_defs type_ctx mod_type_ctx; |
| 109 | + Ok () |
| 110 | + | Error e -> Error e |
| 111 | + end |
| 112 | +diff --git a/lib/typecheck.ml b/lib/typecheck.ml |
| 113 | +index e38e302..4390c71 100644 |
| 114 | +--- a/lib/typecheck.ml |
| 115 | ++++ b/lib/typecheck.ml |
| 116 | +@@ -2401,6 +2401,8 @@ let populate_call_effects (ctx : context) (prog : Ast.program) : unit = |
| 117 | + Effect_sites.set_async_by_ord async_tbl |
| 118 | + |
| 119 | + let check_program ?(import_types : (string, scheme) Hashtbl.t option) |
| 120 | ++ ?(import_type_env : (string, ty) Hashtbl.t option) |
| 121 | ++ ?(import_constructor_env : (string, ty) Hashtbl.t option) |
| 122 | + (symbols : Symbol.t) (prog : Ast.program) |
| 123 | + : (context, type_error) Result.t = |
| 124 | + try |
| 125 | +@@ -2423,6 +2425,17 @@ let check_program ?(import_types : (string, scheme) Hashtbl.t option) |
| 126 | + Option.iter (fun tbl -> |
| 127 | + Hashtbl.iter (fun name sc -> Hashtbl.replace ctx.name_types name sc) tbl |
| 128 | + ) import_types; |
| 129 | ++ (* Thread imported struct/alias/enum type definitions (type_env) and value |
| 130 | ++ constructors (constructor_env) so field access on an imported struct |
| 131 | ++ resolves: enums already cross via name_types schemes, but a struct's |
| 132 | ++ TRecord definition must be present for a named param type to expand to |
| 133 | ++ its fields (otherwise it stays an opaque TCon -> FieldNotFound). *) |
| 134 | ++ Option.iter (fun tbl -> |
| 135 | ++ Hashtbl.iter (fun name ty -> Hashtbl.replace ctx.type_env name ty) tbl |
| 136 | ++ ) import_type_env; |
| 137 | ++ Option.iter (fun tbl -> |
| 138 | ++ Hashtbl.iter (fun name ty -> Hashtbl.replace ctx.constructor_env name ty) tbl |
| 139 | ++ ) import_constructor_env; |
| 140 | + (* Forward pass: register all types, effects, traits, impls, and |
| 141 | + function signatures so that mutually recursive declarations resolve. *) |
| 142 | + let* () = List.fold_left (fun acc decl -> |
0 commit comments