Repository navigation
Make string literals owned wherever Rust receives a String [patch] - #228
Merged
Merged
Conversation
#120 wrote String::from("…") for a literal returned as, or initialising a local typed as, an owned String, but every other place a String receives a literal still wrote a bare &str and failed with E0308. - A struct literal's field, whether built by a constructor's initialisers, from a field's declared starting value, or by a construction naming the struct, is written for the type the field was declared with. The struct is the one whose impl is being written for Self, and is looked up in the document otherwise. - An assignment to a field of self is written for the field's type. - A conditional answering a String writes each branch as one. - A const or static declared as a string is a &'static str, and a table of them a &[&'static str]: String::from is not a const fn, so no constant can hold a String built from a literal. Fixes #154 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NW9fJSB9VKsuPv7M2UmznC
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #154
What was wrong
#120 added
String::from("…"), but only for the return-statement and typed-local paths. Every other place an ownedStringreceives a string literal still wrote a bare&str, and rustc rejected it withE0308: mismatched types(expectedString, found&str):Change (all in
RustGenerator)WriteValueusing the field's declared type. That covers a constructor's initialisers, a field's declared starting value (which the constructor fills in), and aConstructionExpressionnaming the struct. The struct isconstructedTypeforSelf, or for its own name inside its impl; otherwise it is looked up in the document with the existingDeclaredTypes(GenerationRoot).self.<field>:GenerateAssignmentStatementwrites the value using the field's declared type. Only plain=changes;String += "lit"already compiles.WriteValuenow recurses into both branches of aConditionalExpressionwhen the target type is an ownedString.SpellStorageTypespells a string as&'static str, and an array of them as&[&'static str].String::fromis not aconst fn, so a constant can never hold aStringbuilt from a literal. The lifetime is written out because elision in associated consts is being phased out.Output for the issue's repro now:
Tests
RustGeneratedSourceCompilesTests.StringLiteralsFillingStringFieldsAndConstants_Compile. It covers the issue's repro plus a struct literal built outside the type's own impl, a field's declared starting value, a module-levelconstandstatic, and a string table. It compiles them withrustc --edition 2021.E0308errors. After the change it passes.Coder.Testsuite: 1265 passed, 0 failed.dotnet format --verify-no-changeson the touched files reports only the CHARSET/IMPORTS findings thatmainalready has.Known limits
stringconstant is now&'static str. Code that reads such a constant where aStringis expected (for examplereturn Self::DEFAULT;from a function returningString) still needs a conversion. That is the same kind of bug in another position, but it is not one of the five this issue lists.stringconstant whose initialiser is a call (not a literal) is also spelled&'static str. No reported case needs anything else, andString::fromcouldn't appear there anyway.🤖 Generated with Claude Code
https://claude.ai/code/session_01NW9fJSB9VKsuPv7M2UmznC
Generated by Claude Code