diff --git a/crates/compiler/derive_key/src/decoding.rs b/crates/compiler/derive_key/src/decoding.rs index ec0d732363..639346f299 100644 --- a/crates/compiler/derive_key/src/decoding.rs +++ b/crates/compiler/derive_key/src/decoding.rs @@ -96,7 +96,9 @@ impl FlatDecodable { // by the backend, and the backend treats opaques like structural aliases. _ => 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::Error => Err(Underivable), diff --git a/crates/compiler/derive_key/src/encoding.rs b/crates/compiler/derive_key/src/encoding.rs index 6945358c60..6b923a1774 100644 --- a/crates/compiler/derive_key/src/encoding.rs +++ b/crates/compiler/derive_key/src/encoding.rs @@ -131,7 +131,9 @@ impl FlatEncodable { // by the backend, and the backend treats opaques like structural aliases. _ => 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::Error => Err(Underivable), diff --git a/crates/compiler/test_gen/src/gen_abilities.rs b/crates/compiler/test_gen/src/gen_abilities.rs index 498293824e..56161b2f64 100644 --- a/crates/compiler/test_gen/src/gen_abilities.rs +++ b/crates/compiler/test_gen/src/gen_abilities.rs @@ -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 + _ -> "" + "# + ), + RocStr::from(r"[1,2,3]"), + RocStr + ) + } + macro_rules! num_immediate { ($($num:expr, $typ:ident)*) => {$( #[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 { ($($num:expr, $typ:ident)*) => {$( #[test] diff --git a/crates/compiler/types/src/num.rs b/crates/compiler/types/src/num.rs index 5a85e0a1bb..2de741c1dc 100644 --- a/crates/compiler/types/src/num.rs +++ b/crates/compiler/types/src/num.rs @@ -146,6 +146,11 @@ impl NumericRange { .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)]