| @@ -17,65 +17,76 @@ fn lower(name: &str, text: &str) -> Program { |
| 17 | 17 | program |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | +/// Run `f` on a thread with the room the CLI gives the compiler |
| 21 | +/// (`louise/src/main.rs`): compiling the standard library recurses once |
| 22 | +/// per statement of a function, and std only grows. |
| 23 | +fn big_stack(f: impl FnOnce() + Send + 'static) { |
| 24 | + std::thread::Builder::new() |
| 25 | + .stack_size(64 << 20) |
| 26 | + .spawn(f) |
| 27 | + .expect("spawn the test thread") |
| 28 | + .join() |
| 29 | + .expect("the test thread panicked"); |
| 30 | +} |
| 31 | + |
| 20 | 32 | #[test] |
| 21 | 33 | fn every_run_program_is_balanced() { |
| 22 | 34 | // The passes recurse once per statement of a function, and CPR |
| 23 | 35 | // (issue #200) adds a worker copy of some — the verifier needs the |
| 24 | 36 | // same room the CLI gives the compiler (`louise/src/main.rs`). |
| 25 | | − std::thread::Builder::new() |
| 26 | | − .stack_size(64 << 20) |
| 27 | | − .spawn(|| { |
| 28 | | − let mut failures = Vec::new(); |
| 29 | | − for path in run_programs() { |
| 30 | | − let (compiled, name) = compile_file(&path).unwrap(); |
| 31 | | − let mut program = compiled.program; |
| 32 | | − // The native pipeline (ADR 0043): the optimiser, then Perceus. |
| 33 | | − louise_core::opt::run(&mut program); |
| 34 | | − louise_perceus::insert_rc(&mut program); |
| 35 | | − if let Err(e) = louise_perceus::verify(&program) { |
| 36 | | − failures.push(format!("{name}: {e}")); |
| 37 | | − } |
| 37 | + big_stack(|| { |
| 38 | + let mut failures = Vec::new(); |
| 39 | + for path in run_programs() { |
| 40 | + let (compiled, name) = compile_file(&path).unwrap(); |
| 41 | + let mut program = compiled.program; |
| 42 | + // The native pipeline (ADR 0043): the optimiser, then Perceus. |
| 43 | + louise_core::opt::run(&mut program); |
| 44 | + louise_perceus::insert_rc(&mut program); |
| 45 | + if let Err(e) = louise_perceus::verify(&program) { |
| 46 | + failures.push(format!("{name}: {e}")); |
| 38 | 47 | } |
| 39 | | − assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 40 | | − }) |
| 41 | | − .expect("spawn the test thread") |
| 42 | | − .join() |
| 43 | | − .expect("the test thread panicked"); |
| 48 | + } |
| 49 | + assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 50 | + }); |
| 44 | 51 | } |
| 45 | 52 | |
| 46 | 53 | /// Functions the entries can't reach keep their bodies without counts, and |
| 47 | 54 | /// borrow nothing (issue #114). |
| 48 | 55 | #[test] |
| 49 | 56 | fn unreachable_functions_are_left_alone() { |
| 50 | | − let mut program = lower( |
| 51 | | − "m", |
| 52 | | − "fn used(xs: List<Int>) -> List<Int> { [1, ..xs] }\nfn unused(xs: List<Int>) -> List<Int> { [2, ..xs] }\npub fn main() -> List<Int> { used([]) }", |
| 53 | | − ); |
| 54 | | − let id = |name: &str| program.fns.iter().position(|f| f.name == name).unwrap() as u32; |
| 55 | | − let (main, used, unused) = (id("m.main"), id("m.used"), id("m.unused")); |
| 56 | | − let before = program.fns[unused as usize].body.clone(); |
| 57 | | − let live = [main, used].into_iter().collect(); |
| 58 | | − louise_perceus::insert_rc_in(&mut program, &live); |
| 59 | | − louise_perceus::verify_in(&program, &live).unwrap(); |
| 60 | | − assert_eq!(program.fns[unused as usize].body, before); |
| 61 | | − assert!(program.fns[unused as usize].borrowed.is_empty()); |
| 57 | + big_stack(|| { |
| 58 | + let mut program = lower( |
| 59 | + "m", |
| 60 | + "fn used(xs: List<Int>) -> List<Int> { [1, ..xs] }\nfn unused(xs: List<Int>) -> List<Int> { [2, ..xs] }\npub fn main() -> List<Int> { used([]) }", |
| 61 | + ); |
| 62 | + let id = |name: &str| program.fns.iter().position(|f| f.name == name).unwrap() as u32; |
| 63 | + let (main, used, unused) = (id("m.main"), id("m.used"), id("m.unused")); |
| 64 | + let before = program.fns[unused as usize].body.clone(); |
| 65 | + let live = [main, used].into_iter().collect(); |
| 66 | + louise_perceus::insert_rc_in(&mut program, &live); |
| 67 | + louise_perceus::verify_in(&program, &live).unwrap(); |
| 68 | + assert_eq!(program.fns[unused as usize].body, before); |
| 69 | + assert!(program.fns[unused as usize].borrowed.is_empty()); |
| 70 | + }); |
| 62 | 71 | } |
| 63 | 72 | |
| 64 | 73 | #[test] |
| 65 | 74 | fn unbalanced_programs_are_caught() { |
| 66 | | − let mut program = lower("m", "fn id(x) { x }\npub fn main() -> () { () }"); |
| 67 | | − louise_perceus::insert_rc(&mut program); |
| 68 | | − louise_perceus::verify(&program).unwrap(); |
| 69 | | − // Drop a reference `id` does not have to spare. |
| 70 | | − let f = program |
| 71 | | − .fns |
| 72 | | − .iter_mut() |
| 73 | | − .find(|f| f.name == "m.id") |
| 74 | | − .expect("id"); |
| 75 | | − let body = std::mem::replace(&mut f.body, Expr::Return(Atom::Unit)); |
| 76 | | − f.body = Expr::Drop(f.params[0], Box::new(body)); |
| 77 | | − let err = louise_perceus::verify(&program).unwrap_err(); |
| 78 | | − assert!(err.contains("m.id"), "{err}"); |
| 75 | + big_stack(|| { |
| 76 | + let mut program = lower("m", "fn id(x) { x }\npub fn main() -> () { () }"); |
| 77 | + louise_perceus::insert_rc(&mut program); |
| 78 | + louise_perceus::verify(&program).unwrap(); |
| 79 | + // Drop a reference `id` does not have to spare. |
| 80 | + let f = program |
| 81 | + .fns |
| 82 | + .iter_mut() |
| 83 | + .find(|f| f.name == "m.id") |
| 84 | + .expect("id"); |
| 85 | + let body = std::mem::replace(&mut f.body, Expr::Return(Atom::Unit)); |
| 86 | + f.body = Expr::Drop(f.params[0], Box::new(body)); |
| 87 | + let err = louise_perceus::verify(&program).unwrap_err(); |
| 88 | + assert!(err.contains("m.id"), "{err}"); |
| 89 | + }); |
| 79 | 90 | } |
| 80 | 91 | |
| 81 | 92 | #[test] |
| @@ -95,19 +106,21 @@ fn first(xs, d) { |
| 95 | 106 | |
| 96 | 107 | pub fn main() -> () { () } |
| 97 | 108 | "#; |
| 98 | | − let mut program = lower("main", src); |
| 99 | | − louise_perceus::insert_rc(&mut program); |
| 100 | | − louise_perceus::verify(&program).unwrap(); |
| 101 | | − let text = program.to_string(); |
| 102 | | − let keep: Vec<&str> = text |
| 103 | | − .split("\n\n") |
| 104 | | − .filter(|f| { |
| 105 | | − ["main.area", "main.both", "main.first"] |
| 106 | | − .iter() |
| 107 | | − .any(|n| f.starts_with(&format!("fn {n}("))) |
| 108 | | − }) |
| 109 | | − .collect(); |
| 110 | | − insta::assert_snapshot!(keep.join("\n\n")); |
| 109 | + big_stack(|| { |
| 110 | + let mut program = lower("main", src); |
| 111 | + louise_perceus::insert_rc(&mut program); |
| 112 | + louise_perceus::verify(&program).unwrap(); |
| 113 | + let text = program.to_string(); |
| 114 | + let keep: Vec<&str> = text |
| 115 | + .split("\n\n") |
| 116 | + .filter(|f| { |
| 117 | + ["main.area", "main.both", "main.first"] |
| 118 | + .iter() |
| 119 | + .any(|n| f.starts_with(&format!("fn {n}("))) |
| 120 | + }) |
| 121 | + .collect(); |
| 122 | + insta::assert_snapshot!(keep.join("\n\n")); |
| 123 | + }); |
| 111 | 124 | } |
| 112 | 125 | |
| 113 | 126 | #[test] |
| @@ -126,25 +139,27 @@ fn copy(xs) { |
| 126 | 139 | |
| 127 | 140 | pub fn main() -> () { () } |
| 128 | 141 | "#; |
| 129 | | − let mut program = lower("main", src); |
| 130 | | − louise_perceus::insert_rc(&mut program); |
| 131 | | − louise_perceus::verify(&program).unwrap(); |
| 132 | | − let text = program.to_string(); |
| 133 | | − let keep: Vec<&str> = text |
| 134 | | − .split("\n\n") |
| 135 | | − .filter(|f| { |
| 136 | | − // `copy` calls its TRMC sibling, which reuses the cells. |
| 137 | | − [ |
| 138 | | − "main.first", |
| 139 | | − "main.uses_first", |
| 140 | | − "main.copy", |
| 141 | | − "main.copy$trmc", |
| 142 | | − ] |
| 143 | | − .iter() |
| 144 | | − .any(|n| f.starts_with(&format!("fn {n}("))) |
| 145 | | − }) |
| 146 | | − .collect(); |
| 147 | | − insta::assert_snapshot!(keep.join("\n\n")); |
| 142 | + big_stack(|| { |
| 143 | + let mut program = lower("main", src); |
| 144 | + louise_perceus::insert_rc(&mut program); |
| 145 | + louise_perceus::verify(&program).unwrap(); |
| 146 | + let text = program.to_string(); |
| 147 | + let keep: Vec<&str> = text |
| 148 | + .split("\n\n") |
| 149 | + .filter(|f| { |
| 150 | + // `copy` calls its TRMC sibling, which reuses the cells. |
| 151 | + [ |
| 152 | + "main.first", |
| 153 | + "main.uses_first", |
| 154 | + "main.copy", |
| 155 | + "main.copy$trmc", |
| 156 | + ] |
| 157 | + .iter() |
| 158 | + .any(|n| f.starts_with(&format!("fn {n}("))) |
| 159 | + }) |
| 160 | + .collect(); |
| 161 | + insta::assert_snapshot!(keep.join("\n\n")); |
| 162 | + }); |
| 148 | 163 | } |
| 149 | 164 | |
| 150 | 165 | /// Early `return`s (ADR 0037): an `exit` two blocks down releases what the |
| @@ -203,19 +218,21 @@ fn count(n: Int, keep: List<Int>) -> Int { |
| 203 | 218 | |
| 204 | 219 | pub fn main() -> () { () } |
| 205 | 220 | "#; |
| 206 | | − let mut program = lower("main", src); |
| 207 | | − louise_perceus::insert_rc(&mut program); |
| 208 | | − louise_perceus::verify(&program).unwrap(); |
| 209 | | − let text = program.to_string(); |
| 210 | | − let keep: Vec<&str> = text |
| 211 | | − .split("\n\n") |
| 212 | | − .filter(|f| { |
| 213 | | − ["main.left_value", "main.bump", "main.count"] |
| 214 | | − .iter() |
| 215 | | − .any(|n| f.starts_with(&format!("fn {n}("))) |
| 216 | | − }) |
| 217 | | − .collect(); |
| 218 | | − insta::assert_snapshot!(keep.join("\n\n")); |
| 221 | + big_stack(|| { |
| 222 | + let mut program = lower("main", src); |
| 223 | + louise_perceus::insert_rc(&mut program); |
| 224 | + louise_perceus::verify(&program).unwrap(); |
| 225 | + let text = program.to_string(); |
| 226 | + let keep: Vec<&str> = text |
| 227 | + .split("\n\n") |
| 228 | + .filter(|f| { |
| 229 | + ["main.left_value", "main.bump", "main.count"] |
| 230 | + .iter() |
| 231 | + .any(|n| f.starts_with(&format!("fn {n}("))) |
| 232 | + }) |
| 233 | + .collect(); |
| 234 | + insta::assert_snapshot!(keep.join("\n\n")); |
| 235 | + }); |
| 219 | 236 | } |
| 220 | 237 | |
| 221 | 238 | /// Perceus v2 (ADR 0040): with arities in Core, reuse is sized (no size |
| @@ -254,25 +271,27 @@ fn flip(p: Pair) -> Pair { |
| 254 | 271 | |
| 255 | 272 | pub fn main() -> () { () } |
| 256 | 273 | "#; |
| 257 | | − let mut program = lower("main", src); |
| 258 | | − louise_perceus::insert_rc(&mut program); |
| 259 | | − louise_perceus::verify(&program).unwrap(); |
| 260 | | − let text = program.to_string(); |
| 261 | | − let keep: Vec<&str> = text |
| 262 | | − .split("\n\n") |
| 263 | | − .filter(|f| { |
| 264 | | − [ |
| 265 | | − "main.bump", |
| 266 | | − "main.swap", |
| 267 | | − "main.inc$trmc", |
| 268 | | − "main.flip", |
| 269 | | − "main.make$reuse2", |
| 270 | | − ] |
| 271 | | − .iter() |
| 272 | | − .any(|n| f.starts_with(&format!("fn {n}("))) |
| 273 | | − }) |
| 274 | | − .collect(); |
| 275 | | − insta::assert_snapshot!(keep.join("\n\n")); |
| 274 | + big_stack(|| { |
| 275 | + let mut program = lower("main", src); |
| 276 | + louise_perceus::insert_rc(&mut program); |
| 277 | + louise_perceus::verify(&program).unwrap(); |
| 278 | + let text = program.to_string(); |
| 279 | + let keep: Vec<&str> = text |
| 280 | + .split("\n\n") |
| 281 | + .filter(|f| { |
| 282 | + [ |
| 283 | + "main.bump", |
| 284 | + "main.swap", |
| 285 | + "main.inc$trmc", |
| 286 | + "main.flip", |
| 287 | + "main.make$reuse2", |
| 288 | + ] |
| 289 | + .iter() |
| 290 | + .any(|n| f.starts_with(&format!("fn {n}("))) |
| 291 | + }) |
| 292 | + .collect(); |
| 293 | + insta::assert_snapshot!(keep.join("\n\n")); |
| 294 | + }); |
| 276 | 295 | } |
| 277 | 296 | |
| 278 | 297 | /// A one-function program: `fn f(v0) { body }`, `v0` borrowed if asked. |
| @@ -394,31 +413,33 @@ fn reads(xs) { read(xs) } |
| 394 | 413 | |
| 395 | 414 | pub fn main() -> () { () } |
| 396 | 415 | "#; |
| 397 | | − let mut program = lower("main", src); |
| 398 | | − assert!(program.owned.contains("tl_test_take")); |
| 399 | | − louise_perceus::insert_rc(&mut program); |
| 400 | | − louise_perceus::verify(&program).unwrap(); |
| 401 | | − let borrowed = |name: &str| { |
| 402 | | − let f = program.fns.iter().find(|f| f.name == name); |
| 403 | | − f.unwrap_or_else(|| panic!("{name}")).borrowed.clone() |
| 404 | | − }; |
| 405 | | − let text = program.to_string(); |
| 406 | | − let body = |name: &str| { |
| 407 | | − let f = text |
| 408 | | − .split("\n\n") |
| 409 | | − .find(|t| t.starts_with(&format!("fn {name}("))); |
| 410 | | − f.unwrap_or_else(|| panic!("{name}")).to_owned() |
| 411 | | − }; |
| 412 | | − // The last use moves into an owned extern, without counting; the |
| 413 | | − // parameter must be owned for that. |
| 414 | | − assert!(borrowed("main.moves").is_empty()); |
| 415 | | − assert!( |
| 416 | | − !body("main.moves").contains("dup"), |
| 417 | | − "{}", |
| 418 | | − body("main.moves") |
| 419 | | − ); |
| 420 | | − // A use followed by another one takes a reference of its own. |
| 421 | | − assert!(body("main.keeps").contains("dup"), "{}", body("main.keeps")); |
| 422 | | − // A borrowing extern leaves the parameter borrowed. |
| 423 | | − assert_eq!(borrowed("main.reads"), [0].into()); |
| 416 | + big_stack(|| { |
| 417 | + let mut program = lower("main", src); |
| 418 | + assert!(program.owned.contains("tl_test_take")); |
| 419 | + louise_perceus::insert_rc(&mut program); |
| 420 | + louise_perceus::verify(&program).unwrap(); |
| 421 | + let borrowed = |name: &str| { |
| 422 | + let f = program.fns.iter().find(|f| f.name == name); |
| 423 | + f.unwrap_or_else(|| panic!("{name}")).borrowed.clone() |
| 424 | + }; |
| 425 | + let text = program.to_string(); |
| 426 | + let body = |name: &str| { |
| 427 | + let f = text |
| 428 | + .split("\n\n") |
| 429 | + .find(|t| t.starts_with(&format!("fn {name}("))); |
| 430 | + f.unwrap_or_else(|| panic!("{name}")).to_owned() |
| 431 | + }; |
| 432 | + // The last use moves into an owned extern, without counting; the |
| 433 | + // parameter must be owned for that. |
| 434 | + assert!(borrowed("main.moves").is_empty()); |
| 435 | + assert!( |
| 436 | + !body("main.moves").contains("dup"), |
| 437 | + "{}", |
| 438 | + body("main.moves") |
| 439 | + ); |
| 440 | + // A use followed by another one takes a reference of its own. |
| 441 | + assert!(body("main.keeps").contains("dup"), "{}", body("main.keeps")); |
| 442 | + // A borrowing extern leaves the parameter borrowed. |
| 443 | + assert_eq!(borrowed("main.reads"), [0].into()); |
| 444 | + }); |
| 424 | 445 | } |