Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Borrowing of inline types allows violating of unique value constraints #803

Open
yorickpeterse opened this issue Jan 28, 2025 · 3 comments
Labels
bug Defects, unintended behaviour, etc compiler Changes related to the compiler
Milestone

Comments

@yorickpeterse
Copy link
Collaborator

Please describe the bug

When an inline type contains a uni T value, it's possible to borrow the inline type and still access the uni T (through e.g. a uni ref T) even after the uni T is sent to another process. For example:

type async Unsound {
  fn async unsound(value: uni Foo) {}
}

type Foo {
  let @value: Int
}

type inline Bar {
  let @value: uni Foo
}

type async Main {
  fn async main {
    let a = Bar(recover Foo(42))
    let b = ref a

    match a {
      case { @value = v } -> Unsound().unsound(v)
    }

    # we can continue to use `uni Foo` here through `b.value`
  }
}

The issue here is that inline types being copied results in an alias to the uni T value.

A potential option is to disallow borrowing of inline types if they contain a uni T value, though this may prove too heavy handed.

Operating system

Fedora

Inko version

main

@yorickpeterse yorickpeterse added bug Defects, unintended behaviour, etc compiler Changes related to the compiler labels Jan 28, 2025
@yorickpeterse yorickpeterse added this to the 0.18.0 milestone Jan 28, 2025
@yorickpeterse
Copy link
Collaborator Author

Not allowing borrows of inline types containing uni T values won't work, as this effectively prohibits calling methods on such inline types (as this borrows the receiver).

@yorickpeterse
Copy link
Collaborator Author

I think we can handle this by extending the type verification done when lowering to MIR: if we encounter a mut T or ref T where T contains a uni V, then we produce an error as using such a borrow isn't safe.

A variation is that we allow such borrows, but restrict access to methods that happen to borrow the field(s) in question. I'm not sure yet which approach is best.

@yorickpeterse yorickpeterse modified the milestones: 0.18.0, 0.19.0 Jan 28, 2025
@yorickpeterse
Copy link
Collaborator Author

If we were to introduce unique types as added in #798, we'd run into a similar issue where borrows of inline types can alias the unique types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Defects, unintended behaviour, etc compiler Changes related to the compiler
Projects
None yet
Development

No branches or pull requests

1 participant