Skip to content

Add support for with(...) elements in collection expressions.#80495

Merged
CyrusNajmabadi merged 112 commits intodotnet:features/collection-expression-argumentsfrom
CyrusNajmabadi:withElementSyntax
Oct 7, 2025
Merged

Add support for with(...) elements in collection expressions.#80495
CyrusNajmabadi merged 112 commits intodotnet:features/collection-expression-argumentsfrom
CyrusNajmabadi:withElementSyntax

Conversation

@CyrusNajmabadi
Copy link
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Sep 29, 2025

Followup to #80545
This is only for collections creatable with 'new', as well as the right errors for arrays/spans. Interfaces/Create collections will also error, but that will change in followup PRs.

Relates to test plan #80613

@@ -0,0 +1,21 @@
# Breaking changes in Roslyn after .NET 10.0.100 through .NET 11 (version number tbd)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure what to put here since we don't have the release numbers for these things yet.

@dotnet-policy-service dotnet-policy-service bot added the Needs API Review Needs to be reviewed by the API review council label Sep 29, 2025
@CyrusNajmabadi CyrusNajmabadi changed the base branch from features/collection-expression-arguments to main September 30, 2025 11:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds parsing support for with(...) elements in collection expressions as part of implementing collection expression arguments. The work represents initial parsing implementation with basic error handling, while binding and emit functionality remain incomplete.

  • Introduces WithElementSyntax as a new collection element type for with(...) syntax
  • Adds language version gating for the new feature (requires C# Preview/C# 14+)
  • Updates parser to recognize with followed by parenthesized argument list as collection arguments

Reviewed Changes

Copilot reviewed 26 out of 32 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Compilers/CSharp/Portable/Syntax/SyntaxKind.cs Adds WithElement syntax kind enumeration
src/Compilers/CSharp/Portable/Syntax/Syntax.xml Defines WithElementSyntax node structure with WithKeyword and ArgumentList
src/Compilers/CSharp/Portable/Parser/LanguageParser.cs Implements parsing logic for with(...) collection elements
src/Compilers/CSharp/Portable/Errors/*.cs Adds error codes and messages for collection argument validation
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs Provides basic binding with error reporting (incomplete implementation)
Various generated files Auto-generated syntax tree visitors, factory methods, and API surface

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@CyrusNajmabadi CyrusNajmabadi requested a review from 333fred October 7, 2025 16:56
@RikkiGibson
Copy link
Member

Taking a look

CreateCompilation(source, targetFramework: TargetFramework.Net80).VerifyDiagnostics(
// (8,14): error CS9244: The type 'ReadOnlySpan<int>' may not be a ref struct or a type parameter allowing ref structs in order to use it as parameter 'T' in the generic type or method 'List<T>'
// List<ReadOnlySpan<int>> list = [[with(), 1, 2, 3]];
Diagnostic(ErrorCode.ERR_NotRefStructConstraintNotSatisfied, "ReadOnlySpan<int>").WithArguments("System.Collections.Generic.List<T>", "T", "System.ReadOnlySpan<int>").WithLocation(8, 14),
Copy link
Member

@RikkiGibson RikkiGibson Oct 7, 2025

Choose a reason for hiding this comment

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

It might be worthwhile to also test IEnumerable<ReadOnlySpan<int>> which is a valid type (maybe starting in net9?), but tends to break things as it's not a valid type for a collection expression

Copy link
Contributor Author

Choose a reason for hiding this comment

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

interface work comes in teh followup PR. i'd like to add that test there if i may.

Copy link
Member

Choose a reason for hiding this comment

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

you may! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
""";

// PROTOTYPE: This error is not correct.
Copy link
Member

Choose a reason for hiding this comment

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

Are you saying that a different error message should be reported instead?

Copy link
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Oct 7, 2025

Choose a reason for hiding this comment

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

yes. the current diagnostic says: "must have an applicable constructor that can be called with no arguments."

That's not true. The issue here is that it has two parameters, but only one arg is being supplied. so we should hav a different error message.

so: good that we have an error. but bad that the error message is incorrect.


CreateCompilation(source).VerifyDiagnostics(
// (12,28): error CS1729: 'MyList<int>' does not contain a constructor that takes 0 arguments
// MyList<int> list = [with(10)];
Copy link
Member

Choose a reason for hiding this comment

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

nit: normally we would report that the candidate isn't accessible. can track this in followup or whatever as preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

[Fact]
public void WithElement_WithLambda_InferenceWithArgAndConstructor_3()
Copy link
Member

Choose a reason for hiding this comment

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

Did any of these tests demonstrate that the 'with' element is not contributing to type inference? e.g. perhaps a test showing M<T>(MyCollection<T>) should be ambiguous for M([with(value)]).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CyrusNajmabadi CyrusNajmabadi merged commit 01334a7 into dotnet:features/collection-expression-arguments Oct 7, 2025
24 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the withElementSyntax branch October 7, 2025 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature - Collection Arguments Collection expression with() arguments Needs API Review Needs to be reviewed by the API review council

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants