Skip to content

Don't use whole program analysis results when array covariance is involved #117273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MichalStrehovsky
Copy link
Member

Array covariance makes things difficult to model, so just abort. We already had some abort logic but it wasn't sufficient and it wasn't in all the places where it was necessary.

Fixes #117249.

Cc @dotnet/ilc-contrib

@Copilot Copilot AI review requested due to automatic review settings July 3, 2025 14:04
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot 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 adds logic to abort whole-program analysis when array covariance is involved and includes a regression test for bug #117249.

  • Introduces IsArrayVariantCastable and related checks in compiler components to detect array variance scenarios.
  • Updates RyuJIT interface, IL importer, scanner, substituted IL provider, and dependency analysis nodes to respect array variance abort logic.
  • Adds a new regression test RegressionBug117249 to Devirtualization.cs to verify correct handling.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/tests/nativeaot/SmokeTests/UnitTests/Devirtualization.cs Add regression test for array covariance abort (RegressionBug117249)
src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs Abort devirtualization when array covariance applies
src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs Add array variance check in branch import logic
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/SubstitutedILProvider.cs Add array variance check before expanding isinst
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ILScanner.cs Pass TypeSystemContext to devirtualization manager
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/VariantInterfaceMethodUseNode.cs Correct reference to ArrayOfTEnumeratorType via context
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.cs Remove outdated ArrayOfTEnumeratorType cache
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/GenericDefinitionEETypeNode.cs Update variance-flag logic to use new context property
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs Update variance-flag logic to use new context property
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/CompilerTypeSystemContext.Aot.cs Add ArrayOfTEnumeratorType, IsArrayVariantCastable, and IsGenericArrayEnumeratorInterfaceType
Comments suppressed due to low confidence (3)

src/tests/nativeaot/SmokeTests/UnitTests/Devirtualization.cs:94

  • C# does not allow forward declarations of enums; you need to define the enum body (e.g., enum IntEnum1 { Member = 0 }) or provide an empty body enum IntEnum1 { }.
        enum IntEnum1;

src/tests/nativeaot/SmokeTests/UnitTests/Devirtualization.cs:126

  • Casting an int[] to IEnumerable<IntEnum1> will throw at runtime since variance does not apply to value types. Consider using an array of the enum type (e.g., new IntEnum1[] { ... }) or switch to a reference type to exercise covariance.
                foreach (var v in (IEnumerable<IntEnum1>)GetIntInstance())

src/tests/nativeaot/SmokeTests/UnitTests/Devirtualization.cs:147

  • Casting an int[] to IEnumerable<IntEnum3> will throw at runtime since variance does not apply to value types. Adjust the test to use an enum array or a reference-type scenario for valid covariance behavior.
                foreach (var v in (IEnumerable<IntEnum3>)GetIntInstance())

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum.GetValuesAsUnderlyingType<T> with LINQ freezes on NativeAOT
2 participants