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
use std::cmp::{PartialOrd, Ordering};
use super::node::Node;
use super::ArgNames;
/// 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 fn arg_lifetime(
declaration: usize,
arg: &Node,
nodes: &[Node],
arg_names: &ArgNames
) -> Option<Lifetime> {
return Some(if let Some(ref lt) = arg.lifetime {
if &**lt == "return" {
return Some(Lifetime::Return(vec![declaration]));
} else {
// Resolve lifetimes among arguments.
let parent = arg.parent.expect("Expected parent");
let mut args: Vec<usize> = vec![];
args.push(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 Some(Lifetime::Return(args));
}
name = lt.clone();
} else {
break;
}
}
Lifetime::Argument(args)
}
} else {
Lifetime::Argument(vec![declaration])
})
}
pub fn compare_lifetimes(
l: &Option<Lifetime>,
r: &Option<Lifetime>,
nodes: &Vec<Node>
) -> Result<(), String> {
match (l, r) {
(&Some(ref l), &Some(ref r)) => {
match l.partial_cmp(&r) {
Some(Ordering::Greater) | Some(Ordering::Equal) => {
match r {
&Lifetime::Local(r) => {
return Err(format!("`{}` does not live long enough",
nodes[r].name().expect("Expected name")));
}
&Lifetime::Argument(ref r) => {
return Err(format!("`{}` does not live long enough",
nodes[r[0]].name().expect("Expected name")));
}
&Lifetime::Current(r) => {
return Err(format!("`{}` does not live long enough",
nodes[r].name().expect("Expected name")));
}
_ => unimplemented!()
}
}
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.len() > 0 {
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.len() > 0 && r.len() > 0 {
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.len() == 0 {
let last = *r.last().expect("Expected argument index");
return Err(format!("Requires `{}: 'return`",
nodes[last].name().expect("Expected name")));
} else {
unimplemented!();
}
}
(&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(())
}