hylo-lang / hylo

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

Ill consumption? #1499

Open dabrahams opened 3 months ago

dabrahams commented 3 months ago

I don't know why this compiles. I also don't know why it prints 0 instead of -1.

type X: Deinitializable {
  public var a: Int
  public memberwise init
  public fun deinit() sink { &a = -1 }
  public fun print() { print(a) }
}

fun eat(_ x: sink X) {}

fun test0() {
  let x = X(a: 0)
  let y = x
  eat(x)
  y.print() // !!y is consumed. print(y.a) is repoerted as an error.
}

public fun main() {
  test0()
}
kyouko-taiga commented 3 months ago

The invalid print is caused by a bug in the detection of trivially deinitializable types. deinit is ignored because the compiler thinks there's no part of X that needs to run a deinitializer.

The missing error is seemingly because of a bug in access checking that lets us consume through an access that has a live let binding. The logic is actually rather complex and this bug report makes me think we should perhaps revisit the idea that consumption through let is ever allowed.

kyouko-taiga commented 3 months ago

See also https://github.com/orgs/hylo-lang/discussions/1501