Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 287 additions & 19 deletions Foxtrot/Driver/Rewriting/EmitAsyncClosure.cs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Foxtrot/Driver/Rewriting/Utils/TypeNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ public static int CountOrDefault(this TypeNodeList list)
{
return list == null ? 0 : list.Count;
}

public static bool IsNullOrEmpty(this TypeNodeList list)
{
return list == null || list.Count == 0;
}
}
}
19 changes: 18 additions & 1 deletion Foxtrot/Foxtrot/Extraction/AsyncReturnValueQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ public override void VisitMethodCall(MethodCall call)
}
}

// For async methods calledMethod (Contract.Result) would be called in the generated generic state machine,
// and calledMethod.ReturnType would be generic type argument and actualResultType can be generic method argument
// (if async postcondition declared in the generic method from non-generic class).
// In this case calledMethod.ReturnType would be != this.actualResultType
// and different comparison logic should be used (reflected in EquivalentGenercTypes method).
if (this.actualResultType != null && contractNodes.IsResultMethod(template) &&
calledMethod.ReturnType == this.actualResultType)
(calledMethod.ReturnType == this.actualResultType || EquivalentGenericTypes(calledMethod, actualResultType)))
{
// using Contract.Result<T>() in a Task<T> returning method, this is a shorthand for
// Contract.Result<Task<T>>().Result
Expand All @@ -87,5 +92,17 @@ public override void VisitMethodCall(MethodCall call)

base.VisitMethodCall(call);
}

private static bool EquivalentGenericTypes(Method calledMethod, TypeNode actualResultType)
{
if (calledMethod.IsGeneric && actualResultType.IsTemplateParameter)
{
// Relatively naive implementation for equality, but still correct
// and should not lead to false positives.
return calledMethod.ReturnType.FullName == actualResultType.FullName;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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;

class FooClass<T> where T: class
{
public static Task<T> Foo(T source)
{
Contract.Ensures(Contract.Result<T>() != null);

return Task.FromResult(source);
}
}

partial class TestMain
{
partial void Run()
{
if (behave)
{
FooClass<object>.Foo(new object()).Wait();
}
else
{
FooClass<object>.Foo(null).Wait();
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Postcondition;
public string NegativeExpectedCondition = "Contract.Result<T>() != null";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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;

class FooClass<T, U> where U: class
{
public static Task<U> Foo(U source)
{
Contract.Ensures(Contract.Result<U>() != null);

return Task.FromResult(source);
}
}

partial class TestMain
{
partial void Run()
{
if (behave)
{
FooClass<string, object>.Foo(new object()).Wait();
}
else
{
FooClass<string, object>.Foo(null).Wait();
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Postcondition;
public string NegativeExpectedCondition = "Contract.Result<U>() != null";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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;

public class Outer<T> {
public class Inner {
public static Task<T> OuterClassTypeArgument(T obj) {
Contract.Ensures(Contract.Result<T>() != null);
return Task.FromResult(obj);
}
}
}

partial class TestMain
{
partial void Run()
{
if (behave)
{
Outer<string>.Inner.OuterClassTypeArgument("foo").Wait();
}
else
{
Outer<string>.Inner.OuterClassTypeArgument(null).Wait();
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Postcondition;
public string NegativeExpectedCondition = "Contract.Result<T>() != null";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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;

class FooClass
{
public static Task<T> Foo<T>(T source) where T : class
{
Contract.Ensures(Contract.Result<T>() != null);

return Task.FromResult(source);
}
}

partial class TestMain
{
partial void Run()
{
if (behave)
{
FooClass.Foo(new object()).Wait();
}
else
{
FooClass.Foo<object>(null).Wait();
// throw new ArgumentNullException();
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Postcondition;
public string NegativeExpectedCondition = "Contract.Result<T>() != null";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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;

class FooClass
{
public static Task<U> Foo<T, U>(U source) where U : class
{
Contract.Ensures(Contract.Result<U>() != null);

return Task.FromResult(source);
}
}

partial class TestMain
{
partial void Run()
{
if (behave)
{
FooClass.Foo<string, object>(new object()).Wait();
}
else
{
FooClass.Foo<string, object>(null).Wait();
// throw new ArgumentNullException();
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Postcondition;
public string NegativeExpectedCondition = "Contract.Result<U>() != null";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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;

class FooClass
{
public Task<U> Foo<T, U>(U source) where U : class where T: U
{
Contract.Ensures(Contract.Result<U>() != null);
Contract.Ensures((T)Contract.Result<U>() != null);

return Task.FromResult(source);
}
}

partial class TestMain
{
partial void Run()
{
if (behave)
{
new FooClass().Foo<string, object>("foo").Wait();
}
else
{
new FooClass().Foo<string, object>(null).Wait();
// throw new ArgumentNullException();
}
}

public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Postcondition;
public string NegativeExpectedCondition = "Contract.Result<U>() != null";
}
}
Loading