fix: mark split/split_rev string methods as pure for const string support#1082
Merged
Merged
Conversation
…port The methods split, split_rev and all their overloads take &mut ImmutableString for early-exit optimisation, which caused the engine to classify them as non-pure and reject calls on const strings with ErrorNonPureMethodCallOnConstant. Add #[rhai_fn(pure)] to each affected function so the engine treats them as read-only, restoring the behaviour that was present before 1.22.0. Fixes rhaiscript#1081. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Problem
Calling
split(or any of its overloads /split_rev) on aconststring fails at runtime withErrorNonPureMethodCallOnConstant.This is a regression introduced in commit
7b94e575which changed the first parameter of these functions from&strto&mut ImmutableStringfor early-exit optimisation. That signature change caused the engine to classify them as non-pure (mutating), so they can no longer be called on constants.Minimal reproduction:
Fix
Add
#[rhai_fn(pure)]to each affected function, matching the same fix applied in #1037 forcontains,get,filter, andto_jsonon object maps and BLOBs.Affected functions in
src/packages/string_more.rs:split_atsplitsplit_whitespacesplitsplitsplitsplitnsplitsplit_charsplitsplitn_charsplitrsplitsplit_revrsplitnsplit_revrsplit_charsplit_revrsplitn_charsplit_revTesting
Regression tests are added to
tests/string.rsinsidetest_string_splitverifying that every overload ofsplitandsplit_revworks when called on aconststring.Closes #1081.