Skip to content

STALE: Add IOp support for with(...) elements.#81058

Merged
CyrusNajmabadi merged 88 commits into
dotnet:features/collection-expression-argumentsfrom
CyrusNajmabadi:creationArguments
Nov 24, 2025
Merged

STALE: Add IOp support for with(...) elements.#81058
CyrusNajmabadi merged 88 commits into
dotnet:features/collection-expression-argumentsfrom
CyrusNajmabadi:creationArguments

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Nov 5, 2025

Relates to test plan #80613
Closes #80998

@dotnet-policy-service dotnet-policy-service Bot added VSCode Needs UX Triage Needs API Review Needs to be reviewed by the API review council labels Nov 5, 2025
Comment thread src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs Outdated
@@ -393,8 +393,17 @@ static void Main()
var operation = semanticModel.GetOperation(root.DescendantNodes().OfType<CollectionExpressionSyntax>().ToArray()[1]);
VerifyOperationTree(compilation, operation, """
ICollectionExpressionOperation (1 elements, ConstructMethod: MyList<System.Int32>..ctor([System.Int32 capacity = 0], [System.String name = "default"])) (OperationKind.CollectionExpression, Type: MyList<System.Int32>) (Syntax: '[with(capacity: 10), 2]')
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review with whitespace off.

{
EvalStackFrame frame = PushStackFrame();
var creationArguments = VisitArguments(operation.CreationArguments, instancePushed: false);
PopStackFrame(frame);
Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seemed sensible to me to push/pop a frame here independent of the frame for visiting the elements. But i wasn't 100% sure. LMK if this is correct. #Closed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the decision to keep the frames for the arguments and elements "independent" versus nested like in VisitObjectCreation could impact.

Copy link
Copy Markdown
Member

@jcouv jcouv Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Aleksey can advise. Otherwise this does need some more research
I suspect we can do await with the split

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: my intent was to match VisitObjectCreation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: my intent was to match VisitObjectCreation.

Thanks for the clarification
In that case, would it be an outer frame with an inner frame (push, push, pop, pop) instead of two separate frames (push, pop, push, pop)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Aleksey can advise.

See my other comments

</summary>
</Comments>
</Property>
<Property Name="CreationArguments" Type="ImmutableArray&lt;IArgumentOperation&gt;">
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i went with CreationArguments as i think it's nicely 'generic'. They are the arguments that control the 'creation' of the collection. I didn't really want to be very explicit that they were the 'WithElement' arguments. But i don't feel strongly on this. If people think it should align more closely with the syntax, that's ok with me.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like ConstructArguments to emphasize the connection with ConstructMethod would be reasonable here. However, I also don't feel strongly. :)

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review November 5, 2025 20:33
@CyrusNajmabadi CyrusNajmabadi requested review from a team as code owners November 5, 2025 20:33
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@RikkiGibson @jjonescz @333fred ptal.

// (12,74): warning CS8620: Argument of type 'T' cannot be used for parameter 'items' of type 'ReadOnlySpan<T?>' in 'MyCollection<T?> MyCollectionBuilder.Create<T?>(ReadOnlySpan<T?> items)' due to differences in the nullability of reference types.
// static IMyCollection<T?> F<T>(ReadOnlySpan<T> items, T arg) => [with(arg), ..items];
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "arg").WithArguments("T", "System.ReadOnlySpan<T?>", "items", "MyCollection<T?> MyCollectionBuilder.Create<T?>(ReadOnlySpan<T?> items)").WithLocation(12, 74));
Diagnostic(ErrorCode.ERR_BadCollectionArgumentsArgCount, "with(arg)").WithArguments("Create", "1").WithLocation(12, 69));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this now matches CollectionBuilder_SpreadElement_BoxingConversion_A, which also has no nullable warning here. the only error we should report is the bad-with, since we only bind to (ReadOnlySpan<T> items) not (ReadOnlySpan<T> items, T arg) (since arguments can't go at builder end, they must be at the start).

@jcouv jcouv added the Feature - Collection Arguments Collection expression with() arguments label Nov 6, 2025
@jcouv
Copy link
Copy Markdown
Member

jcouv commented Nov 6, 2025

Closes #80998 ?

Comment thread src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Operations/CSharpOperationFactory.cs Outdated
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Assert added in d4da134

Comment thread src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs Outdated
@AlekseyTs
Copy link
Copy Markdown
Contributor

AlekseyTs commented Nov 20, 2025

Done with review pass (commit 75), only glanced over the test changes #Closed

@jcouv jcouv dismissed their stale review November 20, 2025 15:23

Code changes

@AlekseyTs
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi It looks like there are legitimate CI failures

}

[Fact]
public void ControlFlowBuilder()
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ControlFlowBuilder

I think this test should be moved to IOperation tests #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved.

IFlowCaptureReferenceOperation: 0 (OperationKind.FlowCaptureReference, Type: System.Int32, IsImplicit) (Syntax: 'ComputeCapacity()')
InConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
OutConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
IArgumentOperation (ArgumentKind.Explicit, Matching Parameter: items) (OperationKind.Argument, Type: null, IsImplicit) (Syntax: 'with(ComputeCapacity())')
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicit

I assume this isn't an explicit argument #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah. good catch. investigating.

Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened issue #81377 to track this.

Comment thread src/Compilers/Core/Portable/Operations/ControlFlowGraphBuilder.cs Outdated
@AlekseyTs
Copy link
Copy Markdown
Contributor

Done with review pass (commit 83), only glanced over the test changes

@CyrusNajmabadi CyrusNajmabadi merged commit 62026b2 into dotnet:features/collection-expression-arguments Nov 24, 2025
26 of 29 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the creationArguments branch November 24, 2025 14:13
@AlekseyTs
Copy link
Copy Markdown
Contributor

AlekseyTs commented Nov 24, 2025

@CyrusNajmabadi It looks like there are no sign-offs for this PR. Am I missing something?

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

I didn't merge this. I have no idea how a merge would even be possible given that there were no sign-offs...

@AlekseyTs
Copy link
Copy Markdown
Contributor

I didn't merge this. I have no idea how a merge would even be possible given that there were no sign-offs...

Let's revert the merge then. Based on GitHub status updates, it merged and deleted the branch on you behalf. If that was not the case, I guess it would be good to understand what happened.

@CyrusNajmabadi CyrusNajmabadi restored the creationArguments branch November 24, 2025 19:27
@CyrusNajmabadi CyrusNajmabadi changed the title Add IOp support for with(...) elements. DEADPR: Add IOp support for with(...) elements. Nov 26, 2025
@CyrusNajmabadi CyrusNajmabadi changed the title DEADPR: Add IOp support for with(...) elements. STALE: Add IOp support for with(...) elements. Nov 26, 2025
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

PR continues in #81427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers Feature - Collection Arguments Collection expression with() arguments Needs API Review Needs to be reviewed by the API review council Needs UX Triage VSCode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants