Skip to content

Reads of a reassigned variable from an inlined @inline body are moved past the assignment #9533

In review

Description

@cknitt

Three optimizations move a read of a reassigned variable past the assignment.

These bugs are old, not new regressions.

All examples use:

// Other.res
module type S = {
  let x: string
}
module A = {
  let x = "a"
}
module B = {
  let x = "b"
}

1. A module field read from a known tuple.

// Other.res
@inline
let viaTuple = (n: int, q: module(S), q2: module(S)) => {
  let s = ref(q)
  let t = (
    {
      let module(M) = s.contents
      M.x
    },
    1,
  )
  for _ in 0 to n {
    s := q2
  }
  let (a, _) = t
  let module(M2) = s.contents
  (a, M2.x)
}

// Main.res
let tupleEarly = () => Other.viaTuple(3, module(Other.A), module(Other.B))

tupleEarly() returns ["b", "b"] instead of ["a", "b"]: the alias pass replaces t[0] with s.x, read after the loop.

2. A module field bound once and used once.

// Other.res
let get = (m: module(S)) => {
  let module(M) = m
  M.x
}
@inline
let viaGet = (n: int, q: module(S), q2: module(S)) => {
  let s = ref(q)
  let a = get(s.contents)
  for _ in 0 to n {
    s := q2
  }
  (a, get(s.contents))
}

// Main.res
let early = () => Other.viaGet(3, module(Other.A), module(Other.B))

early() returns ["b", "b"] instead of ["a", "b"]: Lam_util.refine_let substitutes s.x for a (return [s.x, s.x]).

3. A tuple element, when the function is inlined while compiling to JavaScript.

// Other.res
@inline
let tupleBeforeAssign = (n: int, q: 'a, q2: 'a) => {
  let s = ref(q)
  let t = (s.contents, 1)
  for _ in 0 to n {
    s := q2
  }
  let (a, _) = t
  (a, s.contents)
}

// Main.res
let gLate = (n, q) => Other.tupleBeforeAssign(n, q, "b")

gLate(3, "a") returns ["b", "b"] instead of ["a", "b"]. The function is inlined during code generation, because its arguments are parameters of gLate. Js_pass_flatten_and_mark_dead then replaces t[0] with s. #9284 taught that pass to leave reassigned variables alone, but its set of them misses variables from bodies inlined during code generation.

Found by a review of #9531, which fixes the same problem for Some(x) payloads and plain tuple elements.

(Filed by Claude.)

Activity

  1. added theissue type on Dec 5, 2026
  2. added 2 commits that reference this issue on Dec 5, 2026
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions