[compiler] Fix for loops in try/catch#460
Conversation
Fix for for..in and for..of within try/catch. The logic to extract the init/test values didn't work in the presence of maybe-throw terminals. We fix that with more robust extraction of the value block values for this case, consistently flattening nested sequence expressions into a single ReactiveValue.
Greptile OverviewGreptile SummaryThis PR fixes React Compiler handling for It also updates related HIR utilities (printing, successor iteration, aliasing inference) for nullable handlers, adds new repro fixtures that now compile successfully, and improves One issue: Confidence Score: 4/5
Important Files Changed
|
0c7b7ca to
f09e434
Compare
for loops in try/catch
f09e434 to
7e8ae2d
Compare
| // Read the input file | ||
| const input = fs.readFileSync(inputPath, 'utf-8'); | ||
| const filename = path.basename(inputPath); | ||
| const firstLine = input.substring(0, input.indexOf('\n')); | ||
| const language = parseLanguage(firstLine); | ||
| const sourceType = parseSourceType(firstLine); | ||
|
|
There was a problem hiding this comment.
Single-line input breaks
runMinimizeCommand() computes firstLine via input.substring(0, input.indexOf('\n')) (runner.ts:161). If the file has no newline, indexOf returns -1 and JS substring(0, -1) yields an empty string, so parseLanguage/parseSourceType fall back to defaults and any fixture directives on the first line are ignored. This can change minimization behavior/output for single-line fixtures.
| // Read the input file | |
| const input = fs.readFileSync(inputPath, 'utf-8'); | |
| const filename = path.basename(inputPath); | |
| const firstLine = input.substring(0, input.indexOf('\n')); | |
| const language = parseLanguage(firstLine); | |
| const sourceType = parseSourceType(firstLine); | |
| const newlineIdx = input.indexOf('\n'); | |
| const firstLine = newlineIdx === -1 ? input : input.substring(0, newlineIdx); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/snap/src/runner.ts
Line: 158:164
Comment:
**Single-line input breaks**
`runMinimizeCommand()` computes `firstLine` via `input.substring(0, input.indexOf('\n'))` (runner.ts:161). If the file has no newline, `indexOf` returns `-1` and JS `substring(0, -1)` yields an empty string, so `parseLanguage/parseSourceType` fall back to defaults and any fixture directives on the first line are ignored. This can change minimization behavior/output for single-line fixtures.
```suggestion
const newlineIdx = input.indexOf('\n');
const firstLine = newlineIdx === -1 ? input : input.substring(0, newlineIdx);
```
How can I resolve this? If you propose a fix, please make it concise.|
Upstream PR was closed or merged. Code is synced via branch mirror. |
Mirror of facebook/react#35686
Original author: josephsavona
This is a combination of a) a subagent for investigating compiler errors and b) testing that agent by fixing bugs with for loops within try/catch. My recent diffs to support maybe-throw within value blocks was incomplete and handled many cases, like optionals/logicals/etc within try/catch. However, the handling for for loops was making more assumptions and needed additional fixes.
Key changes:
maybe-throwterminalhandleris now nullable. PruneMaybeThrows nulls the handler for blocks that cannot throw, rather than changing to agoto. This preserves more information, and makes it easier for BuildReactiveFunction's visitValueBlock() to reconstruct the value blocksforinit/test/update (and similar forfor..ofandfor..in) to correctly extract value blocks. The previous logic made assumptions about the shape of the SequenceExpression which were incorrect in some cases within try/catch. The new helper extracts a flattened SequenceExpression.Supporting changes:
compiler/as the working directory, allowing relative paths to work more easily--update(-u) flag toyarn snap minimize, which updates the fixture in place w the minimized version