hylo-lang / hylo

The Hylo programming language
https://www.hylo-lang.org
Apache License 2.0
1.15k stars 53 forks source link

Checking mutability of global bindings #1445

Open tothambrus11 opened 2 months ago

tothambrus11 commented 2 months ago

The compiler doesn't seem to take into account the immutability of global let bindings, so the following program compiles and runs happily:

let counter: Int = 0 // should be an immutable variable initialized to 0

public fun myMethod() {
  &counter += 1; // also compiles without the &
  print(counter.copy())
}

public fun main() {
  myMethod() // prints 1
  myMethod() // prints 2
  myMethod() // prints 3
}

This makes it possible to introduce sneaky side-effects through global variables.

Metalymph commented 2 months ago

About the & mark, I think it's a chosen style, indeed in the Stdlib every infix operator is defined in that way. However changing it is easy in the Emitter. About the global state , I have checked and is effectively declared as immutable in the data segment, but for some reason it escapes the exclusivity law check I still didn't figure out why.

kyouko-taiga commented 2 months ago

It's not about exclusivity but immutability. The program shows a compiler bug. Global bindings are immutable.