fixed rewriting bug with returned uncontrolled task arrays (#451)

This commit is contained in:
Pantazis Deligiannis 2023-01-26 15:58:58 -08:00 коммит произвёл GitHub
Родитель 787ffb9827
Коммит 380b05e4cf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 58 добавлений и 22 удалений

Просмотреть файл

@ -249,37 +249,49 @@ namespace Microsoft.Coyote.Rewriting
/// <summary>
/// Resolves the generic arguments of the specified type using the given method reference.
/// </summary>
private static GenericInstanceType ResolveGenericTypeArguments(GenericInstanceType genericType,
MethodReference methodReference)
private static GenericInstanceType ResolveGenericTypeArguments(GenericInstanceType genericType, MethodReference methodReference)
{
GenericInstanceType resolvedType = new GenericInstanceType(genericType.ElementType);
foreach (var genericArgument in genericType.GenericArguments)
{
TypeReference argType = genericArgument;
if (argType is GenericParameter gp)
{
if (gp.Type is GenericParameterType.Type &&
methodReference.DeclaringType is GenericInstanceType dgt)
{
argType = dgt.GenericArguments[gp.Position];
}
else if (gp.Type is GenericParameterType.Method &&
methodReference is GenericInstanceMethod gim)
{
argType = gim.GenericArguments[gp.Position];
}
}
else if (argType is GenericInstanceType git)
{
argType = ResolveGenericTypeArguments(git, methodReference);
}
TypeReference argType = ResolveArgumentType(genericArgument, methodReference);
resolvedType.GenericArguments.Add(argType);
}
return resolvedType;
}
/// <summary>
/// Resolves the specified argument type using the given method reference.
/// </summary>
private static TypeReference ResolveArgumentType(TypeReference argType, MethodReference methodReference)
{
if (argType is GenericParameter gp)
{
if (gp.Type is GenericParameterType.Type &&
methodReference.DeclaringType is GenericInstanceType dgt)
{
argType = dgt.GenericArguments[gp.Position];
}
else if (gp.Type is GenericParameterType.Method &&
methodReference is GenericInstanceMethod gim)
{
argType = gim.GenericArguments[gp.Position];
}
}
else if (argType is ArrayType at)
{
TypeReference newElementType = ResolveArgumentType(at.ElementType, methodReference);
argType = new ArrayType(newElementType, at.Rank);
}
else if (argType is GenericInstanceType git)
{
argType = ResolveGenericTypeArguments(git, methodReference);
}
return argType;
}
/// <summary>
/// Checks if the specified type is the expected task type.
/// </summary>

Просмотреть файл

@ -18,6 +18,8 @@ namespace Microsoft.Coyote.Tests.Common.Tasks
public static Task<T> GetGenericTask<T>() => Task.FromResult<T>(default(T));
public static Task<T[]> GetGenericTaskArray<T>() => Task.FromResult<T[]>(default(T[]));
public static Task<(int, bool)> GetValueTupleTask() => Task.FromResult((0, true));
public static Task<(TLeft, TRight)> GetGenericValueTupleTask<TLeft, TRight>() =>
@ -60,6 +62,8 @@ namespace Microsoft.Coyote.Tests.Common.Tasks
public static ValueTask GetTask() => ValueTask.CompletedTask;
public static ValueTask<T> GetGenericTask<T>() => ValueTask.FromResult<T>(default(T));
public static ValueTask<T[]> GetGenericTaskArray<T>() => ValueTask.FromResult<T[]>(default(T[]));
}
/// <summary>

Просмотреть файл

@ -71,6 +71,16 @@ namespace Microsoft.Coyote.Rewriting.Tests
});
}
[Fact(Timeout = 5000)]
public void TestUncontrolledMethodReturnsGenericTaskArray()
{
this.Test(async () =>
{
var task = TaskProvider.GetGenericTaskArray<int>();
await task;
});
}
[Fact(Timeout = 5000)]
public void TestUncontrolledMethodReturnsValueTupleTask()
{
@ -187,6 +197,16 @@ namespace Microsoft.Coyote.Rewriting.Tests
});
}
[Fact(Timeout = 5000)]
public void TestUncontrolledMethodReturnsGenericValueTaskArray()
{
this.Test(async () =>
{
var task = ValueTaskProvider.GetGenericTaskArray<int>();
await task;
});
}
[Fact(Timeout = 5000)]
public void TestUncontrolledMethodReturnsGenericValueTaskFromGenericClass()
{

2
Tests/compare-rewriting-diff-logs.ps1 поставляемый
Просмотреть файл

@ -13,7 +13,7 @@ $targets = [ordered]@{
}
$expected_hashes = [ordered]@{
"rewriting" = "04C2579293B61EEFC8C3D5D6E762D4AC89C006ADDBFBB83E293F2B3411A2CCD8"
"rewriting" = "C2D91562C4FA80D04F687BD7A91D96C9EC76FE9CA4CAC386E809B1F38110ED8F"
"rewriting-helpers" = "273EB27559DFD824CB93F0CDE4F375C5A295DEF90B74A220CD921CD509C5F012"
"testing" = "10C337E0845B525745826FDA103FFBDD67487888203BBA4FBDB05C05DD82C76A"
"actors" = "46EAB0644FB7516C32EC654CA54C239F62B192FEEC84AF0952FAEFEBBCC4AB3B"