Registry Mirror
Skip to content

Conversation

@christophwille

Copy link
Copy Markdown
Member

Summary

ThisResolveResult now inherits from ILVariableResolveResult and carries the this parameter (ILVariable with Kind == Parameter, Index < 0) of the function being decompiled. Every this/base reference the decompiler emits - explicit this.x, base.M(), and the target of an unqualified x - is annotated with that variable.

Analysis

Where ThisResolveResult was created

Site Has an ILVariable?
ExpressionBuilder.TranslateTarget, base branch yes - the target instruction is ldloc this (or box T(ldobj T(ldloc this)) for structs)
ExpressionBuilder.TranslateTarget, explicit this branch (added in 13c6cc6 for #3984) yes - ConvertVariable already annotated the ThisReferenceExpression with an ILVariableResolveResult
CSharpResolver.ResolveThisReference() / ResolveBaseReference() no - the resolver only knows a CSharpTypeResolveContext, not an ILFunction

The resolver-created one mattered in exactly one place: LookInCurrentType used ResolveThisReference() as the lookup target for simple names in the current type, and ExpressionBuilder.ConvertField annotated the resulting MemberResolveResult on an unqualified field access. That is why TransformFieldAndConstructorInitializers.ReferencesInstanceMember saw a ThisResolveResult target for A but (before #3984) not for this.A.

Options considered

  1. Plumb the this variable into CSharpResolver (WithCurrentThisVariable). Rejected: threads an IL concept through the resolver's immutable-copy constructor and every With* method, and still needs a fallback for resolvers created without an ILFunction (TypeSystemAstBuilder, IntroduceUsingDeclarations, IntroduceExtensionMethods, static contexts).
  2. Make the variable nullable on ILVariableResolveResult. Rejected: breaks the non-null contract every consumer relies on.
  3. Stop synthesizing this in the resolver; annotate from the translated target instead. Chosen - see below.

Why the lookup does not need a ThisResolveResult

MemberLookup uses is ThisResolveResult for two things: (a) granting protected access, which IsProtectedAccessAllowed(type) already grants for the current type; (b) rewriting the target of a static member to a TypeResolveResult, which is a no-op when the target already is one. The other consumers of ResolveSimpleName in the decompiler (CallBuilder overload checks) only use the member lists.

One subtlety surfaced by the Pretty suite (114 failures on the first attempt): the lookup target for the current type must stay self-parameterized (C<T> rather than the bare definition), otherwise the members come out unspecialized and SpecializedMember.Equals(..., TypeErasure) never matches the specialized members that IL references inside the type resolve to - which made every unqualified access in a generic type look ambiguous and get a this. qualifier.

Changes

  • Semantics/ThisResolveResult.cs - : ILVariableResolveResult; ctor (ILVariable thisVariable, IType type, bool causesNonVirtualInvocation = false). The Type is the one the reference is spelled with (base -> base type; struct this -> the struct, while the variable itself is by-ref).
  • IL/Instructions/PatternMatching.cs - new MatchLdThis(out ILVariable? variable); the parameterless overload delegates to it.
  • CSharp/ExpressionBuilder.cs
    • TranslateTarget: ShouldUseBaseReference/MatchLdThis return the matched this variable for the base branch; the explicit-this branch takes it from the ILVariableResolveResult ConvertVariable produced.
    • ConvertField: an unqualified field access is annotated with the translated target's resolve result (this/base/type) instead of the resolver's synthesized one. This keeps ReferencesInstanceMember seeing a ThisResolveResult for the unqualified spelling (the Primary-constructor conversion emits this in a field initializer (CS0027) #3984 fixture StructInitializerDependsOnField covers this).
  • CSharp/Resolver/CSharpResolver.cs
    • LookInCurrentType looks names up against TypeResolveResult(self-parameterized current type).
    • ResolveThisReference() / ResolveBaseReference() removed. They are public, but had no callers left in the repo, and nothing can construct a ThisResolveResult without an ILVariable any more. Flagging this as the one public-API removal in case stubs are preferred.
  • ICSharpCode.Decompiler.Tests/Semantics/ThisResolveResultTests.cs (new) - explicit this, unqualified field target, base (base type + CausesNonVirtualInvocation), struct by-ref this. All four fail against the pre-change sources (verified by stashing) and pass now.

Behavior notes

  • Output is unchanged (Pretty suite green).
  • Annotation shape: an unqualified static field access now carries MemberResolveResult(TypeResolveResult(field.DeclaringType), field) (same as the qualified spelling produces) instead of TypeResolveResult(currentType). No in-repo consumer distinguishes these.
  • No ILSpy/ILSpyX code references ThisResolveResult or the removed resolver methods.

Test plan

  • PrettyTestRunner - 1980 passed, 5 skipped
  • all other in-process decompiler tests (excluding Correctness/Roundtrip) - 1362 passed, 9 skipped
  • new ThisResolveResultTests - red before, 4/4 green after
  • ILSpy.csproj builds
  • Correctness / Roundtrip suites not run locally

🤖 Generated with Claude Code

'this' and 'base' both read the 'this' parameter of the function being
decompiled, but their resolve result did not say so: consumers that key on
ILVariableResolveResult (local-reference output, highlighting, hover) could
not connect the keyword to the variable, and the qualified/unqualified
spellings of the same access carried differently shaped annotations.

The resolver has no ILFunction and thus no variable to put into a
ThisResolveResult, so it stops synthesizing one: LookInCurrentType looks
the name up against the (self-parameterized) current type, which grants
the same protected access, and the annotation of an unqualified field
access is built from the translated target instead. ResolveThisReference
and ResolveBaseReference had no callers left and are removed.

Assisted-by: Claude:claude-fable-5:Claude Code
@christophwille
christophwille force-pushed the feature/this-resolve-result-ilvariable branch from 52dc5cf to e475e26 Compare August 15, 2026 11:17
@christophwille
christophwille merged commit 9cdbbb4 into master Aug 15, 2026
15 checks passed
@christophwille
christophwille deleted the feature/this-resolve-result-ilvariable branch August 15, 2026 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants