primitives in the CLI for running top-level expectations

This commit is contained in:
Folkert 2022-07-06 14:17:50 +02:00
parent 3017a14599
commit 0d59429061
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
2 changed files with 209 additions and 34 deletions

View file

@ -4165,6 +4165,89 @@ pub fn build_procedures_return_main<'a, 'ctx, 'env>(
promote_to_main_function(env, mod_solutions, entry_point.symbol, entry_point.layout)
}
pub fn build_procedures_expose_expects<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
opt_level: OptLevel,
procedures: MutMap<(Symbol, ProcLayout<'a>), roc_mono::ir::Proc<'a>>,
entry_point: EntryPoint<'a>,
) -> Vec<'a, &'a str> {
use bumpalo::collections::CollectIn;
// this is not entirely accurate: it will treat every top-level bool value (turned into a
// zero-argument thunk) as an expect.
let expects: Vec<_> = procedures
.keys()
.filter_map(|(symbol, proc_layout)| {
if proc_layout.arguments.is_empty() && proc_layout.result == Layout::bool() {
Some(*symbol)
} else {
None
}
})
.collect_in(env.arena);
let mod_solutions = build_procedures_help(
env,
opt_level,
procedures,
entry_point,
Some(Path::new("/tmp/test.ll")),
);
let captures_niche = CapturesNiche::no_niche();
let top_level = ProcLayout {
arguments: &[],
result: Layout::bool(),
captures_niche,
};
let mut expect_names = Vec::with_capacity_in(expects.len(), env.arena);
for symbol in expects.iter().copied() {
let it = top_level.arguments.iter().copied();
let bytes =
roc_alias_analysis::func_name_bytes_help(symbol, it, captures_niche, &top_level.result);
let func_name = FuncName(&bytes);
let func_solutions = mod_solutions.func_solutions(func_name).unwrap();
let mut it = func_solutions.specs();
let func_spec = it.next().unwrap();
debug_assert!(
it.next().is_none(),
"we expect only one specialization of this symbol"
);
// NOTE fake layout; it is only used for debug prints
let roc_main_fn = function_value_by_func_spec(
env,
*func_spec,
symbol,
&[],
captures_niche,
&Layout::UNIT,
);
let name = roc_main_fn.get_name().to_str().unwrap();
let expect_name = &format!("Expect_{}", name);
let expect_name = env.arena.alloc_str(expect_name);
expect_names.push(&*expect_name);
// Add main to the module.
let _ = expose_function_to_host_help_c_abi(
env,
name,
roc_main_fn,
top_level.arguments,
top_level.result,
&format!("Expect_{}", name),
);
}
expect_names
}
fn build_procedures_help<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
opt_level: OptLevel,