Skip to content

Commit 65c1f69

Browse files
authored
Fix Crossgen2 variance validation for type constraints (dotnet#132809)
## Summary Crossgen2 applied method generic-constraint variance rules to a generic type's own constraints. ECMA-335 II.9.7 explicitly leaves type-owned constraints unrestricted, so valid variant interfaces could fail automatic validation and omit `READYTORUN_FLAG_SkipTypeValidation`. This change: - removes the invalid variance check for type-owned generic constraints - aligns interface method signature validation with the VM by checking all instance methods and virtual methods, including static virtual and static abstract declarations - adds a ReadyToRun regression test based on the reported constraint pattern Fixes dotnet#132724 > [!NOTE] > This pull request was created with GitHub Copilot.
1 parent f047cd1 commit 65c1f69

3 files changed

Lines changed: 45 additions & 13 deletions

File tree

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/R2RTestSuites.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ static void Validate(ReadyToRunReader reader)
6262
}
6363
}
6464

65+
[Fact]
66+
public void GenericTypeConstraintsAllowVariantParameters()
67+
{
68+
var genericTypeConstraints = new CompiledAssembly
69+
{
70+
AssemblyName = nameof(GenericTypeConstraintsAllowVariantParameters),
71+
SourceResourceNames = ["TypeValidation/GenericTypeConstraints.cs"],
72+
};
73+
74+
new R2RTestRunner(_output).Run(new R2RTestCase(
75+
nameof(GenericTypeConstraintsAllowVariantParameters),
76+
[
77+
new(nameof(GenericTypeConstraintsAllowVariantParameters), [new CrossgenAssembly(genericTypeConstraints)])
78+
{
79+
AdditionalArgs =
80+
{
81+
"--compile-no-methods",
82+
"--type-validation",
83+
"AutomaticWithLogging",
84+
},
85+
Validate = Validate,
86+
},
87+
]));
88+
89+
static void Validate(ReadyToRunReader reader)
90+
{
91+
Assert.True(
92+
(reader.ReadyToRunHeader.Flags & (uint)ReadyToRunFlags.READYTORUN_FLAG_SkipTypeValidation) != 0,
93+
"Expected the ReadyToRun image to skip runtime type validation.");
94+
}
95+
}
96+
6597
[ConditionalFact(typeof(TestPaths), nameof(TestPaths.IsWasmTarget))]
6698
public void WasmWebcilModule()
6799
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
public interface IStrongEnumerator<T>
5+
{
6+
}
7+
8+
public interface IStrongEnumerable<out T, TEnumerator>
9+
where TEnumerator : struct, IStrongEnumerator<T>
10+
{
11+
}

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/TypeValidationChecker.cs‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Task<bool> ValidateTypeWorkerHelper(TypeDesc typeToCheckForSkipValidation)
310310

311311

312312
// Validate that generic variance is properly respected in method signatures
313-
if (type.IsInterface && method.IsVirtual && !method.Signature.IsStatic && type.HasInstantiation)
313+
if (type.IsInterface && type.HasInstantiation && (method.IsVirtual || !method.Signature.IsStatic))
314314
{
315315
if (!ValidateVarianceInSignature(type.Instantiation, method.Signature, Internal.TypeSystem.GenericVariance.Covariant, Internal.TypeSystem.GenericVariance.Contravariant))
316316
{
@@ -341,18 +341,7 @@ Task<bool> ValidateTypeWorkerHelper(TypeDesc typeToCheckForSkipValidation)
341341

342342
// Generic class special rules
343343
// Validate that a generic class cannot be a ComImport class, or a ComEventInterface class -- UNIMPLEMENTED
344-
345-
foreach (var genericParameterType in type.Instantiation)
346-
{
347-
foreach (var constraint in ((GenericParameterDesc)genericParameterType).TypeConstraints)
348-
{
349-
if (!ValidateVarianceInType(type.Instantiation, constraint, Internal.TypeSystem.GenericVariance.Contravariant))
350-
{
351-
AddTypeValidationError(type, $"'{genericParameterType}' has invalid variance in generic parameter constraint {constraint}");
352-
return false;
353-
}
354-
}
355-
}
344+
// A generic type's own parameter constraints have no variance restrictions (ECMA-335 II.9.7).
356345

357346
// Validate that generic variance is properly respected in interface signatures
358347
if (type.HasInstantiation && type.IsInterface)

0 commit comments

Comments
 (0)