summaryrefslogtreecommitdiff
path: root/notes/lifetime_rules.txt
blob: 18cea262a7fc0f72a060573e7f0c515c4fbd9add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


ownref
------
Via each code path in a function, one or more "ownref" references may be
acquired, either from the start of the function, or during it's execution.

Each acquired "ownref" must, for each possible code path, either be passed
into another function, returned or stored inside another data type (where
an "ownref" is accepted).

A data structure that contains an "ownref" is considered to be owned by that
data structure.

When an "ownref" is de-acquired, it must be guaranteed that nobody has "ref"
references to it ("ownref" references are forbidden by the rules above).
** TODO solve this... or is it solved by the "ref" rules below? **

ref
---
(including "ownref" referenced from data structures, when the data structure
is not owned)

When a function or structure is passed/assigned a "ref" reference, the
caller/assigner promises that the reference must be valid throughout the
runtime of the function or the lifetime of the data structure.

For function calls, this means that it is sufficient that the "ref" is
valid at function entry, and that the "ref" is not "aliased" or "threaded".
- But sometimes it might be nice to be able to move pointers around,
  if the lifetime is still preserved.
  Does this require a new keyword?

For data structures, the "ref" must:
1. be valid at least throughout the lifetime of the structure,
2. be cleared before the end its lifetime, or
3. TODO more?

We probably need the same rules about "aliased" and "threaded" as for
"ownref".

ownref(X)
-----------
This overrides the "owner" of the reference.
X could reference:
- some data structure (e.g. another parameter in a function, or a struct
  field inside it)
- a "lifetime parameter placeholder" (i.e. to indicate that two variables
  have the same lifetime)
  --> how to define those?
  --> or skip this, and go with the solution below.

alternative solution:
- some kind of conditions/pre-conditions/post-conditions?