Skip to content

Rust: reading a string constant or static where a String is expected fails with E0308, because it is stored as &'static str but read as an owned String #497

Open

Description

@matt-edmondson

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 WriteValue writes NAME.to_string() / String::from(NAME) wherever the receiving type is an owned String.
  • In WriteValue, widen the owned test 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

  1. added
    bug
    on Nov 12, 2026
  2. matt-edmondson commented on Nov 13, 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

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions