Skip to content

Commit

Permalink
make an inline expect triggered by a top-level expect work
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Feb 1, 2024
1 parent 39a90ec commit 41ad915
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 41 deletions.
54 changes: 41 additions & 13 deletions crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,22 +553,24 @@ mod cli_run {
&[],
indoc!(
r#"
── EXPECT FAILED in ...roc/roc/crates/cli_testing_examples/expects/expects.roc ─
This expectation failed:
19│ expect words == []
28│ expect words == []
^^^^^^^^^^^
When it failed, these variables had these values:
words : List Str
words = ["this", "will", "for", "sure", "be", "a", "large", "string", "so", "when", "we", "split", "it", "it", "will", "use", "seamless", "slices", "which", "affect", "printing"]
[<ignored for tests>:22] x = 42
[<ignored for tests>:23] "Fjoer en ferdjer frieten oan dyn geve lea" = "Fjoer en ferdjer frieten oan dyn geve lea"
[<ignored for tests>:24] "this is line 24" = "this is line 24"
[<ignored for tests>:13] x = "abc"
[<ignored for tests>:13] x = 10
[<ignored for tests>:13] x = (A (B C))
[<ignored for tests>:31] x = 42
[<ignored for tests>:33] "Fjoer en ferdjer frieten oan dyn geve lea" = "Fjoer en ferdjer frieten oan dyn geve lea"
[<ignored for tests>:35] "this is line 24" = "this is line 24"
[<ignored for tests>:21] x = "abc"
[<ignored for tests>:21] x = 10
[<ignored for tests>:21] x = (A (B C))
Program finished!
"#
),
Expand All @@ -584,20 +586,46 @@ mod cli_run {
&[],
indoc!(
r#"
── EXPECT FAILED in ...roc/roc/crates/cli_testing_examples/expects/expects.roc ─
This expectation failed:
6│> expect
7│> a = 1
8│> b = 2
9│>
10│> a == b
9│ expect a == 2
^^^^^^
When it failed, these variables had these values:
a : Num *
a = 1
b : Num *
── EXPECT FAILED in ...roc/roc/crates/cli_testing_examples/expects/expects.roc ─
This expectation failed:
10│ expect a == 3
^^^^^^
When it failed, these variables had these values:
a : Num *
a = 1
── EXPECT FAILED in ...roc/roc/crates/cli_testing_examples/expects/expects.roc ─
This expectation failed:
14│> expect
15│> a = makeA
16│> b = 2i64
17│>
18│> a == b
When it failed, these variables had these values:
a : Int Signed64
a = 1
b : I64
b = 2
Expand Down
17 changes: 14 additions & 3 deletions crates/cli_testing_examples/expects/expects.roc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ app "expects-test"
imports []
provides [main] to pf

expect
makeA =
a = 1
b = 2

expect a == 2
expect a == 3

a

expect
a = makeA
b = 2i64

a == b

polyDbg = \x ->
dbg x

x

main =
Expand All @@ -20,10 +29,12 @@ main =

x = 42
dbg x

dbg "Fjoer en ferdjer frieten oan dyn geve lea"

dbg "this is line 24"

r = {x : polyDbg "abc", y: polyDbg 10u8, z : polyDbg (A (B C))}
r = { x: polyDbg "abc", y: polyDbg 10u8, z: polyDbg (A (B C)) }

when r is
_ -> "Program finished!\n"
3 changes: 2 additions & 1 deletion crates/compiler/build/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ pub fn rebuild_host(

// Clean up c_host.o
if c_host_dest.exists() {
std::fs::remove_file(c_host_dest).unwrap();
// there can be a race condition on this file cleanup
let _ = std::fs::remove_file(c_host_dest);
}
}
} else if rust_host_src.exists() {
Expand Down
46 changes: 23 additions & 23 deletions crates/repl_expect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
roc_module::symbol::Interns,
roc_mono::{
ir::ProcLayout,
layout::{GlobalLayoutInterner, LayoutCache, Niche},
layout::{GlobalLayoutInterner, LayoutCache, LayoutInterner, Niche},
},
roc_parse::ast::Expr,
roc_repl_eval::{eval::jit_to_ast, ReplAppMemory},
Expand Down Expand Up @@ -57,30 +57,30 @@ pub fn get_values<'a>(

app.offset = start;

let expr = {
// TODO: pass layout_cache to jit_to_ast directly
let mut layout_cache = LayoutCache::new(layout_interner.fork(), target_info);
let layout = layout_cache.from_var(arena, variable, subs).unwrap();

let proc_layout = ProcLayout {
arguments: &[],
result: layout,
niche: Niche::NONE,
};

jit_to_ast(
arena,
app,
"expect_repl_main_fn",
proc_layout,
variable,
subs,
interns,
layout_interner.fork(),
target_info,
)
// TODO: pass layout_cache to jit_to_ast directly
let mut layout_cache = LayoutCache::new(layout_interner.fork(), target_info);
let layout = layout_cache.from_var(arena, variable, subs).unwrap();

let proc_layout = ProcLayout {
arguments: &[],
result: layout,
niche: Niche::NONE,
};

let expr = jit_to_ast(
arena,
app,
"expect_repl_main_fn",
proc_layout,
variable,
subs,
interns,
layout_interner.fork(),
target_info,
);

app.offset += layout_cache.interner.stack_size_and_alignment(layout).0 as usize;

result.push(expr);
result_vars.push(variable);
}
Expand Down
6 changes: 5 additions & 1 deletion crates/repl_expect/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn run_expect_pure<'a, W: std::io::Write>(
let mut offset = ExpectSequence::START_OFFSET;

for _ in 0..sequence.count_failures() {
offset += render_expect_failure(
offset = render_expect_failure(
writer,
&renderer,
arena,
Expand Down Expand Up @@ -731,5 +731,9 @@ pub fn expect_mono_module_to_dylib<'a>(
);
}

if let Ok(path) = std::env::var("ROC_DEBUG_LLVM") {
env.module.print_to_file(path).unwrap();
}

llvm_module_to_dylib(env.module, &target, opt_level).map(|lib| (lib, expects, layout_interner))
}

0 comments on commit 41ad915

Please sign in to comment.