Skip to content
代码片段 群组 项目
未验证 提交 7931076b 编辑于 作者: AlekseyTs's avatar AlekseyTs 提交者: GitHub
浏览文件

Allow declaring ref fields as elements of inline arrays. (#68820)

Closes #68819.
Consumption of such inline array types is still not supported.
上级 02dc1194
No related branches found
No related tags found
无相关合并请求
显示
87 个添加49 个删除
......@@ -7641,7 +7641,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>Inline array struct must not have explicit layout.</value>
</data>
<data name="ERR_InvalidInlineArrayFields" xml:space="preserve">
<value>Inline array struct must declare one and only one instance field which must not be a ref field.</value>
<value>Inline array struct must declare one and only one instance field.</value>
</data>
<data name="ERR_ExpressionTreeContainsInlineArrayOperation" xml:space="preserve">
<value>An expression tree may not contain an inline array access or conversion</value>
......
......@@ -1752,7 +1752,7 @@ protected override void AfterMembersCompletedChecks(BindingDiagnosticBag diagnos
diagnostics.Add(ErrorCode.ERR_InvalidInlineArrayLayout, GetFirstLocation());
}
if (TryGetInlineArrayElementField() is null)
if (TryGetPossiblyUnsupportedByLanguageInlineArrayElementField() is null)
{
diagnostics.Add(ErrorCode.ERR_InvalidInlineArrayFields, GetFirstLocation());
}
......
......@@ -2470,7 +2470,7 @@ ITypeSymbol ITypeSymbolInternal.GetITypeSymbol()
internal abstract bool HasInlineArrayAttribute(out int length);
#nullable enable
internal FieldSymbol? TryGetInlineArrayElementField()
internal FieldSymbol? TryGetPossiblyUnsupportedByLanguageInlineArrayElementField()
{
Debug.Assert(HasInlineArrayAttribute(out var length) && length > 0);
......@@ -2482,7 +2482,7 @@ ITypeSymbol ITypeSymbolInternal.GetITypeSymbol()
{
if (!field.IsStatic)
{
if (field.RefKind != RefKind.None || elementField is not null)
if (elementField is not null)
{
return null;
}
......@@ -2501,5 +2501,10 @@ ITypeSymbol ITypeSymbolInternal.GetITypeSymbol()
return elementField;
}
internal FieldSymbol? TryGetInlineArrayElementField()
{
return TryGetPossiblyUnsupportedByLanguageInlineArrayElementField() is { RefKind: RefKind.None } field ? field : null;
}
}
}
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -1048,8 +1048,8 @@
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayFields">
<source>Inline array struct must declare one and only one instance field which must not be a ref field.</source>
<target state="new">Inline array struct must declare one and only one instance field which must not be a ref field.</target>
<source>Inline array struct must declare one and only one instance field.</source>
<target state="new">Inline array struct must declare one and only one instance field.</target>
<note />
</trans-unit>
<trans-unit id="ERR_InvalidInlineArrayLayout">
......
......@@ -447,7 +447,7 @@ struct Buffer
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,8): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// (3,8): error CS9169: Inline array struct must declare one and only one instance field.
// struct Buffer
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 8)
);
......@@ -512,7 +512,7 @@ struct Buffer
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,8): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// (3,8): error CS9169: Inline array struct must declare one and only one instance field.
// struct Buffer
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 8)
);
......@@ -602,7 +602,7 @@ struct Buffer
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,8): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// (3,8): error CS9169: Inline array struct must declare one and only one instance field.
// struct Buffer
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 8)
);
......@@ -983,17 +983,28 @@ ref struct Buffer
private ref int _element0;
}
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
string consumer = @"
class C
{
void Test(Buffer b)
{
_ = b[0];
}
}
";
var comp = CreateCompilation(consumer + src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,12): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// ref struct Buffer
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 12)
// (6,13): error CS0021: Cannot apply indexing with [] to an expression of type 'Buffer'
// _ = b[0];
Diagnostic(ErrorCode.ERR_BadIndexLHS, "b[0]").WithArguments("Buffer").WithLocation(6, 13)
);
 
verify(comp);
 
var ilSource = @"
.class private sequential ansi sealed beforefieldinit Buffer
.class public sequential ansi sealed beforefieldinit Buffer
extends [mscorlib]System.ValueType
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = (
......@@ -1028,7 +1039,13 @@ .maxstack 8
}
";
 
comp = CreateCompilationWithIL("", ilSource, options: TestOptions.DebugDll.WithMetadataImportOptions(MetadataImportOptions.All));
comp = CreateCompilationWithIL(consumer, ilSource, options: TestOptions.DebugDll.WithMetadataImportOptions(MetadataImportOptions.All));
comp.VerifyDiagnostics(
// (6,13): error CS0021: Cannot apply indexing with [] to an expression of type 'Buffer'
// _ = b[0];
Diagnostic(ErrorCode.ERR_BadIndexLHS, "b[0]").WithArguments("Buffer").WithLocation(6, 13)
);
verify(comp);
 
void verify(CSharpCompilation comp)
......@@ -1051,17 +1068,27 @@ ref struct Buffer
private ref readonly int _element0;
}
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
string consumer = @"
class C
{
void Test(Buffer b)
{
_ = b[0];
}
}
";
var comp = CreateCompilation(consumer + src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,12): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// ref struct Buffer
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 12)
// (6,13): error CS0021: Cannot apply indexing with [] to an expression of type 'Buffer'
// _ = b[0];
Diagnostic(ErrorCode.ERR_BadIndexLHS, "b[0]").WithArguments("Buffer").WithLocation(6, 13)
);
 
verify(comp);
 
var ilSource = @"
.class private sequential ansi sealed beforefieldinit Buffer
.class public sequential ansi sealed beforefieldinit Buffer
extends [mscorlib]System.ValueType
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = (
......@@ -1099,7 +1126,13 @@ .maxstack 8
}
";
 
comp = CreateCompilationWithIL("", ilSource, options: TestOptions.DebugDll.WithMetadataImportOptions(MetadataImportOptions.All));
comp = CreateCompilationWithIL(consumer, ilSource, options: TestOptions.DebugDll.WithMetadataImportOptions(MetadataImportOptions.All));
comp.VerifyDiagnostics(
// (6,13): error CS0021: Cannot apply indexing with [] to an expression of type 'Buffer'
// _ = b[0];
Diagnostic(ErrorCode.ERR_BadIndexLHS, "b[0]").WithArguments("Buffer").WithLocation(6, 13)
);
verify(comp);
 
void verify(CSharpCompilation comp)
......@@ -1346,7 +1379,7 @@ struct Buffer
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,8): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// (3,8): error CS9169: Inline array struct must declare one and only one instance field.
// struct Buffer
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 8)
);
......@@ -1371,7 +1404,7 @@ struct Buffer
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,8): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// (3,8): error CS9169: Inline array struct must declare one and only one instance field.
// struct Buffer
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 8)
);
......@@ -1395,7 +1428,7 @@ 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 which must not be a ref field.
// (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)
);
......@@ -1693,7 +1726,7 @@ struct Buffer(int p)
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
comp.VerifyDiagnostics(
// (3,8): error CS9169: Inline array struct must declare one and only one instance field which must not be a ref field.
// (3,8): error CS9169: Inline array struct must declare one and only one instance field.
// struct Buffer(int p)
Diagnostic(ErrorCode.ERR_InvalidInlineArrayFields, "Buffer").WithLocation(3, 8)
);
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册