Skip to content

Commit

Permalink
Disallow InlineArray attribute on a record struct type.
Browse files Browse the repository at this point in the history
Closes dotnet#73504.
  • Loading branch information
AlekseyTs committed Jun 20, 2024
1 parent 8085c11 commit 84d2c6e
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7983,4 +7983,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="IDS_FeatureAllowsRefStructConstraint" xml:space="preserve">
<value>allows ref struct constraint</value>
</data>
<data name="ERR_InlineArrayAttributeOnRecord" xml:space="preserve">
<value>Attribute 'System.Runtime.CompilerServices.InlineArray' cannot be applied to a record struct.</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,8 @@ internal enum ErrorCode

INF_IdentifierConflictWithContextualKeyword = 9258,

ERR_InlineArrayAttributeOnRecord = 9259,

// Note: you will need to do the following after adding errors:
// 1) Update ErrorFacts.IsBuildOnlyDiagnostic (src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs)

Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,7 @@ or ErrorCode.ERR_PartialPropertyTypeDifference
or ErrorCode.WRN_PartialPropertySignatureDifference
or ErrorCode.ERR_PartialPropertyRequiredDifference
or ErrorCode.INF_IdentifierConflictWithContextualKeyword
or ErrorCode.ERR_InlineArrayAttributeOnRecord
=> false,
};
#pragma warning restore CS8524 // The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,10 @@ protected sealed override void DecodeWellKnownAttributeImpl(ref DecodeWellKnownA
{
diagnostics.Add(ErrorCode.ERR_AttributeOnBadSymbolType, arguments.AttributeSyntaxOpt.Name.Location, arguments.AttributeSyntaxOpt.GetErrorDisplayName(), "struct");
}
else if (IsRecordStruct)
{
diagnostics.Add(ErrorCode.ERR_InlineArrayAttributeOnRecord, arguments.AttributeSyntaxOpt.Name.Location);
}
}
else
{
Expand Down Expand Up @@ -1813,7 +1817,7 @@ protected override void AfterMembersCompletedChecks(BindingDiagnosticBag diagnos
}
}

if (TypeKind == TypeKind.Struct && HasInlineArrayAttribute(out _))
if (TypeKind == TypeKind.Struct && !IsRecordStruct && HasInlineArrayAttribute(out _))
{
if (Layout.Kind == LayoutKind.Explicit)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 77 additions & 4 deletions src/Compilers/CSharp/Test/Emit2/Semantics/InlineArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ struct Buffer
}

[Fact]
public void InlineArrayType_28()
public void InlineArrayType_28_Record()
{
var src = @"
[System.Runtime.CompilerServices.InlineArray(10)]
Expand All @@ -1434,9 +1434,9 @@ record struct Buffer(int p)
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,15): error CS9169: Inline array struct must declare one and only one instance field.
// record struct Buffer(int p)
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 15)
// (2,2): error CS9259: Attribute 'System.Runtime.CompilerServices.InlineArray' cannot be applied to a record struct.
// [System.Runtime.CompilerServices.InlineArray(10)]
Diagnostic(ErrorCode.ERR_InlineArrayAttributeOnRecord, "System.Runtime.CompilerServices.InlineArray").WithLocation(2, 2)
);

var buffer = comp.GlobalNamespace.GetTypeMember("Buffer");
Expand Down Expand Up @@ -2188,6 +2188,79 @@ static void Main()
);
}

[Fact]
public void InlineArrayType_52_Record()
{
var src = @"
[System.Runtime.CompilerServices.InlineArray(10)]
record struct Buffer(int p1, int p2)
{
}
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (2,2): error CS9259: Attribute 'System.Runtime.CompilerServices.InlineArray' cannot be applied to a record struct.
// [System.Runtime.CompilerServices.InlineArray(10)]
Diagnostic(ErrorCode.ERR_InlineArrayAttributeOnRecord, "System.Runtime.CompilerServices.InlineArray").WithLocation(2, 2)
);

var buffer = comp.GlobalNamespace.GetTypeMember("Buffer");

Assert.True(buffer.HasInlineArrayAttribute(out int length));
Assert.Equal(10, length);
Assert.Null(buffer.TryGetInlineArrayElementField());
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void InlineArrayType_53_Record(int size)
{
var src = @"
[System.Runtime.CompilerServices.InlineArray(" + size + @")]
record struct Buffer()
{
private int _element0;
}
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (2,2): error CS9259: Attribute 'System.Runtime.CompilerServices.InlineArray' cannot be applied to a record struct.
// [System.Runtime.CompilerServices.InlineArray(1)]
Diagnostic(ErrorCode.ERR_InlineArrayAttributeOnRecord, "System.Runtime.CompilerServices.InlineArray").WithLocation(2, 2)
);

var buffer = comp.GlobalNamespace.GetTypeMember("Buffer");

Assert.True(buffer.HasInlineArrayAttribute(out int length));
Assert.Equal(size, length);
Assert.Equal(SpecialType.System_Int32, buffer.TryGetInlineArrayElementField().Type.SpecialType);
}

[Fact]
public void InlineArrayType_54_Record()
{
var src = @"
[System.Runtime.CompilerServices.InlineArray(10)]
record struct Buffer()
{
}
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (2,2): error CS9259: Attribute 'System.Runtime.CompilerServices.InlineArray' cannot be applied to a record struct.
// [System.Runtime.CompilerServices.InlineArray(10)]
Diagnostic(ErrorCode.ERR_InlineArrayAttributeOnRecord, "System.Runtime.CompilerServices.InlineArray").WithLocation(2, 2)
);

var buffer = comp.GlobalNamespace.GetTypeMember("Buffer");

Assert.True(buffer.HasInlineArrayAttribute(out int length));
Assert.Equal(10, length);
Assert.Null(buffer.TryGetInlineArrayElementField());
}

[Fact]
public void Access_ArgumentType_01()
{
Expand Down

0 comments on commit 84d2c6e

Please sign in to comment.