diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs index 2ae08c5ca..6d9d1672a 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Generics.cs @@ -286,5 +286,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty Console.WriteLine("{}, {}", a < b, a > b); Console.WriteLine("{}, {}", a < Environment.GetLogicalDrives().Length, a > (c ?? b)); } + + public static Type Issue2231() + { + return default(T).GetType(); + } + + public static string Issue2231b() + { + return default(T).ToString(); + } } } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ValueTypes.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ValueTypes.cs index a33954feb..b57b90d07 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ValueTypes.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/ValueTypes.cs @@ -281,5 +281,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { return ulong.MaxValue.ToString(); } + + public static void M4() + { + Test(default(DateTime).GetType()); + Test(default(DateTime).ToString()); + } + + public static void Test(object x) + { + } } } \ No newline at end of file diff --git a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs index 6a00c714d..bb6eefa0c 100644 --- a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs @@ -94,6 +94,7 @@ namespace ICSharpCode.Decompiler.CSharp new AsyncAwaitDecompiler(), // must run after inlining but before loop detection new DetectCatchWhenConditionBlocks(), // must run after inlining but before loop detection new DetectExitPoints(canIntroduceExitForReturn: false), + new LdLocaDupInitObjTransform(), new EarlyExpressionTransforms(), // RemoveDeadVariableInit must run after EarlyExpressionTransforms so that stobj(ldloca V, ...) // is already collapsed into stloc(V, ...). diff --git a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj index 80c9977ca..fbb8424c0 100644 --- a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj +++ b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj @@ -91,6 +91,7 @@ + diff --git a/ICSharpCode.Decompiler/IL/Transforms/LdLocaDupInitObjTransform.cs b/ICSharpCode.Decompiler/IL/Transforms/LdLocaDupInitObjTransform.cs new file mode 100644 index 000000000..2fd1c831f --- /dev/null +++ b/ICSharpCode.Decompiler/IL/Transforms/LdLocaDupInitObjTransform.cs @@ -0,0 +1,77 @@ +// Copyright (c) 2021 Siegfried Pammer +// +// 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. + +#nullable enable + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using ICSharpCode.Decompiler.TypeSystem; + +namespace ICSharpCode.Decompiler.IL.Transforms +{ + // stloc s(ldloca v) + // stobj System.DateTime(ldloc s, default.value T) + // where s.LoadCount > 1 + // which is: ldloca; dup; initobj in IL (generated by Roslyn >= 2) + // => + // stloc v(default.value T) + // stloc s(ldloca v) + // which is: ldloca; ldloca; initobj in IL (generated by legacy csc) + // + // The second pattern allows inlining in the subsequent uses. + class LdLocaDupInitObjTransform : IILTransform + { + void IILTransform.Run(ILFunction function, ILTransformContext context) + { + foreach (var block in function.Descendants.OfType()) + { + for (int i = 0; i < block.Instructions.Count; i++) + { + TryTransform(block, i, context); + } + } + } + + private bool TryTransform(Block block, int i, ILTransformContext context) + { + if (block.Instructions[i] is not StLoc { Variable: var s, Value: LdLoca { Variable: var v } } inst1) + { + return false; + } + if (block.Instructions.ElementAtOrDefault(i + 1) is not StObj inst2) + { + return false; + } + if (!(inst2.Target.MatchLdLoc(s) + && TypeUtils.IsCompatibleTypeForMemoryAccess(v.Type, inst2.Type) + && inst2.UnalignedPrefix == 0 + && !inst2.IsVolatile + && inst2.Value is DefaultValue)) + { + return false; + } + context.Step("LdLocaDupInitObjTransform", inst1); + block.Instructions[i] = new StLoc(v, inst2.Value).WithILRange(inst2); + block.Instructions[i + 1] = inst1; + return true; + } + } +}