-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1121 from tlakollo/RunClassConstructor
Support RunClassConstructor in the linker
- Loading branch information
Showing
3 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
test/Mono.Linker.Tests.Cases/Reflection/RunClassConstructorUsedViaReflection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
using System; | ||
using System.Reflection; | ||
using Mono.Linker.Tests.Cases.Expectations.Assertions; | ||
using Mono.Linker.Tests.Cases.Expectations.Metadata; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Mono.Linker.Tests.Cases.Reflection | ||
{ | ||
[SetupCSharpCompilerToUse ("csc")] | ||
public class RunClassConstructorUsedViaReflection | ||
{ | ||
public static void Main () | ||
{ | ||
TestRunClassConstructor (); | ||
TestNonKeptStaticConstructor (); | ||
TestNull (); | ||
TestDataFlowType (); | ||
TestIfElseUsingRuntimeTypeHandle (1); | ||
TestIfElseUsingType (1); | ||
} | ||
|
||
[Kept] | ||
[RecognizedReflectionAccessPattern] | ||
static void TestRunClassConstructor () | ||
{ | ||
RuntimeHelpers.RunClassConstructor (typeof (OnlyUsedViaReflection).TypeHandle); | ||
} | ||
|
||
[Kept] | ||
static void TestNonKeptStaticConstructor () | ||
{ | ||
var a = new NonKeptStaticConstructorClass(); | ||
} | ||
|
||
[Kept] | ||
[RecognizedReflectionAccessPattern] | ||
static void TestNull () | ||
{ | ||
Type type = null; | ||
RuntimeHelpers.RunClassConstructor (type.TypeHandle); | ||
} | ||
|
||
[Kept] | ||
static Type FindType () | ||
{ | ||
return null; | ||
} | ||
|
||
[Kept] | ||
[UnrecognizedReflectionAccessPattern (typeof (RuntimeHelpers), nameof (RuntimeHelpers.RunClassConstructor), new Type [] { typeof (RuntimeTypeHandle) }, | ||
"A value from unknown source is passed into the parameter 'type' of method "+ | ||
"'System.Void System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor(System.RuntimeTypeHandle)'. "+ | ||
"It's not possible to guarantee availability of the target static constructor.")] | ||
|
||
static void TestDataFlowType () | ||
{ | ||
Type type = FindType (); | ||
RuntimeHelpers.RunClassConstructor (type.TypeHandle); | ||
} | ||
|
||
[Kept] | ||
[RecognizedReflectionAccessPattern] | ||
[UnrecognizedReflectionAccessPattern (typeof (RuntimeHelpers), nameof (RuntimeHelpers.RunClassConstructor), new Type [] { typeof (RuntimeTypeHandle) }, | ||
"A value from unknown source is passed into the parameter 'type' of method "+ | ||
"'System.Void System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor(System.RuntimeTypeHandle)'. "+ | ||
"It's not possible to guarantee availability of the target static constructor.")] | ||
|
||
static void TestIfElseUsingRuntimeTypeHandle (int i) | ||
{ | ||
RuntimeTypeHandle myType; | ||
if (i == 1) { | ||
myType = typeof (IfClass).TypeHandle; | ||
} else if (i == 2) { | ||
myType = FindType ().TypeHandle; | ||
} else { | ||
myType = typeof (ElseClass).TypeHandle; | ||
} | ||
RuntimeHelpers.RunClassConstructor (myType); | ||
} | ||
|
||
[Kept] | ||
[RecognizedReflectionAccessPattern] | ||
static void TestIfElseUsingType (int i) | ||
{ | ||
Type myType; | ||
if (i == 1) { | ||
myType = typeof (IfClass2); | ||
}else if (i==2) { | ||
myType = null; | ||
} | ||
else { | ||
myType = typeof (ElseClass2); | ||
} | ||
RuntimeHelpers.RunClassConstructor (myType.TypeHandle); | ||
} | ||
|
||
[Kept] | ||
[KeptMember (".cctor()")] | ||
class OnlyUsedViaReflection | ||
{ | ||
[Kept] | ||
static int i = 5; | ||
} | ||
|
||
[Kept] | ||
[KeptMember (".ctor()")] | ||
class NonKeptStaticConstructorClass | ||
{ | ||
static int i = 5; | ||
} | ||
|
||
[Kept] | ||
[KeptMember (".cctor()")] | ||
class IfClass | ||
{ | ||
public IfClass () | ||
{ } | ||
private IfClass (int foo) | ||
{ } | ||
} | ||
|
||
[Kept] | ||
[KeptMember (".cctor()")] | ||
class ElseClass | ||
{ | ||
[Kept] | ||
static ElseClass () | ||
{ } | ||
public ElseClass (int foo) | ||
{ } | ||
} | ||
[Kept] | ||
[KeptMember (".cctor()")] | ||
class IfClass2 | ||
{ | ||
public IfClass2 () | ||
{ } | ||
private IfClass2 (int foo) | ||
{ } | ||
} | ||
|
||
[Kept] | ||
[KeptMember (".cctor()")] | ||
class ElseClass2 | ||
{ | ||
[Kept] | ||
static ElseClass2 () | ||
{ } | ||
public ElseClass2 (int foo) | ||
{ } | ||
} | ||
} | ||
} |