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
68 changes: 68 additions & 0 deletions Foxtrot/Tests/Sources/ComplexGeneric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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.

//
// 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;

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<out TIdentity>
{
}

public abstract class DefaultDomainBLLBase<TPersistentDomainObjectBase, TDomainObjectBase, TDomainObject, TIdent>
where TPersistentDomainObjectBase : class, IIdentityObject<TIdent>, TDomainObjectBase
where TDomainObjectBase : class
where TDomainObject : class, TPersistentDomainObjectBase
{
protected TDomainObject GetNested<TNestedDomainObject>(TDomainObject x) where TNestedDomainObject : TDomainObject
{
return default(TDomainObject);
}
}

public abstract class DefaultSecurityDomainBLLBase<TPersistentDomainObjectBase, TDomainObjectBase, TDomainObject, TIdent>
: DefaultDomainBLLBase<TPersistentDomainObjectBase, TDomainObjectBase, TDomainObject, TIdent>
where TPersistentDomainObjectBase : class, IIdentityObject<TIdent>, 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.";
}

}
9 changes: 9 additions & 0 deletions Foxtrot/Tests/TestInputs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
Compiler="CS"
/>

<TestFile
Name="Foxtrot\Tests\Sources\ComplexGeneric.cs"
CompilerOptions=""
FoxtrotOptions=""
References="System.Core.dll"
ContractReferenceAssemblies="false"
BinDir="false"
Compiler="CS"
/>
<TestFile
Name="Foxtrot\Tests\Sources\PostAmbleDeletion.cs"
CompilerOptions=""
Expand Down
43 changes: 26 additions & 17 deletions System.Compiler/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down