Skip to content

fix: mark split/split_rev string methods as pure for const string support#1082

Merged
schungx merged 1 commit into
rhaiscript:mainfrom
theJC:fix/split-pure-const-string
Apr 15, 2026
Merged

fix: mark split/split_rev string methods as pure for const string support#1082
schungx merged 1 commit into
rhaiscript:mainfrom
theJC:fix/split-pure-const-string

Conversation

@theJC

@theJC theJC commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Problem

Calling split (or any of its overloads / split_rev) on a const string fails at runtime with ErrorNonPureMethodCallOnConstant.

This is a regression introduced in commit 7b94e575 which changed the first parameter of these functions from &str to &mut ImmutableString for 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:

const x = "a,b,c";
x.split(",")  // ErrorNonPureMethodCallOnConstant

Fix

Add #[rhai_fn(pure)] to each affected function, matching the same fix applied in #1037 for contains, get, filter, and to_json on object maps and BLOBs.

Affected functions in src/packages/string_more.rs:

Rust fn Rhai name
split_at split
split_whitespace split
split split
splitn split
split_char split
splitn_char split
rsplit split_rev
rsplitn split_rev
rsplit_char split_rev
rsplitn_char split_rev

Testing

Regression tests are added to tests/string.rs inside test_string_split verifying that every overload of split and split_rev works when called on a const string.

Closes #1081.

…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>
@schungx schungx added the bug label Apr 15, 2026
@schungx schungx merged commit c3202b6 into rhaiscript:main Apr 15, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

split on a const string raises ErrorNonPureMethodCallOnConstant

2 participants