Skip to content

Commit

Permalink
Merge branch 'master' into pattern-matching
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer authored Oct 4, 2021
2 parents 96db0a5 + 105cdfd commit 6290e2f
Show file tree
Hide file tree
Showing 77 changed files with 1,291 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<PackageReference Include="Microsoft.DiaSymReader.Converter.Xml" Version="$(DSRConverterXmlVersion)" />
<PackageReference Include="NUnit3TestAdapter" Version="$(NUnitAdapterVersion)" />
<PackageReference Include="NUnit" Version="$(NUnitVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="Mono.Cecil" Version="$(MonoCecilVersion)" />
<PackageReference Include="Microsoft.NETCore.ILAsm" Version="$(ILAsmVersion)" />
Expand Down Expand Up @@ -108,6 +109,7 @@
<Compile Include="TestCases\Correctness\DynamicTests.cs" />
<Compile Include="TestCases\Correctness\StringConcat.cs" />
<Compile Include="TestCases\Pretty\PatternMatching.cs" />
<None Include="TestCases\Pretty\CovariantReturns.cs" />
<Compile Include="TestCases\VBPretty\VBPropertiesTest.cs" />
<None Include="TestCases\ILPretty\Issue2260SwitchString.cs" />
<None Include="TestCases\Pretty\Records.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ public void CS9_ExtensionGetEnumerator([ValueSource(nameof(dotnetCoreOnlyOptions
RunForLibrary(cscOptions: cscOptions | CompilerOptions.Preview);
}

[Test]
public void CovariantReturns([ValueSource(nameof(dotnetCoreOnlyOptions))] CompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions | CompilerOptions.Preview);
}

void RunForLibrary([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None, DecompilerSettings decompilerSettings = null)
{
Run(testName, asmOptions | AssemblerOptions.Library, cscOptions | CompilerOptions.Library, decompilerSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static void Main()
Issue1747();
CallAmbiguousOutParam();
CallWithInParam();
Issue2444.M2();
}

#region ConstructorTest
Expand Down Expand Up @@ -334,6 +335,38 @@ static void InVsRegularParam(int i)
}
#endif
#endregion

#region #2444
public struct Issue2444
{
public class X { }
public class Y { }

public static implicit operator Issue2444(X x)
{
Console.WriteLine("#2444: op_Implicit(X)");
return new Issue2444();
}

public static implicit operator Issue2444(Y y)
{
Console.WriteLine("#2444: op_Implicit(Y)");
return new Issue2444();
}

public static void M1(Issue2444 z)
{
Console.WriteLine(string.Format("#2444: M1({0})", z));
}

public static void M2()
{
Console.WriteLine("#2444: before M1");
M1((X)null);
Console.WriteLine("#2444: after M1");
}
}
#endregion
}

class IndexerTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<symbols>
<files>
<file id="1" name="ICSharpCode.Decompiler.Tests.TestCases.PdbGen\ForLoopTests.cs" language="C#" checksumAlgorithm="SHA256"><![CDATA[using System;
<file id="1" name="ICSharpCode\Decompiler\Tests\TestCases\PdbGen\ForLoopTests.cs" language="C#" checksumAlgorithm="SHA256"><![CDATA[using System;
namespace ICSharpCode.Decompiler.Tests.TestCases.PdbGen
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<symbols>
<files>
<file id="1" name="ICSharpCode.Decompiler.Tests.TestCases.PdbGen\HelloWorld.cs" language="C#" checksumAlgorithm="SHA256"><![CDATA[using System;
<file id="1" name="ICSharpCode\Decompiler\Tests\TestCases\PdbGen\HelloWorld.cs" language="C#" checksumAlgorithm="SHA256"><![CDATA[using System;
namespace ICSharpCode.Decompiler.Tests.TestCases.PdbGen
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<symbols>
<files>
<file id="1" name="ICSharpCode.Decompiler.Tests.TestCases.PdbGen\LambdaCapturing.cs" language="C#" checksumAlgorithm="SHA256"><![CDATA[using System;
<file id="1" name="ICSharpCode\Decompiler\Tests\TestCases\PdbGen\LambdaCapturing.cs" language="C#" checksumAlgorithm="SHA256"><![CDATA[using System;
namespace ICSharpCode.Decompiler.Tests.TestCases.PdbGen
{
Expand Down
50 changes: 50 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/CovariantReturns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.CovariantReturns
{
public abstract class Base
{
public abstract Base Instance { get; }

public abstract Base this[int index] { get; }

public virtual Base Build()
{
throw null;
}

protected abstract Base SetParent(object parent);
}

public class Derived : Base
{
public override Derived Instance { get; }

public override Derived this[int index] {
get {
throw null;
}
}

public override Derived Build()
{
throw null;
}

protected override Derived SetParent(object parent)
{
throw null;
}
}

public class UseSites
{
public Base Test(Base x)
{
return x.Build();
}

public Derived Test(Derived x)
{
return x.Build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public interface IA
void Method();

#if CS80
static IA()
{

}

void DefaultMethod()
{
Method();
Expand Down
23 changes: 23 additions & 0 deletions ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,9 +1453,24 @@ EntityDeclaration DoDecompile(IMethod method, DecompileRun decompileRun, ITypeRe
{
SetNewModifier(methodDecl);
}
if (IsCovariantReturnOverride(method))
{
RemoveAttribute(methodDecl, KnownAttribute.PreserveBaseOverrides);
methodDecl.Modifiers &= ~(Modifiers.New | Modifiers.Virtual);
methodDecl.Modifiers |= Modifiers.Override;
}
return methodDecl;
}

private bool IsCovariantReturnOverride(IEntity entity)
{
if (!settings.CovariantReturns)
return false;
if (!entity.HasAttribute(KnownAttribute.PreserveBaseOverrides))
return false;
return true;
}

internal static bool IsWindowsFormsInitializeComponentMethod(IMethod method)
{
return method.ReturnType.Kind == TypeKind.Void && method.Name == "InitializeComponent" && method.DeclaringTypeDefinition.GetNonInterfaceBaseTypes().Any(t => t.FullName == "System.Windows.Forms.Control");
Expand Down Expand Up @@ -1768,7 +1783,15 @@ EntityDeclaration DoDecompile(IProperty property, DecompileRun decompileRun, ITy
var accessorHandle = (MethodDefinitionHandle)(property.Getter ?? property.Setter).MetadataToken;
var accessor = metadata.GetMethodDefinition(accessorHandle);
if (!accessorHandle.GetMethodImplementations(metadata).Any() && accessor.HasFlag(System.Reflection.MethodAttributes.Virtual) == accessor.HasFlag(System.Reflection.MethodAttributes.NewSlot))
{
SetNewModifier(propertyDecl);
}
if (getterHasBody && IsCovariantReturnOverride(property.Getter))
{
RemoveAttribute(getter, KnownAttribute.PreserveBaseOverrides);
propertyDecl.Modifiers &= ~(Modifiers.New | Modifiers.Virtual);
propertyDecl.Modifiers |= Modifiers.Override;
}
return propertyDecl;
}
catch (Exception innerException) when (!(innerException is OperationCanceledException || innerException is DecompilerException))
Expand Down
2 changes: 1 addition & 1 deletion ICSharpCode.Decompiler/CSharp/CallBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ private ExpressionWithResolveResult HandleImplicitConversion(IMethod method, Tra
var conversions = CSharpConversions.Get(expressionBuilder.compilation);
IType targetType = method.ReturnType;
var conv = conversions.ImplicitConversion(argument.Type, targetType);
if (!(conv.IsUserDefined && conv.Method.Equals(method)))
if (!(conv.IsUserDefined && conv.IsValid && conv.Method.Equals(method)))
{
// implicit conversion to targetType isn't directly possible, so first insert a cast to the argument type
argument = argument.ConvertTo(method.Parameters[0].Type, expressionBuilder);
Expand Down
8 changes: 4 additions & 4 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ IType FindType(StackType stackType, Sign sign)
}
else
{
return compilation.FindType(stackType.ToKnownTypeCode(sign));
return compilation.FindType(stackType, sign);
}
}

Expand All @@ -1644,7 +1644,7 @@ IType FindArithmeticType(StackType stackType, Sign sign)
stackType = StackType.I8;
}
}
return compilation.FindType(stackType.ToKnownTypeCode(sign));
return compilation.FindType(stackType, sign);
}

/// <summary>
Expand Down Expand Up @@ -3568,7 +3568,7 @@ protected internal override TranslatedExpression VisitNullCoalescingInstruction(
}
else if (!value.Type.Equals(SpecialType.NullType) && !fallback.Type.Equals(SpecialType.NullType) && !value.Type.Equals(fallback.Type))
{
targetType = compilation.FindType(inst.UnderlyingResultType.ToKnownTypeCode());
targetType = compilation.FindType(inst.UnderlyingResultType);
}
else
{
Expand Down Expand Up @@ -3731,7 +3731,7 @@ protected internal override TranslatedExpression VisitSwitchInstruction(SwitchIn
}
else
{
resultType = compilation.FindType(inst.ResultType.ToKnownTypeCode());
resultType = compilation.FindType(inst.ResultType);
}

foreach (var section in inst.Sections)
Expand Down
Loading

0 comments on commit 6290e2f

Please sign in to comment.