Skip to content

Commit e9cfb54

Browse files
authored
Fix invalid cast of type parameter (#2643)
1 parent 74d6967 commit e9cfb54

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ public override void Initialize (AnalysisContext context)
172172
for (int i = 0; i < typeParams.Length; i++) {
173173
var typeParam = typeParams[i];
174174
var typeArg = typeArgs[i];
175-
if (!typeParam.HasConstructorConstraint)
175+
if (!typeParam.HasConstructorConstraint ||
176+
typeArg is not INamedTypeSymbol { InstanceConstructors: { } typeArgCtors })
176177
continue;
177178

178-
var typeArgCtors = ((INamedTypeSymbol) typeArg).InstanceConstructors;
179179
foreach (var instanceCtor in typeArgCtors) {
180180
if (instanceCtor.Arity > 0)
181181
continue;

test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresCapability.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -748,9 +748,13 @@ public static void GenericTypeWithStaticMethodViaLdftn ()
748748
var _ = new Action (GenericWithStaticMethod<TestType>.GenericTypeWithStaticMethodWhichRequires);
749749
}
750750

751+
static T MakeNew<T> () where T : new() => new T ();
752+
static T MakeNew2<T> () where T : new() => MakeNew<T> ();
753+
751754
public static void Test ()
752755
{
753756
GenericTypeWithStaticMethodViaLdftn ();
757+
MakeNew2<TestType> ();
754758
}
755759
}
756760

0 commit comments

Comments
 (0)