Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement infra to support marshalling blittable generics (equivalent to .NET 5 support) #80

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ class GeneratedMarshallingAttribute : Attribute
public class BlittableTypeAttribute : Attribute
{
}

[AttributeUsage(AttributeTargets.Struct)]
public class BlittableTypeIfGenericParametersBlittableAttribute : Attribute
{
public BlittableTypeIfGenericParametersBlittableAttribute()
{
}
}

[AttributeUsage(AttributeTargets.GenericParameter)]
public class ContributesToBlittabilityAttribute : Attribute
{
public ContributesToBlittabilityAttribute()
{
}
}

[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
public class NativeMarshallingAttribute : Attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ struct T
}
";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(BlittableTypeMustBeBlittableRule).WithSpan(4, 2, 4, 15).WithArguments("S"),
VerifyCS.Diagnostic(BlittableTypeMustBeBlittableRule).WithSpan(10, 2, 10, 15).WithArguments("T"));
}

Expand Down Expand Up @@ -243,7 +242,7 @@ public Native(S s)
public S ToManaged() => new S();
}";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(NativeTypeMustBeBlittableRule).WithSpan(11, 1, 20, 2).WithArguments("Native", "S"));
VerifyCS.Diagnostic(NativeTypeMustHaveRequiredShapeRule).WithSpan(11, 1, 20, 2).WithArguments("Native", "S"));
}

[Fact]
Expand Down Expand Up @@ -970,5 +969,153 @@ unsafe struct S
}";
await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task BlittableGenericTypeInBlittableType_DoesNotReportDiagnostic()
{

var source = @"
using System.Runtime.InteropServices;

[BlittableTypeIfGenericParametersBlittable]
struct G<[ContributesToBlittability] T>
{
T fld;
}

[BlittableType]
unsafe struct S
{
private G<int> field;
}";
await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task NonBlittableGenericTypeInBlittableType_ReportsDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

[BlittableTypeIfGenericParametersBlittable]
struct G<[ContributesToBlittability] T>
{
T fld;
}

[BlittableType]
unsafe struct S
{
private G<string> field;
}";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(BlittableTypeMustBeBlittableRule).WithSpan(10, 2, 10, 15).WithArguments("S"));
}

[Fact]
public async Task BlittableTypeIfGenericParametersBlittableOnNonGenericType_ReportsDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

[BlittableTypeIfGenericParametersBlittable]
struct G
{
int fld;
}";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(BlittableTypeIfGenericParametersBlittableInvalidTargetRule).WithSpan(4, 2, 4, 43).WithArguments("G"));
}

[Fact]
public async Task ConditionallyBlittableGenericTypeWithNonContributesToBlittability_ReportsDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

[BlittableTypeIfGenericParametersBlittable]
struct G<[ContributesToBlittability] T, U>
{
T fld;
U fld2;
}";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(ConditionallyBlittableTypeMustBeConditionallyBlittableRule).WithSpan(4, 2, 4, 43).WithArguments("G<T, U>"));
}

[Fact]
public async Task ConditionallyBlittableGenericTypeConditionalTypeParameterReferenceType_ReportsDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

[BlittableTypeIfGenericParametersBlittable]
struct G<[ContributesToBlittability] T> where T : class
{
T fld;
}";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(ReferenceTypeParameterCanNeverBeBlittableRule).WithSpan(5, 11, 5, 36).WithArguments("T"),
VerifyCS.Diagnostic(ConditionallyBlittableTypeMustBeConditionallyBlittableRule).WithSpan(4, 2, 4, 43).WithArguments("G<T>"));
}

[Fact]
public async Task ConditionallyBlittableGenericTypeContainingConditionallyGenericType_DoesNotReportDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

[BlittableTypeIfGenericParametersBlittable]
struct G<[ContributesToBlittability] T>
{
T fld;
}

[BlittableTypeIfGenericParametersBlittable]
struct F<[ContributesToBlittability] T>
{
G<T> fld;
}
";
await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task ConditionallyBlittableNestedGenericType_DoesNotReportDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

struct C<[ContributesToBlittability] T>
{
[BlittableTypeIfGenericParametersBlittable]
struct G
{
T fld;
}
}
";
await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task ConditionallyBlittableNestedGenericTypeWithReferenceTypeGenericParameter_DoesNotReportDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

struct C<[ContributesToBlittability] T> where T : class
{
[BlittableTypeIfGenericParametersBlittable]
struct G
{
T fld;
}
}
";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(ReferenceTypeParameterCanNeverBeBlittableRule).WithSpan(4, 11, 4, 36).WithArguments("T"),
VerifyCS.Diagnostic(ConditionallyBlittableTypeMustBeConditionallyBlittableRule).WithSpan(6, 6, 6, 47).WithArguments("C<T>.G"));
}
}
}
Loading