Skip to content

Commit 51b918a

Browse files
committed
fix rest of private type and analysis
1 parent 3410196 commit 51b918a

File tree

9 files changed

+74
-11
lines changed

9 files changed

+74
-11
lines changed

analysis/src/ProcessCmt.ml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,27 @@ let rec forStructureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
512512
| Tpat_tuple pats | Tpat_array pats | Tpat_construct (_, _, pats) ->
513513
pats |> List.iter (fun p -> handlePattern [] p)
514514
| Tpat_or (p, _, _) -> handlePattern [] p
515-
| Tpat_record (items, _, _rest) ->
516-
items |> List.iter (fun (_, _, p, _) -> handlePattern [] p)
515+
| Tpat_record (record_items, _, rest) -> (
516+
record_items |> List.iter (fun (_, _, p, _) -> handlePattern [] p);
517+
match rest with
518+
| None -> ()
519+
| Some rest ->
520+
let declared =
521+
addDeclared ~name:rest.rest_name
522+
~stamp:(Ident.binding_time rest.rest_ident)
523+
~env ~extent:rest.rest_name.loc ~item:rest.rest_type []
524+
(Exported.add exported Exported.Value)
525+
Stamps.addValue
526+
in
527+
items :=
528+
{
529+
Module.kind = Module.Value declared.item;
530+
name = declared.name.txt;
531+
docstring = declared.docstring;
532+
deprecated = declared.deprecated;
533+
loc = declared.extentLoc;
534+
}
535+
:: !items)
517536
| Tpat_variant (_, Some p, _) -> handlePattern [] p
518537
| Tpat_variant (_, None, _) | Tpat_any | Tpat_constant _ -> ()
519538
in

analysis/src/ProcessExtra.ml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,22 +377,32 @@ let pat ~(file : File.t) ~env ~extra (iter : Tast_iterator.iterator)
377377
| Tpackage (path, _, _) -> Some path
378378
| _ -> None
379379
in
380-
let addForPattern stamp name =
380+
let addForDeclaredPattern ~stamp ~name ~extent ~item ~attributes =
381381
if Stamps.findValue file.stamps stamp = None then (
382382
let declared =
383383
ProcessAttributes.newDeclared ~name ~stamp ~modulePath:NotVisible
384-
~extent:pattern.pat_loc ~item:pattern.pat_type false
385-
pattern.pat_attributes
384+
~extent ~item false attributes
386385
in
387386
Stamps.addValue file.stamps stamp declared;
388387
addReference ~extra stamp name.loc;
389388
addLocItem extra name.loc
390-
(Typed (name.txt, pattern.pat_type, Definition (stamp, Value))))
389+
(Typed (name.txt, item, Definition (stamp, Value))))
390+
in
391+
let addForPattern stamp name =
392+
addForDeclaredPattern ~stamp ~name ~extent:pattern.pat_loc
393+
~item:pattern.pat_type ~attributes:pattern.pat_attributes
391394
in
392395
(* Log.log("Entering pattern " ++ Utils.showLocation(pat_loc)); *)
393396
(match pattern.pat_desc with
394-
| Tpat_record (items, _, _rest) ->
395-
addForRecord ~env ~extra ~recordType:pattern.pat_type items
397+
| Tpat_record (items, _, rest) -> (
398+
addForRecord ~env ~extra ~recordType:pattern.pat_type items;
399+
match rest with
400+
| None -> ()
401+
| Some rest ->
402+
addForDeclaredPattern
403+
~stamp:(Ident.binding_time rest.rest_ident)
404+
~name:rest.rest_name ~extent:rest.rest_name.loc ~item:rest.rest_type
405+
~attributes:pattern.pat_attributes)
396406
| Tpat_construct (lident, constructor, _) ->
397407
addForConstructor ~env ~extra pattern.pat_type lident constructor
398408
| Tpat_alias (_inner, ident, name) -> (

compiler/ml/typecore.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,8 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
16201620
let rest_type_expr =
16211621
newgenty (Tconstr (rest_path, rest_type_args, ref Mnil))
16221622
in
1623+
if rest_decl.type_private = Private then
1624+
raise (Error (rest_type_lid.loc, !env, Private_type rest_type_expr));
16231625
List.iter2
16241626
(fun param arg -> unify_pat_types rest_type_lid.loc !env param arg)
16251627
rest_decl.type_params rest_type_args;
@@ -1753,6 +1755,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
17531755
Some
17541756
{
17551757
Typedtree.rest_ident;
1758+
rest_name;
17561759
rest_type = rest_type_expr;
17571760
rest_path;
17581761
rest_labels;

compiler/ml/typedtree.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type pattern = {
3737

3838
and record_pat_rest = {
3939
rest_ident: Ident.t;
40+
rest_name: string loc;
4041
rest_type: type_expr;
4142
rest_path: Path.t;
4243
rest_labels: Types.label_declaration list;
@@ -459,9 +460,7 @@ let rec bound_idents pat =
459460
| Tpat_record (_, _, Some rest) ->
460461
(* Rest ident is added via enter_variable during type checking,
461462
but we also need it in bound_idents for Lambda compilation *)
462-
idents :=
463-
(rest.rest_ident, Location.mknoloc (Ident.name rest.rest_ident))
464-
:: !idents;
463+
idents := (rest.rest_ident, rest.rest_name) :: !idents;
465464
iter_pattern_desc bound_idents pat.pat_desc
466465
| d -> iter_pattern_desc bound_idents d
467466

compiler/ml/typedtree.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type pattern = {
4545

4646
and record_pat_rest = {
4747
rest_ident: Ident.t;
48+
rest_name: string loc;
4849
rest_type: type_expr;
4950
rest_path: Path.t;
5051
rest_labels: Types.label_declaration list;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type config = {name: string, version: string}
2+
type subConfig = {version: string}
3+
4+
let getVersion = (config: config) =>
5+
switch config {
6+
| {name: _, ...subConfig as rest} =>
7+
rest.version
8+
// ^def
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Definition src/RecordRest.res 6:4
2+
{"uri": "RecordRest.res", "range": {"start": {"line": 5, "character": 30}, "end": {"line": 5, "character": 34}}}
3+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/record_rest_private_type.res:9:12-14
4+
5+
7 │ type source = {a: int, b: string}
6+
8 │
7+
9 │ let {a, ...M.t as rest} = ({a: 1, b: "x"}: source)
8+
10 │
9+
10+
Cannot create values of the private type M.t
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module M: {
2+
type t = private {b: string}
3+
} = {
4+
type t = {b: string}
5+
}
6+
7+
type source = {a: int, b: string}
8+
9+
let {a, ...M.t as rest} = ({a: 1, b: "x"}: source)

0 commit comments

Comments
 (0)