diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cf78e3e12..e9aa2d03b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ - Make "rescript format" work with node 10 again and set minimum required node version to 10 in package.json. https://github.com/rescript-lang/rescript-compiler/pull/6186 - Fix partial application for uncurried functions with labeled args https://github.com/rescript-lang/rescript-compiler/pull/6198 +- Add error messages for dangling doc comments/attributes and mutable in record type definition. https://github.com/rescript-lang/rescript-compiler/pull/6206 # 11.0.0-alpha.4 diff --git a/res_syntax/src/res_core.ml b/res_syntax/src/res_core.ml index ed48370320..d8e90407f1 100644 --- a/res_syntax/src/res_core.ml +++ b/res_syntax/src/res_core.ml @@ -4498,7 +4498,18 @@ and parseFieldDeclarationRegion ?foundObjectField p = let loc = mkLoc startPos typ.ptyp_loc.loc_end in let attrs = if optional then optionalAttr :: attrs else attrs in Some (Ast_helper.Type.field ~attrs ~loc ~mut name typ) - | _ -> None + | _ -> + if attrs <> [] then + Parser.err ~startPos p + (Diagnostics.message + "Attributes and doc comments can only be used at the beginning of a \ + field declaration"); + if mut = Mutable then + Parser.err ~startPos p + (Diagnostics.message + "The `mutable` qualifier can only be used at the beginning of a \ + field declaration"); + None (* record-decl ::= * | { field-decl } diff --git a/res_syntax/tests/parsing/errors/typeDef/expected/recordDocComment.res.txt b/res_syntax/tests/parsing/errors/typeDef/expected/recordDocComment.res.txt new file mode 100644 index 0000000000..134e41fff1 --- /dev/null +++ b/res_syntax/tests/parsing/errors/typeDef/expected/recordDocComment.res.txt @@ -0,0 +1,13 @@ + + Syntax error! + tests/parsing/errors/typeDef/recordDocComment.res:2:16-3:1 + + 1 │ type a = { + 2 │ foo: string, /** here */ + 3 │ } + 4 │ + + Attributes and doc comments can only be used at the beginning of a field declaration + +type nonrec a = { + foo: string } \ No newline at end of file diff --git a/res_syntax/tests/parsing/errors/typeDef/expected/recordMutable.res.txt b/res_syntax/tests/parsing/errors/typeDef/expected/recordMutable.res.txt new file mode 100644 index 0000000000..0e362f5be2 --- /dev/null +++ b/res_syntax/tests/parsing/errors/typeDef/expected/recordMutable.res.txt @@ -0,0 +1,13 @@ + + Syntax error! + tests/parsing/errors/typeDef/recordMutable.res:2:16-3:1 + + 1 │ type d = { + 2 │ foo: string, mutable + 3 │ } + 4 │ + + The `mutable` qualifier can only be used at the beginning of a field declaration + +type nonrec d = { + foo: string } \ No newline at end of file diff --git a/res_syntax/tests/parsing/errors/typeDef/recordDocComment.res b/res_syntax/tests/parsing/errors/typeDef/recordDocComment.res new file mode 100644 index 0000000000..ada0efe3fd --- /dev/null +++ b/res_syntax/tests/parsing/errors/typeDef/recordDocComment.res @@ -0,0 +1,3 @@ +type a = { + foo: string, /** here */ +} diff --git a/res_syntax/tests/parsing/errors/typeDef/recordMutable.res b/res_syntax/tests/parsing/errors/typeDef/recordMutable.res new file mode 100644 index 0000000000..a7c991ef2e --- /dev/null +++ b/res_syntax/tests/parsing/errors/typeDef/recordMutable.res @@ -0,0 +1,3 @@ +type d = { + foo: string, mutable +}