Repository navigation
Closed
Labels
bugSomething isn't working
Description
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:
WriteValue: RustGenerator.cs#L1016-L1031- callers: L998 and L1249
#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 StringThe same AST compiles as C++, Go and Python.
Where
- Associated const: RustGenerator.cs#L650-L659
- File/module const or static: RustGenerator.cs#L1299-L1313.
SpellStorageTypeat L1321-L1331 mapsstringtoString. - Constructor initialisers: RustGenerator.cs#L1063-L1081
- Conditional: RustGenerator.cs#L1420-L1432.
WriteValueonly checks whethervalue is LiteralExpression<string>; it never looks inside the branches. - Assignment: goes through the base emitter with no target type. LanguageGeneratorBase.cs#L737-L746
Suggested fix
- Consts and statics: spell
str/stringas&str(arrays as&[&str]) when the declaration isconst/static. The bare literal is then correct there. - Conditionals: make
WriteValuerecurse intoConditionalExpressionbranches. - Struct literals and field assignments: for a struct literal (
Self { … }/Type { … }) and for an assignment toself.<field>, look up the field's declared type on the enclosing or namedClassDeclarationwhen it is known, and route the value throughWriteValue.
Acceptance criteria
- A
RustGeneratedSourceCompilesTestscase covering each of the five positions above builds cleanly with rustc.
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
matt-edmondson commentedon Oct 5, 2026
Triage
str/stringtype, which maps to ownedString#84, closed by [patch] Make a string literal an owned String where Rust receives one #120)String. The failure is loud, and the same AST compiles on the other targets.RustGenerator(WriteValue, associated and module consts, constructor initialisers, conditionals) and assignment emission inLanguageGeneratorBase. Suggested owner: @matt-edmondson, Rust generatorstr/stringtype, which maps to ownedString#84. Related to Rust output fails to compile ("literal out of range for i32") when an inferred local is initialised with a long literal above i32 range #122 (Rust literal typing for inferredlonglocals) and float/long literals lose their width in inferred locals: Go and C output fails to compile, and C++ silently becomes double/int #151 (literal widths in inferred locals), which also touch literal and type-context handling.Generated by Claude Code
Make a string literal owned where it is reassigned to, or passed as, …