Skip to content

Commit 5efb54c

Browse files
authored
feat(compiler)!: Require module prefix on use/provide for modules (#1868)
1 parent 973f3f3 commit 5efb54c

File tree

17 files changed

+87
-42
lines changed

17 files changed

+87
-42
lines changed

compiler/src/formatting/format.re

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3956,8 +3956,9 @@ and print_expression_inner =
39563956
};
39573957

39583958
switch (item) {
3959-
| PUseValue({name, alias})
3960-
| PUseModule({name, alias}) => item_name(name, alias)
3959+
| PUseValue({name, alias}) => item_name(name, alias)
3960+
| PUseModule({name, alias}) =>
3961+
Doc.concat([Doc.text("module "), item_name(name, alias)])
39613962
| PUseType({name, alias}) =>
39623963
Doc.concat([Doc.text("type "), item_name(name, alias)])
39633964
};
@@ -5106,8 +5107,9 @@ let rec toplevel_print =
51065107
};
51075108

51085109
switch (item) {
5109-
| PProvideValue({name, alias})
5110-
| PProvideModule({name, alias}) => item_name(name, alias)
5110+
| PProvideValue({name, alias}) => item_name(name, alias)
5111+
| PProvideModule({name, alias}) =>
5112+
Doc.concat([Doc.text("module "), item_name(name, alias)])
51115113
| PProvideType({name, alias}) =>
51125114
Doc.concat([Doc.text("type "), item_name(name, alias)])
51135115
};

compiler/src/parsing/parser.messages

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ program: MODULE UIDENT EOL INCLUDE STRING AS YIELD
6363
## The known suffix of the stack is as follows:
6464
## AS
6565
##
66+
program: MODULE UIDENT EOL PROVIDE LBRACE MODULE YIELD
67+
##
68+
## Ends in an error in state: 829.
69+
##
70+
## provide_item -> MODULE . aliasable(uid) [ RBRACE EOL COMMA ]
71+
##
72+
## The known suffix of the stack is as follows:
73+
## MODULE
74+
##
6675

6776
Expected an uppercase module identifier.
6877

@@ -269,22 +278,21 @@ program: MODULE UIDENT EOL FOREIGN WASM LIDENT COLON UIDENT AS YIELD
269278
## The known suffix of the stack is as follows:
270279
## AS
271280
##
272-
program: MODULE UIDENT EOL PROVIDE LBRACE UIDENT AS EOL YIELD
281+
program: MODULE UIDENT EOL PROVIDE LBRACE MODULE UIDENT AS EOL YIELD
273282
##
274-
## Ends in an error in state: 51.
283+
## Ends in an error in state: 52.
275284
##
276-
## as_prefix(uid) -> AS option(eols) . uid [ RBRACE EOL COMMA ]
285+
## as_prefix(uid) -> AS eols . uid [ RBRACE EOL COMMA ]
277286
##
278287
## The known suffix of the stack is as follows:
279-
## AS option(eols)
288+
## AS eols
280289
##
281290
## WARNING: This example involves spurious reductions.
282291
## This implies that, although the LR(1) items shown above provide an
283292
## accurate view of the past (what has been recognized so far), they
284293
## may provide an INCOMPLETE view of the future (what was expected next).
285294
## In state 3, spurious reduction of production nonempty_list(eol) -> EOL
286295
## In state 6, spurious reduction of production eols -> nonempty_list(eol)
287-
## In state 53, spurious reduction of production option(eols) -> eols
288296
##
289297
program: MODULE UIDENT EOL PROVIDE LBRACE LIDENT AS EOL YIELD
290298
##
@@ -424,7 +432,7 @@ program: MODULE UIDENT EOL FROM UIDENT USE LBRACE YIELD
424432
## In state 31, spurious reduction of production lbrace -> LBRACE
425433
##
426434

427-
Expected a lowercase identifier to use a value, an uppercase identifier to use a module, or the keyword `type` followed by an uppercase identifier to use a type.
435+
Expected a lowercase identifier to use a value, the keyword `module` followed by an uppercase identifier to use a module, or the keyword `type` followed by an uppercase identifier to use a type.
428436

