-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Collection expressions: emit Array.Empty<T>() for [] with target type T[] or IEnumerable<T> #69355
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could also do for IReadOnlyList/IReadOnlyCollection. #Resolved
IL_0000: call "string[] System.Array.Empty<string>()" | ||
IL_0005: newobj "System.ReadOnlySpan<string>..ctor(string[])" | ||
IL_000a: call "MyCollection<string> MyCollectionBuilder.Create<string>(System.ReadOnlySpan<string>)" | ||
IL_000f: ret | ||
} | ||
"""); | ||
verifier.VerifyIL("Program.F1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pity about these last two. #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we are calling Array.Empty for these. Did I miss something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: consider adding equivalent pseudo-code // new ArrayType[0]
#Closed
type: arrayEmpty.ReturnType); | ||
} | ||
|
||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a test with missing Array.Empty
API? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with review pass (iteration 3). Minor nits only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks (iteration 4)
…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