Skip to content

Commit 39aa39d

Browse files
committed
Remove specialized enter_attribute function
1 parent 03b45e7 commit 39aa39d

File tree

7 files changed

+56
-118
lines changed

7 files changed

+56
-118
lines changed

compiler/src/formatting/fmt.re

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,12 +3928,6 @@ let print_comment_range =
39283928
};
39293929

39303930
let print_program = (fmt, parsed_program) => {
3931-
let first_loc =
3932-
switch (parsed_program.attributes) {
3933-
| [fst, ..._] => fst.attr_loc
3934-
| _ => parsed_program.prog_core_loc
3935-
};
3936-
39373931
let toplevel =
39383932
switch (parsed_program.statements) {
39393933
| [] =>
@@ -4000,11 +3994,11 @@ let print_program = (fmt, parsed_program) => {
40003994
group @@
40013995
concat_map(
40023996
~lead=
4003-
_ =>
3997+
first =>
40043998
fmt.print_comment_range(
40053999
fmt,
40064000
enclosing_start_location(parsed_program.prog_loc),
4007-
first_loc,
4001+
first.attr_loc,
40084002
),
40094003
~sep=
40104004
(prev, next) =>

compiler/src/parsing/parsetree.re

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,6 @@ type attribute = Asttypes.attribute;
501501
[@deriving (sexp, yojson)]
502502
type attributes = Asttypes.attributes;
503503

504-
type attribute_context =
505-
| ModuleAttribute
506-
| ToplevelAttribute
507-
| ExpressionAttribute;
508-
509504
/** Type for expressions (i.e. things which evaluate to something) */
510505

511506
[@deriving (sexp, yojson)]

compiler/src/parsing/parsetree_iter.re

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ open Parsetree;
33
type hooks = {
44
enter_location: Location.t => unit,
55
leave_location: Location.t => unit,
6-
enter_attribute: (attribute, attribute_context) => unit,
7-
leave_attribute: (attribute, attribute_context) => unit,
86
enter_parsed_program: parsed_program => unit,
97
leave_parsed_program: parsed_program => unit,
108
enter_include: include_declaration => unit,
@@ -73,21 +71,19 @@ let iter_ident = (hooks, id) => {
7371
};
7472

7573
let iter_attribute =
76-
(hooks, attr_context, {Asttypes.attr_name, attr_args, attr_loc} as attr) => {
77-
hooks.enter_attribute(attr, attr_context);
74+
(hooks, {Asttypes.attr_name, attr_args, attr_loc} as attr) => {
7875
iter_loc(hooks, attr_name);
7976
List.iter(iter_loc(hooks), attr_args);
8077
iter_location(hooks, attr_loc);
81-
hooks.leave_attribute(attr, attr_context);
8278
};
8379

84-
let iter_attributes = (hooks, attr_context, attrs) => {
85-
List.iter(iter_attribute(hooks, attr_context), attrs);
80+
let iter_attributes = (hooks, attrs) => {
81+
List.iter(iter_attribute(hooks), attrs);
8682
};
8783

8884
let rec iter_parsed_program = (hooks, {statements} as program) => {
8985
hooks.enter_parsed_program(program);
90-
iter_attributes(hooks, ModuleAttribute, program.attributes);
86+
iter_attributes(hooks, program.attributes);
9187
iter_toplevel_stmts(hooks, statements);
9288
hooks.leave_parsed_program(program);
9389
}
@@ -109,7 +105,7 @@ and iter_toplevel_stmt =
109105
hooks.enter_toplevel_stmt(top);
110106
iter_location(hooks, loc);
111107
iter_location(hooks, core_loc);
112-
iter_attributes(hooks, ToplevelAttribute, attrs);
108+
iter_attributes(hooks, attrs);
113109
switch (desc) {
114110
| PTopInclude(id) => iter_include(hooks, id)
115111
| PTopProvide(ex) => iter_provide(hooks, ex)
@@ -255,7 +251,7 @@ and iter_expression =
255251
hooks.enter_expression(expr);
256252
iter_location(hooks, loc);
257253
iter_location(hooks, core_loc);
258-
iter_attributes(hooks, ExpressionAttribute, attrs);
254+
iter_attributes(hooks, attrs);
259255
switch (desc) {
260256
| PExpId(i) => iter_ident(hooks, i)
261257
| PExpConstant(c) => iter_constant(hooks, c)
@@ -525,9 +521,6 @@ let default_hooks = {
525521
enter_location: _ => (),
526522
leave_location: _ => (),
527523

528-
enter_attribute: (_, _) => (),
529-
leave_attribute: (_, _) => (),
530-
531524
enter_parsed_program: _ => (),
532525
leave_parsed_program: _ => (),
533526

compiler/src/parsing/parsetree_iter.rei

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ open Parsetree;
33
type hooks = {
44
enter_location: Location.t => unit,
55
leave_location: Location.t => unit,
6-
enter_attribute: (attribute, attribute_context) => unit,
7-
leave_attribute: (attribute, attribute_context) => unit,
86
enter_parsed_program: parsed_program => unit,
97
leave_parsed_program: parsed_program => unit,
108
enter_include: include_declaration => unit,

compiler/src/parsing/well_formedness.re

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -300,53 +300,28 @@ type known_attribute = {
300300
arity: int,
301301
};
302302

303-
let valid_attributes = (errs, super) => {
304-
let enter_attribute =
305-
(
306-
{Asttypes.attr_name: {txt, loc}, attr_args: args} as attr,
307-
attr_context,
308-
) => {
309-
let known_attributes =
310-
switch (attr_context) {
311-
| ModuleAttribute => [
312-
{name: "runtimeMode", arity: 0},
313-
{name: "noPervasives", arity: 0},
314-
]
315-
| ToplevelAttribute
316-
| ExpressionAttribute => [
317-
{name: "disableGC", arity: 0},
318-
{name: "unsafe", arity: 0},
319-
{name: "externalName", arity: 1},
320-
]
321-
};
322-
323-
switch (List.find_opt(({name}) => name == txt, known_attributes)) {
324-
| Some({arity}) when List.length(args) != arity =>
325-
errs := [InvalidAttributeArity(txt, arity, loc), ...errs^]
326-
| None =>
327-
let context_string =
328-
switch (attr_context) {
329-
| ModuleAttribute => "module"
330-
| ToplevelAttribute => "top-level"
331-
| ExpressionAttribute => "expression"
332-
};
333-
errs := [UnknownAttribute(context_string, txt, loc), ...errs^];
334-
| _ => ()
335-
};
336-
super.enter_attribute(attr, attr_context);
303+
let disallowed_attributes = (errs, super) => {
304+
let validate_against_known = (attrs, known_attributes, context) => {
305+
List.iter(
306+
({Asttypes.attr_name: {txt, loc}, attr_args: args}) => {
307+
switch (List.find_opt(({name}) => name == txt, known_attributes)) {
308+
| Some({arity}) when List.length(args) != arity =>
309+
errs := [InvalidAttributeArity(txt, arity, loc), ...errs^]
310+
| None => errs := [UnknownAttribute(context, txt, loc), ...errs^]
311+
| _ => ()
312+
}
313+
},
314+
attrs,
315+
);
337316
};
338317

339-
{
340-
errs,
341-
iter_hooks: {
342-
...super,
343-
enter_attribute,
344-
},
345-
};
346-
};
318+
let known_expr_attributes = [
319+
{name: "disableGC", arity: 0},
320+
{name: "unsafe", arity: 0},
321+
{name: "externalName", arity: 1},
322+
];
347323

348-
let disallowed_attributes = (errs, super) => {
349-
let enter_expression = ({pexp_desc: desc, pexp_attributes: attrs} as e) => {
324+
let enter_expression = ({pexp_attributes: attrs} as e) => {
350325
switch (
351326
List.find_opt(
352327
({Asttypes.attr_name: {txt}}) => txt == "externalName",
@@ -363,8 +338,10 @@ let disallowed_attributes = (errs, super) => {
363338
]
364339
| None => ()
365340
};
341+
validate_against_known(attrs, known_expr_attributes, "expression");
366342
super.enter_expression(e);
367343
};
344+
368345
let enter_toplevel_stmt =
369346
({ptop_desc: desc, ptop_attributes: attrs} as top) => {
370347
switch (
@@ -417,15 +394,26 @@ let disallowed_attributes = (errs, super) => {
417394
}
418395
| None => ()
419396
};
397+
validate_against_known(attrs, known_expr_attributes, "top-level");
420398
super.enter_toplevel_stmt(top);
421399
};
422400

401+
let enter_parsed_program = ({attributes} as prog) => {
402+
let known_module_attributes = [
403+
{name: "runtimeMode", arity: 0},
404+
{name: "noPervasives", arity: 0},
405+
];
406+
validate_against_known(attributes, known_module_attributes, "module");
407+
super.enter_parsed_program(prog);
408+
};
409+
423410
{
424411
errs,
425412
iter_hooks: {
426413
...super,
427414
enter_expression,
428415
enter_toplevel_stmt,
416+
enter_parsed_program,
429417
},
430418
};
431419
};
@@ -860,7 +848,6 @@ let well_formedness_checks = [
860848
only_functions_oh_rhs_letrec,
861849
no_letrec_mut,
862850
no_zero_denominator_rational,
863-
valid_attributes,
864851
disallowed_attributes,
865852
no_loop_control_statement_outside_of_loop,
866853
malformed_return_statements,

compiler/test/runner.re

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -216,31 +216,26 @@ let doc = (file, arguments) => {
216216
let module_header = "module Test; ";
217217

218218
let makeSnapshotRunner =
219-
(~config_fn=?, test, ~default_module_header=true, name, prog) => {
219+
(~config_fn=?, test, ~module_header=module_header, name, prog) => {
220220
test(name, ({expect}) => {
221221
Config.preserve_all_configs(() => {
222222
ignore @@
223223
compile(
224224
~hook=stop_after_object_file_emitted,
225225
~config_fn?,
226226
name,
227-
(if (default_module_header) {module_header} else {""}) ++ prog,
227+
module_header ++ prog,
228228
);
229229
expect.file(watfile(name)).toMatchSnapshot();
230230
})
231231
});
232232
};
233233

234234
let makeFilesizeRunner =
235-
(test, ~config_fn=?, ~default_module_header=true, name, prog, size) => {
235+
(test, ~config_fn=?, ~module_header=module_header, name, prog, size) => {
236236
test(name, ({expect}) => {
237237
Config.preserve_all_configs(() => {
238-
ignore @@
239-
compile(
240-
~config_fn?,
241-
name,
242-
(if (default_module_header) {module_header} else {""}) ++ prog,
243-
);
238+
ignore @@ compile(~config_fn?, name, module_header ++ prog);
244239
let ic = open_in_bin(wasmfile(name));
245240
let filesize = in_channel_length(ic);
246241
close_in(ic);
@@ -268,18 +263,14 @@ let makeSnapshotFileRunner = (test, ~config_fn=?, name, filename) => {
268263
};
269264

270265
let makeCompileErrorRunner =
271-
(test, ~default_module_header=true, name, prog, msg) => {
266+
(test, ~module_header=module_header, name, prog, msg) => {
272267
test(
273268
name,
274269
({expect}) => {
275270
let error =
276271
try(
277272
{
278-
ignore @@
279-
compile(
280-
name,
281-
(if (default_module_header) {module_header} else {""}) ++ prog,
282-
);
273+
ignore @@ compile(name, module_header ++ prog);
283274
"";
284275
}
285276
) {
@@ -291,29 +282,21 @@ let makeCompileErrorRunner =
291282
};
292283

293284
let makeWarningRunner =
294-
(test, ~default_module_header=true, name, prog, warning) => {
285+
(test, ~module_header=module_header, name, prog, warning) => {
295286
test(name, ({expect}) => {
296287
Config.preserve_all_configs(() => {
297288
Config.print_warnings := false;
298-
ignore @@
299-
compile(
300-
name,
301-
(if (default_module_header) {module_header} else {""}) ++ prog,
302-
);
289+
ignore @@ compile(name, module_header ++ prog);
303290
expect.ext.warning.toHaveTriggered(warning);
304291
})
305292
});
306293
};
307294

308-
let makeNoWarningRunner = (test, ~default_module_header=true, name, prog) => {
295+
let makeNoWarningRunner = (test, ~module_header=module_header, name, prog) => {
309296
test(name, ({expect}) => {
310297
Config.preserve_all_configs(() => {
311298
Config.print_warnings := false;
312-
ignore @@
313-
compile(
314-
name,
315-
(if (default_module_header) {module_header} else {""}) ++ prog,
316-
);
299+
ignore @@ compile(name, module_header ++ prog);
317300
expect.ext.warning.toHaveTriggeredNoWarnings();
318301
})
319302
});
@@ -325,20 +308,14 @@ let makeRunner =
325308
~num_pages=?,
326309
~config_fn=?,
327310
~extra_args=?,
328-
~default_module_header=true,
311+
~module_header=module_header,
329312
name,
330313
prog,
331314
expected,
332315
) => {
333316
test(name, ({expect}) => {
334317
Config.preserve_all_configs(() => {
335-
ignore @@
336-
compile(
337-
~num_pages?,
338-
~config_fn?,
339-
name,
340-
(if (default_module_header) {module_header} else {""}) ++ prog,
341-
);
318+
ignore @@ compile(~num_pages?, ~config_fn?, name, module_header ++ prog);
342319
let (result, _) = run(~num_pages?, ~extra_args?, wasmfile(name));
343320
expect.string(result).toEqual(expected);
344321
})
@@ -351,20 +328,14 @@ let makeErrorRunner =
351328
~check_exists=true,
352329
~num_pages=?,
353330
~config_fn=?,
354-
~default_module_header=true,
331+
~module_header=module_header,
355332
name,
356333
prog,
357334
expected,
358335
) => {
359336
test(name, ({expect}) => {
360337
Config.preserve_all_configs(() => {
361-
ignore @@
362-
compile(
363-
~num_pages?,
364-
~config_fn?,
365-
name,
366-
(if (default_module_header) {module_header} else {""}) ++ prog,
367-
);
338+
ignore @@ compile(~num_pages?, ~config_fn?, name, module_header ++ prog);
368339
let (result, _) = run(~num_pages?, wasmfile(name));
369340
if (check_exists) {
370341
expect.string(result).toMatch(expected);

compiler/test/suites/functions.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ describe("functions", ({test, testSkip}) => {
155155
"Unknown top-level attribute",
156156
);
157157
assertCompileError(
158-
~default_module_header=false,
158+
~module_header="@unsafe module Test",
159159
"unknown_module_attribute",
160-
"@unsafe module Test",
160+
"",
161161
"Unknown module attribute",
162162
);
163163
assertSnapshot(

0 commit comments

Comments
 (0)