From 6a22330063af006219f112e67b939504cb63b2b2 Mon Sep 17 00:00:00 2001 From: hubuk Date: Tue, 26 May 2015 20:24:21 +0200 Subject: [PATCH 1/3] Fix for generic type parameters used as constraints for another type parameters. Fixes #20 --- System.Compiler/Reader.cs | 43 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/System.Compiler/Reader.cs b/System.Compiler/Reader.cs index 7926a3e6..8c818852 100644 --- a/System.Compiler/Reader.cs +++ b/System.Compiler/Reader.cs @@ -1798,25 +1798,34 @@ private void GetTypeParameterConstraints(int parentIndex, TypeNodeList parameter private TypeNodeList GetTypeParametersFor(int parentIndex, Member parent){ GenericParamRow[] genericParameters = this.tables.GenericParamTable; TypeNodeList types = new TypeNodeList(); - int i = 0, n = genericParameters.Length, j = n-1; - bool sorted = (this.sortedTablesMask >> (int)TableIndices.GenericParam) % 2 == 1; - if (sorted){ - while (i < j){ - int k = (i+j) / 2; - if (genericParameters[k].Owner < parentIndex) - i = k+1; - else - j = k; + TypeNodeList savedTypes = this.currentTypeParameters; + this.currentTypeParameters = types; + try + { + int i = 0, n = genericParameters.Length, j = n-1; + bool sorted = (this.sortedTablesMask >> (int)TableIndices.GenericParam) % 2 == 1; + if (sorted){ + while (i < j){ + int k = (i+j) / 2; + if (genericParameters[k].Owner < parentIndex) + i = k+1; + else + j = k; + } + while (i > 0 && genericParameters[i-1].Owner == parentIndex) i--; } - while (i > 0 && genericParameters[i-1].Owner == parentIndex) i--; + for (int index = 0; i < n; i++, index++) + if (genericParameters[i].Owner == parentIndex) + types.Add(this.GetGenericParameter(i, index, parent)); + else if (sorted) + break; + if (types.Count == 0) return null; + return types; + } + finally + { + this.currentTypeParameters = savedTypes; } - for (int index = 0; i < n; i++, index++) - if (genericParameters[i].Owner == parentIndex) - types.Add(this.GetGenericParameter(i, index, parent)); - else if (sorted) - break; - if (types.Count == 0) return null; - return types; } private TypeNode GetGenericParameter(int index, int parameterListIndex, Member parent){ GenericParamRow[] genericParameters = this.tables.GenericParamTable; From 0140337cdd6359fb57e82ce81a49d932db292bed Mon Sep 17 00:00:00 2001 From: Sergey Tepliakov Date: Wed, 8 Jul 2015 13:23:19 -0700 Subject: [PATCH 2/3] Added test case to cover the change. --- Foxtrot/Tests/Sources/ComplexGeneric.cs | 65 +++++++++++++++++++++++++ Foxtrot/Tests/TestInputs.xml | 9 ++++ 2 files changed, 74 insertions(+) create mode 100644 Foxtrot/Tests/Sources/ComplexGeneric.cs diff --git a/Foxtrot/Tests/Sources/ComplexGeneric.cs b/Foxtrot/Tests/Sources/ComplexGeneric.cs new file mode 100644 index 00000000..afb464b3 --- /dev/null +++ b/Foxtrot/Tests/Sources/ComplexGeneric.cs @@ -0,0 +1,65 @@ +// 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. + +// +// We test here that ?? operators which are compiled to Dup if then pop else .. by C# do not cause +// unwarranted warnings in the extractor. +// + +using System.Diagnostics.Contracts; + +namespace Tests.Sources +{ + + // CCRewriter was unable to read this code from the IL due to an issue in CCI. + // This test just makes sure that the fix is in place. + + public interface IIdentityObject + { + } + + public abstract class DefaultDomainBLLBase + where TPersistentDomainObjectBase : class, IIdentityObject, TDomainObjectBase + where TDomainObjectBase : class + where TDomainObject : class, TPersistentDomainObjectBase + { + protected TDomainObject GetNested(TDomainObject x) where TNestedDomainObject : TDomainObject + { + return default(TDomainObject); + } + } + + public abstract class DefaultSecurityDomainBLLBase + : DefaultDomainBLLBase + where TPersistentDomainObjectBase : class, IIdentityObject, TDomainObjectBase + where TDomainObjectBase : class + where TDomainObject : class, TPersistentDomainObjectBase + { + } + + partial class TestMain + { + partial void Run() + { + if (!this.behave) + { + throw new System.ArgumentNullException(); + } + } + + public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Precondition; + public string NegativeExpectedCondition = "Value cannot be null."; + } + +} diff --git a/Foxtrot/Tests/TestInputs.xml b/Foxtrot/Tests/TestInputs.xml index 9e8eeeea..2eb5c6c7 100644 --- a/Foxtrot/Tests/TestInputs.xml +++ b/Foxtrot/Tests/TestInputs.xml @@ -118,6 +118,15 @@ Compiler="CS" /> + Date: Wed, 8 Jul 2015 17:28:29 -0500 Subject: [PATCH 3/3] Fix comment explaining test --- Foxtrot/Tests/Sources/ComplexGeneric.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Foxtrot/Tests/Sources/ComplexGeneric.cs b/Foxtrot/Tests/Sources/ComplexGeneric.cs index afb464b3..54717cba 100644 --- a/Foxtrot/Tests/Sources/ComplexGeneric.cs +++ b/Foxtrot/Tests/Sources/ComplexGeneric.cs @@ -13,8 +13,11 @@ // 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. // -// We test here that ?? operators which are compiled to Dup if then pop else .. by C# do not cause -// unwarranted warnings in the extractor. +// This is a regression test for Microsoft/CodeContracts#20: +// +// Unable to cast object of type 'System.Compiler.TypeParameter' to type 'System.Compiler.Class'. +// +// https://github.com/Microsoft/CodeContracts/issues/20 // using System.Diagnostics.Contracts;