Fix #3008: local function forwarding a display class loses its scope - #4100
Merged
Merged
Conversation
Roslyn passes a display class into a local function by ref, and a local function that only forwards that parameter to a sibling has no closure variable of its own. The closure analysis therefore found nothing to anchor it and fell back to the root method body, which put it out of reach of the callees it forwards to; CallBuilder then hit the assert guarding a local function reference it cannot resolve and emitted the raw metadata name of the target instead. The constructor path also mixed use-site containers into a scope the closure analysis had already determined; when the use-sites live in separate function bodies there is no common container, and resetting to the constructor body threw that scope away. Assisted-by: Claude:claude-opus-5:Claude Code
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 #3008.
Decompiling
Microsoft.CodeAnalysis.CSharp.Binder.CreateConversionasserted atDebug.Assert(localFunction != null)inCallBuilder, and the Release build emitted uncompilable code - calls written as raw metadata names,<CreateConversion>g__reportUseSiteDiagnostics|253_1(...).DetermineCaptureAndDeclarationScopederives a local function's declaration scope from the display-class variable at its use sites. Roslyn passes display classes into local functions asrefparameters, andGetClosureInitializerreturns null for a parameter, so a local function used only from inside another local function never resolved a scope. It fell through to the blanket root-body fallback and was hoisted out of the function whose closure its callees read, leaving those callees out of scope. A closure parameter now resolves to the variable that actually holds the display class, so all three end up siblings in the same local function, matching the source layout.The constructor branch had a second, smaller instance of the same problem: use sites in separate function bodies share no
BlockContainer, soFindCommonAncestorInstructionreturned null and a valid closure-derived scope was discarded. The use-site scope is now computed separately and the closure-derived one kept when there is none.Two points for review:
TryValidateSkipCount's assert onGeneric<T1>.MixedLocalFunction, which assumes generic local functions are declared at the root. Worth knowing if anyone revisits this area.Pretty fixture
Issue3008_DisplayClassForwardedThroughLocalFunctioncovers the shape. A whole-assembly Debug decompile of the reporter's assembly no longer asserts.