From 9eb0a98854fc61580a25955c8220e535c0d91ed9 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 16 Aug 2020 15:07:47 +0200 Subject: [PATCH] Fix NRE with tuple discards in DeconstructionTransform --- .../TestCases/Pretty/DeconstructionTests.cs | 5 +++++ .../IL/Transforms/DeconstructionTransform.cs | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs index 29a2c6e99..f425ea5b9 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs @@ -216,5 +216,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { (Get(0).NMy, Get(1).My) = GetTuple(); } + + public void Tuple_Property_NoConversion_DiscardLast() + { + (Get(0).NMy, Get(1).My, _) = GetTuple(); + } } } diff --git a/ICSharpCode.Decompiler/IL/Transforms/DeconstructionTransform.cs b/ICSharpCode.Decompiler/IL/Transforms/DeconstructionTransform.cs index 4bc725926..f8fa5eeac 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/DeconstructionTransform.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/DeconstructionTransform.cs @@ -209,8 +209,15 @@ namespace ICSharpCode.Decompiler.IL.Transforms IsDeconstructTuple = this.tupleType != null }; int index = 0; - foreach (ILVariable result in deconstructionResults) { - result.Kind = VariableKind.PatternLocal; + foreach (ILVariable v in deconstructionResults) { + var result = v; + if (result == null) { + var freshVar = new ILVariable(VariableKind.PatternLocal, this.tupleType.ElementTypes[index]) { Name = "E_" + index }; + context.Function.Variables.Add(freshVar); + result = freshVar; + } else { + result.Kind = VariableKind.PatternLocal; + } replacement.Pattern.SubPatterns.Add( new MatchInstruction( result,