From 2059d0c28e3c78950ad6f9c2927c5820d17a292a Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 00:35:40 +0000 Subject: [PATCH 1/3] Write a typeless member list as new() in C# and a struct literal in Rust [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 ktsu-dev/Coder#473 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9 --- .../CSharpGeneratedSourceCompilesTests.cs | 71 +++++++++++++++++ .../RustGeneratedSourceCompilesTests.cs | 76 +++++++++++++++++++ Coder/Languages/CSharpGenerator.cs | 9 +++ Coder/Languages/RustGenerator.cs | 20 +++++ 4 files changed, 176 insertions(+) diff --git a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs index f992748..5936caa 100644 --- a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs +++ b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs @@ -767,4 +767,75 @@ public static int Use() => Assert.AreEqual(0, exitCode, $"C# rejected the generated source:{Environment.NewLine}{output}{Environment.NewLine}{source}"); }); } + + /// + /// Builds a struct Point { x, y }, a struct Frame whose origin field starts + /// at a typeless list naming Point's members, and a function sum whose local is + /// initialised the same way (#473). + /// + /// The source file. + private static SourceFile DesignatedLists() + { + static ConstructionExpression Designated(int x, int y) + { + ConstructionExpression list = new(); + list.Arguments.Add(new MemberInitialiser("x", Literal.Number(x))); + list.Arguments.Add(new MemberInitialiser("y", Literal.Number(y))); + return list; + } + + ClassDeclaration point = new("Point") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; + point.Members.Add(new FieldDeclaration("x", "int") { Visibility = Visibility.Public }); + point.Members.Add(new FieldDeclaration("y", "int") { Visibility = Visibility.Public }); + + ClassDeclaration frame = new("Frame") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; + frame.Members.Add(new FieldDeclaration("origin", "Point") { Visibility = Visibility.Public, InitialValue = Designated(3, 4) }); + frame.Members.Add(new FunctionDeclaration("Frame") { Kind = FunctionKind.Constructor, Visibility = Visibility.Public }); + + FunctionDeclaration sum = new("sum") { ReturnType = "int", Visibility = Visibility.Public, IsStatic = true }; + sum.Body.Add(new VariableDeclaration("p", "Point", Designated(1, 2))); + sum.Body.Add(new ReturnStatement(new BinaryExpression(new VariableReference("p.x"), BinaryOperator.Add, new VariableReference("p.y")))); + frame.Members.Add(sum); + + SourceFile file = new("designated"); + file.Members.Add(point); + file.Members.Add(frame); + return file; + } + + /// + /// Tests that a typeless list naming its members, initialising a local and a field, compiles as + /// a target-typed new() { … } rather than bare braces, which C# takes only for an array + /// (CS0622), and that the values arrive (#473). + /// + [TestMethod] + public void TypelessDesignatedLists_Compile() + { + string source = new CSharpGenerator().Generate(DesignatedLists()); + Assert.Contains("Point p = new() { x = 1, y = 2 };", source, StringComparison.Ordinal, source); + Assert.Contains("Point origin = new() { x = 3, y = 4 };", source, StringComparison.Ordinal, source); + + if (ToolchainHarness.FindOnPath("--version", "dotnet") is null) + { + Assert.Inconclusive("No dotnet on the path, so nothing was compiled."); + return; + } + + const string Driver = """ + public static class DesignatedDriver + { + public static int Use() => Frame.sum() + new Frame().origin.x + new Frame().origin.y; + } + """; + + ToolchainHarness.InTemporaryDirectory(directory => + { + File.WriteAllText(Path.Combine(directory, "Generated.csproj"), Project); + File.WriteAllText(Path.Combine(directory, "DesignatedDriver.cs"), Driver); + File.WriteAllText(Path.Combine(directory, "Generated.cs"), source); + + (int exitCode, string output) = ToolchainHarness.Run("dotnet", "build -nologo", directory); + Assert.AreEqual(0, exitCode, $"C# rejected the generated source:{Environment.NewLine}{output}{Environment.NewLine}{source}"); + }); + } } diff --git a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs index 595311c..3cbff02 100644 --- a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs +++ b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs @@ -1242,4 +1242,80 @@ public void AbstractMembers_CompileInStructsAndTraits() Assert.AreEqual(0, exitCode, $"rustc rejected the generated source:{Environment.NewLine}{output}{Environment.NewLine}{source}"); }); } + + /// + /// Builds a struct Point { x, y }, a struct Frame whose origin field starts + /// at a typeless list naming Point's members, and a function sum whose local is + /// initialised the same way (#473). + /// + /// The source file. + private static SourceFile DesignatedLists() + { + static ConstructionExpression Designated(int x, int y) + { + ConstructionExpression list = new(); + list.Arguments.Add(new MemberInitialiser("x", Literal.Number(x))); + list.Arguments.Add(new MemberInitialiser("y", Literal.Number(y))); + return list; + } + + ClassDeclaration point = new("Point") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; + point.Members.Add(new FieldDeclaration("x", "int") { Visibility = Visibility.Public }); + point.Members.Add(new FieldDeclaration("y", "int") { Visibility = Visibility.Public }); + + ClassDeclaration frame = new("Frame") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; + frame.Members.Add(new FieldDeclaration("origin", "Point") { Visibility = Visibility.Public, InitialValue = Designated(3, 4) }); + frame.Members.Add(new FunctionDeclaration("Frame") { Kind = FunctionKind.Constructor, Visibility = Visibility.Public }); + + FunctionDeclaration sum = new("sum") { ReturnType = "int", Visibility = Visibility.Public, IsStatic = true }; + sum.Body.Add(new VariableDeclaration("p", "Point", Designated(1, 2))); + sum.Body.Add(new ReturnStatement(new BinaryExpression(new VariableReference("p.x"), BinaryOperator.Add, new VariableReference("p.y")))); + frame.Members.Add(sum); + + SourceFile file = new("designated"); + file.Members.Add(point); + file.Members.Add(frame); + return file; + } + + /// + /// Tests that a typeless list naming its members, initialising a local and a field, is written + /// as a struct literal of the receiving type rather than [x: 1, y: 2], which is not Rust, + /// and that the values arrive when run (#473). + /// + [TestMethod] + public void TypelessDesignatedLists_CompileAndRun() + { + string source = new RustGenerator().Generate(DesignatedLists()); + Assert.Contains("Point { x: 1, y: 2 }", source, StringComparison.Ordinal, source); + Assert.Contains("Point { x: 3, y: 4 }", source, StringComparison.Ordinal, source); + + if (ToolchainHarness.FindOnPath("--version", "rustc") is null) + { + Assert.Inconclusive("No Rust compiler on the path, so nothing was compiled."); + return; + } + + const string Driver = """ + fn main() { + let f = Frame::new(); + if Frame::sum() + f.origin.x + f.origin.y != 10 { + std::process::exit(1); + } + } + """; + + ToolchainHarness.InTemporaryDirectory(directory => + { + File.WriteAllText( + Path.Combine(directory, "designated.rs"), + $"#![allow(unused_parens, dead_code, unused_mut)]{Environment.NewLine}{source}{Environment.NewLine}{Driver}"); + + (int exitCode, string output) = ToolchainHarness.Run("rustc", "--edition 2021 -o designated designated.rs", directory); + Assert.AreEqual(0, exitCode, $"rustc rejected the generated source:{Environment.NewLine}{source}{Environment.NewLine}{output}"); + + (int ran, string printed) = ToolchainHarness.Run(Path.Combine(directory, "designated"), string.Empty, directory); + Assert.AreEqual(0, ran, $"The program computed the wrong total:{Environment.NewLine}{printed}"); + }); + } } diff --git a/Coder/Languages/CSharpGenerator.cs b/Coder/Languages/CSharpGenerator.cs index acd5cf4..b98bd98 100644 --- a/Coder/Languages/CSharpGenerator.cs +++ b/Coder/Languages/CSharpGenerator.cs @@ -1210,6 +1210,8 @@ private void GenerateUsingAlias(UsingAlias usingAlias, CodeBlocker code) /// because that is the one spelling C# takes for an array and a List<T> alike, and /// in a return, an assignment or an argument as well as an initialiser. The bare braces /// { 1, 2 } are only an array's initialiser and not an expression at all (#418). + /// A list with no type that names its members is a target-typed new() { Name = value }, + /// for the same reason (#473). /// Everything else is a constructor call. /// private void GenerateConstruction(ConstructionExpression construction, CodeBlocker code) @@ -1218,6 +1220,13 @@ private void GenerateConstruction(ConstructionExpression construction, CodeBlock if (construction.Type is null) { + // A list naming its members is an object initialiser, and C# takes one only after a + // `new`. Bare braces are an array's initialiser (CS0622), so it is target-typed (#473). + if (designated) + { + code.Write("new() "); + } + WriteBracedList(construction, code, asCollection: !designated); return; } diff --git a/Coder/Languages/RustGenerator.cs b/Coder/Languages/RustGenerator.cs index 18a5fdd..c27f0a8 100644 --- a/Coder/Languages/RustGenerator.cs +++ b/Coder/Languages/RustGenerator.cs @@ -1511,9 +1511,29 @@ protected override void GenerateReturnStatement(ReturnStatement returnStmt, Code /// /// A conditional answers what its branches do, so each branch is written for the same type. /// + /// + /// A list with no type that names its members takes the receiving type as its own, so it is + /// written as that type's struct literal. + /// /// private void WriteValue(AstNode value, TypeReference? target, CodeBlocker code) { + // A list with no type that names its members is a struct literal of the type receiving it, + // which is the only place the type can come from: `[x: 1]` is not Rust at all (#473). + if (value is ConstructionExpression { Type: null } list + && target is { IsArray: false } + && list.Arguments.Any(argument => argument is MemberInitialiser)) + { + ConstructionExpression typed = new(target); + foreach (AstNode argument in list.Arguments) + { + typed.Arguments.Add(argument); + } + + GenerateConstructionExpression(typed, code); + return; + } + if (value is ConditionalExpression conditional && IsOwnedString(target)) { code.Write("(if "); From 0f765c5aa6228dc27dca39fc75893a40d0a817a5 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 00:38:45 +0000 Subject: [PATCH 2/3] Name the designated-list test's driver apart from the class's Driver Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9 --- Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs index 5936caa..634765b 100644 --- a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs +++ b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs @@ -821,7 +821,7 @@ public void TypelessDesignatedLists_Compile() return; } - const string Driver = """ + const string DesignatedDriver = """ public static class DesignatedDriver { public static int Use() => Frame.sum() + new Frame().origin.x + new Frame().origin.y; @@ -831,7 +831,7 @@ public static class DesignatedDriver ToolchainHarness.InTemporaryDirectory(directory => { File.WriteAllText(Path.Combine(directory, "Generated.csproj"), Project); - File.WriteAllText(Path.Combine(directory, "DesignatedDriver.cs"), Driver); + File.WriteAllText(Path.Combine(directory, "DesignatedDriver.cs"), DesignatedDriver); File.WriteAllText(Path.Combine(directory, "Generated.cs"), source); (int exitCode, string output) = ToolchainHarness.Run("dotnet", "build -nologo", directory); From 6a805bd4bc6a1d299db489bbbd1fa5e7c41044cf Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 01:17:48 +0000 Subject: [PATCH 3/3] Move the #473 tests into a file of their own Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW3rprSpduYCEWPi7P9 --- .../CSharpGeneratedSourceCompilesTests.cs | 89 +++++------- .../RustGeneratedSourceCompilesTests.cs | 76 ---------- .../Languages/TypelessDesignatedListTests.cs | 137 ++++++++++++++++++ 3 files changed, 176 insertions(+), 126 deletions(-) create mode 100644 Coder.Test/Languages/TypelessDesignatedListTests.cs diff --git a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs index 0b58dc6..dcbde77 100644 --- a/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs +++ b/Coder.Test/Languages/CSharpGeneratedSourceCompilesTests.cs @@ -875,73 +875,62 @@ public static void Main() } /// - /// Builds a struct Point { x, y }, a struct Frame whose origin field starts - /// at a typeless list naming Point's members, and a function sum whose local is - /// initialised the same way (#473). - /// - /// The source file. - private static SourceFile DesignatedLists() - { - static ConstructionExpression Designated(int x, int y) - { - ConstructionExpression list = new(); - list.Arguments.Add(new MemberInitialiser("x", Literal.Number(x))); - list.Arguments.Add(new MemberInitialiser("y", Literal.Number(y))); - return list; - } - - ClassDeclaration point = new("Point") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; - point.Members.Add(new FieldDeclaration("x", "int") { Visibility = Visibility.Public }); - point.Members.Add(new FieldDeclaration("y", "int") { Visibility = Visibility.Public }); - - ClassDeclaration frame = new("Frame") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; - frame.Members.Add(new FieldDeclaration("origin", "Point") { Visibility = Visibility.Public, InitialValue = Designated(3, 4) }); - frame.Members.Add(new FunctionDeclaration("Frame") { Kind = FunctionKind.Constructor, Visibility = Visibility.Public }); - - FunctionDeclaration sum = new("sum") { ReturnType = "int", Visibility = Visibility.Public, IsStatic = true }; - sum.Body.Add(new VariableDeclaration("p", "Point", Designated(1, 2))); - sum.Body.Add(new ReturnStatement(new BinaryExpression(new VariableReference("p.x"), BinaryOperator.Add, new VariableReference("p.y")))); - frame.Members.Add(sum); - - SourceFile file = new("designated"); - file.Members.Add(point); - file.Members.Add(frame); - return file; - } - - /// - /// Tests that a typeless list naming its members, initialising a local and a field, compiles as - /// a target-typed new() { … } rather than bare braces, which C# takes only for an array - /// (CS0622), and that the values arrive (#473). + /// Tests that an inferred variable declared as a member of a type compiles, with the type it was + /// declared with or else the one its initialiser establishes (#432). /// + /// + /// var is only for a local, so a member written var count = 0; fails with CS0825. + /// The driver reads each member into its type, which is what holds the type that was said. + /// [TestMethod] - public void TypelessDesignatedLists_Compile() + public void InferredTypeMembers_Compile() { - string source = new CSharpGenerator().Generate(DesignatedLists()); - Assert.Contains("Point p = new() { x = 1, y = 2 };", source, StringComparison.Ordinal, source); - Assert.Contains("Point origin = new() { x = 3, y = 4 };", source, StringComparison.Ordinal, source); - if (ToolchainHarness.FindOnPath("--version", "dotnet") is null) { Assert.Inconclusive("No dotnet on the path, so nothing was compiled."); return; } - const string DesignatedDriver = """ - public static class DesignatedDriver - { - public static int Use() => Frame.sum() + new Frame().origin.x + new Frame().origin.y; - } - """; + SourceFile file = new("Generated"); + file.Members.Add(InferredMembers()); ToolchainHarness.InTemporaryDirectory(directory => { + string source = new CSharpGenerator().Generate(file); File.WriteAllText(Path.Combine(directory, "Generated.csproj"), Project); - File.WriteAllText(Path.Combine(directory, "DesignatedDriver.cs"), DesignatedDriver); + File.WriteAllText( + Path.Combine(directory, "CounterDriver.cs"), + """ + public static class CounterDriver + { + public static double Use() + { + Counter counter = new Counter(); + int count = counter.count; + double ratio = counter.ratio; + string label = counter.label; + return count + ratio + label.Length; + } + } + """); File.WriteAllText(Path.Combine(directory, "Generated.cs"), source); (int exitCode, string output) = ToolchainHarness.Run("dotnet", "build -nologo", directory); Assert.AreEqual(0, exitCode, $"C# rejected the generated source:{Environment.NewLine}{output}{Environment.NewLine}{source}"); }); } + + /// + /// Builds a class whose members are inferred variables: one with no declared type, one whose + /// declared type is wider than its initialiser's, and one holding a string. + /// + /// The class. + internal static ClassDeclaration InferredMembers() + { + ClassDeclaration counter = new("Counter"); + counter.Members.Add(new VariableDeclaration("count") { IsTypeInferred = true, InitialValue = new LiteralExpression(0) }); + counter.Members.Add(new VariableDeclaration("ratio", "double") { IsTypeInferred = true, InitialValue = new LiteralExpression(1) }); + counter.Members.Add(new VariableDeclaration("label") { IsTypeInferred = true, InitialValue = Literal.Text("c") }); + return counter; + } } diff --git a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs index d4db887..8c5b47a 100644 --- a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs +++ b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs @@ -1299,80 +1299,4 @@ public void StringLiteralsReassignedOrPassedAsString_Compile() Assert.AreEqual(0, exitCode, $"rustc rejected the generated source:{Environment.NewLine}{source}{Environment.NewLine}{output}"); }); } - - /// - /// Builds a struct Point { x, y }, a struct Frame whose origin field starts - /// at a typeless list naming Point's members, and a function sum whose local is - /// initialised the same way (#473). - /// - /// The source file. - private static SourceFile DesignatedLists() - { - static ConstructionExpression Designated(int x, int y) - { - ConstructionExpression list = new(); - list.Arguments.Add(new MemberInitialiser("x", Literal.Number(x))); - list.Arguments.Add(new MemberInitialiser("y", Literal.Number(y))); - return list; - } - - ClassDeclaration point = new("Point") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; - point.Members.Add(new FieldDeclaration("x", "int") { Visibility = Visibility.Public }); - point.Members.Add(new FieldDeclaration("y", "int") { Visibility = Visibility.Public }); - - ClassDeclaration frame = new("Frame") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; - frame.Members.Add(new FieldDeclaration("origin", "Point") { Visibility = Visibility.Public, InitialValue = Designated(3, 4) }); - frame.Members.Add(new FunctionDeclaration("Frame") { Kind = FunctionKind.Constructor, Visibility = Visibility.Public }); - - FunctionDeclaration sum = new("sum") { ReturnType = "int", Visibility = Visibility.Public, IsStatic = true }; - sum.Body.Add(new VariableDeclaration("p", "Point", Designated(1, 2))); - sum.Body.Add(new ReturnStatement(new BinaryExpression(new VariableReference("p.x"), BinaryOperator.Add, new VariableReference("p.y")))); - frame.Members.Add(sum); - - SourceFile file = new("designated"); - file.Members.Add(point); - file.Members.Add(frame); - return file; - } - - /// - /// Tests that a typeless list naming its members, initialising a local and a field, is written - /// as a struct literal of the receiving type rather than [x: 1, y: 2], which is not Rust, - /// and that the values arrive when run (#473). - /// - [TestMethod] - public void TypelessDesignatedLists_CompileAndRun() - { - string source = new RustGenerator().Generate(DesignatedLists()); - Assert.Contains("Point { x: 1, y: 2 }", source, StringComparison.Ordinal, source); - Assert.Contains("Point { x: 3, y: 4 }", source, StringComparison.Ordinal, source); - - if (ToolchainHarness.FindOnPath("--version", "rustc") is null) - { - Assert.Inconclusive("No Rust compiler on the path, so nothing was compiled."); - return; - } - - const string Driver = """ - fn main() { - let f = Frame::new(); - if Frame::sum() + f.origin.x + f.origin.y != 10 { - std::process::exit(1); - } - } - """; - - ToolchainHarness.InTemporaryDirectory(directory => - { - File.WriteAllText( - Path.Combine(directory, "designated.rs"), - $"#![allow(unused_parens, dead_code, unused_mut)]{Environment.NewLine}{source}{Environment.NewLine}{Driver}"); - - (int exitCode, string output) = ToolchainHarness.Run("rustc", "--edition 2021 -o designated designated.rs", directory); - Assert.AreEqual(0, exitCode, $"rustc rejected the generated source:{Environment.NewLine}{source}{Environment.NewLine}{output}"); - - (int ran, string printed) = ToolchainHarness.Run(Path.Combine(directory, "designated"), string.Empty, directory); - Assert.AreEqual(0, ran, $"The program computed the wrong total:{Environment.NewLine}{printed}"); - }); - } } diff --git a/Coder.Test/Languages/TypelessDesignatedListTests.cs b/Coder.Test/Languages/TypelessDesignatedListTests.cs new file mode 100644 index 0000000..dc619d8 --- /dev/null +++ b/Coder.Test/Languages/TypelessDesignatedListTests.cs @@ -0,0 +1,137 @@ +// Copyright (c) 2023-2026 ktsu-dev contributors + +namespace ktsu.Coder.Test.Languages; + +using ktsu.Coder.Ast; +using ktsu.Coder.Languages; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +/// +/// Tests that a typeless list naming its members, which initialises a declaration that has said its +/// type, compiles in C# and Rust (#473). +/// +[TestClass] +public class TypelessDesignatedListTests +{ + private const string CSharpProject = """ + + + net10.0 + disable + disable + + + """; + + /// + /// Builds a struct Point { x, y }, a struct Frame whose origin field starts + /// at a typeless list naming Point's members, and a function sum whose local is + /// initialised the same way (#473). + /// + /// The source file. + private static SourceFile DesignatedLists() + { + static ConstructionExpression Designated(int x, int y) + { + ConstructionExpression list = new(); + list.Arguments.Add(new MemberInitialiser("x", Literal.Number(x))); + list.Arguments.Add(new MemberInitialiser("y", Literal.Number(y))); + return list; + } + + ClassDeclaration point = new("Point") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; + point.Members.Add(new FieldDeclaration("x", "int") { Visibility = Visibility.Public }); + point.Members.Add(new FieldDeclaration("y", "int") { Visibility = Visibility.Public }); + + ClassDeclaration frame = new("Frame") { Kind = TypeDeclarationKind.Struct, Visibility = Visibility.Public }; + frame.Members.Add(new FieldDeclaration("origin", "Point") { Visibility = Visibility.Public, InitialValue = Designated(3, 4) }); + frame.Members.Add(new FunctionDeclaration("Frame") { Kind = FunctionKind.Constructor, Visibility = Visibility.Public }); + + FunctionDeclaration sum = new("sum") { ReturnType = "int", Visibility = Visibility.Public, IsStatic = true }; + sum.Body.Add(new VariableDeclaration("p", "Point", Designated(1, 2))); + sum.Body.Add(new ReturnStatement(new BinaryExpression(new VariableReference("p.x"), BinaryOperator.Add, new VariableReference("p.y")))); + frame.Members.Add(sum); + + SourceFile file = new("designated"); + file.Members.Add(point); + file.Members.Add(frame); + return file; + } + + /// + /// Tests that a typeless list naming its members, initialising a local and a field, compiles as + /// a target-typed new() { … } rather than bare braces, which C# takes only for an array + /// (CS0622), and that the values arrive (#473). + /// + [TestMethod] + public void TypelessDesignatedLists_Compile() + { + string source = new CSharpGenerator().Generate(DesignatedLists()); + Assert.Contains("Point p = new() { x = 1, y = 2 };", source, StringComparison.Ordinal, source); + Assert.Contains("Point origin = new() { x = 3, y = 4 };", source, StringComparison.Ordinal, source); + + if (ToolchainHarness.FindOnPath("--version", "dotnet") is null) + { + Assert.Inconclusive("No dotnet on the path, so nothing was compiled."); + return; + } + + const string DesignatedDriver = """ + public static class DesignatedDriver + { + public static int Use() => Frame.sum() + new Frame().origin.x + new Frame().origin.y; + } + """; + + ToolchainHarness.InTemporaryDirectory(directory => + { + File.WriteAllText(Path.Combine(directory, "Generated.csproj"), CSharpProject); + File.WriteAllText(Path.Combine(directory, "DesignatedDriver.cs"), DesignatedDriver); + File.WriteAllText(Path.Combine(directory, "Generated.cs"), source); + + (int exitCode, string output) = ToolchainHarness.Run("dotnet", "build -nologo", directory); + Assert.AreEqual(0, exitCode, $"C# rejected the generated source:{Environment.NewLine}{output}{Environment.NewLine}{source}"); + }); + } + + /// + /// Tests that a typeless list naming its members, initialising a local and a field, is written + /// as a struct literal of the receiving type rather than [x: 1, y: 2], which is not Rust, + /// and that the values arrive when run (#473). + /// + [TestMethod] + public void TypelessDesignatedLists_CompileAndRun() + { + string source = new RustGenerator().Generate(DesignatedLists()); + Assert.Contains("Point { x: 1, y: 2 }", source, StringComparison.Ordinal, source); + Assert.Contains("Point { x: 3, y: 4 }", source, StringComparison.Ordinal, source); + + if (ToolchainHarness.FindOnPath("--version", "rustc") is null) + { + Assert.Inconclusive("No Rust compiler on the path, so nothing was compiled."); + return; + } + + const string Driver = """ + fn main() { + let f = Frame::new(); + if Frame::sum() + f.origin.x + f.origin.y != 10 { + std::process::exit(1); + } + } + """; + + ToolchainHarness.InTemporaryDirectory(directory => + { + File.WriteAllText( + Path.Combine(directory, "designated.rs"), + $"#![allow(unused_parens, dead_code, unused_mut)]{Environment.NewLine}{source}{Environment.NewLine}{Driver}"); + + (int exitCode, string output) = ToolchainHarness.Run("rustc", "--edition 2021 -o designated designated.rs", directory); + Assert.AreEqual(0, exitCode, $"rustc rejected the generated source:{Environment.NewLine}{source}{Environment.NewLine}{output}"); + + (int ran, string printed) = ToolchainHarness.Run(Path.Combine(directory, "designated"), string.Empty, directory); + Assert.AreEqual(0, ran, $"The program computed the wrong total:{Environment.NewLine}{printed}"); + }); + } +}