Skip to content

[Compiler Bug]: Compiler errors when optimizing component with spread assignment in if in while loop #33196

@kumpelstachu

Description

@kumpelstachu

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEUYCAKgmDgIwAUAlEcADrFFyHVEBuAhgBsoCIgF4SZAGqDhjNmyJEYCHLGJ0+MAOZgmogHzMFiogHcAFngEi6OGMKat2JogBMIAZQgBbFZcxaGtq6xi4A9GFEAHQxoSZ4aERBOgD8UQAOMBDpjnHhkXB8pFRECDBZMHkmmjpizNExNWAANESZ2ci8MiIAvlV9zkQDigMDbBjY+IQSFFQ4AEyMRuycmNz8QiLixdKbcpjGyqow6k16hk4uFlY2dg7LLoruXr44-oFnVRENUVUJSU00u0cg9HopvqYIDAANZgKqKJoZLLpOobYT9OLDIZsMaYCa4AjEYqUagAZiWlw4XBwXU2dR23X2hxUagBwXOoMU12sSTuCFygyenh8fjwAWSIUFRG+MV+UohUNhVSadWAPyarWBnTRvUxxlGIB6QA

Repro steps

React compiler fails to optimize components like this:

function Component() {
  const value = useValue()

  const fn = (args) => {
    while (true) {
      // do something with args
      // ...

      // ✅ works outside of if
      args = { ...args, prop: value }

      if (args?.prop) {
        // ✅ works when assigning prop directly
        args.prop = value

        // 👇 Invariant: Invalid mutable range for scope. Scope @0 has range [0:21] but the valid range is [1:25]
        args = { ...args, prop: value }
      }
    }
  }

  // ...
}

How often does this bug happen?

Every time

What version of React are you using?

19.0.0

What version of React Compiler are you using?

19.1.0-rc.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions