rune icon indicating copy to clipboard operation
rune copied to clipboard

Incorrect error when trying to mutate constants.

Open tgolsson opened this issue 2 years ago • 2 comments

Found this while trying some stuff on the playground over lunch...

const FOOBAR = 1;

pub fn main() {
    FOOBAR = 1;
}

gives the errors:

error: compile error
  ┌─ entry:4:5
  │
4 │     FOOBAR = 1;
  │     ^^^^^^^^^^ no local variable `FOOBAR`

warning: warning
  ┌─ entry:1:1
  │
1 │ const FOOBAR = 1;
  │ ^^^^^^^^^^^^^^^^ not used

https://rune-rs.github.io/play/?c=Y29uc3QgRk9PQkFSID0gMTsKCnB1YiBmbiBtYWluKCkgewogICAgRk9PQkFSID0gMTsKfQo%3D

tgolsson avatar Nov 17 '21 12:11 tgolsson

So I'm bug triaging right now. Since mutating a constant shouldn't be possible, what is the preferred solution here? Is it simply a matter of improving the compilation so that it queries for any items which also matches the left hand side of an assignment and raises a different more descriptive error which says that FOOBAR is used but incorrectly:

error: compile error
  ┌─ entry:4:5
  │
4 │     FOOBAR = 1;
  │     ^^^^^^^^^^ cannot assign to `const FOOBAR`

udoprog avatar Nov 29 '21 19:11 udoprog

I think that'd be a much better error message!

tgolsson avatar Nov 30 '21 14:11 tgolsson