Repository navigation
Write a typeless member list as new() in C# and a struct literal in Rust [patch] - #478
Merged
Merged
Conversation
…ust [patch]
A ConstructionExpression with no type whose arguments name members is a
designated initialiser for a declaration that has said its type. C# wrote
it as bare braces, `Point p = { x = 1, y = 2 };`, which C# takes only as
an array initialiser (CS0622), and Rust wrote `[ x: 1, y: 2 ]`, which is
not Rust.
C# now writes a target-typed `new() { x = 1, y = 2 }`. Rust writes the
receiving type's struct literal, `Point { x: 1, y: 2 }`, wherever a
declared type receives the value: a local, a return, and a field's
starting value in a constructor. The Python/JS criterion is left to #335.
Fixes #473
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
A typeless
ConstructionExpressionwhose arguments areMemberInitialisers is a designated initialiser for a declaration that has already said its type. C, C++ and Go already got it right. C# and Rust didn't:Point p = { x = 1, y = 2 };(CS0622, CS0103)Point p = new() { x = 1, y = 2 };let mut p: Point = [ x: 1, y: 2 ];(syntax error)let mut p: Point = Point { x: 1, y: 2 };GenerateConstructionwrites a target-typednew()in front of a typeless designated list. Non-designated lists are still collection expressions (C#: a typeless braced list initialising a list<T> is writtenList<int> xs = { 1, 2 };(CS0622), andreturn { 1, 2 };/xs = { 1, 2 };do not parse #418/Write a typeless C# list as a collection expression [patch] #442).WriteValueis where a declared type receives a value: a local's annotation, a return, and a field in a struct literal. It now writes a typeless designated list as the receiving type's struct literal. This covers a field's starting value as well, because Rust writes that inside the constructor'sSelf { … }.Fixes #473
Tests
TypelessDesignatedListTests.TypelessDesignatedLists_Compilecovers a local and a field initialiser of aPointstruct. It is compiled withdotnet build, alongside a driver that reads both.TypelessDesignatedListTests.TypelessDesignatedLists_CompileAndRuncompiles the same AST withrustcand runs it. The run checks that both the local and the field hold the values they were given.Coder.Test/Languages/TypelessDesignatedListTests.cs, rather than at the end of the shared C# and Rust compile-test classes, where every newly merged test collided with them.Notes
Veccase for non-designated typeless lists is Rust writes a typeless braced list as an array where a Vec is expected (let xs: Vec<i32> = [1, 2]; E0308), and Go writes a barereturn {1}#409, and this PR doesn't touch it. The Rust change applies only when at least one argument names a member, and never when the receiving type is an array.Generated by Claude Code