mirror of
https://github.com/roc-lang/roc.git
synced 2025-07-24 15:03:46 +00:00
Crash at runtime rather than panicking when if condition is erroneous
Closes #5318
This commit is contained in:
parent
cdf8677dfb
commit
a9975b1f7f
4 changed files with 31 additions and 9 deletions
|
@ -6613,12 +6613,16 @@ pub fn from_can<'a>(
|
||||||
branches,
|
branches,
|
||||||
final_else,
|
final_else,
|
||||||
} => {
|
} => {
|
||||||
let ret_layout = layout_cache
|
let ret_layout = return_on_layout_error!(
|
||||||
.from_var(env.arena, branch_var, env.subs)
|
env,
|
||||||
.expect("invalid ret_layout");
|
layout_cache.from_var(env.arena, branch_var, env.subs),
|
||||||
let cond_layout = layout_cache
|
"invalid return type in if expression"
|
||||||
.from_var(env.arena, cond_var, env.subs)
|
);
|
||||||
.expect("invalid cond_layout");
|
let cond_layout = return_on_layout_error!(
|
||||||
|
env,
|
||||||
|
layout_cache.from_var(env.arena, cond_var, env.subs),
|
||||||
|
"invalid condition type in if expression"
|
||||||
|
);
|
||||||
|
|
||||||
let mut stmt = from_can(env, branch_var, final_else.value, procs, layout_cache);
|
let mut stmt = from_can(env, branch_var, final_else.value, procs, layout_cache);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
procedure Test.0 ():
|
||||||
|
let Test.2 : Str = "Erroneous: invalid condition type in if expression";
|
||||||
|
Crash Test.2
|
|
@ -76,7 +76,7 @@ fn promote_expr_to_module(src: &str) -> String {
|
||||||
buffer
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compiles_to_ir(test_name: &str, src: &str, mode: &str, no_check: bool) {
|
fn compiles_to_ir(test_name: &str, src: &str, mode: &str, allow_type_errors: bool, no_check: bool) {
|
||||||
use roc_packaging::cache::RocCacheDir;
|
use roc_packaging::cache::RocCacheDir;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ fn compiles_to_ir(test_name: &str, src: &str, mode: &str, no_check: bool) {
|
||||||
println!("Ignoring {} canonicalization problems", can_problems.len());
|
println!("Ignoring {} canonicalization problems", can_problems.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(type_problems.is_empty());
|
assert!(allow_type_errors || type_problems.is_empty());
|
||||||
|
|
||||||
let main_fn_symbol = exposed_to_host.top_level_values.keys().copied().next();
|
let main_fn_symbol = exposed_to_host.top_level_values.keys().copied().next();
|
||||||
|
|
||||||
|
@ -2888,3 +2888,14 @@ fn layout_cache_structure_with_multiple_recursive_structures() {
|
||||||
"#
|
"#
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[mono_test(allow_type_errors = "true")]
|
||||||
|
fn error_on_erroneous_condition() {
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
app "test" provides [main] to "./platform"
|
||||||
|
|
||||||
|
main = if True then 1 else 2
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use quote::quote;
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn mono_test(args: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn mono_test(args: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
let mut no_check = false;
|
let mut no_check = false;
|
||||||
|
let mut allow_type_errors = false;
|
||||||
let mut mode = "exec".to_owned();
|
let mut mode = "exec".to_owned();
|
||||||
for arg in syn::parse_macro_input!(args as syn::AttributeArgs) {
|
for arg in syn::parse_macro_input!(args as syn::AttributeArgs) {
|
||||||
use syn::{Lit, Meta, MetaNameValue, NestedMeta};
|
use syn::{Lit, Meta, MetaNameValue, NestedMeta};
|
||||||
|
@ -22,6 +23,9 @@ pub fn mono_test(args: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
if path.is_ident("no_check") {
|
if path.is_ident("no_check") {
|
||||||
no_check = true;
|
no_check = true;
|
||||||
}
|
}
|
||||||
|
if path.is_ident("allow_type_errors") {
|
||||||
|
allow_type_errors = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +44,7 @@ pub fn mono_test(args: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
#[test]
|
#[test]
|
||||||
#(#attributes)*
|
#(#attributes)*
|
||||||
#visibility fn #name(#args) {
|
#visibility fn #name(#args) {
|
||||||
compiles_to_ir(#name_str, #body, &#mode, #no_check);
|
compiles_to_ir(#name_str, #body, &#mode, #allow_type_errors, #no_check);
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue