-
Notifications
You must be signed in to change notification settings - Fork 74
/
test.rs
320 lines (280 loc) · 11.4 KB
/
test.rs
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#![cfg(test)]
use crate::facts::{AllFacts, Loan, Point, Region};
use crate::intern;
use crate::program::parse_from_program;
use crate::tab_delim;
use crate::test_util::assert_equal;
use failure::Error;
use polonius_engine::{Algorithm, Output};
use rustc_hash::FxHashMap;
use std::path::Path;
fn test_facts(all_facts: &AllFacts, algorithms: &[Algorithm]) {
let naive = Output::compute(all_facts, Algorithm::Naive, true);
// Check that the "naive errors" are a subset of the "insensitive
// ones".
let insensitive = Output::compute(all_facts, Algorithm::LocationInsensitive, false);
for (naive_point, naive_loans) in &naive.errors {
match insensitive.errors.get(&naive_point) {
Some(insensitive_loans) => {
for naive_loan in naive_loans {
if !insensitive_loans.contains(naive_loan) {
panic!(
"naive analysis had error for `{:?}` at `{:?}` \
but insensitive analysis did not \
(loans = {:#?})",
naive_loan, naive_point, insensitive_loans,
);
}
}
}
None => {
panic!(
"naive analysis had errors at `{:?}` but insensitive analysis did not \
(loans = {:#?})",
naive_point, naive_loans,
);
}
}
}
// The optimized checks should behave exactly the same as the naive check.
for &optimized_algorithm in algorithms {
println!("Algorithm {:?}", optimized_algorithm);
let opt = Output::compute(all_facts, optimized_algorithm, true);
assert_equal(&naive.borrow_live_at, &opt.borrow_live_at);
assert_equal(&naive.errors, &opt.errors);
}
// The hybrid algorithm gets the same errors as the naive version
let opt = Output::compute(all_facts, Algorithm::Hybrid, true);
assert_equal(&naive.errors, &opt.errors);
}
fn test_fn(dir_name: &str, fn_name: &str, algorithm: Algorithm) -> Result<(), Error> {
let facts_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("inputs")
.join(dir_name)
.join("nll-facts")
.join(fn_name);
println!("facts_dir = {:?}", facts_dir);
let tables = &mut intern::InternerTables::new();
let all_facts = tab_delim::load_tab_delimited_facts(tables, &facts_dir)?;
Ok(test_facts(&all_facts, &[algorithm]))
}
macro_rules! tests {
($($name:ident($dir:expr, $fn:expr),)*) => {
$(
mod $name {
use super::*;
#[test]
fn datafrog_opt() -> Result<(), Error> {
test_fn($dir, $fn, Algorithm::DatafrogOpt)
}
}
)*
}
}
tests! {
issue_47680("issue-47680", "main"),
vec_push_ref_foo1("vec-push-ref", "foo1"),
vec_push_ref_foo2("vec-push-ref", "foo2"),
vec_push_ref_foo3("vec-push-ref", "foo3"),
}
#[test]
fn test_insensitive_errors() -> Result<(), Error> {
let facts_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("inputs")
.join("issue-47680")
.join("nll-facts")
.join("main");
println!("facts_dir = {:?}", facts_dir);
let tables = &mut intern::InternerTables::new();
let all_facts = tab_delim::load_tab_delimited_facts(tables, &facts_dir)?;
let insensitive = Output::compute(&all_facts, Algorithm::LocationInsensitive, false);
let mut expected = FxHashMap::default();
expected.insert(Point::from(1), vec![Loan::from(1)]);
expected.insert(Point::from(2), vec![Loan::from(2)]);
assert_equal(&insensitive.errors, &expected);
Ok(())
}
#[test]
fn test_sensitive_passes_issue_47680() -> Result<(), Error> {
let facts_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("inputs")
.join("issue-47680")
.join("nll-facts")
.join("main");
let tables = &mut intern::InternerTables::new();
let all_facts = tab_delim::load_tab_delimited_facts(tables, &facts_dir)?;
let sensitive = Output::compute(&all_facts, Algorithm::DatafrogOpt, false);
assert!(sensitive.errors.is_empty());
Ok(())
}
#[test]
fn no_subset_symmetries_exist() -> Result<(), Error> {
let facts_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("inputs")
.join("issue-47680")
.join("nll-facts")
.join("main");
let tables = &mut intern::InternerTables::new();
let all_facts = tab_delim::load_tab_delimited_facts(tables, &facts_dir)?;
let subset_symmetries_exist = |output: &Output<Region, Loan, Point>| {
for (_, subsets) in &output.subset {
for (r1, rs) in subsets {
if rs.contains(&r1) {
return true;
}
}
}
false
};
let naive = Output::compute(&all_facts, Algorithm::Naive, true);
assert!(!subset_symmetries_exist(&naive));
// FIXME: the issue-47680 dataset is suboptimal here as DatafrogOpt does not
// produce subset symmetries for it. It does for clap, and it was used to manually verify
// that the assert in verbose mode didn't trigger. Therefore, switch to this dataset
// whenever it's fast enough to be enabled in tests, or somehow create a test facts program
// or reduce it from clap.
let opt = Output::compute(&all_facts, Algorithm::DatafrogOpt, true);
assert!(!subset_symmetries_exist(&opt));
Ok(())
}
// The following 3 tests, `send_is_not_static_std_sync`, `escape_upvar_nested`, and `issue_31567`
// are extracted from rustc's test suite, and fail because of differences between the Naive
// and DatafrogOpt variants, on the computation of the transitive closure.
// They are part of the same pattern that the optimized variant misses, and only differ in
// the length of the `outlives` chain reaching a live region at a specific point.
#[test]
fn send_is_not_static_std_sync() {
// Reduced from rustc test: ui/span/send-is-not-static-std-sync.rs
// (in the functions: `mutex` and `rwlock`)
let program = r"
universal_regions { }
block B0 {
borrow_region_at('a, L0), outlives('a: 'b), region_live_at('b);
}
";
let mut tables = intern::InternerTables::new();
let facts = parse_from_program(program, &mut tables).expect("Parsing failure");
test_facts(&facts, Algorithm::OPTIMIZED);
}
#[test]
fn escape_upvar_nested() {
// Reduced from rustc test: ui/nll/closure-requirements/escape-upvar-nested.rs
// (in the function: `test-\{\{closure\}\}-\{\{closure\}\}/`)
// This reduction is also present in other tests:
// - ui/nll/closure-requirements/escape-upvar-ref.rs, in the `test-\{\{closure\}\}/` function
let program = r"
universal_regions { }
block B0 {
borrow_region_at('a, L0), outlives('a: 'b), outlives('b: 'c), region_live_at('c);
}
";
let mut tables = intern::InternerTables::new();
let facts = parse_from_program(program, &mut tables).expect("Parsing failure");
test_facts(&facts, Algorithm::OPTIMIZED);
}
#[test]
fn issue_31567() {
// Reduced from rustc test: ui/nll/issue-31567.rs
// This is one of two tuples present in the Naive results and missing from the Opt results,
// the second tuple having the same pattern as the one in this test.
// This reduction is also present in other tests:
// - ui/issue-48803.rs, in the `flatten` function
let program = r"
universal_regions { }
block B0 {
borrow_region_at('a, L0),
outlives('a: 'b),
outlives('b: 'c),
outlives('c: 'd),
region_live_at('d);
}
";
let mut tables = intern::InternerTables::new();
let facts = parse_from_program(program, &mut tables).expect("Parsing failure");
test_facts(&facts, Algorithm::OPTIMIZED);
}
#[test]
fn borrowed_local_error() {
// This test is related to the previous 3: there is still a borrow_region outliving a live region,
// through a chain of `outlives` at a single point, but this time there are also 2 points
// and an edge.
// Reduced from rustc test: ui/nll/borrowed-local-error.rs
// (in the function: `gimme`)
// This reduction is also present in other tests:
// - ui/nll/borrowed-temporary-error.rs, in the `gimme` function
// - ui/nll/borrowed-referent-issue-38899.rs, in the `bump` function
// - ui/nll/return-ref-mut-issue-46557.rs, in the `gimme_static_mut` function
// - ui/span/dropck_direct_cycle_with_drop.rs, in the `{{impl}}[1]-drop-{{closure}}` function
// - ui/span/wf-method-late-bound-regions.rs, in the `{{impl}}-xmute` function
let program = r"
universal_regions { 'c }
block B0 {
borrow_region_at('a, L0), outlives('a: 'b), outlives('b: 'c);
}
";
let mut tables = intern::InternerTables::new();
let facts = parse_from_program(program, &mut tables).expect("Parsing failure");
test_facts(&facts, Algorithm::OPTIMIZED);
}
#[test]
fn smoke_test_errors() {
let failures = [
"return_ref_to_local",
"use_while_mut",
"use_while_mut_fr",
"well_formed_function_inputs",
];
for test_fn in &failures {
let facts_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("inputs")
.join("smoke-test")
.join("nll-facts")
.join(test_fn);
println!("facts_dir = {:?}", facts_dir);
let tables = &mut intern::InternerTables::new();
let facts = tab_delim::load_tab_delimited_facts(tables, &facts_dir).expect("facts");
let location_insensitive = Output::compute(&facts, Algorithm::LocationInsensitive, true);
assert!(
!location_insensitive.errors.is_empty(),
format!("LocationInsensitive didn't find errors for '{}'", test_fn)
);
let naive = Output::compute(&facts, Algorithm::Naive, true);
assert!(
!naive.errors.is_empty(),
format!("Naive didn't find errors for '{}'", test_fn)
);
let opt = Output::compute(&facts, Algorithm::DatafrogOpt, true);
assert!(
!opt.errors.is_empty(),
format!("DatafrogOpt didn't find errors for '{}'", test_fn)
);
}
}
#[test]
fn smoke_test_success_1() {
let facts_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("inputs")
.join("smoke-test")
.join("nll-facts")
.join("position_dependent_outlives");
println!("facts_dir = {:?}", facts_dir);
let tables = &mut intern::InternerTables::new();
let facts = tab_delim::load_tab_delimited_facts(tables, &facts_dir).expect("facts");
let location_insensitive = Output::compute(&facts, Algorithm::LocationInsensitive, true);
assert!(!location_insensitive.errors.is_empty());
test_facts(&facts, Algorithm::OPTIMIZED);
}
#[test]
fn smoke_test_success_2() {
let facts_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("inputs")
.join("smoke-test")
.join("nll-facts")
.join("foo");
println!("facts_dir = {:?}", facts_dir);
let tables = &mut intern::InternerTables::new();
let facts = tab_delim::load_tab_delimited_facts(tables, &facts_dir).expect("facts");
let location_insensitive = Output::compute(&facts, Algorithm::LocationInsensitive, true);
assert!(location_insensitive.errors.is_empty());
test_facts(&facts, Algorithm::OPTIMIZED);
}