Skip to content

Returned-origin tracking pins a field view to the loop/element binding instead of the borrowed parameter it came from #1297

Closed

Description

@ehartford

Found rewriting `examples/json-parser` under #1150 ruling 4 (`json_get_field(val: &JsonValue, key) -> Option[&JsonValue]`).

D22/D27: a view projected out of a borrowed parameter has that parameter's origin, and a function may return it. The checker instead pins a field projection of a loop binding (or of `xs[i]` / `xs.get(i)`) to that binding and rejects the return:

```
enum JV { Null | Str(str) | Object(Vec[KV]) }
type KV { key: str, value: JV }
fn get(val: &JV, key: str) -> Option[&JV]:
match val:
.Object(entries) =>
for entry in entries:
if entry.key == key:
return Some(&entry.value) // error: returned ephemeral value may outlive its origin 'entry'
None
_ => None
```

The same for `return Some(&entries[i].value)` ("origin 'entries'") and `let entry = entries.get(i); return Some(&entry.value)`, and for the method form (`extend JV: fn get(key)` on `self`). Returning the element itself works (`return Some(entries.get(i))`, `Some(items.get(i))` for an array), so the missing piece is the field projection step: `&entry.value` should inherit `entry`'s origin, which is `val`.

Installed `with v0.15.2.1-g478b26481`. Spec: §3.4 (view-liveness), D22 ("patterns are structural projection"), D27 (element views).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions