Skip to content

Track type qualifier in member access for inherited static members#141

Merged
dfederm merged 1 commit intomainfrom
dfederm/track-inherited-static-member-qualifier
Apr 29, 2026
Merged

Track type qualifier in member access for inherited static members#141
dfederm merged 1 commit intomainfrom
dfederm/track-inherited-static-member-qualifier

Conversation

@dfederm
Copy link
Copy Markdown
Owner

@dfederm dfederm commented Apr 29, 2026

Problem

In the symbol-based analysis path (ReferenceTrimmerUseSymbolAnalysis=true, opt-in via PR #135), inherited static members aren't credited to the derived class's assembly. For code like:

Dep.Derived.InheritedStaticMethod();

…where InheritedStaticMethod is defined on a base class in a different assembly than Derived, the analyzer would track only the base assembly. The derived assembly's ProjectReference was wrongly flagged as removable (RT0002), even though removing it would leave the call unresolved.

Why the original logic missed it

For static invocations and static member references:

  • IInvocationOperation.Instance / IMemberReferenceOperation.Instance are null (no instance receiver).
  • invocation.TargetMethod.ContainingAssembly / memberRef.Member.ContainingAssembly resolve to the defining assembly, which can be a base class in another assembly.
  • The qualifier (Derived) only appears at the syntax level — there's no IOperation node carrying its type.

Fix

Register a syntax action on SimpleMemberAccessExpression that resolves the qualifier expression via SemanticModel.GetSymbolInfo and, when the qualifier resolves to an ITypeSymbol, tracks it via the existing TrackType path.

Tests

Two regression tests added, both verified to fail without the fix:

  • UsedViaInheritedStaticMethodDep.Derived.Foo() where Foo is inherited from Dep.Base in a different assembly.
  • UsedViaInheritedStaticField — same pattern for a static field.

Also added an EmitDependency overload that accepts additional metadata references so the test infra can build a derived assembly that references a base assembly.

All 38 analyzer tests pass (was 36).

Scope

C# only. The default GetUsedAssemblyReferences path is unaffected. The opt-in symbol-analysis path is still experimental.

In the symbol-based analysis path, calls like `Derived.InheritedStaticMethod()` only credited the base class's containing assembly (where the method is defined) and not the derived class's. The qualifier `Derived` is only present at the syntax level — for static invocations and member references, `IInvocationOperation.Instance` / `IMemberReferenceOperation.Instance` are null, and `TargetMethod`/`Member` resolve to the *defining* assembly. Without tracking the qualifier separately, the derived assembly's ProjectReference would be wrongly flagged as removable.

Fix: register a syntax action on `SimpleMemberAccessExpression` that resolves the qualifier expression and tracks it as a type when applicable.

Adds two regression tests (`UsedViaInheritedStaticMethod`, `UsedViaInheritedStaticField`) that fail without this change. Also adds an `EmitDependency` overload accepting additional metadata references so a derived assembly can reference a base assembly in tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dfederm dfederm merged commit ecb5ae5 into main Apr 29, 2026
2 checks passed
@dfederm dfederm deleted the dfederm/track-inherited-static-member-qualifier branch April 29, 2026 14:43
dfederm added a commit that referenced this pull request Apr 30, 2026
In the symbol-based analysis path, member access via an `override` only
credited the override's containing assembly. The C# compiler validates the
entire override chain at compile time, so any assembly that declares an
overridden base member must remain a reference -- removing it produces
CS0012 errors.

Symbol-based RT, on `IPropertyReferenceOperation` /
`IInvocationOperation` / `IEventReferenceOperation`, walked only
`Member.ContainingAssembly` (= override's assembly) and never visited
`Member.OverriddenProperty?.ContainingAssembly` (= base's assembly), so
the consumer's reference to the base assembly was wrongly flagged RT0002
removable.

Fix: add a `TrackOverriddenChain` helper that walks `OverriddenMethod`
/ `OverriddenProperty` / `OverriddenEvent` chains and tracks every
containing assembly. Call it from the `IInvocationOperation` and
`IMemberReferenceOperation` branches. Fields don't have override chains.

Adds five regression tests that all fail without this change:
- `UsedViaOverridePropertyAccess`: the canonical scenario
- `UsedViaOverrideMethodCall`: method variant
- `UsedViaOverrideEventAccess`: event variant
- `UsedViaOverrideMultiLevelChain`: three-level A -> B -> C chain
- `UnrelatedReferenceNotMarkedByOverride`: negative test ensuring the
  fix doesn't over-credit unrelated assemblies

Different code path from #141 (qualifier tracking on inherited static
member access) and #142 (delegate parameter / return types). Surfaced by
a QuickBuild trial of the symbol-based mode.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dfederm added a commit that referenced this pull request Apr 30, 2026
…145)

In the symbol-based analysis path, only the immediate base type's
containing assembly was credited via `TrackType(namedType.BaseType)`.
The C# compiler validates the entire base-type chain plus implemented
interfaces at type-check time -- CS0012 fires when *any* link's defining
assembly is missing -- so any assembly along the chain must remain a
reference. With `<DisableTransitiveProjectReferences>true</...>`, the
chain doesn't flow transitively, so the consumer must explicitly
reference grandparent assemblies.

Symbol-based RT, when handling `INamedTypeSymbol`, walked only the
immediate `BaseType` and direct `Interfaces`, never recursing further
up. So for `Consumer : Provider` where `Provider : ProviderDependency`
in another assembly, the consumer's reference to `ProviderDependency`
was wrongly flagged RT0002 removable.

Fix: extend `TrackType`'s `INamedTypeSymbol` branch to walk the full
`BaseType` chain and `AllInterfaces` collection. `AllInterfaces` is
broader than `Interfaces` (includes transitively-implemented interfaces)
and mirrors what the compiler validates. A `ConcurrentDictionary<ISymbol,
byte>` keyed via `SymbolEqualityComparer.Default` gates the chain walk
to break self-referential cycles such as `int -> AllInterfaces[IComparable<int>]
-> typeArg int -> ...` and to avoid redundant work.

Adds seven regression tests that all fail without this change:
- `UsedViaInheritedBaseType`: the canonical issue #144 scenario
- `UsedViaImplementedInterface`: interface in the chain
- `UsedViaMultiLevelInheritanceChain`: three-level A <- B <- C
- `UsedViaInheritanceChainOnVariableType`: chain via parameter type
- `UsedViaMixedBaseAndInterfaceChain`: base + interface, four asms
- `UsedViaGenericConstraintBaseChain`: `where T : Provider` constraint
- `UnrelatedReferenceNotMarkedByInheritance`: negative test ensuring
  the fix doesn't over-credit unrelated assemblies

Verified against upstream repro https://github.com/olstakh/RT_Gap_Inheritance:
`Consumer` builds with 0 RT warnings (was emitting RT0002 with v3.5.2).

Different code path from #141 (qualifier tracking on inherited static
member access), #142 (delegate parameter / return types), and #143
(override chains). Same family of "symbol traversal misses a chain"
gaps; same reporter (olstakh).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

1 participant