{"action":"created","issue":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/7373","repository_url":"https://api.github.com/repos/nim-lang/Nim","labels_url":"https://api.github.com/repos/nim-lang/Nim/issues/7373/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/Nim/issues/7373/comments","events_url":"https://api.github.com/repos/nim-lang/Nim/issues/7373/events","html_url":"https://github.com/nim-lang/Nim/issues/7373","id":306651343,"number":7373,"title":"Borrowing for ``var T`` and ``lent T`` to improve Nim's memory safety","user":{"login":"Araq","id":287816,"avatar_url":"https://avatars1.githubusercontent.com/u/287816?v=4","gravatar_id":"","url":"https://api.github.com/users/Araq","html_url":"https://github.com/Araq","followers_url":"https://api.github.com/users/Araq/followers","following_url":"https://api.github.com/users/Araq/following{/other_user}","gists_url":"https://api.github.com/users/Araq/gists{/gist_id}","starred_url":"https://api.github.com/users/Araq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Araq/subscriptions","organizations_url":"https://api.github.com/users/Araq/orgs","repos_url":"https://api.github.com/users/Araq/repos","events_url":"https://api.github.com/users/Araq/events{/privacy}","received_events_url":"https://api.github.com/users/Araq/received_events","type":"User","site_admin":false},"labels":[{"id":32256242,"url":"https://api.github.com/repos/nim-lang/Nim/labels/RFC","name":"RFC","color":"1d1d1d","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-03-19T21:53:15Z","updated_at":"2018-03-19T22:28:46Z","closed_at":null,"author_association":"OWNER","body":"This proposal finally solves issue #124 by a simple borrow checking rule: A return type of ``var T`` is assumed and enforced to be a view into the location reachable by the *first* parameter of the proc. \r\n\r\nEvery known usage of ``var T`` in the standard library is derived from the first parameter. Other code like \r\n\r\n```nim\r\n\r\nvar global: int\r\nproc foo(): var int = global\r\n```\r\n\r\nwill fail to compile. Furthermore the analysis needs to forbit mutating operations on the collection for as long as the ``var T`` views are \"borrowed\". Since ``let x = returnsVarT(collection)`` conceptually turns the pointer view into a copy, this borrow check should be rather easy to enforce. I hope.\r\n\r\nLet us see how this outlined borrowing rule solves #124: \r\n\r\n```nim\r\n\r\nproc forward[T](x: var T): var T = result = x # ok, derives from the first parameter.\r\nproc p(param: var int): var int =\r\n  var x: int\r\n  result = forward(x) # Error: location is derived from ``x`` which is not p's first parameter.\r\n```\r\n\r\n## Future directions\r\n\r\nLater versions can be more precise with a syntax like\r\n\r\n```nim\r\n\r\nproc foo(container: var X): var T scope container\r\n```\r\n\r\nOr maybe the ``from`` keyword can be used instead of ``scope``. \r\n"},"comment":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/comments/374405198","html_url":"https://github.com/nim-lang/Nim/issues/7373#issuecomment-374405198","issue_url":"https://api.github.com/repos/nim-lang/Nim/issues/7373","id":374405198,"user":{"login":"mratsim","id":22738317,"avatar_url":"https://avatars3.githubusercontent.com/u/22738317?v=4","gravatar_id":"","url":"https://api.github.com/users/mratsim","html_url":"https://github.com/mratsim","followers_url":"https://api.github.com/users/mratsim/followers","following_url":"https://api.github.com/users/mratsim/following{/other_user}","gists_url":"https://api.github.com/users/mratsim/gists{/gist_id}","starred_url":"https://api.github.com/users/mratsim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mratsim/subscriptions","organizations_url":"https://api.github.com/users/mratsim/orgs","repos_url":"https://api.github.com/users/mratsim/repos","events_url":"https://api.github.com/users/mratsim/events{/privacy}","received_events_url":"https://api.github.com/users/mratsim/received_events","type":"User","site_admin":false},"created_at":"2018-03-19T22:28:46Z","updated_at":"2018-03-19T22:28:46Z","author_association":"CONTRIBUTOR","body":"I've deleted that part. Bad example."}}
{"actor":{"display_login":"mratsim"}}