From 4ecf2a8c24a4ee99f8988afcf8f15be38ea3ff78 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 11:23:33 +0000 Subject: [PATCH 01/20] Modify division behaviour to panic when dividing by 0, and add `divChecked`, divFloorChecked` and `divCeilingChecked` for safe alternatives which return a Result, mimicking the previous behaviour. --- compiler/builtins/bitcode/src/dec.zig | 4 +- compiler/builtins/bitcode/src/num.zig | 2 +- compiler/builtins/roc/Num.roc | 12 +- compiler/builtins/src/std.rs | 25 ++- compiler/can/src/builtins.rs | 24 ++- compiler/module/src/low_level.rs | 6 +- compiler/module/src/symbol.rs | 224 +++++++++++++------------- compiler/solve/tests/solve_expr.rs | 54 ++++++- compiler/test_gen/src/gen_num.rs | 96 ++++++++++- 9 files changed, 318 insertions(+), 129 deletions(-) diff --git a/compiler/builtins/bitcode/src/dec.zig b/compiler/builtins/bitcode/src/dec.zig index 2f0dcb6abe..b9eba9c0cc 100644 --- a/compiler/builtins/bitcode/src/dec.zig +++ b/compiler/builtins/bitcode/src/dec.zig @@ -310,9 +310,7 @@ pub const RocDec = extern struct { // (n / 0) is an error if (denominator_i128 == 0) { - // The compiler frontend does the `denominator == 0` check for us, - // therefore this case is unreachable from roc user code - unreachable; + @panic("TODO runtime exception for dividing by 0!"); } // If they're both negative, or if neither is negative, the final answer diff --git a/compiler/builtins/bitcode/src/num.zig b/compiler/builtins/bitcode/src/num.zig index 41d93df976..54bcf8ff5a 100644 --- a/compiler/builtins/bitcode/src/num.zig +++ b/compiler/builtins/bitcode/src/num.zig @@ -102,7 +102,7 @@ pub fn exportRound(comptime T: type, comptime name: []const u8) void { pub fn exportDivCeil(comptime T: type, comptime name: []const u8) void { comptime var f = struct { fn func(a: T, b: T) callconv(.C) T { - return math.divCeil(T, a, b) catch unreachable; + return math.divCeil(T, a, b) catch @panic("TODO runtime exception for dividing by 0!"); } }.func; @export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong }); diff --git a/compiler/builtins/roc/Num.roc b/compiler/builtins/roc/Num.roc index fc337ce9ba..549e392d84 100644 --- a/compiler/builtins/roc/Num.roc +++ b/compiler/builtins/roc/Num.roc @@ -69,6 +69,7 @@ interface Num isNegative, rem, div, + divChecked, modInt, modFloat, sqrt, @@ -97,7 +98,9 @@ interface Num bytesToU16, bytesToU32, divCeil, + divCeilChecked, divFloor, + divFloorChecked, toStr, isMultipleOf, minI8, @@ -229,10 +232,13 @@ atan : Float a -> Float a sqrt : Float a -> Result (Float a) [ SqrtOfNegative ]* log : Float a -> Result (Float a) [ LogNeedsPositive ]* -div : Float a, Float a -> Result (Float a) [ DivByZero ]* +div : Float a, Float a -> Float a +divChecked : Float a, Float a -> Result (Float a) [ DivByZero ]* -divCeil: Int a, Int a -> Result (Int a) [ DivByZero ]* -divFloor: Int a, Int a -> Result (Int a) [ DivByZero ]* +divCeil : Int a, Int a -> Int a +divCeilChecked : Int a, Int a -> Result (Int a) [ DivByZero ]* +divFloor : Int a, Int a -> Int a +divFloorChecked : Int a, Int a -> Result (Int a) [ DivByZero ]* # mod : Float a, Float a -> Result (Float a) [ DivByZero ]* rem : Int a, Int a -> Result (Int a) [ DivByZero ]* diff --git a/compiler/builtins/src/std.rs b/compiler/builtins/src/std.rs index ac408f5f56..11b11347f9 100644 --- a/compiler/builtins/src/std.rs +++ b/compiler/builtins/src/std.rs @@ -316,17 +316,31 @@ pub fn types() -> MutMap { Box::new(SolvedType::Wildcard), ); - // divInt : Int a, Int a -> Result (Int a) [ DivByZero ]* + // divInt : Int a, Int a -> Int a add_top_level_function_type!( Symbol::NUM_DIV_INT, vec![int_type(flex(TVAR1)), int_type(flex(TVAR1))], + Box::new(int_type(flex(TVAR1))) + ); + + // divIntChecked : Int a, Int a -> Result (Int a) [ DivByZero ]* + add_top_level_function_type!( + Symbol::NUM_DIV_INT_CHECKED, + vec![int_type(flex(TVAR1)), int_type(flex(TVAR1))], Box::new(result_type(int_type(flex(TVAR1)), div_by_zero.clone())), ); - //divCeil: Int a, Int a -> Result (Int a) [ DivByZero ]* + // divCeil : Int a, Int a -> Int a add_top_level_function_type!( Symbol::NUM_DIV_CEIL, vec![int_type(flex(TVAR1)), int_type(flex(TVAR1))], + Box::new(int_type(flex(TVAR1))) + ); + + //divCeilChecked : Int a, Int a -> Result (Int a) [ DivByZero ]* + add_top_level_function_type!( + Symbol::NUM_DIV_CEIL_CHECKED, + vec![int_type(flex(TVAR1)), int_type(flex(TVAR1))], Box::new(result_type(int_type(flex(TVAR1)), div_by_zero.clone())), ); @@ -631,6 +645,13 @@ pub fn types() -> MutMap { add_top_level_function_type!( Symbol::NUM_DIV_FLOAT, vec![float_type(flex(TVAR1)), float_type(flex(TVAR1))], + Box::new(float_type(flex(TVAR1))) + ); + + // divChecked : Float a, Float a -> Result (Float a) [ DivByZero ]* + add_top_level_function_type!( + Symbol::NUM_DIV_FLOAT_CHECKED, + vec![float_type(flex(TVAR1)), float_type(flex(TVAR1))], Box::new(result_type(float_type(flex(TVAR1)), div_by_zero.clone())), ); diff --git a/compiler/can/src/builtins.rs b/compiler/can/src/builtins.rs index 496d2aea11..833851c845 100644 --- a/compiler/can/src/builtins.rs +++ b/compiler/can/src/builtins.rs @@ -195,8 +195,11 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option NUM_COS => num_cos, NUM_TAN => num_tan, NUM_DIV_FLOAT => num_div_float, + NUM_DIV_FLOAT_CHECKED => num_div_float_checked, NUM_DIV_INT => num_div_int, + NUM_DIV_INT_CHECKED => num_div_int_checked, NUM_DIV_CEIL => num_div_ceil, + NUM_DIV_CEIL_CHECKED => num_div_ceil_checked, NUM_ABS => num_abs, NUM_NEG => num_neg, NUM_REM => num_rem, @@ -4277,8 +4280,13 @@ fn num_abs(symbol: Symbol, var_store: &mut VarStore) -> Def { ) } -/// Num.div : Float, Float -> Result Float [ DivByZero ]* +/// Num.div : Float, Float -> Float fn num_div_float(symbol: Symbol, var_store: &mut VarStore) -> Def { + num_binop(symbol, var_store, LowLevel::NumDivUnchecked) +} + +/// Num.divChecked : Float, Float -> Result Float [ DivByZero ]* +fn num_div_float_checked(symbol: Symbol, var_store: &mut VarStore) -> Def { let bool_var = var_store.fresh(); let num_var = var_store.fresh(); let unbound_zero_var = var_store.fresh(); @@ -4343,8 +4351,13 @@ fn num_div_float(symbol: Symbol, var_store: &mut VarStore) -> Def { ) } -/// Num.div : Int a , Int a -> Result (Int a) [ DivByZero ]* +/// Num.div : Int a, Int a -> Int a fn num_div_int(symbol: Symbol, var_store: &mut VarStore) -> Def { + num_binop(symbol, var_store, LowLevel::NumDivUnchecked) +} + +/// Num.divChecked : Int a , Int a -> Result (Int a) [ DivByZero ]* +fn num_div_int_checked(symbol: Symbol, var_store: &mut VarStore) -> Def { let bool_var = var_store.fresh(); let num_var = var_store.fresh(); let unbound_zero_var = var_store.fresh(); @@ -4414,8 +4427,13 @@ fn num_div_int(symbol: Symbol, var_store: &mut VarStore) -> Def { ) } -/// Num.divCeil : Int a , Int a -> Result (Int a) [ DivByZero ]* +/// Num.divCeil : Int a, Int a -> Int a fn num_div_ceil(symbol: Symbol, var_store: &mut VarStore) -> Def { + num_binop(symbol, var_store, LowLevel::NumDivCeilUnchecked) +} + +/// Num.divCeilChecked : Int a , Int a -> Result (Int a) [ DivByZero ]* +fn num_div_ceil_checked(symbol: Symbol, var_store: &mut VarStore) -> Def { let bool_var = var_store.fresh(); let num_var = var_store.fresh(); let unbound_zero_var = var_store.fresh(); diff --git a/compiler/module/src/low_level.rs b/compiler/module/src/low_level.rs index 1271816043..205bfcd1d6 100644 --- a/compiler/module/src/low_level.rs +++ b/compiler/module/src/low_level.rs @@ -287,8 +287,10 @@ impl LowLevelWrapperType { Symbol::NUM_LT => CanBeReplacedBy(NumLt), Symbol::NUM_LTE => CanBeReplacedBy(NumLte), Symbol::NUM_COMPARE => CanBeReplacedBy(NumCompare), - Symbol::NUM_DIV_FLOAT => WrapperIsRequired, - Symbol::NUM_DIV_CEIL => WrapperIsRequired, + Symbol::NUM_DIV_FLOAT => CanBeReplacedBy(NumDivUnchecked), + Symbol::NUM_DIV_FLOAT_CHECKED => WrapperIsRequired, + Symbol::NUM_DIV_CEIL => CanBeReplacedBy(NumDivCeilUnchecked), + Symbol::NUM_DIV_CEIL_CHECKED => WrapperIsRequired, Symbol::NUM_REM => WrapperIsRequired, Symbol::NUM_IS_MULTIPLE_OF => CanBeReplacedBy(NumIsMultipleOf), Symbol::NUM_ABS => CanBeReplacedBy(NumAbs), diff --git a/compiler/module/src/symbol.rs b/compiler/module/src/symbol.rs index db2e02bfdb..ea48f28590 100644 --- a/compiler/module/src/symbol.rs +++ b/compiler/module/src/symbol.rs @@ -945,114 +945,122 @@ define_builtins! { 36 NUM_IS_POSITIVE: "isPositive" 37 NUM_IS_NEGATIVE: "isNegative" 38 NUM_REM: "rem" - 39 NUM_DIV_FLOAT: "div" - 40 NUM_DIV_INT: "divFloor" - 41 NUM_MOD_INT: "modInt" - 42 NUM_MOD_FLOAT: "modFloat" - 43 NUM_SQRT: "sqrt" - 44 NUM_LOG: "log" - 45 NUM_ROUND: "round" - 46 NUM_COMPARE: "compare" - 47 NUM_POW: "pow" - 48 NUM_CEILING: "ceiling" - 49 NUM_POW_INT: "powInt" - 50 NUM_FLOOR: "floor" - 51 NUM_ADD_WRAP: "addWrap" - 52 NUM_ADD_CHECKED: "addChecked" - 53 NUM_ADD_SATURATED: "addSaturated" - 54 NUM_ATAN: "atan" - 55 NUM_ACOS: "acos" - 56 NUM_ASIN: "asin" - 57 NUM_AT_SIGNED128: "@Signed128" - 58 NUM_SIGNED128: "Signed128" imported - 59 NUM_AT_SIGNED64: "@Signed64" - 60 NUM_SIGNED64: "Signed64" imported - 61 NUM_AT_SIGNED32: "@Signed32" - 62 NUM_SIGNED32: "Signed32" imported - 63 NUM_AT_SIGNED16: "@Signed16" - 64 NUM_SIGNED16: "Signed16" imported - 65 NUM_AT_SIGNED8: "@Signed8" - 66 NUM_SIGNED8: "Signed8" imported - 67 NUM_AT_UNSIGNED128: "@Unsigned128" - 68 NUM_UNSIGNED128: "Unsigned128" imported - 69 NUM_AT_UNSIGNED64: "@Unsigned64" - 70 NUM_UNSIGNED64: "Unsigned64" imported - 71 NUM_AT_UNSIGNED32: "@Unsigned32" - 72 NUM_UNSIGNED32: "Unsigned32" imported - 73 NUM_AT_UNSIGNED16: "@Unsigned16" - 74 NUM_UNSIGNED16: "Unsigned16" imported - 75 NUM_AT_UNSIGNED8: "@Unsigned8" - 76 NUM_UNSIGNED8: "Unsigned8" imported - 77 NUM_AT_BINARY64: "@Binary64" - 78 NUM_BINARY64: "Binary64" imported - 79 NUM_AT_BINARY32: "@Binary32" - 80 NUM_BINARY32: "Binary32" imported - 81 NUM_BITWISE_AND: "bitwiseAnd" - 82 NUM_BITWISE_XOR: "bitwiseXor" - 83 NUM_BITWISE_OR: "bitwiseOr" - 84 NUM_SHIFT_LEFT: "shiftLeftBy" - 85 NUM_SHIFT_RIGHT: "shiftRightBy" - 86 NUM_SHIFT_RIGHT_ZERO_FILL: "shiftRightZfBy" - 87 NUM_SUB_WRAP: "subWrap" - 88 NUM_SUB_CHECKED: "subChecked" - 89 NUM_SUB_SATURATED: "subSaturated" - 90 NUM_MUL_WRAP: "mulWrap" - 91 NUM_MUL_CHECKED: "mulChecked" - 92 NUM_INT: "Int" imported - 93 NUM_FLOAT: "Float" imported - 94 NUM_AT_NATURAL: "@Natural" - 95 NUM_NATURAL: "Natural" imported - 96 NUM_NAT: "Nat" imported - 97 NUM_INT_CAST: "intCast" - 98 NUM_IS_MULTIPLE_OF: "isMultipleOf" - 99 NUM_AT_DECIMAL: "@Decimal" - 100 NUM_DECIMAL: "Decimal" imported - 101 NUM_DEC: "Dec" imported // the Num.Dectype alias - 102 NUM_BYTES_TO_U16: "bytesToU16" - 103 NUM_BYTES_TO_U32: "bytesToU32" - 104 NUM_CAST_TO_NAT: "#castToNat" - 105 NUM_DIV_CEIL: "divCeil" - 106 NUM_TO_STR: "toStr" - 107 NUM_MIN_I8: "minI8" - 108 NUM_MAX_I8: "maxI8" - 109 NUM_MIN_U8: "minU8" - 110 NUM_MAX_U8: "maxU8" - 111 NUM_MIN_I16: "minI16" - 112 NUM_MAX_I16: "maxI16" - 113 NUM_MIN_U16: "minU16" - 114 NUM_MAX_U16: "maxU16" - 115 NUM_MIN_I32: "minI32" - 116 NUM_MAX_I32: "maxI32" - 117 NUM_MIN_U32: "minU32" - 118 NUM_MAX_U32: "maxU32" - 119 NUM_MIN_I64: "minI64" - 120 NUM_MAX_I64: "maxI64" - 121 NUM_MIN_U64: "minU64" - 122 NUM_MAX_U64: "maxU64" - 123 NUM_MIN_I128: "minI128" - 124 NUM_MAX_I128: "maxI128" - 125 NUM_TO_I8: "toI8" - 126 NUM_TO_I8_CHECKED: "toI8Checked" - 127 NUM_TO_I16: "toI16" - 128 NUM_TO_I16_CHECKED: "toI16Checked" - 129 NUM_TO_I32: "toI32" - 130 NUM_TO_I32_CHECKED: "toI32Checked" - 131 NUM_TO_I64: "toI64" - 132 NUM_TO_I64_CHECKED: "toI64Checked" - 133 NUM_TO_I128: "toI128" - 134 NUM_TO_I128_CHECKED: "toI128Checked" - 135 NUM_TO_U8: "toU8" - 136 NUM_TO_U8_CHECKED: "toU8Checked" - 137 NUM_TO_U16: "toU16" - 138 NUM_TO_U16_CHECKED: "toU16Checked" - 139 NUM_TO_U32: "toU32" - 140 NUM_TO_U32_CHECKED: "toU32Checked" - 141 NUM_TO_U64: "toU64" - 142 NUM_TO_U64_CHECKED: "toU64Checked" - 143 NUM_TO_U128: "toU128" - 144 NUM_TO_U128_CHECKED: "toU128Checked" - 145 NUM_TO_NAT: "toNat" - 146 NUM_TO_NAT_CHECKED: "toNatChecked" + 39 NUM_REM_CHECKED: "remChecked" + 40 NUM_DIV_FLOAT: "div" + 41 NUM_DIV_FLOAT_CHECKED: "divChecked" + 42 NUM_DIV_INT: "divFloor" + 43 NUM_DIV_INT_CHECKED: "divFloorChecked" + 44 NUM_MOD_INT: "modInt" + 45 NUM_MOD_INT_CHECKED: "modIntChecked" + 46 NUM_MOD_FLOAT: "modFloat" + 47 NUM_MOD_FLOAT_CHECKED: "modFloatChecked" + 48 NUM_SQRT: "sqrt" + 49 NUM_SQRT_CHECKED: "sqrtChecked" + 50 NUM_LOG: "log" + 51 NUM_LOG_CHECKED: "logChecked" + 52 NUM_ROUND: "round" + 53 NUM_COMPARE: "compare" + 54 NUM_POW: "pow" + 55 NUM_CEILING: "ceiling" + 56 NUM_POW_INT: "powInt" + 57 NUM_FLOOR: "floor" + 58 NUM_ADD_WRAP: "addWrap" + 59 NUM_ADD_CHECKED: "addChecked" + 60 NUM_ADD_SATURATED: "addSaturated" + 61 NUM_ATAN: "atan" + 62 NUM_ACOS: "acos" + 63 NUM_ASIN: "asin" + 64 NUM_AT_SIGNED128: "@Signed128" + 65 NUM_SIGNED128: "Signed128" imported + 66 NUM_AT_SIGNED64: "@Signed64" + 67 NUM_SIGNED64: "Signed64" imported + 68 NUM_AT_SIGNED32: "@Signed32" + 69 NUM_SIGNED32: "Signed32" imported + 70 NUM_AT_SIGNED16: "@Signed16" + 71 NUM_SIGNED16: "Signed16" imported + 72 NUM_AT_SIGNED8: "@Signed8" + 73 NUM_SIGNED8: "Signed8" imported + 74 NUM_AT_UNSIGNED128: "@Unsigned128" + 75 NUM_UNSIGNED128: "Unsigned128" imported + 76 NUM_AT_UNSIGNED64: "@Unsigned64" + 77 NUM_UNSIGNED64: "Unsigned64" imported + 78 NUM_AT_UNSIGNED32: "@Unsigned32" + 79 NUM_UNSIGNED32: "Unsigned32" imported + 80 NUM_AT_UNSIGNED16: "@Unsigned16" + 81 NUM_UNSIGNED16: "Unsigned16" imported + 82 NUM_AT_UNSIGNED8: "@Unsigned8" + 83 NUM_UNSIGNED8: "Unsigned8" imported + 84 NUM_AT_BINARY64: "@Binary64" + 85 NUM_BINARY64: "Binary64" imported + 86 NUM_AT_BINARY32: "@Binary32" + 87 NUM_BINARY32: "Binary32" imported + 88 NUM_BITWISE_AND: "bitwiseAnd" + 89 NUM_BITWISE_XOR: "bitwiseXor" + 90 NUM_BITWISE_OR: "bitwiseOr" + 91 NUM_SHIFT_LEFT: "shiftLeftBy" + 92 NUM_SHIFT_RIGHT: "shiftRightBy" + 93 NUM_SHIFT_RIGHT_ZERO_FILL: "shiftRightZfBy" + 94 NUM_SUB_WRAP: "subWrap" + 95 NUM_SUB_CHECKED: "subChecked" + 96 NUM_SUB_SATURATED: "subSaturated" + 97 NUM_MUL_WRAP: "mulWrap" + 98 NUM_MUL_CHECKED: "mulChecked" + 99 NUM_INT: "Int" imported + 100 NUM_FLOAT: "Float" imported + 101 NUM_AT_NATURAL: "@Natural" + 102 NUM_NATURAL: "Natural" imported + 103 NUM_NAT: "Nat" imported + 104 NUM_INT_CAST: "intCast" + 105 NUM_IS_MULTIPLE_OF: "isMultipleOf" + 106 NUM_AT_DECIMAL: "@Decimal" + 107 NUM_DECIMAL: "Decimal" imported + 108 NUM_DEC: "Dec" imported // the Num.Dectype alias + 109 NUM_BYTES_TO_U16: "bytesToU16" + 110 NUM_BYTES_TO_U32: "bytesToU32" + 111 NUM_CAST_TO_NAT: "#castToNat" + 112 NUM_DIV_CEIL: "divCeil" + 113 NUM_DIV_CEIL_CHECKED: "divCeilChecked" + 114 NUM_TO_STR: "toStr" + 115 NUM_MIN_I8: "minI8" + 116 NUM_MAX_I8: "maxI8" + 117 NUM_MIN_U8: "minU8" + 118 NUM_MAX_U8: "maxU8" + 119 NUM_MIN_I16: "minI16" + 120 NUM_MAX_I16: "maxI16" + 121 NUM_MIN_U16: "minU16" + 122 NUM_MAX_U16: "maxU16" + 123 NUM_MIN_I32: "minI32" + 124 NUM_MAX_I32: "maxI32" + 125 NUM_MIN_U32: "minU32" + 126 NUM_MAX_U32: "maxU32" + 127 NUM_MIN_I64: "minI64" + 128 NUM_MAX_I64: "maxI64" + 129 NUM_MIN_U64: "minU64" + 130 NUM_MAX_U64: "maxU64" + 131 NUM_MIN_I128: "minI128" + 132 NUM_MAX_I128: "maxI128" + 133 NUM_TO_I8: "toI8" + 134 NUM_TO_I8_CHECKED: "toI8Checked" + 135 NUM_TO_I16: "toI16" + 136 NUM_TO_I16_CHECKED: "toI16Checked" + 137 NUM_TO_I32: "toI32" + 138 NUM_TO_I32_CHECKED: "toI32Checked" + 139 NUM_TO_I64: "toI64" + 140 NUM_TO_I64_CHECKED: "toI64Checked" + 141 NUM_TO_I128: "toI128" + 142 NUM_TO_I128_CHECKED: "toI128Checked" + 143 NUM_TO_U8: "toU8" + 144 NUM_TO_U8_CHECKED: "toU8Checked" + 145 NUM_TO_U16: "toU16" + 146 NUM_TO_U16_CHECKED: "toU16Checked" + 147 NUM_TO_U32: "toU32" + 148 NUM_TO_U32_CHECKED: "toU32Checked" + 149 NUM_TO_U64: "toU64" + 150 NUM_TO_U64_CHECKED: "toU64Checked" + 151 NUM_TO_U128: "toU128" + 152 NUM_TO_U128_CHECKED: "toU128Checked" + 153 NUM_TO_NAT: "toNat" + 154 NUM_TO_NAT_CHECKED: "toNatChecked" } 2 BOOL: "Bool" => { 0 BOOL_BOOL: "Bool" imported // the Bool.Bool type alias diff --git a/compiler/solve/tests/solve_expr.rs b/compiler/solve/tests/solve_expr.rs index 281bf37528..f31cdd5ebc 100644 --- a/compiler/solve/tests/solve_expr.rs +++ b/compiler/solve/tests/solve_expr.rs @@ -3296,6 +3296,30 @@ mod solve_expr { ); } + #[test] + fn div() { + infer_eq_without_problem( + indoc!( + r#" + Num.div + "# + ), + "Float a, Float a -> Float a" + ) + } + + #[test] + fn div_checked() { + infer_eq_without_problem( + indoc!( + r#" + Num.divChecked + "# + ), + "Float a, Float a -> Result (Float a) [ DivByZero ]*" + ) + } + #[test] fn div_ceil() { infer_eq_without_problem( @@ -3304,22 +3328,46 @@ mod solve_expr { Num.divCeil "# ), - "Int a, Int a -> Result (Int a) [ DivByZero ]*", + "Int a, Int a -> Int a" ); } #[test] - fn pow_int() { + fn div_ceil_checked() { infer_eq_without_problem( indoc!( r#" - Num.powInt + Num.divCeilChecked "# ), "Int a, Int a -> Int a", ); } + #[test] + fn div_floor() { + infer_eq_without_problem( + indoc!( + r#" + Num.divFloor + "# + ), + "Int a, Int a -> Int a" + ); + } + + #[test] + fn div_floor_checked() { + infer_eq_without_problem( + indoc!( + r#" + Num.divFloorChecked + "# + ), + "Int a, Int a -> Result (Int a) [ DivByZer ]*" + ); + } + #[test] fn atan() { infer_eq_without_problem( diff --git a/compiler/test_gen/src/gen_num.rs b/compiler/test_gen/src/gen_num.rs index 870d699f99..b9d6f30e25 100644 --- a/compiler/test_gen/src/gen_num.rs +++ b/compiler/test_gen/src/gen_num.rs @@ -723,6 +723,21 @@ fn gen_wrap_add_nums() { #[test] #[cfg(any(feature = "gen-llvm"))] fn gen_div_f64() { + // FIXME this works with normal types, but fails when checking uniqueness types + assert_evals_to!( + indoc!( + r#" + 48 / 2 + "# + ), + 24.0, + f64 + ); +} + +#[test] +#[cfg(any(feature = "gen-llvm"))] +fn gen_div_checked_f64() { // FIXME this works with normal types, but fails when checking uniqueness types assert_evals_to!( indoc!( @@ -736,6 +751,24 @@ fn gen_div_f64() { f64 ); } + +#[test] +#[cfg(any(feature = "gen-llvm"))] +fn gen_div_checked_by_zero_f64() { + // FIXME this works with normal types, but fails when checking uniqueness types + assert_evals_to!( + indoc!( + r#" + when 48 / 0 is + Ok val -> val + Err _ -> -1 + "# + ), + -1, + f64 + ); +} + #[test] #[cfg(any(feature = "gen-llvm"))] fn gen_div_dec() { @@ -748,7 +781,27 @@ fn gen_div_dec() { y : Dec y = 3 - when x / y is + x / y + "# + ), + RocDec::from_str_to_i128_unsafe("3.333333333333333333"), + i128 + ); +} + +#[test] +#[cfg(any(feature = "gen-llvm"))] +fn gen_div_checked_dec() { + assert_evals_to!( + indoc!( + r#" + x : Dec + x = 10 + + y : Dec + y = 3 + + when Num.divChecked x y is Ok val -> val Err _ -> -1 "# @@ -757,6 +810,27 @@ fn gen_div_dec() { i128 ); } +#[test] +#[cfg(any(feature = "gen-llvm"))] +fn gen_div_checked_by_zero_dec() { + assert_evals_to!( + indoc!( + r#" + x : Dec + x = 10 + + y : Dec + y = 0 + + when Num.divChecked x y is + Ok val -> val + Err _ -> -1 + "# + ), + -1, + i128 + ); +} #[test] #[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))] @@ -965,7 +1039,21 @@ fn gen_div_i64() { assert_evals_to!( indoc!( r#" - when 1000 // 10 is + 1000 // 10 + "# + ), + 100, + i64 + ); +} + +#[test] +#[cfg(any(feature = "gen-llvm"))] +fn gen_div_checked_i64() { + assert_evals_to!( + indoc!( + r#" + when Num.divFloorChecked 1000 10 is Ok val -> val Err _ -> -1 "# @@ -977,11 +1065,11 @@ fn gen_div_i64() { #[test] #[cfg(any(feature = "gen-llvm"))] -fn gen_div_by_zero_i64() { +fn gen_div_checked_by_zero_i64() { assert_evals_to!( indoc!( r#" - when 1000 // 0 is + when Num.divFloorChecked 1000 0 is Err DivByZero -> 99 _ -> -24 "# From 9182bb5773d41bb2e094cc7c4c30ea4153f3f428 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 11:35:56 +0000 Subject: [PATCH 02/20] Add toF32/64 checked versions in symbol.rs to avoid conflict. --- compiler/module/src/symbol.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/module/src/symbol.rs b/compiler/module/src/symbol.rs index ea48f28590..f68fb242c4 100644 --- a/compiler/module/src/symbol.rs +++ b/compiler/module/src/symbol.rs @@ -1061,6 +1061,10 @@ define_builtins! { 152 NUM_TO_U128_CHECKED: "toU128Checked" 153 NUM_TO_NAT: "toNat" 154 NUM_TO_NAT_CHECKED: "toNatChecked" + 155 NUM_TO_F32: "toF32" + 156 NUM_TO_F32_CHECKED: "toF32Checked" + 157 NUM_TO_F64: "toF64" + 158 NUM_TO_F64_CHECKED: "toF64Checked" } 2 BOOL: "Bool" => { 0 BOOL_BOOL: "Bool" imported // the Bool.Bool type alias From 1df1b4bc8477f18015c8a524e94377e267de3a02 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 12:34:23 +0000 Subject: [PATCH 03/20] Fix typo. --- compiler/solve/tests/solve_expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/solve/tests/solve_expr.rs b/compiler/solve/tests/solve_expr.rs index 3e58fdaeb0..0acd1551b2 100644 --- a/compiler/solve/tests/solve_expr.rs +++ b/compiler/solve/tests/solve_expr.rs @@ -3364,7 +3364,7 @@ mod solve_expr { Num.divFloorChecked "# ), - "Int a, Int a -> Result (Int a) [ DivByZer ]*" + "Int a, Int a -> Result (Int a) [ DivByZero ]*" ); } From d81228df8059ea34c4fb4a39369779f11eeba96d Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Mon, 11 Apr 2022 13:17:46 -0400 Subject: [PATCH 04/20] cargo fmt --- compiler/builtins/src/std.rs | 2 +- compiler/solve/tests/solve_expr.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/builtins/src/std.rs b/compiler/builtins/src/std.rs index 322607b7a1..dabc694835 100644 --- a/compiler/builtins/src/std.rs +++ b/compiler/builtins/src/std.rs @@ -675,7 +675,7 @@ pub fn types() -> MutMap { vec![float_type(flex(TVAR1)), float_type(flex(TVAR1))], Box::new(float_type(flex(TVAR1))) ); - + // divChecked : Float a, Float a -> Result (Float a) [ DivByZero ]* add_top_level_function_type!( Symbol::NUM_DIV_FLOAT_CHECKED, diff --git a/compiler/solve/tests/solve_expr.rs b/compiler/solve/tests/solve_expr.rs index 0acd1551b2..7250fb4d80 100644 --- a/compiler/solve/tests/solve_expr.rs +++ b/compiler/solve/tests/solve_expr.rs @@ -3304,7 +3304,7 @@ mod solve_expr { Num.div "# ), - "Float a, Float a -> Float a" + "Float a, Float a -> Float a", ) } @@ -3316,7 +3316,7 @@ mod solve_expr { Num.divChecked "# ), - "Float a, Float a -> Result (Float a) [ DivByZero ]*" + "Float a, Float a -> Result (Float a) [ DivByZero ]*", ) } @@ -3328,7 +3328,7 @@ mod solve_expr { Num.divCeil "# ), - "Int a, Int a -> Int a" + "Int a, Int a -> Int a", ); } @@ -3352,7 +3352,7 @@ mod solve_expr { Num.divFloor "# ), - "Int a, Int a -> Int a" + "Int a, Int a -> Int a", ); } @@ -3364,7 +3364,7 @@ mod solve_expr { Num.divFloorChecked "# ), - "Int a, Int a -> Result (Int a) [ DivByZero ]*" + "Int a, Int a -> Result (Int a) [ DivByZero ]*", ); } From 1d5ab1d79c0dd4d8dc7c6a863a9f9334048175fb Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 17:51:38 +0000 Subject: [PATCH 05/20] Merge remote-tracking branch 'origin/trunk' into div-no-result --- compiler/test_gen/src/gen_num.rs | 38 ++++---------------------------- 1 file changed, 4 insertions(+), 34 deletions(-) diff --git a/compiler/test_gen/src/gen_num.rs b/compiler/test_gen/src/gen_num.rs index 445751893f..a975cceee8 100644 --- a/compiler/test_gen/src/gen_num.rs +++ b/compiler/test_gen/src/gen_num.rs @@ -742,7 +742,7 @@ fn gen_div_checked_f64() { assert_evals_to!( indoc!( r#" - when 48 / 2 is + when Num.divChecked 48 2 is Ok val -> val Err _ -> -1 "# @@ -759,7 +759,7 @@ fn gen_div_checked_by_zero_f64() { assert_evals_to!( indoc!( r#" - when 48 / 0 is + when Num.divChecked 48 0 is Ok val -> val Err _ -> -1 "# @@ -2248,7 +2248,7 @@ fn max_u8() { ); } -macro_rules! num_conversion_tests { +macro_rules! to_int_tests { ($($fn:expr, $typ:ty, ($($test_name:ident, $input:expr, $output:expr $(, [ $($support_gen:literal),* ])? )*))*) => {$($( #[test] #[cfg(any(feature = "gen-llvm", $($(feature = $support_gen)*)?))] @@ -2259,7 +2259,7 @@ macro_rules! num_conversion_tests { )*)*} } -num_conversion_tests! { +to_int_tests! { "Num.toI8", i8, ( to_i8_same_width, "15u8", 15, ["gen-wasm"] to_i8_truncate, "115i32", 115, ["gen-wasm"] @@ -2320,36 +2320,6 @@ num_conversion_tests! { to_nat_truncate, "115i128", 115 to_nat_truncate_wraps, "10_000_000_000_000_000_000_000i128", 1864712049423024128 ) - "Num.toF32", f32, ( - to_f32_from_i8, "15i8", 15.0 - to_f32_from_i16, "15i16", 15.0 - to_f32_from_i32, "15i32", 15.0 - to_f32_from_i64, "15i64", 15.0 - to_f32_from_i128, "15i128", 15.0 - to_f32_from_u8, "15u8", 15.0 - to_f32_from_u16, "15u16", 15.0 - to_f32_from_u32, "15u32", 15.0 - to_f32_from_u64, "15u64", 15.0 - to_f32_from_u128, "15u128", 15.0 - to_f32_from_nat, "15nat", 15.0 - to_f32_from_f32, "1.5f32", 1.5 - to_f32_from_f64, "1.5f64", 1.5 - ) - "Num.toF64", f64, ( - to_f64_from_i8, "15i8", 15.0 - to_f64_from_i16, "15i16", 15.0 - to_f64_from_i32, "15i32", 15.0 - to_f64_from_i64, "15i64", 15.0 - to_f64_from_i128, "15i128", 15.0 - to_f64_from_u8, "15u8", 15.0 - to_f64_from_u16, "15u16", 15.0 - to_f64_from_u32, "15u32", 15.0 - to_f64_from_u64, "15u64", 15.0 - to_f64_from_u128, "15u128", 15.0 - to_f64_from_nat, "15nat", 15.0 - to_f64_from_f32, "1.5f32", 1.5 - to_f64_from_f64, "1.5f64", 1.5 - ) } macro_rules! to_int_checked_tests { From 799c05f183ddd80dee1d8355925e0f8433a89af1 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 17:56:43 +0000 Subject: [PATCH 06/20] Fix test. --- compiler/test_gen/src/gen_num.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/test_gen/src/gen_num.rs b/compiler/test_gen/src/gen_num.rs index a975cceee8..6fafd43fb9 100644 --- a/compiler/test_gen/src/gen_num.rs +++ b/compiler/test_gen/src/gen_num.rs @@ -759,7 +759,7 @@ fn gen_div_checked_by_zero_f64() { assert_evals_to!( indoc!( r#" - when Num.divChecked 48 0 is + when Num.divChecked 47 0 is Ok val -> val Err _ -> -1 "# From 20b9f6377c5b2d63bb6cbe775080197268e54fbf Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 19:43:21 +0000 Subject: [PATCH 07/20] Fix f64 ambiguity in test. --- compiler/test_gen/src/gen_num.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/test_gen/src/gen_num.rs b/compiler/test_gen/src/gen_num.rs index 6fafd43fb9..a46b8d01c0 100644 --- a/compiler/test_gen/src/gen_num.rs +++ b/compiler/test_gen/src/gen_num.rs @@ -764,7 +764,7 @@ fn gen_div_checked_by_zero_f64() { Err _ -> -1 "# ), - -1, + -1.0, f64 ); } From dbb91639e92398b21080abfa6ad6846eed602978 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 21:33:22 +0000 Subject: [PATCH 08/20] Revert weird changes in tests. --- compiler/test_gen/src/gen_num.rs | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/compiler/test_gen/src/gen_num.rs b/compiler/test_gen/src/gen_num.rs index a46b8d01c0..f82b96ba36 100644 --- a/compiler/test_gen/src/gen_num.rs +++ b/compiler/test_gen/src/gen_num.rs @@ -2248,7 +2248,7 @@ fn max_u8() { ); } -macro_rules! to_int_tests { +macro_rules! num_conversion_tests { ($($fn:expr, $typ:ty, ($($test_name:ident, $input:expr, $output:expr $(, [ $($support_gen:literal),* ])? )*))*) => {$($( #[test] #[cfg(any(feature = "gen-llvm", $($(feature = $support_gen)*)?))] @@ -2259,7 +2259,7 @@ macro_rules! to_int_tests { )*)*} } -to_int_tests! { +num_conversion_tests! { "Num.toI8", i8, ( to_i8_same_width, "15u8", 15, ["gen-wasm"] to_i8_truncate, "115i32", 115, ["gen-wasm"] @@ -2320,6 +2320,36 @@ to_int_tests! { to_nat_truncate, "115i128", 115 to_nat_truncate_wraps, "10_000_000_000_000_000_000_000i128", 1864712049423024128 ) + "Num.toF32", f32, ( + to_f32_from_i8, "15i8", 15.0 + to_f32_from_i16, "15i16", 15.0 + to_f32_from_i32, "15i32", 15.0 + to_f32_from_i64, "15i64", 15.0 + to_f32_from_i128, "15i128", 15.0 + to_f32_from_u8, "15u8", 15.0 + to_f32_from_u16, "15u16", 15.0 + to_f32_from_u32, "15u32", 15.0 + to_f32_from_u64, "15u64", 15.0 + to_f32_from_u128, "15u128", 15.0 + to_f32_from_nat, "15nat", 15.0 + to_f32_from_f32, "1.5f32", 1.5 + to_f32_from_f64, "1.5f64", 1.5 + ) + "Num.toF64", f64, ( + to_f64_from_i8, "15i8", 15.0 + to_f64_from_i16, "15i16", 15.0 + to_f64_from_i32, "15i32", 15.0 + to_f64_from_i64, "15i64", 15.0 + to_f64_from_i128, "15i128", 15.0 + to_f64_from_u8, "15u8", 15.0 + to_f64_from_u16, "15u16", 15.0 + to_f64_from_u32, "15u32", 15.0 + to_f64_from_u64, "15u64", 15.0 + to_f64_from_u128, "15u128", 15.0 + to_f64_from_nat, "15nat", 15.0 + to_f64_from_f32, "1.5f32", 1.5 + to_f64_from_f64, "1.5f64", 1.5 + ) } macro_rules! to_int_checked_tests { From 82a01c876e15dcb354a9eb5fecb96e4e4da35bcf Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 23:28:50 +0000 Subject: [PATCH 09/20] Update REPL tests --- repl_test/src/tests.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/repl_test/src/tests.rs b/repl_test/src/tests.rs index 84df9024ee..b88e6ba1bc 100644 --- a/repl_test/src/tests.rs +++ b/repl_test/src/tests.rs @@ -61,23 +61,35 @@ fn num_rem() { #[cfg(not(feature = "wasm"))] #[test] -fn num_floor_division_success() { - expect_success("Num.divFloor 4 3", "Ok 1 : Result (Int *) [ DivByZero ]*"); +fn num_floor_division() { + expect_success("Num.divFloor 4 3", "1 : Int *"); } #[cfg(not(feature = "wasm"))] #[test] -fn num_floor_division_divby_zero() { +fn num_floor_checked_division_success() { + expect_success("Num.divFloorChecked 4 3", "Ok 1 : Result (Int *) [ DivByZero ]*"); +} + +#[cfg(not(feature = "wasm"))] +#[test] +fn num_floor_checked_division_divby_zero() { expect_success( - "Num.divFloor 4 0", + "Num.divFloorChecked 4 0", "Err DivByZero : Result (Int *) [ DivByZero ]*", ); } #[cfg(not(feature = "wasm"))] #[test] -fn num_ceil_division_success() { - expect_success("Num.divCeil 4 3", "Ok 2 : Result (Int *) [ DivByZero ]*") +fn num_ceil_division() { + expect_success("Num.divCeil 4 3", "2 : Int *") +} + +#[cfg(not(feature = "wasm"))] +#[test] +fn num_ceil_checked_division_success() { + expect_success("Num.divCeilChecked 4 3", "Ok 2 : Result (Int *) [ DivByZero ]*") } #[test] From e2cccbc7b517a2937344606b15662b5e33e910cb Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 23:32:34 +0000 Subject: [PATCH 10/20] Format previous change. --- repl_test/src/tests.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/repl_test/src/tests.rs b/repl_test/src/tests.rs index b88e6ba1bc..0e54fec311 100644 --- a/repl_test/src/tests.rs +++ b/repl_test/src/tests.rs @@ -68,7 +68,10 @@ fn num_floor_division() { #[cfg(not(feature = "wasm"))] #[test] fn num_floor_checked_division_success() { - expect_success("Num.divFloorChecked 4 3", "Ok 1 : Result (Int *) [ DivByZero ]*"); + expect_success( + "Num.divFloorChecked 4 3", + "Ok 1 : Result (Int *) [ DivByZero ]*", + ); } #[cfg(not(feature = "wasm"))] @@ -89,7 +92,10 @@ fn num_ceil_division() { #[cfg(not(feature = "wasm"))] #[test] fn num_ceil_checked_division_success() { - expect_success("Num.divCeilChecked 4 3", "Ok 2 : Result (Int *) [ DivByZero ]*") + expect_success( + "Num.divCeilChecked 4 3", + "Ok 2 : Result (Int *) [ DivByZero ]*", + ) } #[test] From 6b2d1a7af67ea434c34da9ed437335f0b261d72f Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Mon, 11 Apr 2022 23:36:30 +0000 Subject: [PATCH 11/20] Remove outdated comments. --- compiler/test_gen/src/gen_num.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/test_gen/src/gen_num.rs b/compiler/test_gen/src/gen_num.rs index f82b96ba36..0b72191ca9 100644 --- a/compiler/test_gen/src/gen_num.rs +++ b/compiler/test_gen/src/gen_num.rs @@ -723,7 +723,6 @@ fn gen_wrap_add_nums() { #[test] #[cfg(any(feature = "gen-llvm"))] fn gen_div_f64() { - // FIXME this works with normal types, but fails when checking uniqueness types assert_evals_to!( indoc!( r#" @@ -738,7 +737,6 @@ fn gen_div_f64() { #[test] #[cfg(any(feature = "gen-llvm"))] fn gen_div_checked_f64() { - // FIXME this works with normal types, but fails when checking uniqueness types assert_evals_to!( indoc!( r#" @@ -755,7 +753,6 @@ fn gen_div_checked_f64() { #[test] #[cfg(any(feature = "gen-llvm"))] fn gen_div_checked_by_zero_f64() { - // FIXME this works with normal types, but fails when checking uniqueness types assert_evals_to!( indoc!( r#" From bb8b8fd0c09bc36558c64bd82ecfb860e6c8a5e8 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 00:07:53 +0000 Subject: [PATCH 12/20] Update examples / benchmarks. --- examples/benchmarks/Deriv.roc | 2 +- examples/false-interpreter/False.roc | 2 +- examples/gui/Hello.roc | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/benchmarks/Deriv.roc b/examples/benchmarks/Deriv.roc index 2cad781765..1b1458e915 100644 --- a/examples/benchmarks/Deriv.roc +++ b/examples/benchmarks/Deriv.roc @@ -42,7 +42,7 @@ Expr : [ Val I64, Var Str, Add Expr Expr, Mul Expr Expr, Pow Expr Expr, Ln Expr divmod : I64, I64 -> Result { div : I64, mod : I64 } [ DivByZero ]* divmod = \l, r -> when Pair (l // r) (l % r) is - Pair (Ok div) (Ok mod) -> + Pair (div) (Ok mod) -> Ok { div, mod } _ -> diff --git a/examples/false-interpreter/False.roc b/examples/false-interpreter/False.roc index d7e0ac0c4f..f8306e7d8d 100644 --- a/examples/false-interpreter/False.roc +++ b/examples/false-interpreter/False.roc @@ -434,7 +434,7 @@ stepExecCtx = \ctx, char -> ( (T popCtx1 numR) <- Result.after (popNumber ctx) (T popCtx2 numL) <- Result.after (popNumber popCtx1) - res <- Result.after (Num.divFloor numL numR) + res <- Result.after (Num.divFloorChecked numL numR) Ok (Context.pushStack popCtx2 (Number res)) ) diff --git a/examples/gui/Hello.roc b/examples/gui/Hello.roc index c15f5bd21a..653ccbd8e1 100644 --- a/examples/gui/Hello.roc +++ b/examples/gui/Hello.roc @@ -4,9 +4,7 @@ app "hello-gui" provides [ render ] to pf render = - div0 = \numerator, denominator -> (numerator / denominator) |> Result.withDefault 0 - - rgba = \r, g, b, a -> { r: div0 r 255, g: div0 g 255, b: div0 b 255, a } + rgba = \r, g, b, a -> { r: r / 255, g: g / 255, b: b / 255, a } styles = { bgColor: rgba 100 50 50 1, borderColor: rgba 10 20 30 1, borderWidth: 10, textColor: rgba 220 220 250 1 } From d32e77819bcd37c86bef3362aa3ff468ee022168 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 00:19:09 +0000 Subject: [PATCH 13/20] Update benchmark. --- examples/benchmarks/RBTreeDel.roc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/benchmarks/RBTreeDel.roc b/examples/benchmarks/RBTreeDel.roc index 24419ef84a..43fd309e5d 100644 --- a/examples/benchmarks/RBTreeDel.roc +++ b/examples/benchmarks/RBTreeDel.roc @@ -47,7 +47,7 @@ makeMapHelp = \total, n, m -> isFrequency = n |> Num.isMultipleOf 4 - key = n1 + ((total - n1) // 5 |> resultWithDefault 0) + key = n1 + ((total - n1) // 5) t2 = if isFrequency then delete t1 key else t1 makeMapHelp total n1 t2 From 2ba9566c03cdb1e486626143fbe0897d208561c8 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 00:41:14 +0000 Subject: [PATCH 14/20] Remove parenthesis from Pair --- examples/benchmarks/Deriv.roc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/benchmarks/Deriv.roc b/examples/benchmarks/Deriv.roc index 1b1458e915..caf08906a6 100644 --- a/examples/benchmarks/Deriv.roc +++ b/examples/benchmarks/Deriv.roc @@ -42,7 +42,7 @@ Expr : [ Val I64, Var Str, Add Expr Expr, Mul Expr Expr, Pow Expr Expr, Ln Expr divmod : I64, I64 -> Result { div : I64, mod : I64 } [ DivByZero ]* divmod = \l, r -> when Pair (l // r) (l % r) is - Pair (div) (Ok mod) -> + Pair div (Ok mod) -> Ok { div, mod } _ -> From 1c3700e22e5980dd47ef8adeaaa87bd0f6291733 Mon Sep 17 00:00:00 2001 From: Kevin Gillette Date: Mon, 11 Apr 2022 19:11:37 -0600 Subject: [PATCH 15/20] Minor typo/formatting fixes --- BUILDING_FROM_SOURCE.md | 2 +- compiler/builtins/README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/BUILDING_FROM_SOURCE.md b/BUILDING_FROM_SOURCE.md index bb1e70733c..4182939e77 100644 --- a/BUILDING_FROM_SOURCE.md +++ b/BUILDING_FROM_SOURCE.md @@ -109,7 +109,7 @@ Alternatively, you can use `cargo test --no-fail-fast` or `cargo test -p specifi For debugging LLVM IR, we use [DebugIR](https://github.com/vaivaswatha/debugir). This dependency is only required to build with the `--debug` flag, and for normal developtment you should be fine without it. -### libcxb libraries +### libxcb libraries You may see an error like this during builds: diff --git a/compiler/builtins/README.md b/compiler/builtins/README.md index ceaf3007e3..2ab95cd260 100644 --- a/compiler/builtins/README.md +++ b/compiler/builtins/README.md @@ -60,7 +60,7 @@ Its one thing to actually write these functions, its _another_ thing to let the ## Specifying how we pass args to the function ### builtins/mono/src/borrow.rs -After we have all of this, we need to specify if the arguments we're passing are owned, borrowed or irrelevant. Towards the bottom of this file, add a new case for you builtin and specify each arg. Be sure to read the comment, as it explains this in more detail. +After we have all of this, we need to specify if the arguments we're passing are owned, borrowed or irrelevant. Towards the bottom of this file, add a new case for your builtin and specify each arg. Be sure to read the comment, as it explains this in more detail. ## Testing it ### solve/tests/solve_expr.rs @@ -87,7 +87,7 @@ In this directory, there are a couple files like `gen_num.rs`, `gen_str.rs`, etc fn atan() { assert_evals_to!("Num.atan 10", 1.4711276743037347, f64); } - ``` +``` But replace `Num.atan`, the return value, and the return type with your new builtin. # Mistakes that are easy to make!! From 895ba92239b158224f2b9f4521e61b22466a3605 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 01:21:24 +0000 Subject: [PATCH 16/20] Change expected types in test_load. --- compiler/load_internal/tests/test_load.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/load_internal/tests/test_load.rs b/compiler/load_internal/tests/test_load.rs index f78802553c..d97db1c0c6 100644 --- a/compiler/load_internal/tests/test_load.rs +++ b/compiler/load_internal/tests/test_load.rs @@ -426,12 +426,12 @@ mod test_load { loaded_module, hashmap! { "floatTest" => "Float *", - "divisionFn" => "Float a, Float a -> Result (Float a) [ DivByZero ]*", - "divisionTest" => "Result (Float *) [ DivByZero ]*", + "divisionFn" => "Float a, Float a -> Float a", + "divisionTest" => "Float a", "intTest" => "I64", "x" => "Float *", "constantNum" => "Num *", - "divDep1ByDep2" => "Result (Float *) [ DivByZero ]*", + "divDep1ByDep2" => "Float *", "fromDep2" => "Float *", }, ); From 2a56463b74039c02eddd37c2dfd40b23d284154d Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 01:23:49 +0000 Subject: [PATCH 17/20] Fix typo in test_load. --- compiler/load_internal/tests/test_load.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/load_internal/tests/test_load.rs b/compiler/load_internal/tests/test_load.rs index d97db1c0c6..8f0223d483 100644 --- a/compiler/load_internal/tests/test_load.rs +++ b/compiler/load_internal/tests/test_load.rs @@ -427,7 +427,7 @@ mod test_load { hashmap! { "floatTest" => "Float *", "divisionFn" => "Float a, Float a -> Float a", - "divisionTest" => "Float a", + "divisionTest" => "Float *", "intTest" => "I64", "x" => "Float *", "constantNum" => "Num *", From 0f0e9bdf14acaeee444e3dec8ed8763fe462d2ae Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 12:12:29 +0000 Subject: [PATCH 18/20] Fix test in solve_expr.rs --- compiler/solve/tests/solve_expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/solve/tests/solve_expr.rs b/compiler/solve/tests/solve_expr.rs index 7250fb4d80..7e3e2e8215 100644 --- a/compiler/solve/tests/solve_expr.rs +++ b/compiler/solve/tests/solve_expr.rs @@ -3340,7 +3340,7 @@ mod solve_expr { Num.divCeilChecked "# ), - "Int a, Int a -> Int a", + "Int a, Int a -> Result (Int a) [ DivByZero ]*", ); } From dfba77bd0419036d47825fe99d43dda9611d883d Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 16:41:29 +0000 Subject: [PATCH 19/20] Explicit i128 in test. --- compiler/test_gen/src/gen_num.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/test_gen/src/gen_num.rs b/compiler/test_gen/src/gen_num.rs index 0b72191ca9..88480c6310 100644 --- a/compiler/test_gen/src/gen_num.rs +++ b/compiler/test_gen/src/gen_num.rs @@ -824,7 +824,7 @@ fn gen_div_checked_by_zero_dec() { Err _ -> -1 "# ), - -1, + RocDec::from_str_to_i128_unsafe("-1"), i128 ); } From c035900d64b8f579210d442d391d04c66bcfc468 Mon Sep 17 00:00:00 2001 From: Nikita Mounier Date: Tue, 12 Apr 2022 18:15:19 +0000 Subject: [PATCH 20/20] Update test_mono test. --- compiler/test_mono/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/test_mono/src/tests.rs b/compiler/test_mono/src/tests.rs index ff9b7bfcaa..777a790d52 100644 --- a/compiler/test_mono/src/tests.rs +++ b/compiler/test_mono/src/tests.rs @@ -274,7 +274,7 @@ fn ir_round() { #[mono_test] fn ir_when_idiv() { r#" - when 1000 // 10 is + when Num.divFloorChecked 1000 10 is Ok val -> val Err _ -> -1 "#