Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 4, 2025

Backport of #121351 to release/8.0-staging

Customer Impact

Fixes a customer-reported crash in ILLink analyzer in a net8.0 project that uses C# 14 extension members.
Customer issue: #120728

warning AD0001: Analyzer 'ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer' threw an exception of type 'System.InvalidCastException' with message 'Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.NonErrorNamedTypeSymbol' to type 'Microsoft.CodeAnalysis.IMethodSymbol'.'.

Regression

No - crash only happens for language features introduced after .NET 8 was released.

Testing

Tested locally. We can't add automated testing because this repo doesn't reference a new enough version of Roslyn to build tests with extension members.

Risk

Low, analyzer-only fix, with limited scope.


Fixes #120728

main PR #121351

Description

Backports fix for analyzer crash when encountering C# 14 extension members. The crash occurred because the analyzer assumed parameter.ContainingSymbol is always IMethodSymbol, which is false for extension member parameters where the container is an extension type.

Core analyzer changes:

  • Remove MethodParameterValue constructors that cast parameter.ContainingSymbol to IMethodSymbol
  • Require explicit IMethodSymbol parameter in ParameterProxy constructor
  • Skip analysis for extension members in GetParameterTargetValue (return TopValue when ContainingSymbol is not IMethodSymbol)
  • Update VisitInstanceReference to use new ParameterProxy constructor (without unrelated static method checks)

Test infrastructure:

  • Add Microsoft.Net.Compilers.Toolset package reference for C# preview features
  • Add workaround in ResultChecker.cs to skip validation of compiler-generated extension member types (<>E__ prefix)
  • Add ExtensionsDataFlow.cs test (enabled)
  • Add ExtensionMembersDataFlow.cs test (disabled until C# 14 available)

Note: NativeAOT test changes from original PR excluded as ILCompiler.Trimming.Tests doesn't exist in 8.0.

Customer Impact

Without this fix, the Roslyn analyzer crashes when analyzing code that uses or references C# 14 extension members, breaking the build or IDE experience.

Regression

No. This is a new feature compatibility fix for C# 14, not a regression from 8.0 functionality.

Testing

  • All 60 DataFlow analyzer tests pass
  • New ExtensionsDataFlow test validates fix for regular extension methods
  • ILLink.RoslynAnalyzer builds cleanly

Risk

Low. Changes are surgical and isolated to extension member handling:

  • Early-exit paths for extension members prevent processing unsupported constructs
  • Existing code paths unchanged (all parameters with method containers work as before)
  • Test coverage validates no regressions in standard scenarios

Package authoring signed off?

N/A - Changes are internal to analyzer implementation, no public API surface modified.

Original prompt

Backport #121351 to release/8.0-staging.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Dec 4, 2025
Copilot AI changed the title [WIP] Backport runtime PR 121351 to release 8.0 staging [release/8.0-staging] Fix analyzer crash on C# 14 extensions Dec 4, 2025
Copilot AI requested a review from sbomer December 4, 2025 22:17
Copilot AI requested a review from sbomer December 4, 2025 22:39
This branch will not be updated to support using C# 14 in tests.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR backports a fix for an analyzer crash that occurs when the ILLink Roslyn analyzer encounters C# 14 extension members in a .NET 8.0 project. The crash was caused by an unsafe cast assumption that a parameter's ContainingSymbol is always an IMethodSymbol, which is false for extension member parameters where the container is an extension type.

Key Changes:

  • Refactored parameter handling to require explicit IMethodSymbol instead of unsafe casting
  • Added early-exit logic to skip analysis of extension members (returning TopValue)
  • Added test case to validate regular extension method analysis

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/MethodParameterValue.cs Removed constructors that performed unsafe casts of parameter.ContainingSymbol to IMethodSymbol
src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/ParameterProxy.cs Modified constructor to require explicit IMethodSymbol parameter instead of casting internally
src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisVisitor.cs Added guard to check if parameter's containing symbol is a method before analysis; updated instance reference handling to use new constructor
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/ExtensionsDataFlow.cs New test case validating analyzer behavior with extension methods (not C# 14 extension members)
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs Added test method to run the new ExtensionsDataFlow test case

@agocke agocke added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Dec 6, 2025
@rbhanda rbhanda added this to the 8.0.24 milestone Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linkable-framework Issues associated with delivering a linker friendly framework Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants