From d25fa533787b3634995abeaca2868844fc1c0edb Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 7 Apr 2025 17:22:10 +0200 Subject: [PATCH] [tools] Simplify class_ptr rewriter to not create new instructions when not needed. We can simply change operands of existing instructions instead. Fixes these test failures: MonoTouchFixtures.AppKit.NSGridViewTest [FAIL] CreateWithNSViewArrayOfArrayCheckDifferentArrayLength : System.InvalidProgramException : InvalidProgram_Default at AppKit.NSGridView.Create(NSView[][]) at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithNSViewArrayOfArrayCheckDifferentArrayLength() at System.RuntimeMethodHandle.InvokeMethod(Object, Void**, Signature, Boolean) at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object, BindingFlags) [FAIL] CreateWithNSViewArrayOfArrayCheckNSTextView : System.InvalidProgramException : InvalidProgram_Default at AppKit.NSGridView.Create(NSView[][]) at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithNSViewArrayOfArrayCheckNSTextView() at System.RuntimeMethodHandle.InvokeMethod(Object, Void**, Signature, Boolean) at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object, BindingFlags) [FAIL] CreateWithNSViewArrayOfArrayCheckWithNull : Broken Array #3 Expected: But was: c__DisplayClass6_0.b__0() at NUnit.Framework.Assert.Throws(IResolveConstraint, TestDelegate, String, Object[] )> at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithNSViewArrayOfArrayCheckWithNull() [FAIL] CreateWithNSViewArrayOfArrayCheckWithNullArray : Broken Array #2 Expected: But was: c__DisplayClass5_0.b__0() at NUnit.Framework.Assert.Throws(IResolveConstraint, TestDelegate, String, Object[] )> at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithNSViewArrayOfArrayCheckWithNullArray() [FAIL] CreateWithNSViewArrayOfArrayCheckWithNullNSView : Broken Array #1 Expected: But was: c__DisplayClass4_0.b__0() at NUnit.Framework.Assert.Throws(IResolveConstraint, TestDelegate, String, Object[] )> at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithNSViewArrayOfArrayCheckWithNullNSView() [FAIL] CreateWithTwoDimensionalNSViewArrayCheckDifferentDimensionSize : System.InvalidProgramException : InvalidProgram_Default at AppKit.NSGridView.Create(NSView[,]) at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithTwoDimensionalNSViewArrayCheckDifferentDimensionSize() at System.RuntimeMethodHandle.InvokeMethod(Object, Void**, Signature, Boolean) at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object, BindingFlags) [FAIL] CreateWithTwoDimensionalNSViewArrayCheckWithNull : Broken Array #4 Expected: But was: c__DisplayClass7_0.b__0() at NUnit.Framework.Assert.Throws(IResolveConstraint, TestDelegate, String, Object[] )> at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithTwoDimensionalNSViewArrayCheckWithNull() [FAIL] CreateWithTwoDimensionalNSViewArrayNSTextView : System.InvalidProgramException : InvalidProgram_Default at AppKit.NSGridView.Create(NSView[,]) at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithTwoDimensionalNSViewArrayNSTextView() at System.RuntimeMethodHandle.InvokeMethod(Object, Void**, Signature, Boolean) at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object, BindingFlags) [FAIL] CreateWithTwoDimensionalNSViewArrayWithNullCell : Broken Array #5 Expected: But was: c__DisplayClass8_0.b__0() at NUnit.Framework.Assert.Throws(IResolveConstraint, TestDelegate, String, Object[] )> at MonoTouchFixtures.AppKit.NSGridViewTest.CreateWithTwoDimensionalNSViewArrayWithNullCell() --- tools/common/Rewriter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/common/Rewriter.cs b/tools/common/Rewriter.cs index ec423e5f1b0a..659a01106a22 100644 --- a/tools/common/Rewriter.cs +++ b/tools/common/Rewriter.cs @@ -235,8 +235,7 @@ void PatchMethod (MethodDefinition method, FieldDefinition classPtr, FieldDefini for (var i = 0; i < body.Instructions.Count; i++) { var old = body.Instructions [i]; if (old.OpCode == OpCodes.Ldsfld && old.Operand == classPtr) { - var @new = Instruction.Create (OpCodes.Ldsfld, method.Module.ImportReference (classPtrField)); - ReplaceAndPatch (body, il, i, @new); + old.Operand = method.Module.ImportReference (classPtrField); } } body.OptimizeMacros ();