Repository navigation
Read unassigned class fields as undefined during construction - #803
Merged
Merged
Conversation
Class-typed fields that construction may read before assigning them now observe undefined instead of reading an empty slot. A small analysis walks field initializers and the constructor in evaluation order and stops at the first point that can hand the instance to other code (a method call, an argument, a callback, a base constructor that does); private helpers that only assign fields are followed. Fields not assigned by then take the deferred-init representation: - the slot starts empty and reads check it inline, so assigned reads stay borrowed and only an empty slot reaches the throw; - equality, `typeof`, truthiness, `== null`, optional chaining, `as` casts, undefined-admitting destinations and console output observe the undefined itself; - member reads, method calls, updates and writes through an unassigned field throw Node's TypeErrors in Node's evaluation order. Fields assigned before any exposure keep their plain representation. Definite-assignment-asserted fields assigned through such private helpers no longer need the deferred form either. Corpus 4471 pins inheritance, callbacks, initializer order, private helpers and member updates; unit tests cover the analysis and the emitted checks.
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Class-typed fields that construction may read before assigning them now read as
undefined, as in Node, instead of an empty slot.construction-escapes.ts): walks field initializers and the constructor in evaluation order, and stops at the first point that can hand the instance to other code: a method call, an argument, a callback, or a base constructor that exposesthis. Private helpers that only assign fields are followed. Fields assigned before that point keep their plain representation, and only unproven fields take the deferred form.typeof, truthiness,== null, optional chaining,ascasts, destinations that admitundefined, and console output all seeundefineditself. Member reads, method calls, updates and writes through an unassigned field throw Node's TypeErrors in Node's evaluation order.!) that private helpers assign no longer need the deferred form.4471-unassigned-field-reads.tscovers inheritance, callbacks, initializer order, private helpers and member updates. Unit tests cover the analysis and check that the emitted check is inline, with no union box.Validation: full plain corpus 2172/2172; filtered sanitized lane 448/448; package, diagnostics, coverage, surface-manifest and runtime-tree-shaking tests pass; Effect 125/125, effect4 180/180, npm-static and library-mode pass apart from known environment-only cases; self-hosting suites 54/54, and the compiler compiles itself; TypeScript preflight/order baselines complete; lint and format clean.