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

Require SizeConst for UnmanagedType.ByValArray #68992

Merged
merged 13 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -570,10 +570,10 @@ public void NativeTypeFixedArray()

public class X
{
[MarshalAs(UnmanagedType.ByValArray)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public int ByValArray0;

[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.ByValTStr, IidParameterIndex = -1, MarshalCookie = null, MarshalType = null, MarshalTypeRef = null,
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.ByValTStr, IidParameterIndex = -1, SizeConst = 1, MarshalCookie = null, MarshalType = null, MarshalTypeRef = null,
SafeArrayUserDefinedSubType = null)]
public int ByValArray1;

Expand All @@ -585,10 +585,10 @@ public class X
MarshalCookie = null, MarshalType = null, MarshalTypeRef = null, SafeArrayUserDefinedSubType = null)]
public int ByValArray3;

[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.AsAny)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.AsAny, SizeConst = 1)]
public int ByValArray4;

[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.CustomMarshaler)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.CustomMarshaler, SizeConst = 1)]
public int ByValArray5;
}
";
Expand Down Expand Up @@ -656,9 +656,15 @@ public class X
// (8,170): error CS7045: Parameter not valid for the specified unmanaged type.
// [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.ByValTStr, SafeArraySubType = VarEnum.VT_BSTR, SafeArrayUserDefinedSubType = null, SizeConst = -1, SizeParamIndex = -1)]int ByValArray_e1;
Diagnostic(ErrorCode.ERR_ParameterNotValidForType, "SizeParamIndex = -1"),
// (9,6): error CS7046: Attribute parameter 'SizeConst' must be specified.
// [MarshalAs(UnmanagedType.ByValArray, SizeParamIndex = short.MaxValue)] int ByValArray_e2;
Diagnostic(ErrorCode.ERR_AttributeParameterRequired1, "MarshalAs").WithArguments("SizeConst"),
// (9,42): error CS7045: Parameter not valid for the specified unmanaged type.
// [MarshalAs(UnmanagedType.ByValArray, SizeParamIndex = short.MaxValue)] int ByValArray_e2;
Diagnostic(ErrorCode.ERR_ParameterNotValidForType, "SizeParamIndex = short.MaxValue"),
// (10,6): error CS7046: Attribute parameter 'SizeConst' must be specified.
// [MarshalAs(UnmanagedType.ByValArray, SafeArraySubType = VarEnum.VT_I2)] int ByValArray_e3;
Diagnostic(ErrorCode.ERR_AttributeParameterRequired1, "MarshalAs").WithArguments("SizeConst"),
// (10,42): error CS7045: Parameter not valid for the specified unmanaged type.
// [MarshalAs(UnmanagedType.ByValArray, SafeArraySubType = VarEnum.VT_I2)] int ByValArray_e3;
Diagnostic(ErrorCode.ERR_ParameterNotValidForType, "SafeArraySubType = VarEnum.VT_I2"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private static void DecodeMarshalAsArray(ref DecodeWellKnownAttributeArguments<T
Debug.Assert((object)arguments.AttributeSyntaxOpt != null);

UnmanagedType? elementType = null;
int? elementCount = isFixed ? 1 : (int?)null;
int? elementCount = null;
short? parameterIndex = null;
bool hasErrors = false;

Expand Down Expand Up @@ -268,6 +268,16 @@ private static void DecodeMarshalAsArray(ref DecodeWellKnownAttributeArguments<T
position++;
}

if (isFixed)
{
if (elementCount is null)
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
{
// SizeConst must be specified:
messageProvider.ReportAttributeParameterRequired(arguments.Diagnostics, arguments.AttributeSyntaxOpt, "SizeConst");
hasErrors = true;
}
}

if (!hasErrors)
{
var data = arguments.GetOrCreateData<TWellKnownAttributeData>().GetOrCreateData();
Expand Down
Loading