What's wrong
RustGenerator's TypeMappings maps str and string to the owned type String. A string literal is written as a bare "..." (LanguageGeneratorBase, and RustGenerator does not override it), which in Rust is a &'static str. Wherever the two meet, the generated code does not compile.
Reproduction
Found with a scratch app against Coder at aae2bb6, and the output compiled with rustc (edition 2021):
| AST |
Rust output |
rustc |
FunctionDeclaration("greet") { ReturnType = "str" } whose body is ReturnStatement(Literal.Text("hello")) |
pub fn greet() -> String { return "hello"; } |
error[E0308]: mismatched types (expected String, found &str) |
VariableDeclaration("name", "str", Literal.Text("x")) |
let mut name: String = "x"; |
error[E0308]: mismatched types |
The same AST compiles in the other targets. The tests miss it because the Rust compile tests only use &str parameters, and RustGeneratorTests.Types_AreMappedToRustSpellings asserts -> String without ever returning a literal.
Suggested fix
When a string literal's target is an owned String, write String::from("…") (or "…".to_string()). The target can be:
- the return of a function whose return type maps to
String
- the initialiser of a
String-typed local, let, or field
- a field value in a construction
Keep the bare literal where the target is &str or unknown. A simpler alternative is to always write String::from("…") for text literals in Rust. That is correct everywhere an owned String is expected, and borrows to &str automatically only in some positions, so check the call-argument cases.
Acceptance criteria
- A rustc compile test that returns and assigns a string literal to
str/string-typed targets builds cleanly.
What's wrong
RustGenerator'sTypeMappingsmapsstrandstringto the owned typeString. A string literal is written as a bare"..."(LanguageGeneratorBase, andRustGeneratordoes not override it), which in Rust is a&'static str. Wherever the two meet, the generated code does not compile.Reproduction
Found with a scratch app against
Coderat aae2bb6, and the output compiled with rustc (edition 2021):FunctionDeclaration("greet") { ReturnType = "str" }whose body isReturnStatement(Literal.Text("hello"))pub fn greet() -> String { return "hello"; }error[E0308]: mismatched types(expectedString, found&str)VariableDeclaration("name", "str", Literal.Text("x"))let mut name: String = "x";error[E0308]: mismatched typesThe same AST compiles in the other targets. The tests miss it because the Rust compile tests only use
&strparameters, andRustGeneratorTests.Types_AreMappedToRustSpellingsasserts-> Stringwithout ever returning a literal.Suggested fix
When a string literal's target is an owned
String, writeString::from("…")(or"…".to_string()). The target can be:StringString-typed local,let, or fieldKeep the bare literal where the target is
&stror unknown. A simpler alternative is to always writeString::from("…")for text literals in Rust. That is correct everywhere an ownedStringis expected, and borrows to&strautomatically only in some positions, so check the call-argument cases.Acceptance criteria
str/string-typed targets builds cleanly.