Skip to main content

check

Function check 

Source
pub fn check(source: &[u8], name: &str) -> Vec<Diagnostic>
Expand description

Parses source (a .rlua or .lua file, by name’s extension) without running anything, and type-checks a .rlua file’s annotations: its diagnostics. A syntax error is the only diagnostic (the first one). Otherwise, for .rlua, the type errors (Severity::Error, at most 500), and a Severity::Warning if the check stopped at its work or memory bound. A plain .lua file has no annotations, so it gets none. Checking never changes how the program runs (see The type checker).

The parse may use 1 KiB per source byte plus 64 MiB (“not enough memory” past it), and the checker as much again for its own tables; its work is linear in the source.

Examples found in repository?
examples/check.rs (line 17)
7fn main() {
8    let mut args = std::env::args().skip(1);
9    let mut status = 0;
10    while let Some(a) = args.next() {
11        let (src, name) = if a == "-e" {
12            (args.next().unwrap_or_default().into_bytes(), "=e.rlua".to_owned())
13        } else {
14            (std::fs::read(&a).unwrap(), a)
15        };
16        let t = std::time::Instant::now();
17        let diags = remedy_lua::check(&src, &name);
18        for d in &diags {
19            println!("{d}  [{}:{}-{}:{}]", d.pos.line, d.pos.col, d.end.line, d.end.col);
20        }
21        eprintln!("{name}: {} diagnostic(s) in {:?}", diags.len(), t.elapsed());
22        if !diags.is_empty() {
23            status = 1;
24        }
25    }
26    std::process::exit(status);
27}
More examples
Hide additional examples
examples/frontmem.rs (line 26)
16fn main() {
17    let mut args = std::env::args().skip(1);
18    let file = args.next().unwrap();
19    let limit: usize = args.next().and_then(|a| a.parse().ok()).unwrap_or(64 << 20);
20    let mode = args.next().unwrap_or_else(|| "host".into());
21    let src = std::fs::read(&file).unwrap();
22    let name = if file.ends_with(".rlua") { "=t.rlua" } else { "=t" };
23    let before = hwm();
24    let t = std::time::Instant::now();
25    let outcome = match mode.as_str() {
26        "check" => format!("{:?}", remedy_lua::check(&src, name.trim_start_matches('=')).first().map(|d| d.message.clone())),
27        "call" => {
28            let mut lua = Lua::new(Limits::default().memory(limit).fuel(1 << 40), Libs::ALL);
29            let s = lua.create_string(&src).unwrap();
30            lua.set_global("src", s).unwrap();
31            let r = lua.exec(format!("local f, e = load(src, '{name}') return f and 'ok' or e"), "=c");
32            format!("{:?}", r.map(|v| lua.to_string_lossy(v[0])))
33        }
34        _ => {
35            let mut lua = Lua::new(Limits::unmetered().memory(limit), Libs::NONE);
36            format!("{:?}", lua.load(&src, name).map(|_| ()))
37        }
38    };
39    let peak = hwm();
40    println!(
41        "{mode}: {outcome} in {:?}; peak {:.1} MB (before {:.1} MB) under {:.1} MiB: {:.2}x",
42        t.elapsed(),
43        peak as f64 / 1e6,
44        before as f64 / 1e6,
45        limit as f64 / 1048576.0,
46        peak as f64 / limit as f64
47    );
48}
examples/frontbench.rs (line 53)
37fn main() {
38    let mb: usize = std::env::args().nth(1).and_then(|a| a.parse().ok()).unwrap_or(4);
39    let src = match std::env::var("FRONTBENCH_FILE") {
40        Ok(f) => std::fs::read_to_string(f).unwrap(),
41        Err(_) => source(mb << 20),
42    };
43    // `FRONTBENCH_DUMP=path`: writes the source there (for `examples/frontmem.rs`) and stops.
44    if let Ok(path) = std::env::var("FRONTBENCH_DUMP") {
45        std::fs::write(path, &src).unwrap();
46        return;
47    }
48    let runs = 5;
49    let mut parse = f64::MAX;
50    let mut load = f64::MAX;
51    for _ in 0..runs {
52        let t = Instant::now();
53        let d = remedy_lua::check(src.as_bytes(), "bench.lua");
54        assert!(d.is_empty(), "{d:?}");
55        parse = parse.min(t.elapsed().as_secs_f64());
56        let mut lua = Lua::new(Limits::unmetered(), Libs::STANDARD);
57        let t = Instant::now();
58        lua.load(&src, "=bench").unwrap();
59        load = load.min(t.elapsed().as_secs_f64());
60    }
61    let mbs = src.len() as f64 / (1 << 20) as f64;
62    // With the oracle built in, the recursive front end side by side, alternating run by
63    // run so both see the same machine load.
64    #[cfg(feature = "front-oracle")]
65    {
66        let mut best = [[f64::MAX; 3]; 2];
67        for _ in 0..runs * 3 {
68            for (side, old) in best.iter_mut().zip([true, false]) {
69                let t = remedy_lua::__front::time(src.as_bytes(), "bench.lua", old).unwrap();
70                for (b, t) in side.iter_mut().zip(t) {
71                    *b = b.min(t.as_secs_f64() * 1e3);
72                }
73            }
74        }
75        for (label, b) in ["old (recursive)", "new (iterative)"].iter().zip(best) {
76            println!("{label}: parse {:6.1} ms  compile {:6.1} ms  drop {:5.1} ms", b[0], b[1], b[2]);
77        }
78    }
79    println!("source: {:.2} MB, {} lines (best of {runs})", mbs, src.lines().count());
80    println!("parse:           {:8.1} ms  {:6.1} MB/s", parse * 1e3, mbs / parse);
81    println!("parse + compile: {:8.1} ms  {:6.1} MB/s", load * 1e3, mbs / load);
82}