Skip to content

Write a typeless C# list as a collection expression [patch] - #442

Merged
matt-edmondson merged 4 commits into
mainfrom
fix/418-csharp-typeless-list
Nov 4, 2026
Merged

matt-edmondson merged 4 commits into
mainfrom
fix/418-csharp-typeless-list

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #418

What was wrong

C# wrote a ConstructionExpression with no type (the list on its own) as bare braces, { 1, 2 }. C# accepts those only as an array's initialiser, so:

  • a List<int> field or local failed with CS0622
  • a return, an assignment or an argument didn't parse at all (CS1525)

Change

CSharpGenerator.GenerateConstruction now writes a typeless list as a collection expression ([1, 2], and [] when empty). That form works for arrays and List<T> in every one of those positions. Nothing else changes:

  • A table of lists is still written one element per line, now as [ [1, 2], [3, 4], ].
  • A list with designated members is still an object initialiser in braces.
  • A typed construction is unchanged.

Tests

CSharpGeneratedSourceCompilesTests.TypelessLists_Compile builds the issue's repro with dotnet build, plus an array, a jagged table and an empty return, against a driver that reads each one back.

  • With the generator change reverted it fails with CS1525. With the change it passes.
  • The full suite passes: 1553 of 1553.

The test declares using System.Collections.Generic itself. A list<T> that doesn't bring in its own using is #334, so it's left out of this PR.


Generated by Claude Code

A construction with no type, the list on its own, came out as bare
braces in C#. Those are only an array's initialiser, so a List<T> field
or local failed with CS0622, and a return, assignment or argument did
not parse at all. C# now writes a collection expression ([1, 2]), which
fits an array and a List<T> alike in every one of those positions. A
table of lists stays one element per line, and a list with designated
members is still an object initialiser.

Fixes #418.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5vywfZJXnxcJfXxVi
Name the opening, closing and padding marks once rather than choosing
each at its use, which takes SonarCloud's S3776 count from 16 back
under 15. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5vywfZJXnxcJfXxVi
…ss-list

# Conflicts:
#	Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs
…ss-list

# Conflicts:
#	Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs
@sonarqubecloud

sonarqubecloud Bot commented Nov 4, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C#: a typeless braced list initialising a list<T> is written List<int> xs = { 1, 2 }; (CS0622), and return { 1, 2 }; / xs = { 1, 2 }; do not parse

2 participants