-
Notifications
You must be signed in to change notification settings - Fork 47.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Compiler Bug]: null reference exception if you assume a value is not null in a callback (which can be valid) #31551
Comments
Similiar to #31269 but that is fixed on the playground and this is not. |
Thanks for the report! The compiler is making this optimization assuming that react code is using a type system (e.g. typescript / flow) which would error on unguarded accesses to maybe-null variables (see typescript playground). We recognize that not everyone is using a type system, and that even typescript / flow developers may have unsound casts (e.g. While we work on adding type-system aware configs, you can manually configure the compiler to not make these assumptions by adding // babel.config.js
const ReactCompilerConfig = {
/* ... */
environment: {
enableTreatFunctionDepsAsConditional: true,
}
}; |
Thanks - we do use typescript and don’t have any suppressions here - will check what’s happening. I think on our codebase we won’t take the risk and disable the optimization as you suggest, thanks. |
The issue is typescript is not faultless - they make decisions for the best ergonomics but not always accurate. e.g. my issue was this case: notice there is no typescript error but the object will clearly be undefined. I've abstracted this from a 400 line component - in that component the object is referenced in a callback that only ends up being attached if the array has items in it. but initially it has no items, but react compiler pulls up access to this property. So... without react compiler it will never error. With react compiler it will error on a empty array... even if react compiler understands typescript, this optimization is always going to lead to occasional issues, some of which might not be detectable until production. I would love to see this optimization and any other inherently unsafe optimizations opt-in. |
See #31551 for context We've rolled out internally with `enableTreatFunctionDepsAsConditional: false` and encountered no issues, but this is technically an unsound optimization as both flow and typescript have sources of unsoundness: - typing array accesses / other unknown keys as `TValue | undefined` (typescript's noUncheckedIndexedAccess) - explicit and inferred `any` - unsound typecasts Note that removing this optimization results in less granular dependencies for ~3% of files on a large Meta project ([link](https://www.internalfb.com/phabricator/paste/view/P1681742214)).
What kind of issue is this?
Link to repro
https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAKgmDgBTBEC2AhgJ4BGCAclADacA0RAvgEoiwADrEicQhUnMiAXiJQwCAML1uzenADWlSsPkA+EeKLmiaTNSIAPZHSasO3AHS2BggNxmBfANoMLOxcvAC63uK+MAg4sMRBzqFEAPxEADxgAA70xAD0Jg7pACZ4AG5EhKqceLrywHDM-EQFPpj8IPxAA
Repro steps
Similiar to #31550 but with useCallback and no external deps.
You seem to assume that accessing
x.y
is safe in the render method if it is safe in a callback.How often does this bug happen?
Every time
What version of React are you using?
18.3.1
What version of React Compiler are you using?
19.0.0-beta-a7bf2bd-20241110
The text was updated successfully, but these errors were encountered: