This commit is contained in:
Denis Merigoux 2021-04-29 17:46:56 +02:00
parent 3f5e423e9f
commit d69f1a4a39
No known key found for this signature in database
GPG Key ID: EE99DCFA365C3EE3
5 changed files with 54 additions and 18 deletions

View File

@ -98,14 +98,7 @@ let rec translate_expr (ctx : ctx) (e : Ast.expr Pos.marked) : Dcalc.Ast.expr Po
let d_fields, remaining_e_fields =
List.fold_right
(fun (field_name, _) (d_fields, e_fields) ->
let field_e =
try Ast.StructFieldMap.find field_name e_fields
with Not_found ->
Errors.raise_spanned_error
(Format.asprintf "Missing field for structure %a: \"%a\""
Ast.StructName.format_t struct_name Ast.StructFieldName.format_t field_name)
(Pos.get_position e)
in
let field_e = Ast.StructFieldMap.find field_name e_fields in
let field_d = translate_expr ctx field_e in
(field_d :: d_fields, Ast.StructFieldMap.remove field_name e_fields))
struct_sig ([], e_fields)

View File

@ -325,6 +325,17 @@ let rec translate_expr (scope : Scopelang.Ast.ScopeName.t) (ctxt : Name_resoluti
Scopelang.Ast.StructFieldMap.add f_uid f_e s_fields)
Scopelang.Ast.StructFieldMap.empty fields
in
let expected_s_fields = Scopelang.Ast.StructMap.find s_uid ctxt.structs in
Scopelang.Ast.StructFieldMap.iter
(fun expected_f _ ->
if not (Scopelang.Ast.StructFieldMap.mem expected_f s_fields) then
Errors.raise_spanned_error
(Format.asprintf "Missing field for structure %a: \"%a\""
Scopelang.Ast.StructName.format_t s_uid Scopelang.Ast.StructFieldName.format_t
expected_f)
pos)
expected_s_fields;
Bindlib.box_apply
(fun s_fields -> (Scopelang.Ast.EStruct (s_uid, s_fields), pos))
(LiftStructFieldMap.lift_box s_fields)

View File

@ -346,21 +346,19 @@ let process_enum_decl (ctxt : context) (edecl : Ast.enum_decl) : context =
(** Process the names of all declaration items *)
let process_name_item (ctxt : context) (item : Ast.code_item Pos.marked) : context =
let raise_already_defined_error use name pos msg =
let raise_already_defined_error (use : Uid.MarkedString.info) name pos msg =
Errors.raise_multispanned_error
(Format.asprintf "%s name \"%s\" already defined" msg
(Utils.Cli.print_with_style [ ANSITerminal.yellow ] "%s" name))
[
(Some "first definition", Pos.get_position (Scopelang.Ast.ScopeName.get_info use));
(Some "second definition", pos);
]
[ (Some "first definition", Pos.get_position use); (Some "second definition", pos) ]
in
match Pos.unmark item with
| ScopeDecl decl -> (
let name, pos = decl.scope_decl_name in
(* Checks if the name is already used *)
match Desugared.Ast.IdentMap.find_opt name ctxt.scope_idmap with
| Some use -> raise_already_defined_error use name pos "scope"
| Some use ->
raise_already_defined_error (Scopelang.Ast.ScopeName.get_info use) name pos "scope"
| None ->
let scope_uid = Scopelang.Ast.ScopeName.fresh (name, pos) in
{
@ -379,8 +377,9 @@ let process_name_item (ctxt : context) (item : Ast.code_item Pos.marked) : conte
})
| StructDecl sdecl -> (
let name, pos = sdecl.struct_decl_name in
match Desugared.Ast.IdentMap.find_opt name ctxt.scope_idmap with
| Some use -> raise_already_defined_error use name pos "struct"
match Desugared.Ast.IdentMap.find_opt name ctxt.struct_idmap with
| Some use ->
raise_already_defined_error (Scopelang.Ast.StructName.get_info use) name pos "struct"
| None ->
let s_uid = Scopelang.Ast.StructName.fresh sdecl.struct_decl_name in
{
@ -390,8 +389,9 @@ let process_name_item (ctxt : context) (item : Ast.code_item Pos.marked) : conte
})
| EnumDecl edecl -> (
let name, pos = edecl.enum_decl_name in
match Desugared.Ast.IdentMap.find_opt name ctxt.scope_idmap with
| Some use -> raise_already_defined_error use name pos "enum"
match Desugared.Ast.IdentMap.find_opt name ctxt.enum_idmap with
| Some use ->
raise_already_defined_error (Scopelang.Ast.EnumName.get_info use) name pos "enum"
| None ->
let e_uid = Scopelang.Ast.EnumName.fresh edecl.enum_decl_name in

View File

@ -0,0 +1,17 @@
## [https://github.com/CatalaLang/catala/issues/107]
```catala
new struct S:
data x content int
data y content int
new struct S:
data x content int
data y content int
new scope A:
param a content S
scope A:
def a := S { --x : 0 -- y : 1 }
```

View File

@ -0,0 +1,15 @@
[ERROR] struct name "S" already defined
[ERROR]
[ERROR] first definition
[ERROR] --> test_struct/bad/bug_107.catala
[ERROR] |
[ERROR] 4 | new struct S:
[ERROR] | ^
[ERROR] + https://github.com/CatalaLang/catala/issues/107
[ERROR]
[ERROR] second definition
[ERROR] --> test_struct/bad/bug_107.catala
[ERROR] |
[ERROR] 8 | new struct S:
[ERROR] | ^
[ERROR] + https://github.com/CatalaLang/catala/issues/107