Skip to content

Commit ed425d8

Browse files
authored
Better error when defining a type inside of a function (#7843)
* better error when defining a type inside of a function * changelog * complete comment * make syntax test instead * update analysis tests
1 parent e3256ba commit ed425d8

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
#### :nail_care: Polish
3535

36+
- Improve error message for trying to define a type inside a function. https://github.com/rescript-lang/rescript/pull/7843
37+
3638
#### :house: Internal
3739

3840
# 12.0.0-beta.9

compiler/syntax/src/res_core.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ module ErrorMessages = struct
184184
^ "_`)\n If you need the field to be \"" ^ keyword_txt
185185
^ "\" at runtime, annotate the field: `@as(\"" ^ keyword_txt ^ "\") "
186186
^ keyword_txt ^ "_ : ...`"
187+
188+
let type_definition_in_function =
189+
"Type definitions are not allowed inside functions.\n"
190+
^ " Move this `type` declaration to the top level or into a module."
187191
end
188192

189193
module InExternal = struct
@@ -3479,6 +3483,16 @@ and parse_expr_block_item p =
34793483
in
34803484
let loc = mk_loc start_pos p.prev_end_pos in
34813485
Ast_helper.Exp.let_ ~loc rec_flag let_bindings next
3486+
| Typ ->
3487+
(* Parse to be able to give a good error message. *)
3488+
let type_start = start_pos in
3489+
Parser.begin_region p;
3490+
let _ = parse_type_definition_or_extension ~attrs p in
3491+
Parser.end_region p;
3492+
Parser.err ~start_pos:type_start ~end_pos:p.prev_end_pos p
3493+
(Diagnostics.message ErrorMessages.type_definition_in_function);
3494+
parse_newline_or_semicolon_expr_block p;
3495+
parse_expr_block p
34823496
| _ ->
34833497
let e1 =
34843498
let expr = parse_expr p in

tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,8 @@ Path fnTakingPolyVariant
12351235
}]
12361236

12371237
Complete src/CompletionExpressions.res 281:24
1238-
posCursor:[281:24] posNoWhite:[281:23] Found expr:[281:3->290:18]
1239-
Pexp_apply ...[281:3->281:22] (...[281:23->281:25], ...[290:0->290:16])
1238+
posCursor:[281:24] posNoWhite:[281:23] Found expr:[281:3->302:1]
1239+
Pexp_apply ...[281:3->281:22] (...[281:23->281:25], ...[290:0->290:16], ...[292:2->301:20])
12401240
Completable: Cexpression CArgument Value[fnTakingPolyVariant]($0)=#
12411241
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
12421242
Resolved opens 1 Stdlib

tests/analysis_tests/tests/src/expected/CompletionPipeSubmodules.res.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Complete src/CompletionPipeSubmodules.res 13:20
2+
posCursor:[13:20] posNoWhite:[13:19] Found expr:[13:11->23:23]
3+
Pexp_apply ...[21:9->21:10] (...[13:11->21:8], ...[23:2->23:23])
24
posCursor:[13:20] posNoWhite:[13:19] Found expr:[13:11->21:8]
35
Completable: Cpath Value[A, B1, xx]->
46
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
@@ -18,6 +20,8 @@ Path
1820
}]
1921

2022
Complete src/CompletionPipeSubmodules.res 17:18
23+
posCursor:[17:18] posNoWhite:[17:17] Found expr:[17:11->23:23]
24+
Pexp_apply ...[21:9->21:10] (...[17:11->21:8], ...[23:2->23:23])
2125
posCursor:[17:18] posNoWhite:[17:17] Found expr:[17:11->21:8]
2226
Completable: Cpath Value[A, x].v->
2327
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
Syntax error!
3+
syntax_tests/data/parsing/errors/expressions/typeDefInFunction.res:2:3-14
4+
5+
1 │ let f = () => {
6+
2 │ type t = int
7+
3 │ 1
8+
4 │ }
9+
10+
Type definitions are not allowed inside functions.
11+
Move this `type` declaration to the top level or into a module.
12+
13+
let f [arity:1]() = ((1)[@res.braces ])
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let f = () => {
2+
type t = int
3+
1
4+
}
5+

0 commit comments

Comments
 (0)