Model scoped and UnscopedRef lifetimes in the type system#3887
Draft
sailro wants to merge 2 commits into
Draft
Conversation
This was referenced Jul 19, 2026
ScopedRefAttribute only records explicit syntax. Effective lifetime also depends on UnscopedRefAttribute, params collections, out parameters, and the defining module's RefSafetyRules version. Model those distinctions in the type system without changing decompiler output. Assisted-by: Copilot:gpt-5.6-sol:GitHub Copilot CLI Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 86d2918e-5a24-48b4-9a86-41d331ec3720
sailro
force-pushed
the
scoped-ref-type-system
branch
from
July 19, 2026 03:28
284a68f to
0df949c
Compare
siegfriedpammer
marked this pull request as draft
July 19, 2026 04:01
Member
|
@dgrunwald this needs a design/review session |
ScopedKind is now the authoritative lifetime representation, so retaining the preview-era boolean fields would duplicate state. Keep the current ScopedRef compatibility property and group the new metadata attributes with the other C# 11 attributes. Assisted-by: Copilot:gpt-5.6-sol:GitHub Copilot CLI Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 86d2918e-5a24-48b4-9a86-41d331ec3720
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.
Related to #3853. This is the type-system prerequisite requested during review of #3854.
tl;dr: ILSpy currently treats
ScopedRefAttributeas a single boolean and has no semanticmodel for
UnscopedRefAttribute, implicit scopes, or the module's ref-safety rules. This PRaligns the type system with Roslyn without changing decompiler output. A separate follow-up
will use this model to recover erased
scopedlocal modifiers.Problem
LifetimeAnnotation.ScopedRefonly reports whether a parameter hasScopedRefAttribute. That is sufficient to print the explicitscopedkeyword, but not toreason about effective lifetimes:
ScopedRefAttributerepresentsScopedRefon a by-ref parameter andScopedValueon aby-value ref-like parameter.
outis implicitlyScopedRefonly for modules using[module: RefSafetyRules(11)].paramscollections are implicitlyScopedValue, while their compiler-emittedScopedRefAttributeis redundant and must not be printed.UnscopedRefAttributecan remove an implicit scope.ScopedRef;UnscopedRefAttributemay be placed on themethod, property, or accessor.
Without these distinctions, a local-lifetime analysis cannot tell whether an argument may be
captured by a call.
Solution
ScopedKindwithNone,ScopedRef, andScopedValue.LifetimeAnnotationwith the explicit attribute-encoded scope andHasUnscopedRefAttribute, while preserving the currentScopedRefAPI.ScopedKindreplaces the obsolete preview-eraRefScopedandValueScopedfields.System.Diagnostics.CodeAnalysis.UnscopedRefAttributeandSystem.Runtime.CompilerServices.RefSafetyRulesAttribute.ScopedRefAttributeaccording to parameter ref-ness and ref-like type semantics.Conflicting or malformed annotations degrade to no explicit scope.
RefSafetyRules(11)from the module, matching Roslyn's old/new escape-ruleselection.
including method/property/accessor
UnscopedRefAttribute.The display path continues to use only the explicit scope. Implicit
out,this, andref-like
paramsscopes therefore do not introduce redundantscopedkeywords, andUnscopedRefAttributeremains visible as a normal attribute.Tests
scoped refandscopedref-struct parametersout[UnscopedRef] outoutfrom a module withoutRefSafetyRules(11)paramseffective scopeScopedRefoption disabled, including preservation of the raw attributeRefFieldsandParamsCollectionsPretty matrices remain byte-identicalValidation:
Review focus
The main review points are the public
LifetimeAnnotationshape and the exact mapping ofRoslyn's declared versus effective scope rules.