Skip to content

Commit

Permalink
Fix cancellation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed May 5, 2024
1 parent 6f5831d commit b604fb3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void CompareInheritanceTest(Type x, Type y, int expected)
Assert.NotNull(symbolX);
Assert.NotNull(symbolY);

var symbolResult = Symbols.CompareInheritance(compilation, symbolX, symbolY, CancellationToken.None);
var symbolResult = Symbols.CompareInheritance(compilation, symbolX, symbolY);
var memberResult = reflectionFunction.Invoke(x, y);
Assert.Equal(expected, symbolResult);
Assert.Equal(expected, memberResult);
Expand Down Expand Up @@ -184,7 +184,7 @@ public void CompareInheritanceWithInvalidTypeTest(Type x, Type y, string message
Assert.NotNull(symbolX);
Assert.NotNull(symbolY);

var alpha = Assert.Throws<ArgumentException>(() => Symbols.CompareInheritance(compilation, symbolX, symbolY, CancellationToken.None));
var alpha = Assert.Throws<ArgumentException>(() => Symbols.CompareInheritance(compilation, symbolX, symbolY));
var bravo = Assert.Throws<ArgumentException>(() => reflectionFunction.Invoke(x, y));
Assert.Null(alpha.ParamName);
Assert.Null(bravo.ParamName);
Expand Down Expand Up @@ -236,19 +236,4 @@ public void GetAllFieldsAndPropertiesForNonInterfaceTypeWithInvalidTypeTest(Type
Assert.Equal(message, alpha.Message);
Assert.Equal(message, bravo.Message);
}

[Fact(DisplayName = "Compare Inheritance With Cancellation Token Test")]
public void CompareInheritanceCancellationTest()
{
var source = new CancellationTokenSource();
var compilation = CompilationModule.CreateCompilationFromThisAssembly();
var symbolString = compilation.GetSpecialType(SpecialType.System_String);
var symbolObject = compilation.GetSpecialType(SpecialType.System_Object);
var result = Symbols.CompareInheritance(compilation, symbolObject, symbolString, source.Token);
Assert.Equal(1, result);

source.Cancel();
var exception = Assert.Throws<OperationCanceledException>(() => Symbols.CompareInheritance(compilation, symbolObject, symbolString, source.Token));
Assert.Equal(source.Token, exception.CancellationToken);
}
}
6 changes: 3 additions & 3 deletions code/Binary.SourceGeneration/Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ public static ImmutableArray<ISymbol> FilterFieldsAndProperties(ImmutableArray<I
return builder.ToImmutable();
}

public static int CompareInheritance(Compilation compilation, ITypeSymbol x, ITypeSymbol y, CancellationToken cancellation)
public static int CompareInheritance(Compilation compilation, ITypeSymbol x, ITypeSymbol y)
{
if (x.IsValueType || y.IsValueType)
throw new ArgumentException("Require reference type.");
cancellation.ThrowIfCancellationRequested();
var alpha = compilation.ClassifyCommonConversion(x, y);
var bravo = compilation.ClassifyCommonConversion(y, x);
if (alpha.IsIdentity || bravo.IsIdentity)
Expand Down Expand Up @@ -243,11 +242,12 @@ void Insert(IPropertySymbol member)
{
foreach (var i in values)
{
var signal = CompareInheritance(compilation, i.ContainingType, member.ContainingType, cancellation);
var signal = CompareInheritance(compilation, i.ContainingType, member.ContainingType);
if (signal is 0)
same.Add(i);
else if (signal is -1)
less.Add(i);
cancellation.ThrowIfCancellationRequested();
}
}

Expand Down

0 comments on commit b604fb3

Please sign in to comment.