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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use super::node::Node;
use super::ArgNames;
use std::cmp::{Ordering, PartialOrd};
/// Describes the lifetime of a variable.
/// When a lifetime `a` > `b` it means `a` outlives `b`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Lifetime {
/// Return value with optional list of arguments that outlives other arguments.
Return(Vec<usize>),
/// An argument outlives other arguments, but does not outlive the return.
Argument(Vec<usize>),
/// Local variable.
Local(usize),
/// Current variable.
///
/// Is equal to itself and outlives local variables.
///
/// Unknown to return because lifetime checker infers
/// lifetime or return value from argument, which does not work
/// with current objects.
Current(usize),
}
impl PartialOrd for Lifetime {
fn partial_cmp(&self, other: &Lifetime) -> Option<Ordering> {
use self::Lifetime::*;
Some(match (self, other) {
(&Current(_), &Local(_)) => Ordering::Greater,
(&Local(_), &Current(_)) => Ordering::Less,
(&Current(a), &Current(b)) if a == b => Ordering::Equal,
(&Current(_), _) => return None,
(_, &Current(_)) => return None,
(&Local(a), &Local(b)) => b.cmp(&a),
(&Return(_), &Local(_)) => Ordering::Greater,
(&Local(_), &Return(_)) => Ordering::Less,
(&Return(ref a), &Return(ref b)) => match (a.len(), b.len()) {
(0, 0) => Ordering::Equal,
(0, _) => Ordering::Less,
(_, 0) => Ordering::Greater,
(_, _) => {
return compare_argument_outlives(a, b);
}
},
(&Argument(_), &Local(_)) => Ordering::Greater,
(&Local(_), &Argument(_)) => Ordering::Less,
(&Return(_), &Argument(_)) => return None,
(&Argument(_), &Return(_)) => return None,
(&Argument(ref a), &Argument(ref b)) => {
return compare_argument_outlives(a, b);
}
})
}
}
/// Takes two lists of arguments.
/// If they have any argument in common, the longer list outlives the shorter.
/// If they have no argument in common, it is not known whether one outlives
/// the other.
fn compare_argument_outlives(a: &[usize], b: &[usize]) -> Option<Ordering> {
for &i in a {
for &j in b {
if i == j {
return Some(a.len().cmp(&b.len()));
}
}
}
None
}
/// Gets the lifetime of a function argument.
pub(crate) fn arg_lifetime(
declaration: usize,
arg: &Node,
nodes: &[Node],
arg_names: &ArgNames,
) -> LifetimeResult {
Ok(if let Some(ref lt) = arg.lifetime {
if &**lt == "return" {
return Ok(Lifetime::Return(vec![declaration]));
} else {
// Resolve lifetimes among arguments.
let parent = arg.parent.expect("Expected parent");
let mut args: Vec<usize> = vec![declaration];
let mut name = lt.clone();
loop {
let (arg, _) = *arg_names
.get(&(parent, name))
.expect("Expected argument name");
args.push(arg);
if let Some(ref lt) = nodes[arg].lifetime {
if &**lt == "return" {
// Lifetimes outlive return.
return Ok(Lifetime::Return(args));
}
name = lt.clone();
} else {
break;
}
}
Lifetime::Argument(args)
}
} else {
Lifetime::Argument(vec![declaration])
})
}
#[derive(Debug)]
pub enum LifetimeError {
None,
FailedToUnify,
}
pub type LifetimeResult = Result<Lifetime, LifetimeError>;
pub(crate) fn compare_lifetimes(
l: &LifetimeResult,
r: &LifetimeResult,
nodes: &[Node],
) -> Result<(), String> {
match (l, r) {
(&Ok(Lifetime::Local(_)), Err(LifetimeError::FailedToUnify)) => {}
(Err(LifetimeError::FailedToUnify), _) |
(_, Err(LifetimeError::FailedToUnify)) => {
// This gets triggered in cases like these:
/*
fn foo(mut a, x: 'a, y) {
a = [x, y] // <--- 'x' outlives 'a', but 'y' does not.
}
*/
return Err("Failed to unify lifetimes (perhaps use `clone`?)".into())
}
(&Ok(ref l), &Ok(ref r)) => {
match l.partial_cmp(r) {
Some(Ordering::Greater) | Some(Ordering::Equal) => {
match *r {
Lifetime::Local(r) => {
// This gets triggered in cases like these:
/*
fn main() {
a := [[]]
b := [3] // <--- declared after 'a'.
a[0] = b // <--- attempting to put 'b' inside 'a'.
}
*/
return Err(format!(
"`{}` does not live long enough",
nodes[r].name().expect("Expected name")
));
}
Lifetime::Argument(ref r) => {
// This gets triggered in cases like these:
/*
fn main() {}
fn foo(mut a: 'b, b) {
a = b // <--- attempting to overwrite 'a' with 'b'.
}
*/
// It is known that `a` outlives `b`, so it is an error
// to attempt overwrite `a` with `b`.
// Notice that `b: 'b` is required to trigger the case,
// since `a` and `b` must have some lifetime in common to get an order.
return Err(format!(
"`{}` does not live long enough",
nodes[r[0]].name().expect("Expected name")
));
}
Lifetime::Current(r) => {
// This gets triggered in cases like these:
/*
fn main() {}
fn foo() ~ a {
a = a // <--- attempting to overwrite 'a' with itself.
}
*/
// Since current objects are not permitted to have lifetimes,
// this is the only case where there is an order,
// so this can only happen when attempting to overwrite
// the same current object with itself.
// For this reason, this case is rarely seen in practice.
return Err(format!(
"`{}` does not live long enough",
nodes[r].name().expect("Expected name")
));
}
Lifetime::Return(ref r) => {
// This gets triggered in cases like these:
/*
fn main() {}
fn foo(mut a: 'b, b: 'return) {
a = b // <--- attempting to overwrite 'a' with 'b'.
}
*/
// It is known that `a` outlives `b`, so it is an error
// to attempt overwrite `a` with `b`.
// Notice that `b: 'b` is required to trigger the case,
// since `a` and `b` must have some lifetime in common to get an order.
// In addition they both need `'return` lifetime.
// `a` has an implicit `'return` lifetime through `b`.
return Err(format!(
"`{}` does not live long enough",
nodes[r[0]].name().expect("Expected name")
));
}
}
}
None => {
match (l, r) {
(&Lifetime::Argument(ref l), &Lifetime::Argument(ref r)) => {
// TODO: Report function name for other cases.
let func = nodes[nodes[r[0]].parent.unwrap()].name().unwrap();
return Err(format!(
"Function `{}` requires `{}: '{}`",
func,
nodes[r[0]].name().expect("Expected name"),
nodes[l[0]].name().expect("Expected name")
));
}
(&Lifetime::Argument(ref l), &Lifetime::Return(ref r)) => {
if !r.is_empty() {
return Err(format!(
"Requires `{}: '{}`",
nodes[r[0]].name().expect("Expected name"),
nodes[l[0]].name().expect("Expected name")
));
} else {
unimplemented!();
}
}
(&Lifetime::Return(ref l), &Lifetime::Return(ref r)) => {
if !l.is_empty() && !r.is_empty() {
return Err(format!(
"Requires `{}: '{}`",
nodes[r[0]].name().expect("Expected name"),
nodes[l[0]].name().expect("Expected name")
));
} else {
unimplemented!();
}
}
(&Lifetime::Return(ref l), &Lifetime::Argument(ref r)) => {
if l.is_empty() {
let last = *r.last().expect("Expected argument index");
return Err(format!(
"Requires `{}: 'return`",
nodes[last].name().expect("Expected name")
));
} else {
return Err(format!(
"`{}` does not live long enough",
nodes[r[0]].name().expect("Expected name")
));
}
}
(&Lifetime::Current(n), _) => {
return Err(format!(
"`{}` is a current object, use `clone(_)`",
nodes[n].name().expect("Expected name")
));
}
(_, &Lifetime::Current(n)) => {
return Err(format!(
"`{}` is a current object, use `clone(_)`",
nodes[n].name().expect("Expected name")
));
}
x => panic!("Unknown case {:?}", x),
}
}
_ => {}
}
}
// TODO: Handle other cases.
_ => {}
}
Ok(())
}