mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 12:18:19 +00:00
Add erasure llvm test
This commit is contained in:
parent
535eb80dfb
commit
f2f80f390d
4 changed files with 62 additions and 22 deletions
25
crates/compiler/test_gen/src/gen_erased.rs
Normal file
25
crates/compiler/test_gen/src/gen_erased.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
#[cfg(feature = "gen-llvm")]
|
||||
use crate::helpers::llvm::assert_evals_to_erased;
|
||||
|
||||
use indoc::indoc;
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm"))]
|
||||
fn multi_branch_capturing() {
|
||||
assert_evals_to_erased!(
|
||||
indoc!(
|
||||
r#"
|
||||
app "test" provides [main] to "./platform"
|
||||
|
||||
f = \t, s ->
|
||||
if t
|
||||
then \{} -> 0
|
||||
else \{} -> Str.countGraphemes s
|
||||
|
||||
main = ((f Bool.true "abc") {}, (f Bool.false "abc") {})
|
||||
"#
|
||||
),
|
||||
(0, 3),
|
||||
(usize, usize)
|
||||
);
|
||||
}
|
|
@ -52,10 +52,9 @@ fn create_llvm_module<'a>(
|
|||
config: HelperConfig,
|
||||
context: &'a inkwell::context::Context,
|
||||
target: &Triple,
|
||||
function_kind: FunctionKind,
|
||||
) -> (&'static str, String, &'a Module<'a>) {
|
||||
let target_info = roc_target::TargetInfo::from(target);
|
||||
// TODO parameterize
|
||||
let function_kind = FunctionKind::LambdaSet;
|
||||
|
||||
let filename = PathBuf::from("Test.roc");
|
||||
let src_dir = PathBuf::from("fake/test/path");
|
||||
|
@ -334,11 +333,12 @@ pub fn helper<'a>(
|
|||
config: HelperConfig,
|
||||
src: &str,
|
||||
context: &'a inkwell::context::Context,
|
||||
function_kind: FunctionKind,
|
||||
) -> (&'static str, String, Library) {
|
||||
let target = target_lexicon::Triple::host();
|
||||
|
||||
let (main_fn_name, delayed_errors, module) =
|
||||
create_llvm_module(arena, src, config, context, &target);
|
||||
create_llvm_module(arena, src, config, context, &target, function_kind);
|
||||
|
||||
let res_lib = if config.add_debug_info {
|
||||
let module = annotate_with_debug_info(module, context);
|
||||
|
@ -424,11 +424,12 @@ fn compile_to_wasm_bytes<'a>(
|
|||
config: HelperConfig,
|
||||
src: &str,
|
||||
context: &'a inkwell::context::Context,
|
||||
function_kind: FunctionKind,
|
||||
) -> Vec<u8> {
|
||||
let target = wasm32_target_tripple();
|
||||
|
||||
let (_main_fn_name, _delayed_errors, llvm_module) =
|
||||
create_llvm_module(arena, src, config, context, &target);
|
||||
create_llvm_module(arena, src, config, context, &target, function_kind);
|
||||
|
||||
let content_hash = crate::helpers::src_hash(src);
|
||||
let wasm_file = llvm_module_to_wasm_file(&TEMP_DIR, content_hash, llvm_module);
|
||||
|
@ -516,7 +517,11 @@ fn fake_wasm_main_function(_: u32, _: u32) -> u32 {
|
|||
}
|
||||
|
||||
#[cfg(feature = "gen-llvm-wasm")]
|
||||
pub fn assert_wasm_evals_to_help<T>(src: &str, ignore_problems: bool) -> Result<T, String>
|
||||
pub fn assert_wasm_evals_to_help<T>(
|
||||
src: &str,
|
||||
ignore_problems: bool,
|
||||
function_kind: FunctionKind,
|
||||
) -> Result<T, String>
|
||||
where
|
||||
T: FromWasm32Memory + Wasm32Result,
|
||||
{
|
||||
|
@ -530,12 +535,12 @@ where
|
|||
opt_level: OPT_LEVEL,
|
||||
};
|
||||
|
||||
let wasm_bytes = compile_to_wasm_bytes(&arena, config, src, &context);
|
||||
let wasm_bytes = compile_to_wasm_bytes(&arena, config, src, &context, function_kind);
|
||||
|
||||
crate::helpers::wasm::run_wasm_test_bytes::<T>(TEST_WRAPPER_NAME, wasm_bytes)
|
||||
}
|
||||
|
||||
#[allow(unused_macros)]
|
||||
#[cfg(feature = "gen-llvm-wasm")]
|
||||
macro_rules! assert_wasm_evals_to {
|
||||
($src:expr, $expected:expr, $ty:ty, $transform:expr, $ignore_problems:expr) => {
|
||||
match $crate::helpers::llvm::assert_wasm_evals_to_help::<$ty>($src, $ignore_problems) {
|
||||
|
@ -581,9 +586,13 @@ pub fn try_run_lib_function<T>(
|
|||
}
|
||||
|
||||
// only used in tests
|
||||
#[allow(unused)]
|
||||
pub(crate) fn llvm_evals_to<T, U, F>(src: &str, expected: U, transform: F, ignore_problems: bool)
|
||||
where
|
||||
pub(crate) fn llvm_evals_to<T, U, F>(
|
||||
src: &str,
|
||||
expected: U,
|
||||
transform: F,
|
||||
ignore_problems: bool,
|
||||
function_kind: FunctionKind,
|
||||
) where
|
||||
U: PartialEq + std::fmt::Debug,
|
||||
F: FnOnce(T) -> U,
|
||||
{
|
||||
|
@ -600,7 +609,8 @@ where
|
|||
opt_level: crate::helpers::llvm::OPT_LEVEL,
|
||||
};
|
||||
|
||||
let (main_fn_name, errors, lib) = crate::helpers::llvm::helper(&arena, config, src, &context);
|
||||
let (main_fn_name, errors, lib) =
|
||||
crate::helpers::llvm::helper(&arena, config, src, &context, function_kind);
|
||||
|
||||
let result = crate::helpers::llvm::try_run_lib_function::<T>(main_fn_name, &lib);
|
||||
|
||||
|
@ -624,7 +634,6 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! assert_llvm_evals_to {
|
||||
($src:expr, $expected:expr, $ty:ty, $transform:expr, $ignore_problems:expr) => {
|
||||
crate::helpers::llvm::llvm_evals_to::<$ty, _, _>(
|
||||
|
@ -632,6 +641,7 @@ macro_rules! assert_llvm_evals_to {
|
|||
$expected,
|
||||
$transform,
|
||||
$ignore_problems,
|
||||
roc_load::FunctionKind::LambdaSet,
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -657,8 +667,6 @@ macro_rules! assert_llvm_evals_to {
|
|||
//
|
||||
// let (_main_fn_name, _delayed_errors, _module) =
|
||||
// $crate::helpers::llvm::create_llvm_module(&arena, $src, config, &context, &target);
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! assert_evals_to {
|
||||
($src:expr, $expected:expr, $ty:ty) => {{
|
||||
assert_evals_to!($src, $expected, $ty, $crate::helpers::llvm::identity, false);
|
||||
|
@ -689,14 +697,24 @@ macro_rules! assert_evals_to {
|
|||
}};
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
macro_rules! assert_evals_to_erased {
|
||||
($src:expr, $expected:expr, $ty:ty) => {{
|
||||
crate::helpers::llvm::llvm_evals_to::<$ty, _, _>(
|
||||
$src,
|
||||
$expected,
|
||||
$crate::helpers::llvm::identity,
|
||||
false,
|
||||
roc_load::FunctionKind::Erased,
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn identity<T>(value: T) -> T {
|
||||
value
|
||||
}
|
||||
|
||||
#[allow(unused_imports)]
|
||||
pub(crate) use assert_evals_to;
|
||||
#[allow(unused_imports)]
|
||||
pub(crate) use assert_evals_to_erased;
|
||||
pub(crate) use assert_llvm_evals_to;
|
||||
#[allow(unused_imports)]
|
||||
#[cfg(feature = "gen-llvm-wasm")]
|
||||
pub(crate) use assert_wasm_evals_to;
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
//! Contains all of Roc's [code generation](https://en.wikipedia.org/wiki/Code_generation_(compiler))
|
||||
//! tests.
|
||||
#[cfg(test)]
|
||||
pub mod helpers;
|
|
@ -8,6 +8,7 @@ pub mod gen_abilities;
|
|||
pub mod gen_compare;
|
||||
pub mod gen_definitions;
|
||||
pub mod gen_dict;
|
||||
pub mod gen_erased;
|
||||
pub mod gen_list;
|
||||
pub mod gen_num;
|
||||
pub mod gen_panic;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue