Skip to content

A field bound by a record pattern is read after the callee of the call it feeds #9352

Open

Description

@mununki

let {x} = r reads the field at the let. When x is then the argument of a call, the generated JS reads the field at the call, after JavaScript has evaluated the callee. The field can change in between: a record with a mutable field can be coerced to a type where the field is immutable (#9086).

type m = {mutable x: int}
type v = {x: int}

let r: m = {x: 1}
let view = (r :> v)

let next = () => {
  r.x = r.x + 1
  (y: int) => y
}

let f = () => {
  let {x} = view
  next()(x)
}

Console.log(f())

Output, on master and on 12.3.1:

function f() {
  return next()(r.x);
}

This prints 2. It should print 1. #9296 fixes the same order for let x = view.x; next()(x), but not for the pattern.

The cause is in matching.ml: make_record_matching binds a field of a pattern as Alias when its label is immutable, and an Alias binding is read where it is used. I mentioned this case when closing #9223.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions