From d71f006b22636cea89713f00d8d8bbdd938a07ad Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 10:29:01 +0000 Subject: [PATCH 1/2] Derive Clone, Copy, Debug, PartialEq, Eq and Hash on Rust enums [patch] A generated Rust enum derived nothing, so comparing two of its values failed with E0369, a record holding one failed with E0277, and reading one out of &self failed with E0507. A fieldless enum can always derive these, unlike a struct whose fields might not implement them. Fixes #255 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0175VbFC9TAs3FcivXVHTFum --- .../RustGeneratedSourceCompilesTests.cs | 52 +++++++++++++++++++ Coder.Test/Languages/RustGeneratorTests.cs | 7 +-- Coder/Languages/RustGenerator.cs | 7 +++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs index b80aa46..060f791 100644 --- a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs +++ b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs @@ -474,6 +474,58 @@ public void EntryPointArguments_LeaveOutTheProgramPath() }); } + /// + /// Tests that an enumeration's values can be compared, held in a record and read out of + /// &self, which each need a trait the enumeration has to derive (#255). + /// + [TestMethod] + public void EnumValuesCompareCopyAndSitInARecord_Compile() + { + if (ToolchainHarness.FindOnPath("--version", "rustc") is null) + { + Assert.Inconclusive("No Rust compiler on the path, so nothing was compiled."); + return; + } + + // same(a, b) -> bool { return a == b; } + FunctionDeclaration same = new("same") { ReturnType = "bool" }; + same.Parameters.Add(new Parameter("a", "Colour")); + same.Parameters.Add(new Parameter("b", "Colour")); + same.Body.Add(new ReturnStatement( + new BinaryExpression(new VariableReference("a"), BinaryOperator.Equal, new VariableReference("b")))); + + // A record's derive needs every field to implement what it derives. + ClassDeclaration paint = new("Paint") { Kind = TypeDeclarationKind.Struct, IsRecord = true }; + paint.Members.Add(new VariableDeclaration("colour", "Colour")); + + // colour(&self) -> Colour { return self.colour; } + FunctionDeclaration colour = new("colour") { ReturnType = "Colour", IsReadOnly = true }; + colour.Body.Add(new ReturnStatement(new VariableReference("self.colour"))); + paint.Members.Add(colour); + + SourceFile file = new("enums"); + file.Members.Add(CompiledExemplar.Colour()); + file.Members.Add(same); + file.Members.Add(paint); + + ToolchainHarness.InTemporaryDirectory(directory => + { + string source = new RustGenerator().Generate(file); + string allowed = "#![allow(unused_parens, dead_code)]"; + + File.WriteAllText( + Path.Combine(directory, "enums.rs"), + $"{allowed}{Environment.NewLine}{source}"); + + (int exitCode, string output) = ToolchainHarness.Run( + "rustc", + "--crate-type lib --edition 2021 -o enums.rlib enums.rs", + directory); + + Assert.AreEqual(0, exitCode, $"rustc rejected the generated source:{Environment.NewLine}{source}{Environment.NewLine}{output}"); + }); + } + /// /// Builds a file holding one of everything the generator has a spelling for. /// diff --git a/Coder.Test/Languages/RustGeneratorTests.cs b/Coder.Test/Languages/RustGeneratorTests.cs index ad2d6b8..6a99c01 100644 --- a/Coder.Test/Languages/RustGeneratorTests.cs +++ b/Coder.Test/Languages/RustGeneratorTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023-2026 ktsu-dev contributors +// Copyright (c) 2023-2026 ktsu-dev contributors namespace ktsu.Coder.Test.Languages; @@ -558,7 +558,7 @@ public void Enum_IsScopedAndCanFixItsRepresentation() string code = Generator.Generate(colour); Assert.AreEqual( - $"#[repr(i32)]{NewLine}pub enum Colour {{{NewLine} Red = 1,{NewLine} Green,{NewLine}}}{NewLine}", + $"#[repr(i32)]{NewLine}#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]{NewLine}pub enum Colour {{{NewLine} Red = 1,{NewLine} Green,{NewLine}}}{NewLine}", code); } @@ -572,7 +572,8 @@ public void Enum_WithNoVariants_HasNoRepr() string code = Generator.Generate(new EnumDeclaration("Empty") { UnderlyingType = "int" }); Assert.IsFalse(code.Contains("#[repr", StringComparison.Ordinal)); - StringAssert.StartsWith(code, "pub enum Empty {", StringComparison.Ordinal); + StringAssert.StartsWith(code, "#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]", StringComparison.Ordinal); + Assert.Contains("pub enum Empty {", code, StringComparison.Ordinal); } /// diff --git a/Coder/Languages/RustGenerator.cs b/Coder/Languages/RustGenerator.cs index 2b415fa..72b2f9e 100644 --- a/Coder/Languages/RustGenerator.cs +++ b/Coder/Languages/RustGenerator.cs @@ -1673,6 +1673,12 @@ private static string SpellStorageType(TypeReference type) /// do. A fixed underlying type is #[repr], which is the guarantee rather than a comment /// about one. An enumeration with no variants has no representation to fix, and rustc rejects /// #[repr] on one (E0084), so it is left off. + /// + /// Every enumeration derives the traits a value of it needs to be compared, copied and printed. + /// The reason a plain struct derives nothing, that one of its fields might not implement them, + /// cannot apply to a fieldless enumeration, and without them a == b is E0369, a record + /// holding one is E0277, and reading one out of &self is E0507. + /// /// protected override void GenerateEnumDeclaration(EnumDeclaration enumDecl, CodeBlocker code) { @@ -1686,6 +1692,7 @@ protected override void GenerateEnumDeclaration(EnumDeclaration enumDecl, CodeBl code.WriteLine($"#[repr({SpellType(underlying)})]"); } + code.WriteLine("#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]"); code.Write($"{SpellVisibilityOf(enumDecl)}enum {enumDecl.Name ?? "UnnamedEnum"} "); using Scope body = new(code); From acb68ff9c0d600747b77fb16ed7a48137e213037 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 10:40:13 +0000 Subject: [PATCH 2/2] Keep RustGeneratorTests.cs without a byte order mark Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0175VbFC9TAs3FcivXVHTFum --- Coder.Test/Languages/RustGeneratorTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Coder.Test/Languages/RustGeneratorTests.cs b/Coder.Test/Languages/RustGeneratorTests.cs index 6a99c01..4bea166 100644 --- a/Coder.Test/Languages/RustGeneratorTests.cs +++ b/Coder.Test/Languages/RustGeneratorTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023-2026 ktsu-dev contributors +// Copyright (c) 2023-2026 ktsu-dev contributors namespace ktsu.Coder.Test.Languages;