Merge pull request #239 from CatalaLang/fix_237

Nice error messages for empty structs and enums
This commit is contained in:
Denis Merigoux 2022-03-28 14:53:00 +02:00 committed by GitHub
commit e28587c3ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 7 deletions

View File

@ -377,6 +377,12 @@ let process_struct_decl (ctxt : context) (sdecl : Ast.struct_decl) : context =
let s_uid =
Desugared.Ast.IdentMap.find (fst sdecl.struct_decl_name) ctxt.struct_idmap
in
if List.length sdecl.struct_decl_fields = 0 then
Errors.raise_spanned_error
(Pos.get_position sdecl.struct_decl_name)
"The struct %s does not have any fields; give it some for Catala to be \
able to accept it."
(Pos.unmark sdecl.struct_decl_name);
List.fold_left
(fun ctxt (fdecl, _) ->
let f_uid =
@ -420,6 +426,12 @@ let process_enum_decl (ctxt : context) (edecl : Ast.enum_decl) : context =
let e_uid =
Desugared.Ast.IdentMap.find (fst edecl.enum_decl_name) ctxt.enum_idmap
in
if List.length edecl.enum_decl_cases = 0 then
Errors.raise_spanned_error
(Pos.get_position edecl.enum_decl_name)
"The enum %s does not have any cases; give it some for Catala to be able \
to accept it."
(Pos.unmark edecl.enum_decl_name);
List.fold_left
(fun ctxt (cdecl, cdecl_pos) ->
let c_uid =

View File

@ -2,8 +2,7 @@ source_file: BEGIN_CODE DECLARATION ENUM CONSTRUCTOR COLON ALT CONSTRUCTOR CONTE
##
## Ends in an error in state: 358.
##
## nonempty_list(enum_decl_line) -> enum_decl_line . [ SCOPE END_CODE DECLARATION ]
## nonempty_list(enum_decl_line) -> enum_decl_line . nonempty_list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
## list(enum_decl_line) -> enum_decl_line . list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
##
## The known suffix of the stack is as follows:
## enum_decl_line
@ -51,7 +50,7 @@ source_file: BEGIN_CODE DECLARATION ENUM CONSTRUCTOR COLON YEAR
##
## Ends in an error in state: 350.
##
## code_item -> DECLARATION ENUM constructor COLON . nonempty_list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
## code_item -> DECLARATION ENUM constructor COLON . list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
##
## The known suffix of the stack is as follows:
## DECLARATION ENUM constructor COLON
@ -63,7 +62,7 @@ source_file: BEGIN_CODE DECLARATION ENUM CONSTRUCTOR YEAR
##
## Ends in an error in state: 349.
##
## code_item -> DECLARATION ENUM constructor . COLON nonempty_list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
## code_item -> DECLARATION ENUM constructor . COLON list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
##
## The known suffix of the stack is as follows:
## DECLARATION ENUM constructor
@ -75,7 +74,7 @@ source_file: BEGIN_CODE DECLARATION ENUM YEAR
##
## Ends in an error in state: 348.
##
## code_item -> DECLARATION ENUM . constructor COLON nonempty_list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
## code_item -> DECLARATION ENUM . constructor COLON list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
##
## The known suffix of the stack is as follows:
## DECLARATION ENUM
@ -264,7 +263,7 @@ source_file: BEGIN_CODE DECLARATION YEAR
##
## code_item -> DECLARATION . STRUCT constructor COLON list(struct_scope) [ SCOPE END_CODE DECLARATION ]
## code_item -> DECLARATION . SCOPE constructor COLON nonempty_list(scope_decl_item) [ SCOPE END_CODE DECLARATION ]
## code_item -> DECLARATION . ENUM constructor COLON nonempty_list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
## code_item -> DECLARATION . ENUM constructor COLON list(enum_decl_line) [ SCOPE END_CODE DECLARATION ]
##
## The known suffix of the stack is as follows:
## DECLARATION

View File

@ -617,7 +617,7 @@ code_item:
scope_decl_context = context;
}, Pos.from_lpos $sloc)
}
| DECLARATION ENUM c = constructor COLON cases = nonempty_list(enum_decl_line) {
| DECLARATION ENUM c = constructor COLON cases = list(enum_decl_line) {
(EnumDecl {
enum_decl_name = c;
enum_decl_cases = cases;

View File

@ -0,0 +1,8 @@
## Test
```catala
declaration enumeration Foo:
declaration scope Bar:
internal foo content Foo
```

View File

@ -0,0 +1,7 @@
[ERROR] The enum Foo does not have any cases; give it some for Catala to be able to accept it.
--> tests/test_enum/bad/empty.catala_en
|
4 | declaration enumeration Foo:
| ^^^
+ Test

View File

@ -0,0 +1,8 @@
## Test
```catala
declaration structure Foo:
declaration scope Bar:
internal foo content Foo
```

View File

@ -0,0 +1,7 @@
[ERROR] The struct Foo does not have any fields; give it some for Catala to be able to accept it.
--> tests/test_struct/bad/empty_struct.catala_en
|
4 | declaration structure Foo:
| ^^^
+ Test