Skip to content

Commit

Permalink
Test attribute function pointer execution on net9 (dotnet#74999)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz authored Sep 6, 2024
1 parent 6ad9d42 commit 1ce264c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11604,7 +11604,6 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
CreateCompilation(source, options: TestOptions.UnsafeDebugDll).VerifyEmitDiagnostics(
// (3,16): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
// public A(B<delegate*<void>[]>.E e) { }
Expand All @@ -11616,7 +11615,14 @@ unsafe class C { }
public void Attribute_TypedDefault_Enum_Implicit_ConstructorArgument_WithUnsafeContext([CombinatorialValues("class", "struct")] string kind)
{
var source = $$"""
class A : System.Attribute
using System;
using System.Linq;
var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType == typeof(A));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine((int)arg.Value == 0);
class A : Attribute
{
public unsafe A(B<delegate*<void>[]>.E e) { }
}
Expand All @@ -11630,8 +11636,7 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugDll, symbolValidator: static module =>
var verifier = CompileAndVerify(source, expectedOutput: ExecutionConditionUtil.IsMonoOrCoreClr ? "True" : null, options: TestOptions.UnsafeDebugExe, symbolValidator: static module =>
{
var c = module.GlobalNamespace.GetTypeMember("C");
var attr = c.GetAttributes().Single(d => d.AttributeClass?.Name == "A");
Expand Down Expand Up @@ -11673,7 +11678,14 @@ class C { }
public void Attribute_GenericTypedDefault_Enum_Implicit_ConstructorArgument([CombinatorialValues("class", "struct")] string kind)
{
var source = $$"""
class A<T> : System.Attribute
using System;
using System.Linq;
var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType.GetGenericTypeDefinition() == typeof(A<>));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine((int)arg.Value == 0);
class A<T> : Attribute
{
public A(T t) { }
}
Expand All @@ -11687,8 +11699,7 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugDll, symbolValidator: static module =>
var verifier = CompileAndVerify(source, expectedOutput: ExecutionConditionUtil.IsMonoOrCoreClr ? "True" : null, options: TestOptions.UnsafeDebugExe, symbolValidator: static module =>
{
var c = module.GlobalNamespace.GetTypeMember("C");
var attr = c.GetAttributes().Single(d => d.AttributeClass?.Name == "A");
Expand Down Expand Up @@ -11730,7 +11741,14 @@ class C { }
public void Attribute_TypedDefault_Enum_Explicit_ConstructorArgument([CombinatorialValues("class", "struct")] string kind)
{
var source = $$"""
class A : System.Attribute
using System;
using System.Linq;
var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType == typeof(A));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine((int)arg.Value == 0);
class A : Attribute
{
public unsafe A(B<delegate*<void>[]>.E e) { }
}
Expand All @@ -11744,8 +11762,7 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugDll, symbolValidator: static module =>
var verifier = CompileAndVerify(source, expectedOutput: ExecutionConditionUtil.IsMonoOrCoreClr ? "True" : null, options: TestOptions.UnsafeDebugExe, symbolValidator: static module =>
{
var c = module.GlobalNamespace.GetTypeMember("C");
var attr = c.GetAttributes().Single(d => d.AttributeClass?.Name == "A");
Expand Down Expand Up @@ -12057,7 +12074,14 @@ unsafe class C { }
public void Attribute_GenericTypedConstant_Enum_ConstructorArgument([CombinatorialValues("class", "struct")] string kind)
{
var source = $$"""
class A<T> : System.Attribute
using System;
using System.Linq;
var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType.GetGenericTypeDefinition() == typeof(A<>));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine((int)arg.Value == 33);
class A<T> : Attribute
{
public A(T t) { }
}
Expand All @@ -12072,8 +12096,7 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugDll, symbolValidator: static module =>
var verifier = CompileAndVerify(source, expectedOutput: ExecutionConditionUtil.IsMonoOrCoreClr ? "True" : null, options: TestOptions.UnsafeDebugExe, symbolValidator: static module =>
{
var c = module.GlobalNamespace.GetTypeMember("C");
var attr = c.GetAttributes().Single(d => d.AttributeClass?.Name == "A");
Expand Down Expand Up @@ -12194,7 +12217,14 @@ unsafe class C { }
public void Attribute_TypedConstant_Enum_ConstructorArgument([CombinatorialValues("class", "struct")] string kind)
{
var source = $$"""
class A : System.Attribute
using System;
using System.Linq;
var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType == typeof(A));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine((int)arg.Value == 33);
class A : Attribute
{
public unsafe A(B<delegate*<void>[]>.E o) { }
}
Expand All @@ -12209,8 +12239,7 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugDll, symbolValidator: static module =>
var verifier = CompileAndVerify(source, expectedOutput: ExecutionConditionUtil.IsMonoOrCoreClr ? "True" : null, options: TestOptions.UnsafeDebugExe, symbolValidator: static module =>
{
var c = module.GlobalNamespace.GetTypeMember("C");
var attr = c.GetAttributes().Single(d => d.AttributeClass?.Name == "A");
Expand All @@ -12227,7 +12256,15 @@ unsafe class C { }
public void Attribute_TypedConstant_EnumArray_ConstructorArgument([CombinatorialValues("class", "struct")] string kind)
{
var source = $$"""
class A : System.Attribute
using System;
using System.Collections;
using System.Linq;
var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType == typeof(A));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine(!((IEnumerable)arg.Value).Cast<int>().Any());
class A : Attribute
{
public unsafe A(B<delegate*<void>[]>.E[] a) { }
}
Expand All @@ -12241,8 +12278,7 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugDll, symbolValidator: static module =>
var verifier = CompileAndVerify(source, expectedOutput: ExecutionConditionUtil.IsMonoOrCoreClr ? "True" : null, options: TestOptions.UnsafeDebugExe, symbolValidator: static module =>
{
var c = module.GlobalNamespace.GetTypeMember("C");
var attr = c.GetAttributes().Single(d => d.AttributeClass?.Name == "A");
Expand All @@ -12261,7 +12297,16 @@ public void Attribute_TypedParamsConstant_EnumArray_ConstructorArgument(
[CombinatorialValues("[]{}", "()")] string initializer)
{
var source = $$"""
class A : System.Attribute
using System;
using System.Collections;
using System.Linq;
using System.Reflection;
var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType == typeof(A));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine(((IEnumerable)arg.Value).Cast<CustomAttributeTypedArgument>().SingleOrDefault().Value ?? "null");
class A : Attribute
{
public unsafe A(params B<delegate*<void>[]>.E[] a) { }
}
Expand All @@ -12275,8 +12320,9 @@ public enum E { }
unsafe class C { }
""";

// https://github.com/dotnet/roslyn/issues/66187 tracks enabling runtime reflection support for this scenario.
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugDll, symbolValidator: module =>
var expectedOutput = ExecutionConditionUtil.IsMonoOrCoreClr ? (initializer == "()" ? "0" : "null") : null;

var verifier = CompileAndVerify(source, expectedOutput: expectedOutput, options: TestOptions.UnsafeDebugExe, symbolValidator: module =>
{
var c = module.GlobalNamespace.GetTypeMember("C");
var attr = c.GetAttributes().Single(d => d.AttributeClass?.Name == "A");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.CSharp.UnitTests</RootNamespace>
<TargetFrameworks>$(NetRoslyn);net472</TargetFrameworks>
<TargetFrameworks>$(NetRoslynNext);net472</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup Label="Project References">
Expand Down

0 comments on commit 1ce264c

Please sign in to comment.