Skip to content

Commit 6155500

Browse files
Explicit type annotation for unit (rescript-lang#389)
* check if unit is typed * check for Lindent _ instead of just unit * check for type instead of just lident * remove debug code * added tests for () * added commits * removed test.res ( which got added by mistake)
1 parent 2b2dda7 commit 6155500

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

syntax/src/res_core.ml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,15 @@ let getClosingToken = function
158158
| Lbrace -> Rbrace
159159
| Lbracket -> Rbracket
160160
| List -> Rbrace
161+
| LessThan -> GreaterThan
161162
| _ -> assert false
162163

163164
let rec goToClosing closingToken state =
164165
match (state.Parser.token, closingToken) with
165-
| (Rparen, Token.Rparen) | (Rbrace, Rbrace) | (Rbracket, Rbracket) ->
166+
| (Rparen, Token.Rparen) | (Rbrace, Rbrace) | (Rbracket, Rbracket) | (GreaterThan, GreaterThan) ->
166167
Parser.next state;
167168
()
168-
| (Token.Lbracket | Lparen | Lbrace | List) as t, _ ->
169+
| (Token.Lbracket | Lparen | Lbrace | List | LessThan) as t, _ ->
169170
Parser.next state;
170171
goToClosing (getClosingToken t) state;
171172
goToClosing closingToken state
@@ -194,10 +195,32 @@ let isEs6ArrowExpression ~inTernary p =
194195
let prevEndPos = state.prevEndPos in
195196
Parser.next state;
196197
begin match state.token with
198+
(* arrived at `()` here *)
197199
| Rparen ->
198200
Parser.next state;
199201
begin match state.Parser.token with
200-
| Colon when not inTernary -> true
202+
(* arrived at `() :` here *)
203+
| Colon when not inTernary ->
204+
Parser.next state;
205+
begin match state.Parser.token with
206+
(* arrived at `() :typ` here *)
207+
| Lident _ ->
208+
Parser.next state;
209+
begin match state.Parser.token with
210+
(* arrived at `() :typ<` here *)
211+
| LessThan ->
212+
Parser.next state;
213+
goToClosing GreaterThan state;
214+
| _ -> ()
215+
end;
216+
begin match state.Parser.token with
217+
(* arrived at `() :typ =>` or `() :typ<'a,'b> =>` here *)
218+
| EqualGreater ->
219+
true
220+
| _ -> false
221+
end
222+
| _ -> true
223+
end
201224
| EqualGreater -> true
202225
| _ -> false
203226
end

syntax/tests/parsing/grammar/expressions/arrow.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,15 @@ let x = Constructore(_ => copyChecklistItemCB(), _ => copyChecklistItemCB())
9696
let y = #Constructore(_ => copyChecklistItemCB(), _ => copyChecklistItemCB())
9797

9898
let f = (list) => list + 1
99+
100+
let foo = ((): unit)
101+
102+
type u = unit
103+
let un = (():u)
104+
105+
type d<'a,'b> = ('a,'b)
106+
let c = (): d<'a,'b> => (1,2)
107+
108+
let fn = f => f;
109+
type f = int => unit;
110+
let a = fn(_ => (): f);

syntax/tests/parsing/grammar/expressions/expected/arrow.res.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,12 @@ let x =
7777
let y =
7878
`Constructore
7979
((fun _ -> copyChecklistItemCB ()), (fun _ -> copyChecklistItemCB ()))
80-
let f list = list + 1
80+
let f list = list + 1
81+
let foo = (() : unit)
82+
type nonrec u = unit
83+
let un = (() : u)
84+
type nonrec ('a, 'b) d = ('a * 'b)
85+
let c () = ((1, 2) : ('a, 'b) d)
86+
let fn f = f
87+
type nonrec f = int -> unit
88+
let a = fn (fun _ -> () : f)

0 commit comments

Comments
 (0)