Skip to content

Rust output still fails to compile (E0308) when a string literal fills a String const, constructor initialiser, struct literal, field assignment, or conditional branch #154

Closed

Description

@matt-edmondson

What's wrong

#120 closed #84 by writing String::from("…") when a string literal is received as an owned String. That conversion lives in WriteValue, but only the return-statement and typed-local paths call it:

#84 also named other positions, such as the initialiser of a String-typed field and a field value in a construction. Those still write a bare &str.

A const of type String needs a different fix. String::from is not a const fn, so a const or static has to be typed &str (or &[&str] for an array).

Reproduction (rustc, edition 2021)

var person = new ClassDeclaration("Person") { Kind = TypeDeclarationKind.Struct };
person.Members.Add(new VariableDeclaration("name", "string"));
person.Members.Add(new FieldDeclaration { Name = "DEFAULT", Type = "string", IsStatic = true, IsConstant = true, InitialValue = new LiteralExpression<string>("d") });
var ctor = new FunctionDeclaration("Person") { Kind = FunctionKind.Constructor };
ctor.Initialisers.Add(new MemberInitialiser("name") { Value = new LiteralExpression<string>("Anon") });
var make = new FunctionDeclaration("make") { ReturnType = "Person", IsStatic = true };
var ce = new ConstructionExpression("Person"); ce.Arguments.Add(new MemberInitialiser("name") { Value = new LiteralExpression<string>("x") });
make.Body.Add(new ReturnStatement(ce));
var rename = new FunctionDeclaration("rename");
rename.Body.Add(new AssignmentStatement(new VariableReference("self.name"), new LiteralExpression<string>("y"), AssignmentOperator.Assign));
var pick = new FunctionDeclaration("pick") { ReturnType = "string", IsStatic = true };
pick.Parameters.Add(new Parameter("c", "bool"));
pick.Body.Add(new ReturnStatement(new ConditionalExpression(new VariableReference("c"), new LiteralExpression<string>("a"), new LiteralExpression<string>("b"))));

The output (abridged) is below. rustc rejects each line with error[E0308]: mismatched types, expected String, found &str:

pub const DEFAULT: String = "d";             // associated const
Self { name: "Anon" }                        // constructor initialiser
return Person { name: "x" };                 // construction member initialiser
self.name = "y";                             // assignment to a String field
return (if c { "a" } else { "b" });          // both branches, although the return type is String

The same AST compiles as C++, Go and Python.

Where

Suggested fix

  • Consts and statics: spell str/string as &str (arrays as &[&str]) when the declaration is const/static. The bare literal is then correct there.
  • Conditionals: make WriteValue recurse into ConditionalExpression branches.
  • Struct literals and field assignments: for a struct literal (Self { … } / Type { … }) and for an assignment to self.<field>, look up the field's declared type on the enclosing or named ClassDeclaration when it is known, and route the value through WriteValue.

Acceptance criteria

  • A RustGeneratedSourceCompilesTests case covering each of the five positions above builds cleanly with rustc.

Activity

  1. added
    bug
    on Oct 5, 2026
  2. matt-edmondson commented on Oct 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions