Repository navigation
Description
What's wrong
A string-typed constant or static (a FieldDeclaration at file, namespace or type scope, or a module variable) is declared with the borrowed type. RustGenerator.SpellStorageType does this deliberately, because String::from is not a const fn (Coder/Languages/RustGenerator.cs:2378-2383):
string spelled = IsOwnedString(element) ? "&'static str" : SpellType(element);Reads of that constant are not adjusted to match. The StaticType of a VariableReference to it is the declared string, which the generator treats as an owned String. WriteValue adds an owning conversion only to a string literal (Coder/Languages/RustGenerator.cs:1596):
bool owned = value is LiteralExpression<string> && IsOwnedString(target);So when the constant is returned from a string function, passed to a string parameter, or assigned to a string local, it is written bare, and a &'static str arrives where a String is expected.
Failure scenario
var file = new SourceFile("cfg");
file.Members.Add(new FieldDeclaration("GREETING", "string") { IsStatic = true, IsConstant = true, InitialValue = Literal.Text("hello") });
var greeting = new FunctionDeclaration("greeting") { ReturnType = "string", IsStatic = true };
greeting.Body.Add(new ReturnStatement(new VariableReference("GREETING")));
file.Members.Add(greeting);
var shout = new FunctionDeclaration("shout") { ReturnType = "string", IsStatic = true };
shout.Parameters.Add(new Parameter("s", "string"));
shout.Body.Add(new ReturnStatement(new VariableReference("s")));
file.Members.Add(shout);
var loud = new FunctionDeclaration("loud") { ReturnType = "string", IsStatic = true };
loud.Body.Add(new ReturnStatement(new CallExpression("shout") { Arguments = { new VariableReference("GREETING") } }));
file.Members.Add(loud);Generated Rust:
pub const GREETING: &'static str = "hello";
pub fn greeting() -> String {
return GREETING; // E0308: expected `String`, found `&str`
}
pub fn shout(s: String) -> String {
return s;
}
pub fn loud() -> String {
return shout(GREETING); // E0308: expected `String`, found `&str`
}rustc --crate-type lib --edition 2021 reports both E0308 errors. This was reproduced against the built Coder.dll at f15652d. The same happens with a non-constant static string (pub static label: &'static str = …) and with an associated constant inside an impl. The other six targets compile this input.
Why it matters
A named string constant that a function returns or passes on is one of the most common things a generated file contains, such as a default name, a format string or a key. Each such use is a compile error in Rust. This is the read-side counterpart of #154 and #486, which fixed string literals in the same positions. It is separate from #354 (struct-typed statics and moves, E0507) and from #495 (getters returning fields).
Suggested fix
Track which names are borrowed string storage, meaning the constants and statics written through SpellStorageType. Then do one of the following:
- Report their static type as borrowed, so that
WriteValuewritesNAME.to_string()/String::from(NAME)wherever the receiving type is an ownedString. - In
WriteValue, widen theownedtest to any expression whose storage is&'static str.
Comparisons and format! joins already accept &str, so they can stay as they are. Add a case to RustGeneratedSourceCompilesTests covering a returned constant, a constant passed as an argument, and a constant assigned to a local.
Activity
matt-edmondson commentedon Nov 13, 2026
Triage
Stringis a Rust compile error (E0308). Named string constants are among the most common things in generated files. It reproduces at f15652d.RustGenerator.SpellStorageTypeandRustGenerator.WriteValue(theownedtest)str/stringtype, which maps to ownedString#84 and 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) and Make a string literal owned where it is reassigned to, or passed as, a Rust String [patch] #486. It is separate from Rust: reading a struct-typed file-scope field fails to compile (E0507 "cannot move out of static item") #354 (struct-typed statics, E0507) and Rust: a getter returning a String- or struct-typed field fails with E0507 "cannot move out ofself.namewhich is behind a shared reference" #495 (getters, E0507). The issue is closely related to An inferred local initialised with a string literal isauto s = "x"(const char*) in C++ andlet mut s = "x"(&str) in Rust, sos += …orreturn sdoesn't compile #496 (an inferred local from a literal becomes&str). Both are owned/borrowed string mismatches, so one PR could fix both.Generated by Claude Code