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

[DllImportGenerator] Treat pointer fields in structs as blittable regardless of what they point at #61538

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -31,7 +31,7 @@ private static bool HasOnlyBlittableFields(this ITypeSymbol type, ImmutableHashS
bool fieldBlittable = field switch
{
{ Type: { IsReferenceType: true } } => false,
{ Type: IPointerTypeSymbol ptr } => IsConsideredBlittable(ptr.PointedAtType),
{ Type: IPointerTypeSymbol ptr } => true,
{ Type: IFunctionPointerTypeSymbol } => true,
not { Type: { SpecialType: SpecialType.None } } => IsSpecialTypeBlittable(field.Type.SpecialType),
// Assume that type parameters that can be blittable are blittable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ public static partial void DoubleIntFieldsRefReturn(
public static partial void DoubleIntFieldsOutReturn(
IntFields input,
out IntFields result);

[GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "blittablestructs_increment_invert_ptrfields_byref")]
public static partial void IncrementInvertPointerFieldsByRef(ref PointerFields result);

[GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "blittablestructs_increment_invert_ptrfields_byref")]
public static partial void IncrementInvertPointerFieldsByRefIn(in PointerFields result);

[GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "blittablestructs_increment_invert_ptrfields_refreturn")]
public static partial void IncrementInvertPointerFieldsRefReturn(
PointerFields input,
ref PointerFields result);
}

public class BlittableStructTests
{
[Fact]
public void ValidateBlittableStruct()
public void ValidateIntFields()
{
const int A = 24, B = 37, C = 59;
var initial = new IntFields()
Expand Down Expand Up @@ -82,5 +93,71 @@ public void ValidateBlittableStruct()
Assert.Equal(expected, input); // Updated even when passed with in keyword (matches built-in system)
}
}

[Fact]
public unsafe void ValidatePointerFields()
{
int iInitial = 31;
bool bInitial = false;
char cInitial = 'A';

int iExpected = iInitial + 1;
bool bExpected = !bInitial;
char cExpected = (char)(cInitial + 1);

int i = iInitial;
bool b = bInitial;
char c = cInitial;
var initial = new PointerFields()
{
i = &i,
b = &b,
c = &c,
};

PointerFields input = initial;
{
int iResult;
bool bResult;
char cResult;
var result = new PointerFields()
{
i = &iResult,
b = &bResult,
c = &cResult
};
NativeExportsNE.IncrementInvertPointerFieldsRefReturn(input, ref result);
Assert.Equal(initial, input);
ValidateFieldValues(result);
}

{
ResetFieldValues(input);
NativeExportsNE.IncrementInvertPointerFieldsByRef(ref input);
Assert.Equal(initial, input);
ValidateFieldValues(input);
}

{
ResetFieldValues(input);
NativeExportsNE.IncrementInvertPointerFieldsByRefIn(in input);
Assert.Equal(initial, input);
ValidateFieldValues(input);
}

void ResetFieldValues(PointerFields input)
{
*(input.i) = iInitial;
*(input.b) = bInitial;
*(input.c) = cInitial;
}

void ValidateFieldValues(PointerFields result)
{
Assert.Equal(iExpected, *result.i);
Assert.Equal(bExpected, *result.b);
Assert.Equal(cExpected, *result.c);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1280,34 +1280,20 @@ public Native(S<T> s)
}

[ConditionalFact]
public async Task ValueTypeContainingPointerBlittableType_DoesNotReportDiagnostic()
public async Task ValueTypeContainingPointers_DoesNotReportDiagnostic()
{
var source = @"
using System.Runtime.InteropServices;

[BlittableType]
unsafe struct S
{
private int* ptr;
private int* intPtr;
private bool* boolPtr;
}";
await VerifyCS.VerifyAnalyzerAsync(source);
}

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

[{|#0:BlittableType|}]
unsafe struct S
{
private bool* ptr;
}";
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(BlittableTypeMustBeBlittableRule).WithLocation(0).WithArguments("S"));
}

[ConditionalFact]
public async Task BlittableValueTypeContainingPointerToSelf_DoesNotReportDiagnostic()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,25 @@ public static void DoubleIntFieldsRefReturn(
result->b = input.b * 2;
result->c = input.c * 2;
}

[UnmanagedCallersOnly(EntryPoint = "blittablestructs_increment_invert_ptrfields_byref")]
[DNNE.C99DeclCode("struct ptr_fields { int* i; int* b; uint16_t* c; };")]
public static void IncrementInvertPointerFieldsByRef(
[DNNE.C99Type("struct ptr_fields*")] PointerFields* result)
{
*(result->i) += 1;
*(result->b) = !*(result->b);
*(result->c) += (char)1;
}

[UnmanagedCallersOnly(EntryPoint = "blittablestructs_increment_invert_ptrfields_refreturn")]
public static void IncrementInvertPointerFieldsRefReturn(
[DNNE.C99Type("struct ptr_fields")] PointerFields input,
[DNNE.C99Type("struct ptr_fields*")] PointerFields* result)
{
*(result->i) = *(input.i) + 1;
*(result->b) = !(*input.b);
*(result->c) = (char)(*(input.c) + 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ public struct IntFields
public int b;
public int c;
}

[BlittableType]
public unsafe struct PointerFields
{
public int* i;
public bool* b;
public char* c;
}
}