string_literal_write
Finds a write into a string literal, which is read-only (s := "abc"; s[0] = x;).
Category correctness, deny by default.
#import "Basic";
main :: () {
word := "hello";
word[0] = #char "j";
print("%\n", word);
}error[string_literal_write]: `word[0]` writes into a string literal: `word` still holds one, and literals are read-only
--> example.jai:5:5
|
5 | word[0] = #char "j";
| ^^^^^^^
|
help: copy it where `word` is declared: `copy_string("hello")` (free the copy when done)
|
4 ~ word := copy_string("hello");
|
= note: `string_literal_write` is `deny` by default#import "Basic";
main :: () {
word := copy_string("hello");
word[0] = #char "j";
print("%\n", word);
free(word);
}Literals are read-only and equal literals share storage, so the write is a runtime error (interpreter) or a crash (native).