From be6752542b0b16578ec82a0f6c87f09c53a8fd7a Mon Sep 17 00:00:00 2001 From: Folkert Date: Sat, 19 Sep 2020 21:50:33 +0200 Subject: [PATCH] update to run on our fork of inkwell again --- Cargo.lock | 10 +++-- cli/Cargo.toml | 3 +- cli/tests/repl_eval.rs | 8 ---- compiler/build/Cargo.toml | 3 +- compiler/gen/Cargo.toml | 3 +- compiler/gen/src/run_roc.rs | 5 +++ compiler/gen/tests/helpers/eval.rs | 71 +++--------------------------- 7 files changed, 20 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c88ef5357..785713c0f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1123,9 +1123,10 @@ dependencies = [ [[package]] name = "inkwell" version = "0.1.0" +source = "git+https://github.com/rtfeldman/inkwell#d0a1ce5e678cf0f0e62b757760d1019301c603c9" dependencies = [ "either", - "inkwell_internals 0.1.0", + "inkwell_internals 0.1.0 (git+https://github.com/rtfeldman/inkwell)", "libc", "llvm-sys", "once_cell", @@ -1146,6 +1147,7 @@ dependencies = [ [[package]] name = "inkwell_internals" version = "0.1.0" +source = "git+https://github.com/rtfeldman/inkwell#d0a1ce5e678cf0f0e62b757760d1019301c603c9" dependencies = [ "proc-macro2 0.4.30", "quote 0.6.13", @@ -2119,7 +2121,7 @@ dependencies = [ "im", "im-rc", "indoc", - "inkwell 0.1.0", + "inkwell 0.1.0 (git+https://github.com/rtfeldman/inkwell)", "inlinable_string", "maplit", "pretty_assertions", @@ -2196,7 +2198,7 @@ dependencies = [ "im", "im-rc", "indoc", - "inkwell 0.1.0", + "inkwell 0.1.0 (git+https://github.com/rtfeldman/inkwell)", "inlinable_string", "libc", "maplit", @@ -2327,7 +2329,7 @@ dependencies = [ "im", "im-rc", "indoc", - "inkwell 0.1.0", + "inkwell 0.1.0 (git+https://github.com/rtfeldman/inkwell)", "inlinable_string", "libc", "maplit", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 0d7b690dda..681868d38a 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -76,8 +76,7 @@ libc = "0.2" # commit of TheDan64/inkwell, push a new tag which points to the latest commit, # change the tag value in this Cargo.toml to point to that tag, and `cargo update`. # This way, GitHub Actions works and nobody's builds get broken. -# inkwell = { git = "https://github.com/rtfeldman/inkwell", tag = "llvm10-0.release1" } -inkwell = { path = "/home/folkertdev/roc/inkwell" } +inkwell = { git = "https://github.com/rtfeldman/inkwell" } target-lexicon = "0.10" [dev-dependencies] diff --git a/cli/tests/repl_eval.rs b/cli/tests/repl_eval.rs index f6ea7d787c..ffadf96e7f 100644 --- a/cli/tests/repl_eval.rs +++ b/cli/tests/repl_eval.rs @@ -15,14 +15,6 @@ mod repl_eval { assert!(out.status.success()); } - fn expect_failure(input: &str, expected: &str) { - let out = helpers::repl_eval(input); - - assert_eq!(&out.stderr, ""); - assert_eq!(&out.stdout, expected); - assert!(out.status.success()); - } - #[test] fn literal_0() { expect_success("0", "0 : Num *"); diff --git a/compiler/build/Cargo.toml b/compiler/build/Cargo.toml index abd51f849e..ba6cd9d813 100644 --- a/compiler/build/Cargo.toml +++ b/compiler/build/Cargo.toml @@ -44,8 +44,7 @@ tokio = { version = "0.2", features = ["blocking", "fs", "sync", "rt-threaded", # commit of TheDan64/inkwell, push a new tag which points to the latest commit, # change the tag value in this Cargo.toml to point to that tag, and `cargo update`. # This way, GitHub Actions works and nobody's builds get broken. -# inkwell = { git = "https://github.com/rtfeldman/inkwell", tag = "llvm10-0.release1" } -inkwell = { path = "/home/folkertdev/roc/inkwell" } +inkwell = { git = "https://github.com/rtfeldman/inkwell" } target-lexicon = "0.10" [dev-dependencies] diff --git a/compiler/gen/Cargo.toml b/compiler/gen/Cargo.toml index 338bd8fff0..e1c66444c1 100644 --- a/compiler/gen/Cargo.toml +++ b/compiler/gen/Cargo.toml @@ -38,8 +38,7 @@ inlinable_string = "0.1" # commit of TheDan64/inkwell, push a new tag which points to the latest commit, # change the tag value in this Cargo.toml to point to that tag, and `cargo update`. # This way, GitHub Actions works and nobody's builds get broken. -# inkwell = { git = "https://github.com/rtfeldman/inkwell", tag = "llvm10-0.release1" } -inkwell = { path = "/home/folkertdev/roc/inkwell" } +inkwell = { git = "https://github.com/rtfeldman/inkwell" } target-lexicon = "0.10" [dev-dependencies] diff --git a/compiler/gen/src/run_roc.rs b/compiler/gen/src/run_roc.rs index 7d22479572..fd93036d48 100644 --- a/compiler/gen/src/run_roc.rs +++ b/compiler/gen/src/run_roc.rs @@ -23,6 +23,7 @@ impl Into> for RocCallResult { let result = format!("{:?}", raw); + // make sure rust does not try to free the Roc string std::mem::forget(raw); result @@ -102,13 +103,17 @@ macro_rules! run_jit_function_dynamic_type { use std::ffi::CString; use std::os::raw::c_char; + // first field is a char pointer (to the error message) + // read value, and transmute to a pointer let ptr_as_int = *(result as *const u64).offset(1); let ptr = std::mem::transmute::(ptr_as_int); + // make CString (null-terminated) let raw = CString::from_raw(ptr); let result = format!("{:?}", raw); + // make sure rust doesn't try to free the Roc constant string std::mem::forget(raw); eprintln!("{}", result); diff --git a/compiler/gen/tests/helpers/eval.rs b/compiler/gen/tests/helpers/eval.rs index 91d68557b3..2a556a3119 100644 --- a/compiler/gen/tests/helpers/eval.rs +++ b/compiler/gen/tests/helpers/eval.rs @@ -1,37 +1,5 @@ use roc_collections::all::MutSet; use roc_types::subs::Subs; -use std::ffi::CString; -use std::os::raw::c_char; - -#[repr(C)] -union Payload { - success: T, - failure: *mut c_char, -} - -#[repr(C)] -pub struct RocCallResult { - pub flag: u64, - payload: Payload, -} - -impl Into> for RocCallResult { - fn into(self) -> Result { - if self.flag == 0 { - Ok(unsafe { self.payload.success }) - } else { - Err(unsafe { - let raw = CString::from_raw(self.payload.failure); - - let result = format!("{:?}", raw); - - std::mem::forget(raw); - - result - }) - } - } -} pub fn helper_without_uniqueness<'a>( arena: &'a bumpalo::Bump, @@ -394,10 +362,9 @@ pub fn helper_with_uniqueness<'a>( #[macro_export] macro_rules! assert_opt_evals_to { ($src:expr, $expected:expr, $ty:ty, $transform:expr, $leak:expr) => { - use crate::helpers::eval::RocCallResult; use bumpalo::Bump; use inkwell::context::Context; - use inkwell::execution_engine::JitFunction; + use roc_gen::run_jit_function; let arena = Bump::new(); @@ -406,18 +373,8 @@ macro_rules! assert_opt_evals_to { let (main_fn_name, execution_engine) = $crate::helpers::eval::helper_with_uniqueness(&arena, $src, $leak, &context); - unsafe { - let main: JitFunction RocCallResult<$ty>> = execution_engine - .get_function(main_fn_name) - .ok() - .ok_or(format!("Unable to JIT compile `{}`", main_fn_name)) - .expect("errored"); - - match main.call().into() { - Ok(success) => assert_eq!($transform(success), $expected), - Err(error_msg) => panic!("Roc failed with message: {}", error_msg), - } - } + let transform = |success| assert_eq!($transform(success), $expected); + run_jit_function!(execution_engine, main_fn_name, $ty, transform) }; ($src:expr, $expected:expr, $ty:ty, $transform:expr) => { @@ -428,10 +385,9 @@ macro_rules! assert_opt_evals_to { #[macro_export] macro_rules! assert_llvm_evals_to { ($src:expr, $expected:expr, $ty:ty, $transform:expr, $leak:expr) => { - use crate::helpers::eval::RocCallResult; use bumpalo::Bump; use inkwell::context::Context; - use inkwell::execution_engine::JitFunction; + use roc_gen::run_jit_function; let arena = Bump::new(); @@ -440,23 +396,8 @@ macro_rules! assert_llvm_evals_to { let (main_fn_name, errors, execution_engine) = $crate::helpers::eval::helper_without_uniqueness(&arena, $src, $leak, &context); - unsafe { - let main: JitFunction RocCallResult<$ty>> = execution_engine - .get_function(main_fn_name) - .ok() - .ok_or(format!("Unable to JIT compile `{}`", main_fn_name)) - .expect("errored"); - - match main.call().into() { - Ok(success) => { - // only if there are no exceptions thrown, check for errors - assert_eq!(errors, Vec::new(), "Encountered errors: {:?}", errors); - - assert_eq!($transform(success), $expected); - } - Err(error_msg) => panic!("Roc failed with message: {}", error_msg), - } - } + let transform = |success| assert_eq!($transform(success), $expected); + run_jit_function!(execution_engine, main_fn_name, $ty, transform, errors) }; ($src:expr, $expected:expr, $ty:ty, $transform:expr) => {