Skip to content

Commit

Permalink
[release/8.0] Fix type parameter mapping logic in ILLink/ILCompiler (#…
Browse files Browse the repository at this point in the history
…92139)

* Fix type parameter mapping bug in illink

* Same in ILCompiler

* Check dataflow of mapped type parameter

* Fix test for analyzer

---------

Co-authored-by: Sven Boemer <sbomer@gmail.com>
  • Loading branch information
github-actions[bot] and sbomer authored Sep 16, 2023
1 parent c7252a3 commit 0175113
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ referencedMethod.OwningType is MetadataType generatedType &&
break;

case ILOpcode.stsfld:
case ILOpcode.ldsfld:
{
// Same as above, but stsfld instead of a call to the constructor
// Ldsfld may also trigger a cctor that creates a closure environment
FieldDesc? field = methodBody.GetObject(reader.ReadILToken()) as FieldDesc;
if (field == null)
continue;
Expand Down Expand Up @@ -417,6 +419,7 @@ void MapGeneratedTypeTypeParameters(
break;

case ILOpcode.stsfld:
case ILOpcode.ldsfld:
{
if (body.GetObject(reader.ReadILToken()) is FieldDesc { OwningType: MetadataType owningType }
&& compilerGeneratedType == owningType.GetTypeDefinition())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ referencedMethod.DeclaringType is var generatedType &&

case OperandType.InlineField: {
// Same as above, but stsfld instead of a call to the constructor
if (instruction.OpCode.Code is not Code.Stsfld)
// Ldsfld may also trigger a cctor that creates a closure environment
if (instruction.OpCode.Code is not (Code.Stsfld or Code.Ldsfld))
continue;

FieldDefinition? field = _context.TryResolve ((FieldReference) instruction.Operand);
Expand Down Expand Up @@ -390,7 +391,8 @@ static void MapGeneratedTypeTypeParameters (
handled = true;
}
break;
case Code.Stsfld: {
case Code.Stsfld:
case Code.Ldsfld: {
if (instr.Operand is FieldReference { DeclaringType: GenericInstanceType typeRef }
&& compilerGeneratedType == context.TryResolve (typeRef)) {
return typeRef;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void Main ()
CapturingLocalFunctionInsideIterator<int> ();
LambdaInsideAsync<int> ();
LocalFunctionInsideAsync<int> ();
NestedStaticLambda.Test<int> ();
}

private static void UseIterator ()
Expand Down Expand Up @@ -352,5 +353,21 @@ void LocalFunction ()
await Task.Delay (0);
LocalFunction ();
}

class NestedStaticLambda
{
public static class Container<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> {
public static Func<Func<T, T>, Func<T, T>> NestedLambda =
(Func<T, T> x) => v => x(v);
}

[ExpectedWarning ("IL2091", "T", nameof (Container<T>), nameof (DynamicallyAccessedMemberTypes.PublicMethods),
// https://github.com/dotnet/runtime/issues/84918
ProducedBy = Tool.Trimmer | Tool.NativeAot)]
public static void Test<T> ()
{
Container<T>.NestedLambda ((T t) => t) (default (T));
}
}
}
}

0 comments on commit 0175113

Please sign in to comment.