Skip to content

Commit

Permalink
Remove unneeded WellKnownMember
Browse files Browse the repository at this point in the history
  • Loading branch information
cston committed Jul 31, 2023
1 parent 4cfe4e1 commit 75ead79
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,15 @@ private BoundExpression CreateAndPopulateInlineArray(
}

// Get a span to the inline array.
// ... (ReadOnlySpan<T>)InlineArrayAsSpan<__InlineArrayN<T>, T>(ref tmp, N)
var inlineArrayAsSpan = _factory.ModuleBuilderOpt.EnsureInlineArrayAsSpanExists(syntax, _factory.WellKnownType(WellKnownType.System_Span_T), intType, _diagnostics.DiagnosticBag);
inlineArrayAsSpan = inlineArrayAsSpan.Construct(ImmutableArray.Create(TypeWithAnnotations.Create(inlineArrayType), elementType));

var spanType = (NamedTypeSymbol)inlineArrayAsSpan.ReturnType;
var spanOperator = _factory.WellKnownMethod(WellKnownMember.System_Span_T__op_Implicit_ReadOnlySpan_Span).AsMember(spanType);
// ... InlineArrayAsReadOnlySpan<__InlineArrayN<T>, T>(in tmp, N)
var inlineArrayAsReadOnlySpan = _factory.ModuleBuilderOpt.EnsureInlineArrayAsReadOnlySpanExists(syntax, _factory.WellKnownType(WellKnownType.System_ReadOnlySpan_T), intType, _diagnostics.DiagnosticBag).
Construct(ImmutableArray.Create(TypeWithAnnotations.Create(inlineArrayType), elementType));
return _factory.Call(
receiver: null,
spanOperator,
_factory.Call(
receiver: null,
inlineArrayAsSpan,
inlineArrayLocal,
_factory.Literal(arrayLength),
useStrictArgumentRefKinds: true));
inlineArrayAsReadOnlySpan,
inlineArrayLocal,
_factory.Literal(arrayLength),
useStrictArgumentRefKinds: true);
}

private BoundExpression MakeCollectionExpressionSpreadElement(BoundCollectionExpressionSpreadElement initializer)
Expand Down
139 changes: 40 additions & 99 deletions src/Compilers/CSharp/Test/Emit2/Semantics/CollectionExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5736,7 +5736,7 @@ .maxstack 1
verifier.VerifyIL("Program.F1",
"""
{
// Code size 57 (0x39)
// Code size 52 (0x34)
.maxstack 2
.locals init (__InlineArray3<int> V_0)
IL_0000: ldloca.s V_0
Expand All @@ -5758,16 +5758,15 @@ .locals init (__InlineArray3<int> V_0)
IL_0025: stind.i4
IL_0026: ldloca.s V_0
IL_0028: ldc.i4.3
IL_0029: call "InlineArrayAsSpan<__InlineArray3<int>, int>(ref __InlineArray3<int>, int)"
IL_002e: call "System.ReadOnlySpan<int> System.Span<int>.op_Implicit(System.Span<int>)"
IL_0033: call "MyCollection<int> MyCollectionBuilder.Create<int>(System.ReadOnlySpan<int>)"
IL_0038: ret
IL_0029: call "InlineArrayAsReadOnlySpan<__InlineArray3<int>, int>(in __InlineArray3<int>, int)"
IL_002e: call "MyCollection<int> MyCollectionBuilder.Create<int>(System.ReadOnlySpan<int>)"
IL_0033: ret
}
""");
verifier.VerifyIL("Program.F2",
"""
{
// Code size 62 (0x3e)
// Code size 57 (0x39)
.maxstack 2
.locals init (__InlineArray3<object> V_0)
IL_0000: ldloca.s V_0
Expand All @@ -5790,10 +5789,9 @@ .locals init (__InlineArray3<object> V_0)
IL_002a: stind.ref
IL_002b: ldloca.s V_0
IL_002d: ldc.i4.3
IL_002e: call "InlineArrayAsSpan<__InlineArray3<object>, object>(ref __InlineArray3<object>, int)"
IL_0033: call "System.ReadOnlySpan<object> System.Span<object>.op_Implicit(System.Span<object>)"
IL_0038: call "MyCollection<object> MyCollectionBuilder.Create<object>(System.ReadOnlySpan<object>)"
IL_003d: ret
IL_002e: call "InlineArrayAsReadOnlySpan<__InlineArray3<object>, object>(in __InlineArray3<object>, int)"
IL_0033: call "MyCollection<object> MyCollectionBuilder.Create<object>(System.ReadOnlySpan<object>)"
IL_0038: ret
}
""");

Expand Down Expand Up @@ -5934,7 +5932,7 @@ static void Main()
verifier.VerifyIL("Program.F",
"""
{
// Code size 79 (0x4f)
// Code size 74 (0x4a)
.maxstack 2
.locals init (__InlineArray3<int?> V_0)
IL_0000: ldloca.s V_0
Expand All @@ -5957,10 +5955,9 @@ .locals init (__InlineArray3<int?> V_0)
IL_0036: initobj "int?"
IL_003c: ldloca.s V_0
IL_003e: ldc.i4.3
IL_003f: call "InlineArrayAsSpan<__InlineArray3<int?>, int?>(ref __InlineArray3<int?>, int)"
IL_0044: call "System.ReadOnlySpan<int?> System.Span<int?>.op_Implicit(System.Span<int?>)"
IL_0049: call "MyCollection<int?> MyCollectionBuilder.Create<int?>(System.ReadOnlySpan<int?>)"
IL_004e: ret
IL_003f: call "InlineArrayAsReadOnlySpan<__InlineArray3<int?>, int?>(in __InlineArray3<int?>, int)"
IL_0044: call "MyCollection<int?> MyCollectionBuilder.Create<int?>(System.ReadOnlySpan<int?>)"
IL_0049: ret
}
""");
}
Expand Down Expand Up @@ -6228,7 +6225,7 @@ static MyCollection<object> F()
verifier.VerifyIL("Program.F",
$$"""
{
// Code size 72 (0x48)
// Code size 67 (0x43)
.maxstack 2
.locals init (__InlineArray3<object> V_0)
IL_0000: ldloca.s V_0
Expand All @@ -6253,10 +6250,9 @@ .locals init (__InlineArray3<object> V_0)
IL_0034: stind.ref
IL_0035: ldloca.s V_0
IL_0037: ldc.i4.3
IL_0038: call "InlineArrayAsSpan<__InlineArray3<object>, object>(ref __InlineArray3<object>, int)"
IL_003d: call "System.ReadOnlySpan<object> System.Span<object>.op_Implicit(System.Span<object>)"
IL_0042: call "MyCollection<object> MyCollectionBuilder.Create<object>({{qualifier}}System.ReadOnlySpan<object>)"
IL_0047: ret
IL_0038: call "InlineArrayAsReadOnlySpan<__InlineArray3<object>, object>(in __InlineArray3<object>, int)"
IL_003d: call "MyCollection<object> MyCollectionBuilder.Create<object>(scoped System.ReadOnlySpan<object>)"
IL_0042: ret
}
""");
}
Expand Down Expand Up @@ -9258,60 +9254,6 @@ static void Main()
Diagnostic(ErrorCode.ERR_EscapeVariable, "items").WithArguments("scoped System.ReadOnlySpan<T> items").WithLocation(16, 82));
}

[CombinatorialData]
[Theory]
public void CollectionBuilder_MissingSpanMembers(bool useCompilationReference)
{
string sourceA = """
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
[CollectionBuilder(typeof(MyCollectionBuilder), nameof(MyCollectionBuilder.Create))]
public struct MyCollection<T>
{
public IEnumerator<T> GetEnumerator() => default;
}
public class MyCollectionBuilder
{
public static MyCollection<T> Create<T>(ReadOnlySpan<T> items) => default;
}
""";
var comp = CreateCompilation(new[] { sourceA, CollectionBuilderAttributeDefinition }, targetFramework: TargetFramework.Net80);
var refA = AsReference(comp, useCompilationReference);

string sourceB = """
#pragma warning disable 219
class Program
{
static void Main()
{
MyCollection<string> x = [];
MyCollection<int> y = [1, 2, 3];
MyCollection<object> z = new();
}
}
""";
comp = CreateCompilation(sourceB, references: new[] { refA }, targetFramework: TargetFramework.Net80);
comp.MakeTypeMissing(WellKnownType.System_Span_T);
comp.VerifyEmitDiagnostics(
// error CS0656: Missing compiler required member 'System.Runtime.InteropServices.MemoryMarshal.CreateSpan'
//
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "").WithArguments("System.Runtime.InteropServices.MemoryMarshal", "CreateSpan").WithLocation(1, 1),
// (7,31): error CS0518: Predefined type 'System.Span`1' is not defined or imported
// MyCollection<int> y = [1, 2, 3];
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "[1, 2, 3]").WithArguments("System.Span`1").WithLocation(7, 31),
// (7,31): error CS0656: Missing compiler required member 'System.Span`1.op_Implicit'
// MyCollection<int> y = [1, 2, 3];
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "[1, 2, 3]").WithArguments("System.Span`1", "op_Implicit").WithLocation(7, 31));

