Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve missing field error message #566

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all 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
23 changes: 16 additions & 7 deletions compiler/desugared/from_surface.ml
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,22 @@ let rec translate_expr
StructField.Map.empty fields
in
let expected_s_fields = StructName.Map.find s_uid ctxt.structs in
StructField.Map.iter
(fun expected_f _ ->
if not (StructField.Map.mem expected_f s_fields) then
Message.raise_spanned_error pos
"Missing field for structure %a: \"%a\"" StructName.format s_uid
StructField.format expected_f)
expected_s_fields;
if
StructField.Map.exists
(fun expected_f _ -> not (StructField.Map.mem expected_f s_fields))
expected_s_fields
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally find it nicer to write let missing = Map.diff expected fields in if not (Map.is_empty missing) then ..., but it's generally less efficient and well Map doesn't have a proper diff function (although you can use merge for that 🤷) so this is fine ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you coud use Map.merge to implement some sort of Map.diff. I'll remember it next time :)

then
Message.raise_spanned_error pos "Missing field(s) for structure %a:@\n%a"
StructName.format s_uid
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
(fun fmt (expected_f, _) ->
Format.fprintf fmt "\"%a\"" StructField.format expected_f))
(StructField.Map.bindings
(StructField.Map.filter
(fun expected_f _ ->
not (StructField.Map.mem expected_f s_fields))
expected_s_fields));

Expr.estruct ~name:s_uid ~fields:s_fields emark
| EnumInject (((path, (constructor, pos_constructor)), _), payload) -> (
Expand Down
Loading