Correct repl evaluation of toplevel numbers

Closes #5191
This commit is contained in:
Ayaz Hafiz 2023-04-12 10:18:32 -05:00
parent 77cf60b866
commit 250f4ea046
No known key found for this signature in database
GPG Key ID: 0E2A37416A25EF58
2 changed files with 59 additions and 18 deletions

View File

@ -360,27 +360,30 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
})
}
Layout::Builtin(Builtin::Int(int_width)) => {
use Content::*;
use IntWidth::*;
match (env.subs.get_content_without_compacting(raw_var), int_width) {
(Alias(Symbol::NUM_UNSIGNED8 | Symbol::NUM_U8, ..), U8) => num_helper!(u8),
(_, U8) => {
// This is not a number, it's a tag union or something else
app.call_function(main_fn_name, |_mem: &A::Memory, num: u8| {
byte_to_ast(env, num, env.subs.get_content_without_compacting(raw_var))
})
match int_width {
U8 => {
let raw_content = env.subs.get_content_without_compacting(raw_var);
if matches!(raw_content, Content::Alias(name, ..) if name.module_id() == ModuleId::NUM)
{
num_helper!(u8)
} else {
// This is not a number, it's a tag union or something else
app.call_function(main_fn_name, |_mem: &A::Memory, num: u8| {
byte_to_ast(env, num, env.subs.get_content_without_compacting(raw_var))
})
}
}
// The rest are numbers... for now
(_, U16) => num_helper!(u16),
(_, U32) => num_helper!(u32),
(_, U64) => num_helper!(u64),
(_, U128) => num_helper!(u128),
(_, I8) => num_helper!(i8),
(_, I16) => num_helper!(i16),
(_, I32) => num_helper!(i32),
(_, I64) => num_helper!(i64),
(_, I128) => num_helper!(i128),
U16 => num_helper!(u16),
U32 => num_helper!(u32),
U64 => num_helper!(u64),
U128 => num_helper!(u128),
I8 => num_helper!(i8),
I16 => num_helper!(i16),
I32 => num_helper!(i32),
I64 => num_helper!(i64),
I128 => num_helper!(i128),
}
}
Layout::Builtin(Builtin::Float(float_width)) => {

View File

@ -1212,4 +1212,42 @@ mod test {
),
);
}
#[test]
fn match_on_opaque_number_type() {
run_expect_test(
indoc!(
r#"
interface Test exposes [] imports []
hexToByte : U8, U8 -> U8
hexToByte = \upper, lower ->
Num.bitwiseOr (Num.shiftRightBy upper 4) lower
expect
actual = hexToByte 7 4
expected = 't'
actual == expected
"#
),
indoc!(
r#"
This expectation failed:
7> expect
8> actual = hexToByte 7 4
9> expected = 't'
10> actual == expected
When it failed, these variables had these values:
actual : U8
actual = 4
expected : Int Unsigned8
expected = 116
"#
),
);
}
}