From d79dec35c980c5ccfab7de0313bdbf41bc681ff9 Mon Sep 17 00:00:00 2001 From: Sergey Teplyakov Date: Thu, 11 Sep 2014 08:38:33 -0700 Subject: [PATCH] #43. Fixed a bug that shows an error for inconsistent postcondition with tasks. --- .../Validation/PostconditionValidator.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ContractExtensions/ContractsEx/Validation/PostconditionValidator.cs b/ContractExtensions/ContractsEx/Validation/PostconditionValidator.cs index e55104d..16818bc 100644 --- a/ContractExtensions/ContractsEx/Validation/PostconditionValidator.cs +++ b/ContractExtensions/ContractsEx/Validation/PostconditionValidator.cs @@ -69,9 +69,8 @@ private static string ErrorForIncompatibleEnsuresAndReturnType(IType methodResul name = name.Replace("get_", ""); } - //string methodOrProperty = method. - return string.Format("Detected a call to Contract.Result<{0}>() in {1} '{2}' with return type '{3}'", - contractResult.GetClrName().ShortName, kind, name, + return string.Format("Detected a call to Result with '{0}' in {1} '{2}', should be '{3}'", + contractResult.GetPresentableName(CSharpLanguage.Instance), kind, name, methodResult.GetPresentableName(CSharpLanguage.Instance)); } @@ -84,8 +83,15 @@ private static bool MethodResultIsCompatibleWith(IType methodResult, IList { - // Result compatibility should consider tasks as an - var resultType = methodResult.IsGenericTask() ? methodResult.GetTaskUnderlyingType() : methodResult; + var resultType = methodResult; + var contractResultType = contractResult; + + // Corner case: we can use Contract.Result() for method that returns Task! + if (resultType.IsGenericTask() && !contractResultType.IsGenericTask()) + { + resultType = resultType.GetTaskUnderlyingType(); + } + Contract.Assert(resultType != null); return resultType.IsImplicitlyConvertibleTo(contractResult, rule);