Fix double Dispose in use bindings with as patterns (#12300)#19858
Open
T-Gro wants to merge 5 commits into
Open
Fix double Dispose in use bindings with as patterns (#12300)#19858T-Gro wants to merge 5 commits into
T-Gro wants to merge 5 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Emit exactly one Dispose call per `use` binding, regardless of how many names the pattern introduces, by cleaning up only `patternInputTmp`. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…2300) Track stamps of use-bound locals on TcEnv and skip cleanup emission in TcLetBinding when the binding's right-hand side is a reference to one of them. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
❗ Release notes required
|
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Fixes #12300 —
use a as b = d(anduse b = awhereais alreadyuse-bound) previously generated multipleDisposecalls for the same underlying object.Root Cause
The compiler emitted one
Disposeper name introduced by ausepattern. Withaspatterns (use a as b = d) or rebinding (use b = a), each alias triggered its own cleanup, causing the sameIDisposableto be disposed multiple times.Fix
Two scenarios are addressed in
CheckExpressions.fs:Scenario A —
use a as b = d: Emit exactly oneDisposecall on the canonicalpatternInputTmpvalue (the initial tmp that holds the bound expression) regardless of how many names theaspattern introduces.Scenario B —
use b = awhereais use-bound: Detect when the RHS of ausebinding is a simple reference to an already use-bound value and skip the redundant cleanup. The outerusealready owns the Dispose.The detection for Scenario B uses a new
eUseBoundValStampsset on the type-checking environment to track which values are knownuse-bound.Tests
Added
UseBindingDisposalTests.fswith 6 tests covering:use a as b = ddisposes onceuse b = a(rebind of use-bound) disposes onceusestill disposesuse _ = dstill disposesusebindings each disposeuse a as b as c = ddisposes once