Skip to content

Commit 58cd224

Browse files
authored
feat(compiler)!: Explicit abstract types (#1680)
1 parent 181798d commit 58cd224

File tree

78 files changed

+523
-196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+523
-196
lines changed

compiler/src/formatting/format.re

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,6 +4013,7 @@ and print_value_bind =
40134013
switch (provide_flag) {
40144014
| NotProvided => Doc.nil
40154015
| Provided => Doc.text("provide ")
4016+
| Abstract => Doc.text("abstract ")
40164017
};
40174018
let recursive =
40184019
switch (rec_flag) {
@@ -4627,6 +4628,7 @@ let data_print =
46274628
switch ((expt: Asttypes.provide_flag)) {
46284629
| NotProvided => Doc.nil
46294630
| Provided => Doc.text("provide ")
4631+
| Abstract => Doc.text("abstract ")
46304632
},
46314633
print_data(~original_source, ~comments=data_comments, decl),
46324634
]);
@@ -4745,6 +4747,7 @@ let rec toplevel_print =
47454747
switch (provide_flag) {
47464748
| NotProvided => Doc.nil
47474749
| Provided => Doc.text("provide ")
4750+
| Abstract => Doc.text("abstract ")
47484751
};
47494752
Doc.concat([
47504753
provide,
@@ -4760,6 +4763,7 @@ let rec toplevel_print =
47604763
switch (provide_flag) {
47614764
| NotProvided => Doc.nil
47624765
| Provided => Doc.text("provide ")
4766+
| Abstract => Doc.text("abstract ")
47634767
};
47644768
Doc.concat([
47654769
provide,
@@ -4794,6 +4798,7 @@ let rec toplevel_print =
47944798
switch (provide_flag) {
47954799
| NotProvided => Doc.nil
47964800
| Provided => Doc.text("provide ")
4801+
| Abstract => Doc.text("abstract ")
47974802
};
47984803
let cstr = type_exception.ptyexn_constructor;
47994804

@@ -4956,6 +4961,7 @@ let rec toplevel_print =
49564961
switch (provide_flag) {
49574962
| NotProvided => Doc.nil
49584963
| Provided => Doc.text("provide ")
4964+
| Abstract => Doc.text("abstract ")
49594965
};
49604966

49614967
let start_after_brace = Doc.concat([Doc.hardLine, top_level_stmts]);

compiler/src/parsing/asttypes.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ and number_type =
6464
[@deriving (sexp, yojson)]
6565
type provide_flag =
6666
| NotProvided
67-
| Provided;
67+
| Provided
68+
| Abstract;
6869

6970
/** Marker for recursive/nonrecursive let bindings */
7071

compiler/src/parsing/lexer.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ let rec token = lexbuf => {
271271
| "include" => positioned(INCLUDE)
272272
| "use" => positioned(USE)
273273
| "provide" => positioned(PROVIDE)
274+
| "abstract" => positioned(ABSTRACT)
274275
| "except" => positioned(EXCEPT)
275276
| "from" => positioned(FROM)
276277
| "*" => positioned(STAR)

compiler/src/parsing/parser.messages

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,19 @@ program: MODULE UIDENT EOL PROVIDE WHILE
15911591
## attributes PROVIDE
15921592
##
15931593

1594-
Expected a let binding, data declaration, exception, or comma-separated list of identifiers surrounded by `{}`.
1594+
Expected a let binding, type declaration, exception, or comma-separated list of identifiers surrounded by `{}`.
1595+
1596+
program: MODULE UIDENT EOL ABSTRACT YIELD
1597+
##
1598+
## Ends in an error in state: 827.
1599+
##
1600+
## data_declaration_stmt -> ABSTRACT . data_declaration [ SEMI RBRACE EOL EOF COMMA ]
1601+
##
1602+
## The known suffix of the stack is as follows:
1603+
## ABSTRACT
1604+
##
1605+
1606+
Expected a type declaration.
15951607

15961608
program: MODULE UIDENT EOL FAIL WHEN
15971609
##

compiler/src/parsing/parser.mly

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module Grain_parsing = struct end
3636
%token <string> PREFIX_150
3737
%token <string> INFIX_ASSIGNMENT_10
3838

39-
%token ENUM RECORD TYPE MODULE INCLUDE USE PROVIDE FOREIGN WASM PRIMITIVE
39+
%token ENUM RECORD TYPE MODULE INCLUDE USE PROVIDE ABSTRACT FOREIGN WASM PRIMITIVE
4040
%token EXCEPT FROM STAR
4141
%token SLASH DASH PIPE
4242
%token EOL EOF
@@ -340,6 +340,7 @@ include_stmt:
340340
| INCLUDE file_path include_alias? { IncludeDeclaration.mk ~loc:(to_loc $loc) $2 $3 }
341341

342342
data_declaration_stmt:
343+
| ABSTRACT data_declaration { (Abstract, $2) }
343344
| PROVIDE data_declaration { (Provided, $2) }
344345
| data_declaration { (NotProvided, $1) }
345346

compiler/src/parsing/parsetree.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type loc('a) =
1414
loc: Location.t,
1515
};
1616

17-
type provide_flag = Asttypes.provide_flag = | NotProvided | Provided;
17+
type provide_flag =
18+
Asttypes.provide_flag = | NotProvided | Provided | Abstract;
1819
type rec_flag = Asttypes.rec_flag = | Nonrecursive | Recursive;
1920
type mut_flag = Asttypes.mut_flag = | Mutable | Immutable;
2021

compiler/src/typed/ctype.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ let nondep_instance = (env, level, id, ty) => {
21892189
list (nl2, tl2). raise Not_found if impossible */
21902190
let complete_type_list = (~allow_absent=false, env, nl1, lv2, mty2, nl2, tl2) => {
21912191
let id2 = Ident.create("Pkg");
2192-
let env' = Env.add_module(id2, mty2, None, env);
2192+
let env' = Env.add_module(id2, mty2, None, Location.dummy_loc, env);
21932193
let rec complete = (nl1, ntl2) =>
21942194
switch (nl1, ntl2) {
21952195
| ([], _) => ntl2

compiler/src/typed/env.re

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,7 @@ let strengthen =
628628
module_type,
629629
);
630630

631-
let md = (md_type, md_filepath) => {
632-
md_type,
633-
md_filepath,
634-
md_loc: Location.dummy_loc,
635-
};
631+
let md = (md_type, md_filepath, md_loc) => {md_type, md_filepath, md_loc};
636632

637633
let subst_modtype_maker = ((subst, md)) =>
638634
if (subst === Subst.identity) {
@@ -1072,7 +1068,11 @@ let find_module = (path, filename, env) =>
10721068
let filename = Option.value(~default=Ident.name(id), filename);
10731069
if (Ident.persistent(id) && !(filename == unit_source)) {
10741070
let ps = find_pers_struct(~loc=Location.dummy_loc, filename);
1075-
md(TModSignature(Lazy.force(ps.ps_sig)), Some(filename));
1071+
md(
1072+
TModSignature(Lazy.force(ps.ps_sig)),
1073+
Some(filename),
1074+
Location.dummy_loc,
1075+
);
10761076
} else {
10771077
raise(Not_found);
10781078
};
@@ -1926,8 +1926,8 @@ let add_module_declaration = (~arg=false, ~check, id, md, env) => {
19261926

19271927
and add_modtype = (id, info, env) => store_modtype(id, info, env);
19281928

1929-
let add_module = (~arg=?, id, mty, mf, env) =>
1930-
add_module_declaration(~check=false, ~arg?, id, md(mty, mf), env);
1929+
let add_module = (~arg=?, id, mty, mf, mloc, env) =>
1930+
add_module_declaration(~check=false, ~arg?, id, md(mty, mf, mloc), env);
19311931

19321932
let add_constructor = (id, desc, {constructors, _} as e) => {
19331933
...e,
@@ -1968,9 +1968,9 @@ and enter_module_declaration = (~arg=?, id, md, env) =>
19681968
(id, add_functor_arg ?arg id env) */
19691969
and enter_modtype = enter(store_modtype);
19701970

1971-
let enter_module = (~arg=?, s, mty, env) => {
1971+
let enter_module = (~arg=?, s, mty, mloc, env) => {
19721972
let id = Ident.create(s);
1973-
(id, enter_module_declaration(~arg?, id, md(mty, None), env));
1973+
(id, enter_module_declaration(~arg?, id, md(mty, None, mloc), env));
19741974
};
19751975

19761976
/* Insertion of all components of a signature */
@@ -2077,7 +2077,7 @@ let include_module = (mod_name, mod_: Parsetree.include_declaration, env0) => {
20772077
env0;
20782078
} else {
20792079
let mod_type = TModAlias(path);
2080-
env0 |> add_module(mod_ident, mod_type, filename);
2080+
env0 |> add_module(mod_ident, mod_type, filename, mod_.pinc_loc);
20812081
}
20822082
| _ =>
20832083
let {ps_sig} = find_pers_struct(~loc=mod_.pinc_loc, mod_.pinc_path.txt);
@@ -2089,7 +2089,7 @@ let include_module = (mod_name, mod_: Parsetree.include_declaration, env0) => {
20892089
{mtd_type: Some(mod_type), mtd_loc: mod_.pinc_loc},
20902090
env0,
20912091
)
2092-
|> add_module(mod_ident, mod_type, filename);
2092+
|> add_module(mod_ident, mod_type, filename, mod_.pinc_loc);
20932093
};
20942094
};
20952095

compiler/src/typed/env.rei

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ let add_constructor: (Ident.t, constructor_description, t) => t;
149149

150150
/** Adds a constructor with the given name and description. */
151151

152-
let add_module: (~arg: bool=?, Ident.t, module_type, option(string), t) => t;
152+
let add_module:
153+
(~arg: bool=?, Ident.t, module_type, option(string), Location.t, t) => t;
153154
let add_module_declaration:
154155
(~arg: bool=?, ~check: bool, Ident.t, module_declaration, t) => t;
155156
let add_modtype: (Ident.t, modtype_declaration, t) => t;

compiler/src/typed/mtype.re

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,11 @@ and remove_aliases_sig = (env, excl, sg) =>
567567

568568
[
569569
TSigModule(id, {...md, md_type: mty}, rs),
570-
...remove_aliases_sig(Env.add_module(id, mty, None, env), excl, rem),
570+
...remove_aliases_sig(
571+
Env.add_module(id, mty, None, md.md_loc, env),
572+
excl,
573+
rem,
574+
),
571575
];
572576
| [TSigModType(id, mtd), ...rem] => [
573577
TSigModType(id, mtd),

0 commit comments

Comments
 (0)