Skip to content
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] validation against calling impure functions #31960

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

josephsavona
Copy link
Contributor

For now we just reject all calls of impure functions, and the validation is off by default. Going forward we can make this more precise and only reject impure functions called during render.

Note that I was intentionally imprecise in the return type of these functions in order to avoid changing output of existing code. We lie to the compiler and say that Date.now, performance.now, and Math.random return unknown mutable objects rather than primitives. Once the validation is complete and vetted we can switch this to be more precise.

…g render

For now we just reject all calls of impure functions, and the validation is off by default. Going forward we can make this more precise about impure functions called during render.

Note that I was intentionaly imprecise in the return type of these functions in order to avoid changing output of existing code. We lie to the compiler and say that Date.now, performance.now, and Math.random return unknown mutable objects rather than primitives. Once the validation is complete and vetted we can switch this to be more precise.
Copy link

vercel bot commented Jan 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ❌ Failed (Inspect) Jan 2, 2025 8:10pm

[
'Date',
addObject(DEFAULT_SHAPES, 'Date', [
// Static methods (TODO)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Date() too (no args)

@PeteVanG

This comment was marked as spam.

@kassens
Copy link
Member

kassens commented Jan 3, 2025

This is only checking the top level instructions (no closures) if I read this right? Seems okay, doing more seems like it'd be easy to cause false positives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants