Collection expressions: emit Array.Empty<T>() for [] with target type T[] or IEnumerable<T>#69355
Collection expressions: emit Array.Empty<T>() for [] with target type T[] or IEnumerable<T>#69355cston merged 4 commits intodotnet:mainfrom
Conversation
… T[] or IEnumerable<T>
|
Question: would it make sense to emit |
|
Does array.empty not optimize the enumerator it returns either? |
| var collectionType = (NamedTypeSymbol)node.Type; | ||
| BoundExpression? arrayOrList; | ||
|
|
||
| // For [] with target type IEnumerable<T>, use Array.Empty<T>() rather than List<T>. |
There was a problem hiding this comment.
could also do for IReadOnlyList/IReadOnlyCollection. #Resolved
| IL_000f: ret | ||
| } | ||
| """); | ||
| verifier.VerifyIL("Program.F1", |
There was a problem hiding this comment.
add test where the destination is an int*[][] this should be able to use Array.Empty<int*[]> (inner type is pointer, outer is array). #Resolved
| static ICollection<string> EmptyICollection() => []; | ||
| static IList<string> EmptyIList() => []; | ||
| static IReadOnlyCollection<string> EmptyIReadOnlyCollection() => []; | ||
| static IReadOnlyList<string> EmptyIReadOnlyList() => []; |
There was a problem hiding this comment.
pity about these last two. #Resolved
There was a problem hiding this comment.
It looks like we are calling Array.Empty for these. Did I miss something?
There was a problem hiding this comment.
Yes, these cases were added in commit 2 response to #69355 (comment).
It looks like |
|
|
/azp run roslyn-CI |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Is it recommended to use [] for immutable array as well? |
Yes. |
| var collectionType = (NamedTypeSymbol)node.Type; | ||
| BoundExpression arrayOrList; | ||
|
|
||
| // Use Array.Empty<T>() rather than List<T> for an empty collection expression when |
There was a problem hiding this comment.
I know things are in flux here but the implementation seems fine.
| static ICollection<string> EmptyICollection() => []; | ||
| static IList<string> EmptyIList() => []; | ||
| static IReadOnlyCollection<string> EmptyIReadOnlyCollection() => []; | ||
| static IReadOnlyList<string> EmptyIReadOnlyList() => []; |
There was a problem hiding this comment.
It looks like we are calling Array.Empty for these. Did I miss something?
| { | ||
| return arrayEmpty; | ||
| } | ||
| return new BoundArrayCreation( |
There was a problem hiding this comment.
nit: consider adding equivalent pseudo-code // new ArrayType[0] #Closed
| type: arrayEmpty.ReturnType); | ||
| } | ||
|
|
||
| return null; |
There was a problem hiding this comment.
nit: consider inverting above if #Closed
| IL_0000: ldc.i4.0 | ||
| IL_0001: newarr "int" | ||
| IL_0006: ret | ||
| IL_0000: call "int[] System.Array.Empty<int>()" |
There was a problem hiding this comment.
Do we have a test with missing Array.Empty API? #Closed
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 3). Minor nits only
…get type T[] or IEnumerable<T> (dotnet#69355)" This reverts commit d838fac.
Emit
Array.Empty<T>()for an empty collection expression when the target type is:T[]IEnumerable<T>IReadOnlyCollection<T>IReadOnlyList<T>Relates to test plan #66418