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

borrow sealing doesn't work for borrows within objects #1456

Open
alaviss opened this issue Sep 10, 2024 · 4 comments
Open

borrow sealing doesn't work for borrows within objects #1456

alaviss opened this issue Sep 10, 2024 · 4 comments
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler

Comments

@alaviss
Copy link
Contributor

alaviss commented Sep 10, 2024

Example

{.experimental: "views".}
type
  Mint = object
    data: int

  Option = object
    value: lent Mint

proc `=copy`(dst: var Mint, src: Mint) {.error.}

proc some(x: Mint): Option =
  Option(value: x)

proc main() =
  var x = Mint(data: 10)
  let optlent = some(x)

  echo $optlent.value
  x = Mint(data: 12)
  echo $optlent.value

main()

Actual Output

Compiles and run

Expected Output

Error that x cannot be modified because optlent is holding a live borrow.

@alaviss alaviss added bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler labels Sep 10, 2024
@saem
Copy link
Collaborator

saem commented Sep 12, 2024

Sorry if this is a silly question, but is var x being treated as var x: var Mint? So it's not really a true/independent location, but it's a view into optLen.x? Also is optLen still a live borrow because of the second echo meaning that optLen still has a read against it?

Did I get all those right?

@alaviss
Copy link
Contributor Author

alaviss commented Sep 12, 2024

Nope, what's happening right now is that the borrow checker is not aware that optlent has borrowed x, so none of the constraints are applied.

@saem
Copy link
Collaborator

saem commented Sep 12, 2024

Nope, what's happening right now is that the borrow checker is not aware that optlent has borrowed x, so none of the constraints are applied.

Sorry, I phrased my line of questioning incorrectly, I meant, "Is this what's meant to be happening ... "

@alaviss
Copy link
Contributor Author

alaviss commented Sep 12, 2024

Then no, for this, x is the "owning" location of Mint, and optlent is a view into x.

Since optlent is referred to by the echo after x = , it's considered to be alive at the point of x = , which meant that the location x should remain sealed from mutations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

No branches or pull requests

2 participants