123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using Unity.Collections.LowLevel.Unsafe;
- using Unity.Burst;
- using Unity.Burst.CompilerServices;
-
- #if BURST_TESTS_ONLY
- namespace Unity.Collections.LowLevel.Unsafe
- {
- internal class DisposeSentinel
- {
- }
- }
- #endif
-
- namespace Burst.Compiler.IL.Tests
- {
- /// <summary>
- /// Tests related to usage of partial managed objects (e.g loading null or storing null
- /// reference to a struct, typically used by NativeArray DisposeSentinel)
- /// </summary>
- internal class PartialManaged
- {
- #if BURST_TESTS_ONLY || ENABLE_UNITY_COLLECTIONS_CHECKS
- [TestCompiler]
- public static int TestWriteNullReference()
- {
- var element = new Element();
- WriteNullReference(out element.Reference);
- return element.Value;
- }
-
- [BurstDiscard]
- private static void WriteNullReference(out DisposeSentinel reference)
- {
- reference = null;
- }
-
- private struct Element
- {
- #pragma warning disable 0649
- public int Value;
- public DisposeSentinel Reference;
- #pragma warning restore 0649
-
- }
- #endif
-
- [TestCompiler]
- public static void AssignNullToLocalVariableClass()
- {
- MyClass x = null;
- #pragma warning disable 0219
- MyClass value = x;
- #pragma warning restore 0219
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_CallingManagedMethodNotSupported)]
- public static int GetIndexOfCharFomString()
- {
- return "abc".IndexOf('b');
- }
-
- struct StructWithManaged
- {
- #pragma warning disable 0649
- public MyClass myClassValue;
- public string stringValue;
- public object objectValue;
- public float[] arrayValue;
-
- public int value;
- #pragma warning restore 0649
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
- public static int AccessClassFromStruct()
- {
- var val = new StructWithManaged();
- val.myClassValue.value = val.value;
- return val.myClassValue.value;
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
- public static void AccessStringFromStruct()
- {
- var val = new StructWithManaged();
- #pragma warning disable 0219
- var p = val.stringValue = "abc";
- #pragma warning restore 0219
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
- public static void AccessObjectFromStruct()
- {
- var val = new StructWithManaged();
- #pragma warning disable 0219
- var p = val.objectValue;
- p = new object();
- #pragma warning restore 0219
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
- public static void AccessArrayFromStruct()
- {
- var val = new StructWithManaged();
- var p = val.arrayValue;
- p[0] = val.value;
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_TypeNotSupported)]
- public static int GetValueFromStructWithClassField()
- {
- var val = new StructWithManaged();
- val.value = 5;
-
- return val.value;
- }
-
- [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_InstructionNewobjWithManagedTypeNotSupported)]
- public static void NewMyClass()
- {
- #pragma warning disable 0219
- var value = new MyClass();
- #pragma warning restore 0219
- }
-
- private class MyClass
- {
- public int value;
- }
-
- private class SomeClassWithMixedStatics
- {
- public static int SomeInt = 42;
-
- public static readonly SharedStatic<int> SomeSharedStatic = SharedStatic<int>.GetOrCreate<int>();
-
- [BurstDiscard]
- private static void DoSomethingWithStaticInt(ref int x) => x = SomeInt;
-
- [IgnoreWarning(1371)]
- public static int DoSomething()
- {
- ref var data = ref SomeSharedStatic.Data;
- DoSomethingWithStaticInt(ref data);
- return SomeSharedStatic.Data;
- }
- }
-
- [TestCompiler(OverrideManagedResult = 0)]
- public static int DoSomethingThatUsesMixedStatics()
- {
- return SomeClassWithMixedStatics.DoSomething();
- }
-
- private class SomeClassWithMixedStaticsWithExplicitStaticConstructor
- {
- public static int SomeInt = 42;
-
- public static readonly SharedStatic<int> SomeSharedStatic = SharedStatic<int>.GetOrCreate<int>();
-
- static SomeClassWithMixedStaticsWithExplicitStaticConstructor()
- {
- SomeInt = 1;
- }
-
- [BurstDiscard]
- private static void DoSomethingWithStaticInt(ref int x) => x = SomeInt;
-
- [IgnoreWarning(1371)]
- public static int DoSomething()
- {
- ref var data = ref SomeSharedStatic.Data;
- DoSomethingWithStaticInt(ref data);
- return SomeSharedStatic.Data;
- }
- }
-
- [TestCompiler(OverrideManagedResult = 0)]
- public static int DoSomethingThatUsesMixedStaticsWithExplicitStaticConstructor()
- {
- return SomeClassWithMixedStaticsWithExplicitStaticConstructor.DoSomething();
- }
- }
- }
|