Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#### :nail_care: Polish

- Improve error message for trying to define a type inside a function. https://github.com/rescript-lang/rescript/pull/7843

#### :house: Internal

# 12.0.0-beta.9
Expand Down
14 changes: 14 additions & 0 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ module ErrorMessages = struct
^ "_`)\n If you need the field to be \"" ^ keyword_txt
^ "\" at runtime, annotate the field: `@as(\"" ^ keyword_txt ^ "\") "
^ keyword_txt ^ "_ : ...`"

let type_definition_in_function =
"Type definitions are not allowed inside functions.\n"
^ " Move this `type` declaration to the top level or into a module."
end

module InExternal = struct
Expand Down Expand Up @@ -3479,6 +3483,16 @@ and parse_expr_block_item p =
in
let loc = mk_loc start_pos p.prev_end_pos in
Ast_helper.Exp.let_ ~loc rec_flag let_bindings next
| Typ ->
(* Parse to be able to give a good error message. *)
let type_start = start_pos in
Parser.begin_region p;
let _ = parse_type_definition_or_extension ~attrs p in
Parser.end_region p;
Parser.err ~start_pos:type_start ~end_pos:p.prev_end_pos p
(Diagnostics.message ErrorMessages.type_definition_in_function);
parse_newline_or_semicolon_expr_block p;
parse_expr_block p
| _ ->
let e1 =
let expr = parse_expr p in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

Syntax error!
/.../fixtures/type_in_function.res:2:3-14

1 │ let f = () => {
2 │ type t = int
3 │ 1
4 │ }

Type definitions are not allowed inside functions.
Move this `type` declaration to the top level or into a module.
4 changes: 4 additions & 0 deletions tests/build_tests/super_errors/fixtures/type_in_function.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let f = () => {
type t = int
1
}
Loading