-
-
Notifications
You must be signed in to change notification settings - Fork 156
fix(codegen): class-typed nullish field read throws TypeError (#7153) #7430
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,8 @@ | ||
| Fixed a field read through a class-annotated binding holding `undefined`/`null` | ||
| silently answering `undefined` instead of throwing `TypeError` (#7153) — e.g. | ||
| `short[5].id` on a `Row[]` where index 5 is out of bounds, which Node aborts | ||
| with `Cannot read properties of undefined (reading 'id')`. The class-field | ||
| guard diamond's fallback (value-context and raw-f64 number-context lowerings, | ||
| plus the outlined `js_class_field_get_ic`) now mirrors the generic dispatch | ||
| path's nullish-receiver check on the cold fallback arm; the fast path is | ||
| unchanged. Covered by `test_gap_7153_class_typed_nullish_field_read.ts`. |
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
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
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
71 changes: 71 additions & 0 deletions
71
test-files/test_gap_7153_class_typed_nullish_field_read.ts
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,71 @@ | ||
| // #7153: a field read through a CLASS-ANNOTATED binding whose runtime value is | ||
| // nullish must throw TypeError, exactly like Node — not answer `undefined`. | ||
| // | ||
| // The bug: `const missing = short[5]` on a `Row[]` refines the binding to | ||
| // `Named("Row")`, so the read takes the class-field guard diamond | ||
| // (property_get.rs / property_get/helpers.rs). The guard correctly rejects a | ||
| // non-pointer receiver, but the fallback funneled straight into | ||
| // `js_object_get_field_by_name_f64`, which answers `undefined` for any | ||
| // unrecognized receiver — the program kept running on a silent wrong value. | ||
| // An `any`-typed binding took the generic dispatch path, which has thrown | ||
| // correctly since #462. The reads below are DIRECT (no closure wrapper — a | ||
| // capture could reroute the lowering) and every case prints the caught | ||
| // error's name and message so the file is byte-exact against Node. | ||
|
|
||
| class Row { | ||
| id: number; | ||
| name: string; | ||
| score: number; | ||
| constructor(id: number, name: string, score: number) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.score = score; | ||
| } | ||
| } | ||
|
|
||
| const short: Row[] = []; | ||
| short.push(new Row(9, "s", 9)); | ||
|
|
||
| // 1. Value context: the plain class-field guard diamond. | ||
| const missing = short[5]; | ||
| try { | ||
| console.log("oob value:", missing.id); | ||
| } catch (e) { | ||
| console.log("oob value:", (e as Error).constructor.name + ": " + (e as Error).message); | ||
| } | ||
|
|
||
| // 2. Number context: the raw-f64 number-context variant of the same diamond | ||
| // (`missing.score * 2` routes through the ToNumber-flavored lowering). | ||
| try { | ||
| console.log("oob number:", missing.score * 2); | ||
| } catch (e) { | ||
| console.log("oob number:", (e as Error).constructor.name + ": " + (e as Error).message); | ||
| } | ||
|
|
||
| // 3. String-typed field on the same nullish receiver. | ||
| try { | ||
| console.log("oob string field:", missing.name); | ||
| } catch (e) { | ||
| console.log("oob string field:", (e as Error).constructor.name + ": " + (e as Error).message); | ||
| } | ||
|
|
||
| // 4. A null element behind the same class annotation: the message must say | ||
| // "null", not "undefined". | ||
| const holed: Row[] = []; | ||
| holed.push(null as unknown as Row); | ||
| const nullish = holed[0]; | ||
| try { | ||
| console.log("null value:", nullish.id); | ||
| } catch (e) { | ||
| console.log("null value:", (e as Error).constructor.name + ": " + (e as Error).message); | ||
| } | ||
| try { | ||
| console.log("null number:", nullish.score + 1); | ||
| } catch (e) { | ||
| console.log("null number:", (e as Error).constructor.name + ": " + (e as Error).message); | ||
| } | ||
|
|
||
| // 5. Control: an in-bounds element still reads normally through the same | ||
| // lowering (the fix must not disturb the fast path or valid fallbacks). | ||
| const present = short[0]; | ||
| console.log("in bounds:", present.id, present.name, present.score * 2); |
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Keep outlined fallback feedback consistent with inline lowering.
Move
js_typed_feedback_record_fallback_call(site_id)after the nullish check. The inline paths incrates/perry-codegen/src/expr/property_get.rsandcrates/perry-codegen/src/expr/property_get/helpers.rsrecord feedback only when dynamic lookup runs. The outlined path currently records a fallback for a throwing nullish access. This makes feedback state depend onPERRY_FULL_OUTLINE_IC.🤖 Prompt for AI Agents