Added template parameters mapping to contracts and closures dupicator.
This commit is contained in:
Родитель
9a207ef58e
Коммит
d6e47e68ab
|
@ -72,6 +72,21 @@ namespace Microsoft.Contracts.Foxtrot
|
|||
dup.DuplicateFor[sourceMethod.Parameters[i].UniqueKey] = targetMethod.Parameters[i];
|
||||
}
|
||||
}
|
||||
|
||||
// This code makes sure that generic method parameters used by contracts inherited from contract class
|
||||
// are correctly replaced by the one defined in the target method.
|
||||
// Without this mapping <c>CheckPost</c> method in generated async closure class would contain an invalid
|
||||
// reference to a generic contract method parameter instead of generic async closure type parameter.
|
||||
// For more about this problem see comments for Microsoft.Contracts.Foxtrot.EmitAsyncClosure.GenericTypeMapper class
|
||||
// and issue #380.
|
||||
if (sourceMethod.TemplateParameters != null && targetMethod.TemplateParameters != null
|
||||
&& sourceMethod.TemplateParameters.Count == targetMethod.TemplateParameters.Count)
|
||||
{
|
||||
for (int i = 0, n = sourceMethod.TemplateParameters.Count; i < n; i++)
|
||||
{
|
||||
dup.DuplicateFor[sourceMethod.TemplateParameters[i].UniqueKey] = targetMethod.TemplateParameters[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var originalType = HelperMethods.IsContractTypeForSomeOtherType(sourceMethod.DeclaringType, contractNodes);
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
// CodeContracts
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Threading;
|
||||
|
||||
namespace Tests.Sources
|
||||
{
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[ContractClass(typeof(ITestInterfaceContract))]
|
||||
public interface ITestInterface
|
||||
{
|
||||
Task<T> GetAsync<T>(T value) where T : class;
|
||||
}
|
||||
|
||||
[ContractClassFor(typeof(ITestInterface))]
|
||||
abstract class ITestInterfaceContract : ITestInterface
|
||||
{
|
||||
public Task<T> GetAsync<T>(T value) where T : class
|
||||
{
|
||||
Contract.Ensures(Contract.Result<T>() != null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class TestInterface : ITestInterface
|
||||
{
|
||||
public async Task<T> GetAsync<T>(T value) where T : class
|
||||
{
|
||||
await Task.Delay(100);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
partial class TestMain
|
||||
{
|
||||
partial void Run()
|
||||
{
|
||||
if (behave)
|
||||
{
|
||||
// Should not fail!
|
||||
new TestInterface().GetAsync<object>(new object()).Wait();
|
||||
}
|
||||
else
|
||||
{
|
||||
// This one should fail with postcondition violation
|
||||
new TestInterface().GetAsync<object>(null).Wait();
|
||||
}
|
||||
}
|
||||
|
||||
public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Postcondition;
|
||||
public string NegativeExpectedCondition = "Contract.Result<T>() != null";
|
||||
}
|
||||
}
|
|
@ -65,6 +65,7 @@ namespace Tests
|
|||
|
||||
@"Foxtrot\Tests\AsyncPostconditions\CancelledAsyncPostcondition.cs",
|
||||
@"Foxtrot\Tests\AsyncPostconditions\AsyncPostconditionsInInterface.cs",
|
||||
@"Foxtrot\Tests\AsyncPostconditions\AsyncPostconditionsInGenericMethodInInterface.cs",
|
||||
@"Foxtrot\Tests\AsyncPostconditions\MixedAsyncPostcondition.cs",
|
||||
@"Foxtrot\Tests\AsyncPostconditions\NormalAsyncPostcondition.cs",
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче