| @@ -0,0 +1,62 @@ |
| 1 | +//! The constructors the back ends build or read without going through |
| 2 | +//! lowering have the tags they assume (code-health M4). The runtime |
| 3 | +//! (`tilrt`: `values.rs`, `prims.rs`, `process.rs`, `net.rs`), the |
| 4 | +//! interpreter (`tilc_interp`) and lowering's list literals hard-code |
| 5 | +//! these tags: reordering a std type's constructors must fail here, not |
| 6 | +//! silently change what compiled programs see. |
| 7 | + |
| 8 | +use tilc_db::Db; |
| 9 | +use tilc_hir::DefKind; |
| 10 | + |
| 11 | +/// (module, constructor, tag). |
| 12 | +const ASSUMED: &[(&str, &str, u32)] = &[ |
| 13 | + ("std/prelude", "False", 0), |
| 14 | + ("std/prelude", "True", 1), |
| 15 | + ("std/prelude", "Nil", 0), |
| 16 | + ("std/prelude", "Cons", 1), |
| 17 | + ("std/prelude", "None", 0), |
| 18 | + ("std/prelude", "Some", 1), |
| 19 | + ("std/prelude", "Ok", 0), |
| 20 | + ("std/prelude", "Error", 1), |
| 21 | + ("std/prelude", "Lt", 0), |
| 22 | + ("std/prelude", "Eq", 1), |
| 23 | + ("std/prelude", "Gt", 2), |
| 24 | + ("std/process", "Subject", 0), |
| 25 | + ("std/process", "Normal", 0), |
| 26 | + ("std/process", "Killed", 1), |
| 27 | + ("std/process", "Crashed", 2), |
| 28 | + ("std/process", "Down", 0), |
| 29 | + ("std/process", "ExitSignal", 0), |
| 30 | + // What `tl_http_parse_request` and `tl_http_parse_chunk` return. |
| 31 | + ("std/http", "Partial", 0), |
| 32 | + ("std/http", "Invalid", 1), |
| 33 | + ("std/http", "Complete", 2), |
| 34 | + ("std/http", "ChunkPartial", 0), |
| 35 | + ("std/http", "ChunkInvalid", 1), |
| 36 | + ("std/http", "ChunkData", 2), |
| 37 | + ("std/http", "ChunkEnd", 3), |
| 38 | +]; |
| 39 | + |
| 40 | +#[test] |
| 41 | +fn constructors_the_back_ends_build_have_the_tags_they_assume() { |
| 42 | + let analysis = Db::new().analyze(); |
| 43 | + let hir = &analysis.hir; |
| 44 | + let mut failures = Vec::new(); |
| 45 | + for (module, name, tag) in ASSUMED { |
| 46 | + let found: Vec<u32> = hir |
| 47 | + .defs |
| 48 | + .iter() |
| 49 | + .filter(|(_, d)| d.name == *name && hir.modules[d.module].path == *module) |
| 50 | + .filter_map(|(_, d)| match &d.kind { |
| 51 | + DefKind::Ctor(c) => Some(c.index), |
| 52 | + _ => None, |
| 53 | + }) |
| 54 | + .collect(); |
| 55 | + if found != [*tag] { |
| 56 | + failures.push(format!( |
| 57 | + "{module}.{name}: expected tag {tag}, found {found:?}" |
| 58 | + )); |
| 59 | + } |
| 60 | + } |
| 61 | + assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 62 | +} |