Repository navigation
rc: a ternary branch yielding a fresh object widened to a union hands its reference out (#465) - #466
Merged
Merged
Conversation
… its reference out (#465) `cond ? new C() : null` freed the new object under rc. The then-branch yields `new C()` cast to `C | null`; OwnedReturnConsumptionPass looked for a branch that hands an unclaimed reference out by asking about the yielded value itself, which was the cast, not the `new`. Seeing no branch that carries one, it left the ts.If alone, and the `new`'s reference was released as a discarded temporary at the end of its branch, before the merge: the result pointed at freed memory (and was released a second time by its receiver). The branch's yield is now seen through the casts that only widen it (to a union or an optional, OptionalValue, CreateUnionInstance): the producer's reference goes out with the ts.If result, as it already did when a branch yielded the `new` itself. A cast that makes a new value (a box to `any`) carries its own reference and is not looked through. Test: 00ternary_new_or_null (compile and JIT, and in the rc/none corpus): a local and a returned `cond ? new C() : null`, an optional `cond ? n : undefined`, and a loop, each read after allocations of the same size have reused anything freed. It failed under rc before the change. The own corpus is unchanged (453 / 593 plain, 451 / 593 with --opt). Fixes #465. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…null # Conflicts: # tslang/test/tester/CMakeLists.txt
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.
Fixes #465.
Under rc,
cond ? new C() : nullfreed the new object. The then-branch yieldsnew C()cast toC | null.OwnedReturnConsumptionPassdecides whether a branch hands an unclaimed reference out by looking at the yielded value itself, and here that value was the cast, not thenew. So it saw no branch carrying a reference and left thets.Ifalone. Thenew's reference was then released as a discarded temporary at the end of its branch, before the merge. The result pointed at freed memory, and its receiver released it a second time.The pass now looks through the casts that only widen a branch's yield: a cast to a union or an optional,
OptionalValue, andCreateUnionInstance. The producer's reference goes out with thets.Ifresult, as it already did when a branch yielded thenewitself. A cast that makes a new value, such as a box toany, carries its own reference and is not looked through.Tests
00ternary_new_or_null.ts: registered as compile and JIT tests and in the rc/none corpus. It failed under rc before the change. Each case reads the result after allocations of the same size have reused anything freed:cond ? new C() : null;cond ? n : undefined;5under rc, where before they printed0or crashed.--opt.test-compile-gc-defaultlib-collector, the known local LNK2005 against the stale installed default library; it fails on main the same way.🤖 Generated with Claude Code