comp = CreateCompilation(sourceB, references: new[] { refA }, targetFramework: TargetFramework.Net80);
comp.MakeMemberMissing(WellKnownMember.System_Span_T__op_Implicit_ReadOnlySpan_Span);
comp.VerifyEmitDiagnostics(
// (7,31): error CS0656: Missing compiler required member 'System.Span`1.op_Implicit'
// MyCollection<int> y = [1, 2, 3];
Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "[1, 2, 3]").WithArguments("System.Span`1", "op_Implicit").WithLocation(7, 31));
}

[Fact]
public void CollectionBuilder_Async()
{
Expand Down Expand Up @@ -9360,7 +9302,7 @@ static async Task<int> F(int i)
verifier.VerifyIL("Program.<CreateCollection>d__1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext()",
"""
{
// Code size 329 (0x149)
// Code size 324 (0x144)
.maxstack 3
.locals init (int V_0,
MyCollection<int> V_1,
Expand Down Expand Up @@ -9401,7 +9343,7 @@ .locals init (int V_0,
IL_004a: ldloca.s V_4
IL_004c: ldarg.0
IL_004d: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>>.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter<int>, Program.<CreateCollection>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter<int>, ref Program.<CreateCollection>d__1)"
IL_0052: leave IL_0148
IL_0052: leave IL_0143
IL_0057: ldarg.0
IL_0058: ldfld "System.Runtime.CompilerServices.TaskAwaiter<int> Program.<CreateCollection>d__1.<>u__1"
IL_005d: stloc.s V_4
Expand Down Expand Up @@ -9448,7 +9390,7 @@ .locals init (int V_0,
IL_00c5: ldloca.s V_4
IL_00c7: ldarg.0
IL_00c8: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>>.AwaitUnsafeOnCompleted<System.Runtime.CompilerServices.TaskAwaiter<int>, Program.<CreateCollection>d__1>(ref System.Runtime.CompilerServices.TaskAwaiter<int>, ref Program.<CreateCollection>d__1)"
IL_00cd: leave.s IL_0148
IL_00cd: leave.s IL_0143
IL_00cf: ldarg.0
IL_00d0: ldfld "System.Runtime.CompilerServices.TaskAwaiter<int> Program.<CreateCollection>d__1.<>u__1"
IL_00d5: stloc.s V_4
Expand All @@ -9472,32 +9414,31 @@ .locals init (int V_0,
IL_0102: ldarg.0
IL_0103: ldflda "__InlineArray3<int> Program.<CreateCollection>d__1.<>7__wrap1"
IL_0108: ldc.i4.3
IL_0109: call "InlineArrayAsSpan<__InlineArray3<int>, int>(ref __InlineArray3<int>, int)"
IL_010e: call "System.ReadOnlySpan<int> System.Span<int>.op_Implicit(System.Span<int>)"
IL_0113: call "MyCollection<int> MyCollectionBuilder.Create<int>(System.ReadOnlySpan<int>)"
IL_0118: stloc.1
IL_0119: leave.s IL_0134
IL_0109: call "InlineArrayAsReadOnlySpan<__InlineArray3<int>, int>(in __InlineArray3<int>, int)"
IL_010e: call "MyCollection<int> MyCollectionBuilder.Create<int>(System.ReadOnlySpan<int>)"
IL_0113: stloc.1
IL_0114: leave.s IL_012f
}
catch System.Exception
{
IL_011b: stloc.s V_5
IL_011d: ldarg.0
IL_011e: ldc.i4.s -2
IL_0120: stfld "int Program.<CreateCollection>d__1.<>1__state"
IL_0125: ldarg.0
IL_0126: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>> Program.<CreateCollection>d__1.<>t__builder"
IL_012b: ldloc.s V_5
IL_012d: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>>.SetException(System.Exception)"
IL_0132: leave.s IL_0148
IL_0116: stloc.s V_5
IL_0118: ldarg.0
IL_0119: ldc.i4.s -2
IL_011b: stfld "int Program.<CreateCollection>d__1.<>1__state"
IL_0120: ldarg.0
IL_0121: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>> Program.<CreateCollection>d__1.<>t__builder"
IL_0126: ldloc.s V_5
IL_0128: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>>.SetException(System.Exception)"
IL_012d: leave.s IL_0143
}
IL_0134: ldarg.0
IL_0135: ldc.i4.s -2
IL_0137: stfld "int Program.<CreateCollection>d__1.<>1__state"
IL_013c: ldarg.0
IL_013d: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>> Program.<CreateCollection>d__1.<>t__builder"
IL_0142: ldloc.1
IL_0143: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>>.SetResult(MyCollection<int>)"
IL_0148: ret
IL_012f: ldarg.0
IL_0130: ldc.i4.s -2
IL_0132: stfld "int Program.<CreateCollection>d__1.<>1__state"
IL_0137: ldarg.0
IL_0138: ldflda "System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>> Program.<CreateCollection>d__1.<>t__builder"
IL_013d: ldloc.1
IL_013e: call "void System.Runtime.CompilerServices.AsyncTaskMethodBuilder<MyCollection<int>>.SetResult(MyCollection<int>)"
IL_0143: ret
}
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,6 @@ public void AllWellKnownTypeMembers()
case WellKnownMember.System_Span_T__get_Item:
case WellKnownMember.System_Span_T__get_Length:
case WellKnownMember.System_Span_T__Slice_Int_Int:
case WellKnownMember.System_Span_T__op_Implicit_ReadOnlySpan_Span:
case WellKnownMember.System_ReadOnlySpan_T__ctor_Pointer:
case WellKnownMember.System_ReadOnlySpan_T__ctor_Array:
case WellKnownMember.System_ReadOnlySpan_T__ctor_Array_Start_Length:
Expand Down
1 change: 0 additions & 1 deletion src/Compilers/Core/Portable/WellKnownMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ internal enum WellKnownMember
System_Span_T__get_Item,
System_Span_T__get_Length,
System_Span_T__Slice_Int_Int,
System_Span_T__op_Implicit_ReadOnlySpan_Span,

System_ReadOnlySpan_T__ctor_Pointer,
System_ReadOnlySpan_T__ctor_Array,
Expand Down
15 changes: 0 additions & 15 deletions src/Compilers/Core/Portable/WellKnownMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3412,20 +3412,6 @@ static WellKnownMembers()
(byte)SignatureTypeCode.TypeHandle, (byte)SpecialType.System_Int32,
(byte)SignatureTypeCode.TypeHandle, (byte)SpecialType.System_Int32,

// System_Span_T__op_Implicit_ReadOnlySpan_Span
(byte)(MemberFlags.Method | MemberFlags.Static), // Flags
(byte)WellKnownType.ExtSentinel, (byte)(WellKnownType.System_Span_T - WellKnownType.ExtSentinel), // DeclaringTypeId
0, // Arity
1, // Method Signature
(byte)SignatureTypeCode.GenericTypeInstance, // Return Type
(byte)SignatureTypeCode.TypeHandle, (byte)WellKnownType.ExtSentinel, (byte)(WellKnownType.System_ReadOnlySpan_T - WellKnownType.ExtSentinel),
1,
(byte)SignatureTypeCode.GenericTypeParameter, 0,
(byte)SignatureTypeCode.GenericTypeInstance,
(byte)SignatureTypeCode.TypeHandle, (byte)WellKnownType.ExtSentinel, (byte)(WellKnownType.System_Span_T - WellKnownType.ExtSentinel),
1,
(byte)SignatureTypeCode.GenericTypeParameter, 0,

// System_ReadOnlySpan_T__ctor_Pointer
(byte)(MemberFlags.Constructor), // Flags
(byte)WellKnownType.ExtSentinel, (byte)(WellKnownType.System_ReadOnlySpan_T - WellKnownType.ExtSentinel), // DeclaringTypeId
Expand Down Expand Up @@ -4503,7 +4489,6 @@ static WellKnownMembers()
"get_Item", // System_Span_T__get_Item
"get_Length", // System_Span_T__get_Length
"Slice", // System_Span_T__Slice_Int_Int
"op_Implicit", // System_Span_T__op_Implicit_ReadOnlySpan_Span
".ctor", // System_ReadOnlySpan_T__ctor_Pointer
".ctor", // System_ReadOnlySpan_T__ctor_Array
".ctor", // System_ReadOnlySpan_T__ctor_Array_Start_Length
Expand Down
Loading

0 comments on commit 75ead79

Please sign in to comment.