Skip to content

[Compiler Bug]: using syntax is not preserved #35171

@cormacrelf

Description

@cormacrelf

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-hooks (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://github.com/cormacrelf/react-compiler-using-bug

Repro steps

pnpm i && pnpm dev. Open App.tsx to see there are three diposables with using syntax. Open the console, note that only one of the Disposables had its Symbol.dispose method called.

To show that it's react-compiler's fault, you can:

  • observe that only using syntax inside the component is transformed, the one at the top level is preserved.
  • uncomment effect.useIt() which makes react-compiler skip the component and all the disposals work.
  • skip the component
  • disable the transform in vite.config.ts

Note that react-compiler transforms the using render = new Disposable("render"); to just new Disposable("render"), which is not a valid transform. It would be valid for a const or let binding.

There is another bug in here that I can't reproduce, because react-compiler keeps bailing out when I add the useIt call. But very similar code to the following does get compiled, and in that case the using is just transformed to a const, which again breaks the disposal.

function App() {
  const context = useContext();
  const ref = React.useRef(null);

  useEffect(() => {
    if (ref.current) {
      using effect = new Disposable(context);
      effect.useIt();
    }
  });

  return <div ref={ref}></div>;
}

How often does this bug happen?

Every time

What version of React are you using?

19.2.0

What version of React Compiler are you using?

1.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugType: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions