diff --git a/Coder.Test/Languages/FailedGenerationStateTests.cs b/Coder.Test/Languages/FailedGenerationStateTests.cs index a8ce532..a2ccfce 100644 --- a/Coder.Test/Languages/FailedGenerationStateTests.cs +++ b/Coder.Test/Languages/FailedGenerationStateTests.cs @@ -25,14 +25,91 @@ public void CppGenerator_AfterAFailureInsideAType_WritesWhatAFreshInstanceWrites public void RustGenerator_AfterAFailureInsideAType_WritesWhatAFreshInstanceWrites() => AssertUnaffectedByFailure(() => new RustGenerator(), ClassWithAnUnknownOperatorDefault()); - private static void AssertUnaffectedByFailure(Func create, AstNode failing) + [TestMethod] + public void RustGenerator_AfterAFailureInsideATrait_WritesWhatAFreshInstanceWrites() => + AssertUnaffectedByFailure(() => new RustGenerator(), TraitWithAnUnknownOperatorBody(), FreeFunctions); + + [TestMethod] + public void RustGenerator_AfterAFailureInsideASpecialisation_WritesWhatAFreshInstanceWrites() => + AssertUnaffectedByFailure(() => new RustGenerator(), SpecialisationWithAnUnknownOperatorBody(), FreeFunctions); + + [TestMethod] + public void GoGenerator_AfterAFailureInsideAnInterface_WritesWhatAFreshInstanceWrites() => + AssertUnaffectedByFailure(() => new GoGenerator(), InterfaceWithAnUnknownOperatorDefault(), FreeFunctions); + + private static void AssertUnaffectedByFailure(Func create, AstNode failing) => + AssertUnaffectedByFailure(create, failing, FileScopeConstant); + + private static void AssertUnaffectedByFailure(Func create, AstNode failing, Func next) { - string expected = create().Generate(FileScopeConstant()); + string expected = create().Generate(next()); LanguageGeneratorBase generator = create(); Assert.ThrowsExactly(() => generator.Generate(failing), "Precondition: the document fails inside the type."); - Assert.AreEqual(expected, generator.Generate(FileScopeConstant()), generator.DisplayName); + Assert.AreEqual(expected, generator.Generate(next()), generator.DisplayName); + } + + /// + /// A file holding a free function that returns a value and one with no statements, which a + /// generator that thinks it is inside a trait or an interface writes as bare signatures. + /// + private static SourceFile FreeFunctions() + { + FunctionDeclaration answer = new("Answer") { ReturnType = TypeReference.Parse("int") }; + answer.Body.Add(new ReturnStatement(1)); + SourceFile file = new("m"); + file.Members.Add(answer); + file.Members.Add(new FunctionDeclaration("Noop")); + return file; + } + + /// + /// An expression no generator can spell, as YAML can produce from a number. + /// + private static BinaryExpression UnknownOperator() => + new(Literal.Number(1), (BinaryOperator)999, Literal.Number(2)); + + /// + /// An interface whose method returns an expression Rust refuses while writing the trait. + /// + private static SourceFile TraitWithAnUnknownOperatorBody() + { + FunctionDeclaration area = new("Area") { ReturnType = TypeReference.Parse("int") }; + area.Body.Add(new ReturnStatement(UnknownOperator())); + ClassDeclaration shape = new("Shape") { Kind = TypeDeclarationKind.Interface }; + shape.Members.Add(area); + SourceFile file = new("m"); + file.Members.Add(shape); + return file; + } + + /// + /// A specialisation whose method returns an expression Rust refuses while writing the impl. + /// + private static SourceFile SpecialisationWithAnUnknownOperatorBody() + { + FunctionDeclaration area = new("Area") { ReturnType = TypeReference.Parse("int") }; + area.Body.Add(new ReturnStatement(UnknownOperator())); + ClassDeclaration describe = new("Describe") { SpecialisationArguments = { new TypeReference("Box") } }; + describe.Members.Add(area); + SourceFile file = new("m"); + file.Members.Add(describe); + return file; + } + + /// + /// An interface whose method has a default Go refuses while writing it into a comment. + /// + private static SourceFile InterfaceWithAnUnknownOperatorDefault() + { + FunctionDeclaration area = new("Area") { ReturnType = TypeReference.Parse("int") }; + area.Parameters.Add(new Parameter("scale", "int") { IsOptional = true, DefaultValue = UnknownOperator() }); + ClassDeclaration shape = new("Shape") { Kind = TypeDeclarationKind.Interface }; + shape.Members.Add(area); + SourceFile file = new("m"); + file.Members.Add(shape); + return file; } /// diff --git a/Coder/Languages/GoGenerator.cs b/Coder/Languages/GoGenerator.cs index 2485223..48574ee 100644 --- a/Coder/Languages/GoGenerator.cs +++ b/Coder/Languages/GoGenerator.cs @@ -916,20 +916,27 @@ .. classDecl.Interfaces.Select(SpellType), code.WriteLine(contract); } + // Restored in finally, so a member that throws cannot leave later output on this instance + // generated as interface members. + bool enclosingIsInterface = insideInterface; insideInterface = true; - - foreach (AstNode member in members) + try { - if (member is FunctionDeclaration function) + foreach (AstNode member in members) { - GenerateFunction(function, code, name); - continue; - } + if (member is FunctionDeclaration function) + { + GenerateFunction(function, code, name); + continue; + } - GenerateInternal(member, code); + GenerateInternal(member, code); + } + } + finally + { + insideInterface = enclosingIsInterface; } - - insideInterface = false; } /// diff --git a/Coder/Languages/RustGenerator.cs b/Coder/Languages/RustGenerator.cs index c56b020..1bc432d 100644 --- a/Coder/Languages/RustGenerator.cs +++ b/Coder/Languages/RustGenerator.cs @@ -617,9 +617,18 @@ private void GenerateSpecialisation(ClassDeclaration classDecl, string name, Cod using Scope body = new(code); + // Restored in finally, so a member that throws cannot leave later output on this instance + // generated as though it were still inside the impl. + bool enclosingIsTraitImplementation = insideTraitImplementation; insideTraitImplementation = true; - WriteMembers(classDecl.Members, code); - insideTraitImplementation = false; + try + { + WriteMembers(classDecl.Members, code); + } + finally + { + insideTraitImplementation = enclosingIsTraitImplementation; + } } /// @@ -1034,28 +1043,35 @@ .. classDecl.Interfaces.Select(SpellTypeName), using Scope body = new(code); + // Restored in finally, so a member that throws cannot leave later output on this instance + // generated as trait items. + bool enclosingIsTrait = insideTrait; insideTrait = true; - - bool first = true; - foreach (AstNode member in classDecl.Members.Where(member => member is FunctionDeclaration or FieldDeclaration)) + try { - if (!first) + bool first = true; + foreach (AstNode member in classDecl.Members.Where(member => member is FunctionDeclaration or FieldDeclaration)) { - code.NewLine(); - } + if (!first) + { + code.NewLine(); + } - first = false; + first = false; - if (member is FunctionDeclaration function) - { - GenerateFunction(function, code, name); - continue; - } + if (member is FunctionDeclaration function) + { + GenerateFunction(function, code, name); + continue; + } - GenerateInternal(member, code); + GenerateInternal(member, code); + } + } + finally + { + insideTrait = enclosingIsTrait; } - - insideTrait = false; } ///