Collection expression arguments: initial binding and lowering#76903
Collection expression arguments: initial binding and lowering#76903cston merged 32 commits intodotnet:features/dictionary-expressionsfrom
Conversation
…s' into collection-args
…s' into collection-args
| </data> | ||
| <data name="IDS_FeatureCollectionExpressionArguments" xml:space="preserve"> | ||
| <value>collection expression arguments</value> | ||
| </data> |
There was a problem hiding this comment.
Yes, these two resource strings are essentially the ones you added and removed from #76003.
| </data> | ||
| <data name="ERR_CollectionArgumentsMustBeFirst" xml:space="preserve"> | ||
| <value>Collection arguments must be the first element.</value> | ||
| </data> |
There was a problem hiding this comment.
Grr * 2.
Note i think this should be called "collection argument element" or just "'with' element. Saying the arguments (plural) must be the first element (singular) reads oddly to me. #Closed
| { | ||
| internal partial class BoundUnconvertedCollectionArguments | ||
| { | ||
| internal void GetArguments(AnalyzedArguments analyzedArguments) |
There was a problem hiding this comment.
fwiw, havin thtis be called GetArguments and not return something reads oddly. Consider .AddArgumentsTo, which makes it much clearer that the receiver is adding the arguments to the passed in value. #ByDesign
There was a problem hiding this comment.
I'd like to leave this as is. The intended use is to populate the AnalyzedArguments collection with just the arguments, rather than to add to an existing collection (we assert the collection is empty initially), so renaming to AddArgumentsTo might make the intent less clear.
There was a problem hiding this comment.
Would "BuildArguments" or "CollectArguments" solve the problem?
There was a problem hiding this comment.
When we support collection builder types, where the factory method has an additional, implicit ReadOnlySpan<T> elements argument, it may make sense to rename this to AddArguments() or something similar. For now though, GetArguments() seems appropriate to me.
9a04311 to
b41f88f
Compare
f211702 to
19d5fa0
Compare
| ERR_ImplicitlyTypedParamsParameter = 9272, | ||
| ERR_VariableDeclarationNamedField = 9273, | ||
| ERR_CollectionExpressionKeyValuePairNotSupported = 9274, | ||
| ERR_CollectionArgumentsMustBeFirst = 9275, |
There was a problem hiding this comment.
nit: You may want to leave a gap between errorcodes in main branch and those in dictionary expression branch, so that you don't have to re-number too much (hopefully only once)
| // We only get here if the conversion of the collection expression to the | ||
| // target type failed. In this case, simply visit each argument. | ||
| VisitArgumentsEvaluate(collectionArguments.Arguments, collectionArguments.ArgumentRefKindsOpt, parameterAnnotationsOpt: default, defaultArguments: default); | ||
| break; |
There was a problem hiding this comment.
Consider leaving a PROTOTYPE note or tracking a follow-up elsewhere to implement nullability analysis on happy cases (where the conversion succeeds) #Closed
| <Field Name="Value" Type="BoundExpression"/> | ||
| </Node> | ||
|
|
||
| <Node Name="BoundUnconvertedCollectionArguments" Base="BoundNode"> |
There was a problem hiding this comment.
nit: consider "BoundWithElement" since we already use the "...Element" naming convention for various collection expression elements (spread, key-value pair) and it would line up with the name of the syntax node ("WithElementSyntax")
Also, there's no "Converted" version of this node, so "Unconverted" feels odd #Closed
| MessageID.IDS_FeatureCollectionExpressionArguments.CheckFeatureAvailability(diagnostics, syntax.WithKeyword); | ||
|
|
||
| var arguments = AnalyzedArguments.GetInstance(); | ||
| BindArgumentsAndNames(syntax.ArgumentList, diagnostics, arguments, allowArglist: true); |
There was a problem hiding this comment.
Good catch, thanks, I've added tests.
That said, we should consider not supporting __arglist.
There was a problem hiding this comment.
I'd be fine not supporting :-) I just assumed you wanted anything that is supported in explicit object creation syntax to be allowed
There was a problem hiding this comment.
Added an open question in dotnet/csharplang#9115.
src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionArgumentTests.cs
Show resolved
Hide resolved
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 23)
src/Compilers/CSharp/Portable/BoundTree/BoundUnconvertedCollectionArguments.cs
Outdated
Show resolved
Hide resolved
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 27)
src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionArgumentTests.cs
Show resolved
Hide resolved
| """; | ||
| var comp = CreateCompilation([sourceA, sourceB]); | ||
| comp.VerifyEmitDiagnostics( | ||
| // (7,13): error CS0417: 'U': cannot provide arguments when creating an instance of a variable type |
There was a problem hiding this comment.
I'll be honest: I've reread this error 5 times and still have no idea what it's tell me. Is the issue that we can't find an appropriate constructor? #Resolved
There was a problem hiding this comment.
This is the error reported when constructing an instance of the type parameter with arguments: new U(t). sharplab.io
f5224d8
into
dotnet:features/dictionary-expressions
| var comp = CreateCompilation([sourceA, sourceB]); | ||
| comp.VerifyEmitDiagnostics( | ||
| // (5,5): error CS9223: Creation of params collection 'MyCollection<int>' results in an infinite chain of invocation of constructor 'MyCollection<T>.MyCollection()'. | ||
| // c = [with(1)]; |
There was a problem hiding this comment.
@cston I may be wrong, but is it really an error? We will pass 1 to MyCollection<T>.MyCollection(params MyCollection<T>), then will create empty instance of MyCollection<T> by parameterless constructor and just add this 1 via .Add method. If we unwrap withElement sharplab then there is no error before
Binding and lowering of collection expression arguments for
classandstructtarget types that implementIEnumerableand do not have a[CollectionBuilder]attribute.See proposals/collection-expression-arguments.md
Relates to test plan #76310