Skip to content

Commit 37a64f9

Browse files
authored
feat(compiler): Provide a user friendly error on _start override (#2351)
1 parent 14db204 commit 37a64f9

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

compiler/grainc/grainc.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ let grainc = (single_file_mode, name, outfile) => {
9393
Option.value(~default=Compile.default_wasm_filename(name), outfile);
9494
let dependencies = Module_resolution.get_dependencies();
9595

96-
Link.link(~main_object, ~outfile, dependencies);
96+
error_wrapped(() => Link.link(~main_object, ~outfile, dependencies));
9797
};
9898
};
9999

compiler/src/codegen/comp_utils.re

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ open Mashtree;
22
open Binaryen;
33
open Grain_typed;
44
open Grain_utils;
5+
open Grain_parsing;
6+
7+
type error =
8+
| InvalidStartExport;
9+
exception Error(Location.t, error);
510

611
let grain_main = "_gmain";
712
let grain_start = "_start";
@@ -187,7 +192,7 @@ let write_universal_exports =
187192
List.iter(
188193
item => {
189194
switch (item) {
190-
| TSigValue(id, {val_repr: ReprFunction(args, rets, direct)}) =>
195+
| TSigValue(id, {val_repr: ReprFunction(args, rets, direct), val_loc}) =>
191196
let name = Ident.name(id);
192197
let internal_name = Hashtbl.find(export_map, name);
193198
let get_closure = () =>
@@ -298,7 +303,11 @@ let write_universal_exports =
298303
[||],
299304
function_body,
300305
);
301-
ignore @@ Export.add_function_export(wasm_mod, name, name);
306+
if (! Config.use_start_section^ && name == grain_start) {
307+
raise(Error(val_loc, InvalidStartExport));
308+
} else {
309+
ignore @@ Export.add_function_export(wasm_mod, name, name);
310+
};
302311
| TSigValue(_)
303312
| TSigType(_)
304313
| TSigTypeExt(_)
@@ -309,3 +318,21 @@ let write_universal_exports =
309318
cmi_sign,
310319
);
311320
};
321+
322+
open Format;
323+
324+
let report_error = ppf =>
325+
fun
326+
| InvalidStartExport =>
327+
fprintf(
328+
ppf,
329+
"The export `_start` is only allowed when compiling with `--use-start-section`.",
330+
);
331+
332+
let () =
333+
Location.register_error_of_exn(
334+
fun
335+
| Error(loc, err) =>
336+
Some(Location.error_of_printer(loc, report_error, err))
337+
| _ => None,
338+
);

compiler/test/runner.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,14 @@ let makeSnapshotFileRunner = (test, ~config_fn=?, name, filename) => {
349349
};
350350

351351
let makeCompileErrorRunner =
352-
(test, ~module_header=module_header, name, prog, msg) => {
352+
(test, ~module_header=module_header, ~link=false, name, prog, msg) => {
353353
test(
354354
name,
355355
({expect}) => {
356356
let error =
357357
try(
358358
{
359-
ignore @@ compile(name, module_header ++ prog);
359+
ignore @@ compile(~link, name, module_header ++ prog);
360360
"";
361361
}
362362
) {

compiler/test/suites/provides.re

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ describe("provides", ({test, testSkip}) => {
222222
[("_start", Binaryen.Export.external_function)],
223223
);
224224

225+
assertCompileError(
226+
~link=true,
227+
"provide_start_function_invalid",
228+
{|
229+
provide let _start = () => void
230+
|},
231+
"The export `_start` is only allowed when compiling with `--use-start-section`.",
232+
);
233+
225234
assertHasWasmExport(
226235
"issue_918_annotated_func_provide",
227236
"module Test; provide let foo: () => Number = () => 5",

0 commit comments

Comments
 (0)