Ranged number abilities are derived and compiled correctly

Closes #5089
This commit is contained in:
Ayaz Hafiz 2023-03-22 09:42:06 -05:00
parent 63ef4a486f
commit 240c1f35d6
No known key found for this signature in database
GPG Key ID: 0E2A37416A25EF58
4 changed files with 52 additions and 2 deletions

View File

@ -96,7 +96,9 @@ impl FlatDecodable {
// by the backend, and the backend treats opaques like structural aliases. // by the backend, and the backend treats opaques like structural aliases.
_ => Self::from_var(subs, real_var), _ => Self::from_var(subs, real_var),
}, },
Content::RangedNumber(_) => Err(Underivable), Content::RangedNumber(range) => {
Self::from_var(subs, range.default_compilation_variable())
}
// //
Content::RecursionVar { .. } => Err(Underivable), Content::RecursionVar { .. } => Err(Underivable),
Content::Error => Err(Underivable), Content::Error => Err(Underivable),

View File

@ -131,7 +131,9 @@ impl FlatEncodable {
// by the backend, and the backend treats opaques like structural aliases. // by the backend, and the backend treats opaques like structural aliases.
_ => Self::from_var(subs, real_var), _ => Self::from_var(subs, real_var),
}, },
Content::RangedNumber(_) => Err(Underivable), Content::RangedNumber(range) => {
Self::from_var(subs, range.default_compilation_variable())
}
// //
Content::RecursionVar { .. } => Err(Underivable), Content::RecursionVar { .. } => Err(Underivable),
Content::Error => Err(Underivable), Content::Error => Err(Underivable),

View File

@ -485,6 +485,25 @@ mod encode_immediate {
) )
} }
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn ranged_number() {
assert_evals_to!(
indoc!(
r#"
app "test" imports [Encode, Json] provides [main] to "./platform"
main =
when Str.fromUtf8 (Encode.toBytes [1, 2, 3] Json.toUtf8) is
Ok s -> s
_ -> "<bad>"
"#
),
RocStr::from(r"[1,2,3]"),
RocStr
)
}
macro_rules! num_immediate { macro_rules! num_immediate {
($($num:expr, $typ:ident)*) => {$( ($($num:expr, $typ:ident)*) => {$(
#[test] #[test]
@ -960,6 +979,28 @@ mod decode_immediate {
) )
} }
#[test]
#[cfg(any(feature = "gen-llvm"))]
fn ranged_number() {
assert_evals_to!(
indoc!(
r#"
app "test" imports [Json] provides [main] to "./platform"
main =
input = Str.toUtf8 "[1,2,3]"
expected = [1,2,3]
actual = Decode.fromBytes input Json.fromUtf8 |> Result.withDefault []
actual == expected
"#
),
true,
bool
)
}
macro_rules! num_immediate { macro_rules! num_immediate {
($($num:expr, $typ:ident)*) => {$( ($($num:expr, $typ:ident)*) => {$(
#[test] #[test]

View File

@ -146,6 +146,11 @@ impl NumericRange {
.expect("if number doesn't fit, should have been a type error"), .expect("if number doesn't fit, should have been a type error"),
} }
} }
/// Chooses the type variable to compile this ranged number as.
pub fn default_compilation_variable(&self) -> Variable {
int_lit_width_to_variable(self.default_compilation_width())
}
} }
#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]