V1040 assign_immutable
Only var bindings can be assigned; let bindings and parameters are immutable (design §8.1).
An error from the resolve phase. sakura explain V1040 prints this explanation offline.
Only var bindings can be assigned; let bindings and parameters are immutable (design §8.1).
Wrong:
let n = 0
n = n + 1Fixed:
var n = 0
n = n + 1Wrong program
compiler/tests/ui/V1040_assign_immutable.sakura, which the compiler's ui suite runs through sakura check:
fn bump() -> Int {
let x = 1
x = 2
x
}sakura check --json reports this record (pretty-printed; $DIR is the fixture's directory):
{
"schema_version": "1.0",
"kind": "diagnostic",
"id": "d:0f924446648db328",
"code": "V1040",
"severity": "error",
"phase": "resolve",
"message": "cannot assign to `x`: it is bound by `let`, which is immutable",
"snapshot": "sha256:$SNAPSHOT",
"symbol_id": "main::V1040_assign_immutable::bump",
"spans": [
{
"uri": "file://$DIR/V1040_assign_immutable.sakura",
"content_hash": "sha256:40c9e7936c6de9f647222973e040bb9c2de3da952d0decdb81b480d2fd85436e",
"start_byte": 93,
"end_byte": 94,
"role": "primary"
},
{
"uri": "file://$DIR/V1040_assign_immutable.sakura",
"content_hash": "sha256:40c9e7936c6de9f647222973e040bb9c2de3da952d0decdb81b480d2fd85436e",
"start_byte": 85,
"end_byte": 86,
"role": "definition",
"label": "bound here"
}
],
"related_ids": [],
"expected": null,
"actual": null,
"obligation": null,
"counterexample": null,
"explanation_uri": "sakura://explain/V1040",
"fixes": [
{
"id": "f:0f924446648db328.0",
"title": "Declare it with `var`",
"applicability": "machine_applicable",
"impact": "local",
"edits": [
{
"span": {
"uri": "file://$DIR/V1040_assign_immutable.sakura",
"content_hash": "sha256:40c9e7936c6de9f647222973e040bb9c2de3da952d0decdb81b480d2fd85436e",
"start_byte": 81,
"end_byte": 84,
"role": "primary"
},
"replacement": "var"
}
]
}
]
}Fixed program
compiler/tests/ui/V1040_assign_immutable.fixed.sakura, which sakura check accepts with no diagnostics:
fn bump() -> Int {
var x = 1
x = 2
x
}