Skip to content

fix(transformer/explicit-resource-managment): preserve shadowed for-head block#22451

Merged
graphite-app[bot] merged 1 commit into
mainfrom
codex/fix-explicit-resource-managment
May 16, 2026
Merged

fix(transformer/explicit-resource-managment): preserve shadowed for-head block#22451
graphite-app[bot] merged 1 commit into
mainfrom
codex/fix-explicit-resource-managment

Conversation

@camc314

@camc314 camc314 commented May 15, 2026

Copy link
Copy Markdown
Contributor

Given:

for (using x of y) { const x = z; }

Previously we would produce

var _usingCtx2 = require("@oxc-project/runtime/helpers/usingCtx");
for (const _x of y) try {
	var _usingCtx = _usingCtx2();
	const x = _usingCtx.u(_x);
	const x = z;
} catch (_) {
	_usingCtx.e = _;
} finally {
	_usingCtx.d();
}

Now we produce

var _usingCtx2 = require("@oxc-project/runtime/helpers/usingCtx");
for (const _x of y) try {
	var _usingCtx = _usingCtx2();
	const x = _usingCtx.u(_x);
	{
		const x = z;
	}
} catch (_) {
	_usingCtx.e = _;
} finally {
	_usingCtx.d();
}

Which avoids a syntax error - cannot create a duplicate var named x.

@github-actions github-actions Bot added the A-transformer Area - Transformer / Transpiler label May 15, 2026
@camc314 camc314 marked this pull request as ready for review May 15, 2026 09:28
@camc314 camc314 requested a review from overlookmotel as a code owner May 15, 2026 09:28
Copilot AI review requested due to automatic review settings May 15, 2026 09:28
@camc314 camc314 requested a review from Dunqing as a code owner May 15, 2026 09:28
@codspeed-hq

codspeed-hq Bot commented May 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 44 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing codex/fix-explicit-resource-managment (475448f) with main (ddb6eed)2

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (70a1e2a) during the generation of this report, so ddb6eed was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes an Explicit Resource Management transformer scoping bug where a for (using … of …) head binding could end up in the same lexical scope as a shadowing declaration in the loop body, producing invalid output (duplicate bindings).

Changes:

  • Detect when the for loop body scope already binds the same identifier as the using head binding, and wrap the original body in an additional block scope to preserve correct shadowing.
  • Adjust scope insertion / binding moves so the transformed AST and semantic scopes remain consistent for both block and non-block loop bodies.
  • Update Babel conformance snapshots to reflect the newly passing fixture.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tasks/transform_conformance/snapshots/babel.snap.md Snapshot update reflecting one more passing Babel explicit-resource-management case.
crates/oxc_transformer/src/es2026/explicit_resource_management.rs Adds scope-collision detection and wraps loop bodies to preserve correct shadowing and avoid redeclaration errors.

Comment thread crates/oxc_transformer/src/es2026/explicit_resource_management.rs Outdated
@camc314 camc314 self-assigned this May 16, 2026
@camc314 camc314 added the 0-merge Merge with Graphite Merge Queue label May 16, 2026

camc314 commented May 16, 2026

Copy link
Copy Markdown
Contributor Author

Merge activity

…ead block (#22451)

Given:
```
for (using x of y) { const x = z; }
```
Previously we would produce

```
var _usingCtx2 = require("@oxc-project/runtime/helpers/usingCtx");
for (const _x of y) try {
	var _usingCtx = _usingCtx2();
	const x = _usingCtx.u(_x);
	const x = z;
} catch (_) {
	_usingCtx.e = _;
} finally {
	_usingCtx.d();
}
```

Now we produce

```
var _usingCtx2 = require("@oxc-project/runtime/helpers/usingCtx");
for (const _x of y) try {
	var _usingCtx = _usingCtx2();
	const x = _usingCtx.u(_x);
	{
		const x = z;
	}
} catch (_) {
	_usingCtx.e = _;
} finally {
	_usingCtx.d();
}
```

Which avoids a syntax error - cannot create a duplicate var named `x`.
@graphite-app graphite-app Bot force-pushed the codex/fix-explicit-resource-managment branch from 475448f to 3ce3431 Compare May 16, 2026 11:10
@graphite-app graphite-app Bot merged commit 3ce3431 into main May 16, 2026
28 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label May 16, 2026
@graphite-app graphite-app Bot deleted the codex/fix-explicit-resource-managment branch May 16, 2026 11:13
camc314 pushed a commit that referenced this pull request May 18, 2026
### 🐛 Bug Fixes

- 0f26de6 ecmascript: Resolve identifier value type via tracked
constants (#22234) (Alexander Lichter)
- c27a8cf minifier: Normalize `{ x: x }` shorthand so adjacent-if merge
is idempotent (#22401) (Dunqing)
- e431a0e parser: Break extends clause loop on fatal error (#22517)
(Boshen)
- e9ec7c6 minifier: Fold optional chains by base nullishness (#22236)
(Alexander Lichter)
- e6090e7 transformer: Keep enum IIFE when a non-inlinable value
reference remains (#22501) (Dunqing)
- 931b7d6 transformer: Inline const enum members through type-cast
wrappers (#22500) (Dunqing)
- b9615b2 codegen: Preserve string quotes in require() calls during
minification (#22475) (zennnnnnn11)
- c73c159 transformer/async-to-generator: Reparent parameter initializer
scopes (#22507) (camc314)
- ecfd3ca transformer/async-to-generator: Move only parameter bindings
(#22503) (camc314)
- 3ce3431 transformer/explicit-resource-managment: Preserve shadowed
for-head block (#22451) (camc314)

### ⚡ Performance

- ce92c6c semantic: `#[inline]` `Scoping::get_binding` (#22414)
(Dunqing)
- 98be95c regular_expression: Track regex flags via bitflags (#22427)
(Boshen)
- dbbc059 jsdoc: Skip should_attach_jsdoc when no remaining comments
(#22409) (Boshen)
- 217d7d8 minifier: Index `SymbolValues` by `SymbolId` (#22441)
(Dunqing)
- d782b78 minifier: Use BitSet for LiveUsageCollector live references
(#22425) (Boshen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants