From a960216d5f629b4ae2b8048879a5fa430560ca60 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 28 Dec 2020 20:02:02 +0100 Subject: [PATCH] Add test case for simple records. --- .../ICSharpCode.Decompiler.Tests.csproj | 1 + .../PrettyTestRunner.cs | 6 ++++ .../TestCases/Pretty/Records.cs | 35 +++++++++++++++++++ .../CSharp/RecordDecompiler.cs | 4 ++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index cdcab56dc..02b24a0d1 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -105,6 +105,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index 3c674d65a..d0ab94223 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -404,6 +404,12 @@ namespace ICSharpCode.Decompiler.Tests RunForLibrary(cscOptions: cscOptions | CompilerOptions.Preview); } + [Test] + public void Records([ValueSource(nameof(roslynLatestOnlyOptions))] CompilerOptions cscOptions) + { + RunForLibrary(cscOptions: cscOptions | CompilerOptions.Preview); + } + [Test] public void NullPropagation([ValueSource(nameof(roslynOnlyOptions))] CompilerOptions cscOptions) { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs new file mode 100644 index 000000000..d3c4e26f0 --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs @@ -0,0 +1,35 @@ +namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty +{ + public record Empty + { + } + + public record Properties + { + public int A { + get; + set; + } + public int B { + get; + } + public int C => 43; + public object O { + get; + set; + } + public string S { + get; + set; + } + public dynamic D { + get; + set; + } + + public Properties() + { + B = 42; + } + } +} diff --git a/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs b/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs index b66f1dad6..a259fb128 100644 --- a/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs @@ -284,7 +284,7 @@ namespace ICSharpCode.Decompiler.CSharp if (!valueInst.MatchLdFld(out var rhsTarget, out var rhsField)) return false; - if (!rhsTarget.MatchLdThis()) + if (!rhsTarget.MatchLdLoc(other)) return false; if (!rhsField.Equals(field)) return false; @@ -824,6 +824,8 @@ namespace ICSharpCode.Decompiler.CSharp // Remove the last couple transforms -- we don't need variable names etc. here int lastBlockTransform = transforms.FindLastIndex(t => t is BlockILTransform); transforms.RemoveRange(lastBlockTransform + 1, transforms.Count - (lastBlockTransform + 1)); + // Use CombineExitsTransform so that "return other != null && ...;" is a single statement even in release builds + transforms.Add(new CombineExitsTransform()); il.RunTransforms(transforms, new ILTransformContext(il, typeSystem, debugInfo: null, settings) { CancellationToken = cancellationToken