-
Notifications
You must be signed in to change notification settings - Fork 49.6k
[compiler][repro] Postfix operator is incorrectly compiled #33508
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
+175
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
...piler/src/__tests__/fixtures/compiler/bug-ref-prefix-postfix-operator.expect.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {useRef, useEffect} from 'react'; | ||
|
||
/** | ||
* The postfix increment operator should return the value before incrementing. | ||
* ```js | ||
* const id = count.current; // 0 | ||
* count.current = count.current + 1; // 1 | ||
* return id; | ||
* ``` | ||
* The bug is that we currently increment the value before the expression is evaluated. | ||
* This bug does not trigger when the incremented value is a plain primitive. | ||
* | ||
* Found differences in evaluator results | ||
* Non-forget (expected): | ||
* (kind: ok) {"count":{"current":0},"updateCountPostfix":"[[ function params=0 ]]","updateCountPrefix":"[[ function params=0 ]]"} | ||
* logs: ['id = 0','count = 1'] | ||
* Forget: | ||
* (kind: ok) {"count":{"current":0},"updateCountPostfix":"[[ function params=0 ]]","updateCountPrefix":"[[ function params=0 ]]"} | ||
* logs: ['id = 1','count = 1'] | ||
*/ | ||
function useFoo() { | ||
const count = useRef(0); | ||
const updateCountPostfix = () => { | ||
const id = count.current++; | ||
return id; | ||
}; | ||
const updateCountPrefix = () => { | ||
const id = ++count.current; | ||
return id; | ||
}; | ||
useEffect(() => { | ||
const id = updateCountPostfix(); | ||
console.log(`id = ${id}`); | ||
console.log(`count = ${count.current}`); | ||
}, []); | ||
return {count, updateCountPostfix, updateCountPrefix}; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useRef, useEffect } from "react"; | ||
|
||
/** | ||
* The postfix increment operator should return the value before incrementing. | ||
* ```js | ||
* const id = count.current; // 0 | ||
* count.current = count.current + 1; // 1 | ||
* return id; | ||
* ``` | ||
* The bug is that we currently increment the value before the expression is evaluated. | ||
* This bug does not trigger when the incremented value is a plain primitive. | ||
* | ||
* Found differences in evaluator results | ||
* Non-forget (expected): | ||
* (kind: ok) {"count":{"current":0},"updateCountPostfix":"[[ function params=0 ]]","updateCountPrefix":"[[ function params=0 ]]"} | ||
* logs: ['id = 0','count = 1'] | ||
* Forget: | ||
* (kind: ok) {"count":{"current":0},"updateCountPostfix":"[[ function params=0 ]]","updateCountPrefix":"[[ function params=0 ]]"} | ||
* logs: ['id = 1','count = 1'] | ||
*/ | ||
function useFoo() { | ||
const $ = _c(5); | ||
const count = useRef(0); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = () => { | ||
count.current = count.current + 1; | ||
const id = count.current; | ||
return id; | ||
}; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
const updateCountPostfix = t0; | ||
let t1; | ||
if ($[1] === Symbol.for("react.memo_cache_sentinel")) { | ||
t1 = () => { | ||
const id_0 = (count.current = count.current + 1); | ||
return id_0; | ||
}; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
const updateCountPrefix = t1; | ||
let t2; | ||
let t3; | ||
if ($[2] === Symbol.for("react.memo_cache_sentinel")) { | ||
t2 = () => { | ||
const id_1 = updateCountPostfix(); | ||
console.log(`id = ${id_1}`); | ||
console.log(`count = ${count.current}`); | ||
}; | ||
t3 = []; | ||
$[2] = t2; | ||
$[3] = t3; | ||
} else { | ||
t2 = $[2]; | ||
t3 = $[3]; | ||
} | ||
useEffect(t2, t3); | ||
let t4; | ||
if ($[4] === Symbol.for("react.memo_cache_sentinel")) { | ||
t4 = { count, updateCountPostfix, updateCountPrefix }; | ||
$[4] = t4; | ||
} else { | ||
t4 = $[4]; | ||
} | ||
return t4; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
42 changes: 42 additions & 0 deletions
42
...-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-ref-prefix-postfix-operator.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {useRef, useEffect} from 'react'; | ||
|
||
/** | ||
* The postfix increment operator should return the value before incrementing. | ||
* ```js | ||
* const id = count.current; // 0 | ||
* count.current = count.current + 1; // 1 | ||
* return id; | ||
* ``` | ||
* The bug is that we currently increment the value before the expression is evaluated. | ||
* This bug does not trigger when the incremented value is a plain primitive. | ||
* | ||
* Found differences in evaluator results | ||
* Non-forget (expected): | ||
* (kind: ok) {"count":{"current":0},"updateCountPostfix":"[[ function params=0 ]]","updateCountPrefix":"[[ function params=0 ]]"} | ||
* logs: ['id = 0','count = 1'] | ||
* Forget: | ||
* (kind: ok) {"count":{"current":0},"updateCountPostfix":"[[ function params=0 ]]","updateCountPrefix":"[[ function params=0 ]]"} | ||
* logs: ['id = 1','count = 1'] | ||
*/ | ||
function useFoo() { | ||
const count = useRef(0); | ||
const updateCountPostfix = () => { | ||
const id = count.current++; | ||
return id; | ||
}; | ||
const updateCountPrefix = () => { | ||
const id = ++count.current; | ||
return id; | ||
}; | ||
useEffect(() => { | ||
const id = updateCountPostfix(); | ||
console.log(`id = ${id}`); | ||
console.log(`count = ${count.current}`); | ||
}, []); | ||
return {count, updateCountPostfix, updateCountPrefix}; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [], | ||
}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might be able to get a runnable test (that fails the forget/no-forget comparison) by using effects that setState w the result of the increment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just did a console log for now so sprout catches it. I added this fixture to the sprout filter.