429437
program: MODULE UIDENT EOL FROM UIDENT USE LBRACE TYPE YIELD
430438
##
@@ -435,10 +443,19 @@ program: MODULE UIDENT EOL FROM UIDENT USE LBRACE TYPE YIELD
435443
## The known suffix of the stack is as follows:
436444
## TYPE
437445
##
446+
program: MODULE UIDENT EOL FROM UIDENT USE LBRACE MODULE YIELD
447+
##
448+
## Ends in an error in state: 57.
449+
##
450+
## use_item -> MODULE . aliasable(uid) [ RBRACE EOL COMMA ]
451+
##
452+
## The known suffix of the stack is as follows:
453+
## MODULE
454+
##
438455

439456
Expected an uppercase type identifier.
440457

441-
program: MODULE UIDENT EOL PROVIDE LBRACE UIDENT YIELD
458+
program: MODULE UIDENT EOL PROVIDE LBRACE MODULE UIDENT YIELD
442459
##
443460
## Ends in an error in state: 49.
444461
##
@@ -450,11 +467,12 @@ program: MODULE UIDENT EOL PROVIDE LBRACE UIDENT YIELD
450467

451468
Expected the keyword `as` followed by a module alias, a comma followed by more items to use, or `}` to end the statement.
452469

453-
program: MODULE UIDENT EOL PROVIDE LBRACE UIDENT AS YIELD
470+
program: MODULE UIDENT EOL PROVIDE LBRACE MODULE UIDENT AS YIELD
454471
##
455472
## Ends in an error in state: 50.
456473
##
457474
## as_prefix(uid) -> AS . uid [ RBRACE EOL COMMA ]
475+
## as_prefix(uid) -> AS . eols uid [ RBRACE EOL COMMA ]
458476
##
459477
## The known suffix of the stack is as follows:
460478
## AS
@@ -508,7 +526,7 @@ program: MODULE UIDENT EOL PROVIDE LBRACE LIDENT COMMA YIELD
508526
## In state 63, spurious reduction of production comma -> COMMA
509527
##
510528

511-
Expected more items to provide or `}` to end the provide statement.
529+
Expected a lowercase identifier to provide a value, the keyword `module` followed by an uppercase identifier to provide a module, the keyword `type` followed by an uppercase identifier to provide a type, or `}` to end the provide statement.
512530

513531
program: MODULE UIDENT EOL PROVIDE LBRACE YIELD
514532
##
@@ -526,7 +544,7 @@ program: MODULE UIDENT EOL PROVIDE LBRACE YIELD
526544
## In state 31, spurious reduction of production lbrace -> LBRACE
527545
##
528546

529-
Expected a comma-separated list of items to provide.
547+
Expected a lowercase identifier to provide a value, the keyword `module` followed by an uppercase identifier to provide a module, or the keyword `type` followed by an uppercase identifier to provide a type.
530548

531549
program: MODULE UIDENT EOL PROVIDE LBRACE TYPE YIELD
532550
##
@@ -594,7 +612,7 @@ program: MODULE UIDENT EOL FROM UIDENT USE LBRACE LIDENT COMMA YIELD
594612
## In state 63, spurious reduction of production comma -> COMMA
595613
##
596614

597-
Expected more items to use or `}` to end the statement.
615+
Expected a lowercase identifier to use a value, the keyword `module` followed by an uppercase identifier to use a module, the keyword `type` followed by an uppercase identifier to use a type, or `}` to end the use statement.
598616

599617
program: MODULE UIDENT EOL ASSERT WHEN
600618
##

compiler/src/parsing/parser.mly

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ aliasable(X):
342342

343343
use_item:
344344
| TYPE aliasable(uid) { PUseType { name=fst $2; alias = snd $2; loc=to_loc $loc} }
345-
| aliasable(uid) { PUseModule { name=fst $1; alias = snd $1; loc=to_loc $loc} }
345+
| MODULE aliasable(uid) { PUseModule { name=fst $2; alias = snd $2; loc=to_loc $loc} }
346346
| aliasable(lid) { PUseValue { name=fst $1; alias = snd $1; loc=to_loc $loc} }
347347

348348
use_items:
@@ -371,7 +371,7 @@ data_declaration_stmts:
371371

372372
provide_item:
373373
| TYPE aliasable(uid) { PProvideType { name=fst $2; alias = snd $2; loc=to_loc $loc} }
374-
| aliasable(uid) { PProvideModule { name=fst $1; alias = snd $1; loc=to_loc $loc} }
374+
| MODULE aliasable(uid) { PProvideModule { name=fst $2; alias = snd $2; loc=to_loc $loc} }
375375
| aliasable(lid) { PProvideValue { name=fst $1; alias = snd $1; loc=to_loc $loc} }
376376

377377
provide_items:

compiler/test/grainfmt/includes.expected.gr

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ include "option" as Opt
99
include "array"
1010
include "array" as Foo
1111
from List use { length, map, forEach as each }
12-
from Opt use { MutableOpt, ImmutableOpt as Imm, type Opt, type Opt as OptAlias }
12+
from Opt use {
13+
module MutableOpt,
14+
module ImmutableOpt as Imm,
15+
type Opt,
16+
type Opt as OptAlias,
17+
}
1318
from Opt use { /* comment1 */ /* comment2 */ /* comment3 */ /* comment4 */ /* comment5 */ /* comment6 */ /* comment7 */
14-
MutableOpt,
15-
ImmutableOpt as Imm,
19+
module MutableOpt,
20+
module ImmutableOpt as Imm,
1621
type Opt,
1722
type Opt as OptAlias,
1823
}

compiler/test/grainfmt/includes.input.gr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ include "option" as
1010
include /* special include */ "array"
1111
include "array" as /* special include */ Foo
1212
from List use { length, map, forEach as each }
13-
from Opt use { MutableOpt, ImmutableOpt as Imm, type Opt, type Opt as OptAlias }
14-
from Opt use { MutableOpt, /* comment1 */ ImmutableOpt /* comment2 */ as /* comment3 */ Imm /* comment4 */, /* comment5 */ type /* comment6 */ Opt, type Opt as /* comment7 */ OptAlias }
13+
from Opt use { module MutableOpt, module ImmutableOpt as Imm, type Opt, type Opt as OptAlias }
14+
from Opt use { module MutableOpt, /* comment1 */ module ImmutableOpt /* comment2 */ as /* comment3 */ Imm /* comment4 */, /* comment5 */ type /* comment6 */ Opt, type Opt as /* comment7 */ OptAlias }
1515

1616
include "runtime/unsafe/wasmi32"
1717
from WasmI32 use {

compiler/test/input/modules/provideIncludedModule.gr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module ProvideIncludedModule
33
include "option" as Option
44
include "array" as Array
55

6-
provide { Option as Smoption }
6+
provide { module Option as Smoption }
77

88
module Qux {
99
provide let val = 9
1010
provide module Quux {
11-
provide { Array }
11+
provide { module Array }
1212
}
1313
}
1414

15-
provide { Qux }
15+
provide { module Qux }

compiler/test/input/nestedModules.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Foo {
77
provide let foo = "hello from foo"
88
provide module Bar {
99
provide let bar = "hello from bar"
10-
provide { List }
10+
provide { module List }
1111
}
1212
}
1313

compiler/test/stdlib/array.test.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ assert Array.chunk(3, [> 1, 2, 3, 4, 5, 6, 7, 8]) ==
437437
assert Array.chunk(10, [> 1, 2, 3, 4, 5]) == [> [> 1, 2, 3, 4, 5]]
438438

439439
module Immutable {
440-
from Array use { Immutable as Array }
440+
from Array use { module Immutable as Array }
441441

442442
let fromList = Array.fromList
443443
let arr = fromList([1, 2, 3])

compiler/test/stdlib/map.test.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ Map.update(
413413
assert Map.contains("c", toUpdate) == false
414414

415415
module Immutable {
416-
from Map use { Immutable as Map }
416+
from Map use { module Immutable as Map }
417417

418418
let strKeys = Map.fromList([("🌾", 1), ("🐑", 2), ("🧱", 3)])
419419
let numKeys = Map.fromList([(1, "🌾"), (2, "🐑"), (3, "🧱")])

compiler/test/stdlib/priorityqueue.test.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ assert PriorityQueue.fromList(Array.toList(lotsOfVals), compare) ==
8585
PriorityQueue.fromArray(lotsOfVals, compare)
8686

8787
module Immutable {
88-
from PriorityQueue use { Immutable as PriorityQueue }
88+
from PriorityQueue use { module Immutable as PriorityQueue }
8989

9090
// formatter-ignore
9191
let lotsOfVals = [>

0 commit comments

Comments
 (0)