Skip to content

Conversation

@Kamirus
Copy link
Contributor

@Kamirus Kamirus commented Nov 3, 2025

Improve type inference for record update syntax. Example

let w : Workout = {
  workout with
  exercises = Map.empty(); // should typecheck without type instantiation
  duration;
};

fixes #5628

@github-actions
Copy link
Contributor

github-actions bot commented Nov 4, 2025

Comparing from 5884199 to 61aaabd:
In terms of gas, no changes are observed in 5 tests.
In terms of size, no changes are observed in 5 tests.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 4, 2025

Comparing from 5884199 to 9b55450:
In terms of gas, no changes are observed in 5 tests.
In terms of size, no changes are observed in 5 tests.

@Kamirus Kamirus marked this pull request as ready for review November 4, 2025 14:32
@Kamirus Kamirus requested a review from a team as a code owner November 4, 2025 14:32

let find_field (ef : exp_field) (fts : T.field list) =
let id = ef.it.id.it in
List.find_opt T.(fun ft -> ft.lab = id && not (is_typ ft.typ)) fts
Copy link
Contributor Author

Choose a reason for hiding this comment

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

exp_field is always value-level, yes? So we need to skip the type <id> = <typ> T.fields.
normalize not necessary, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Normalize should not be necessary to distinguish a type field from a value field, no. Ditto for T.mut

Copy link
Contributor

Choose a reason for hiding this comment

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

There should be a Type.find_val_field_opt or similar that does this for I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! lookup_val_field_opt is there, but it returns the type instead of the full field, but I've moved the find_val_field_opt next to that

Comment on lines 2423 to 2434
let missing_field_labs =
List.filter (fun ft -> not (List.exists (fun ft' -> ft.T.lab = ft'.T.lab) fts')) fts
|> List.map (fun ft -> Printf.sprintf "'%s'" ft.T.lab)
in
begin match missing_field_labs with
| [] -> check_inferred env0 env t t' exp
| fts ->
(* Future work: Replace this error with a general subtyping error once better explanations are available. *)
let s = if List.length fts = 1 then "" else "s" in
local_error env exp.at "M0151" "missing field%s %s from expected type%a" s (String.concat ", " fts) display_typ_expand t;
t'
end
Copy link
Contributor Author

@Kamirus Kamirus Nov 4, 2025

Choose a reason for hiding this comment

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

All of this should be redundant (replaced with just check_inferred env0 env t t' exp) once we have time to complete #5409

@Kamirus Kamirus requested review from crusso and ggreif November 4, 2025 15:07
| _ -> []
in
let missing_field_labs =
List.filter (fun ft -> not (List.exists (fun ft' -> ft.T.lab = ft'.T.lab) fts')) fts
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this should be distinguishing type fields from value fields. They can have the same name. Maybe this was a lurking bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, another limitation of this extra check is that it is not checking whether the type matches.
Anyway, if this check does not catch the error, the check_inferred will. This is just an extra check.
But yeah I'll change to only val fields

List.iter2 (check_exp env) ts exps;
t
| ObjE ([], exp_fields) as e, T.Obj(T.Object, fts) -> (* TODO: infer bases? Default does a decent job. *)
check_ids env "object" "field"
Copy link
Contributor

@crusso crusso Nov 4, 2025

Choose a reason for hiding this comment

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

Has this check (check_ids) been removed? I think it checks the field names are distinct and needs to be done somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is still there, in the infer_check_bases_fields

@Kamirus Kamirus requested a review from crusso November 5, 2025 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve type inference upon record update

2 participants