mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-21 15:59:20 +03:00
Add tests for user crash
This commit is contained in:
parent
c8accc90e8
commit
9201cf0b32
68
crates/compiler/test_gen/src/gen_panic.rs
Normal file
68
crates/compiler/test_gen/src/gen_panic.rs
Normal file
@ -0,0 +1,68 @@
|
||||
use indoc::indoc;
|
||||
|
||||
#[cfg(feature = "gen-llvm")]
|
||||
use crate::helpers::llvm::expect_runtime_error_panic;
|
||||
|
||||
#[cfg(feature = "gen-wasm")]
|
||||
use crate::helpers::wasm::expect_runtime_error_panic;
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm"))]
|
||||
#[should_panic = r#"Roc failed with message: "hello crash""#]
|
||||
fn crash_literal() {
|
||||
expect_runtime_error_panic!(indoc!(
|
||||
r#"
|
||||
app "test" provides [main] to "./platform"
|
||||
|
||||
main = if Bool.true then crash "hello crash" else 1u8
|
||||
"#
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm"))]
|
||||
#[should_panic = r#"Roc failed with message: "hello crash""#]
|
||||
fn crash_variable() {
|
||||
expect_runtime_error_panic!(indoc!(
|
||||
r#"
|
||||
app "test" provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
msg = "hello crash"
|
||||
if Bool.true then crash msg else 1u8
|
||||
"#
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm"))]
|
||||
#[should_panic = r#"Roc failed with message: "turns out this was fallible""#]
|
||||
fn crash_in_call() {
|
||||
expect_runtime_error_panic!(indoc!(
|
||||
r#"
|
||||
app "test" provides [main] to "./platform"
|
||||
|
||||
getInfallible = \result -> when result is
|
||||
Ok x -> x
|
||||
_ -> crash "turns out this was fallible"
|
||||
|
||||
main =
|
||||
x : [Ok U64, Err Str]
|
||||
x = Err ""
|
||||
getInfallible x
|
||||
"#
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm"))]
|
||||
#[should_panic = r#"Roc failed with message: "no new even primes""#]
|
||||
fn crash_in_passed_closure() {
|
||||
expect_runtime_error_panic!(indoc!(
|
||||
r#"
|
||||
app "test" provides [main] to "./platform"
|
||||
|
||||
main = List.map [1, 2, 3] \n -> if n == 2 then crash "no new even primes" else ""
|
||||
"#
|
||||
));
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
use core::ffi::c_void;
|
||||
|
||||
use roc_std::RocStr;
|
||||
|
||||
/// # Safety
|
||||
/// The Roc application needs this.
|
||||
#[no_mangle]
|
||||
@ -36,7 +38,7 @@ pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) {
|
||||
/// # Safety
|
||||
/// The Roc application needs this.
|
||||
#[no_mangle]
|
||||
pub unsafe fn roc_panic(c_ptr: *mut c_void, tag_id: u32) {
|
||||
pub unsafe fn roc_panic(msg: &RocStr, tag_id: u32) {
|
||||
use roc_gen_llvm::llvm::build::PanicTagId;
|
||||
|
||||
use std::ffi::CStr;
|
||||
@ -44,9 +46,11 @@ pub unsafe fn roc_panic(c_ptr: *mut c_void, tag_id: u32) {
|
||||
|
||||
match PanicTagId::try_from(tag_id) {
|
||||
Ok(PanicTagId::RocPanic) => {
|
||||
let slice = CStr::from_ptr(c_ptr as *const c_char);
|
||||
let string = slice.to_str().unwrap();
|
||||
eprintln!("Roc hit a panic: {}", string);
|
||||
eprintln!("Roc hit a panic: {}", msg);
|
||||
std::process::exit(1);
|
||||
}
|
||||
Ok(PanicTagId::UserPanic) => {
|
||||
eprintln!("User panic: {}", msg);
|
||||
std::process::exit(1);
|
||||
}
|
||||
Err(_) => unreachable!(),
|
||||
|
@ -9,6 +9,7 @@ pub mod gen_compare;
|
||||
pub mod gen_dict;
|
||||
pub mod gen_list;
|
||||
pub mod gen_num;
|
||||
pub mod gen_panic;
|
||||
pub mod gen_primitives;
|
||||
pub mod gen_records;
|
||||
pub mod gen_refcount;
|
||||
|
Loading…
Reference in New Issue
Block a user