Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Check for calculated read sizes being negative

+40 -35
+32 -23
compiler-core/src/exhaustiveness.rs
··· 862 862 /// pattern where the size is a variable with a negative value will never 863 863 /// match. So we check this to make sure the test will fail. 864 864 /// 865 - VariableIsNotNegative { 866 - variable: VariableUsage, 865 + ReadSizeIsNotNegative { 866 + size: ReadSize, 867 867 }, 868 868 869 869 /// This test checks that the segment read by the given read action is a ··· 871 871 /// 872 872 /// We need this check as `NaN` and `Infinity` will not match with float 873 873 /// segments (like `<<_:32-float>>`) on the Erlang target and we must 874 - /// replicate the same behavious on the JavaScript target as well. 874 + /// replicate the same behaviours on the JavaScript target as well. 875 875 /// 876 876 SegmentIsFiniteFloat { 877 877 read_action: ReadAction, ··· 891 891 892 892 pub(crate) fn referenced_segment_patterns(&self) -> Vec<(&EcoString, &ReadAction)> { 893 893 match self { 894 - BitArrayTest::VariableIsNotNegative { variable } => match variable { 895 - VariableUsage::PatternSegment(segment_value, read_action) => { 896 - vec![(segment_value, read_action)] 897 - } 898 - VariableUsage::OutsideVariable(..) => vec![], 899 - }, 894 + BitArrayTest::ReadSizeIsNotNegative { size } => { 895 + let mut references = Vec::new(); 896 + size.referenced_segment_patterns(&mut references); 897 + references 898 + } 900 899 901 900 BitArrayTest::Size(SizeTest { operator: _, size }) 902 901 | BitArrayTest::CatchAllIsBytes { size_so_far: size } => { ··· 1479 1478 1480 1479 ReadSize::ConstantBits(..) | ReadSize::RemainingBits | ReadSize::RemainingBytes => (), 1481 1480 }; 1481 + } 1482 + 1483 + fn can_be_negative(&self) -> bool { 1484 + match self { 1485 + ReadSize::ConstantBits(value) => *value < BigInt::ZERO, 1486 + ReadSize::VariableBits { variable, .. } => match variable.as_ref() { 1487 + VariableUsage::PatternSegment(_, read_action) => read_action.signed, 1488 + VariableUsage::OutsideVariable(_) => true, 1489 + }, 1490 + ReadSize::BinaryOperator { 1491 + left, 1492 + right, 1493 + operator, 1494 + } => { 1495 + *operator == IntOperator::Subtract 1496 + || left.can_be_negative() 1497 + || right.can_be_negative() 1498 + } 1499 + ReadSize::RemainingBits | ReadSize::RemainingBytes => false, 1500 + } 1482 1501 } 1483 1502 } 1484 1503 ··· 2910 2929 2911 2930 // If we're reading a variable number of bits we need to make sure 2912 2931 // that that variable is not negative! 2913 - if let ReadSize::VariableBits { variable, .. } = &segment_size { 2914 - match variable.as_ref() { 2915 - // If the size variable comes from reading an unsigned 2916 - // number we know that it can't be negative! So we can skip 2917 - // checking it. 2918 - VariableUsage::PatternSegment(_, read_action) if !read_action.signed => (), 2919 - 2920 - // Otherwise we must make sure that the read size is not 2921 - // negative. 2922 - VariableUsage::PatternSegment(..) | VariableUsage::OutsideVariable(_) => tests 2923 - .push_back(BitArrayTest::VariableIsNotNegative { 2924 - variable: variable.as_ref().clone(), 2925 - }), 2926 - } 2932 + if segment_size.can_be_negative() { 2933 + tests.push_back(BitArrayTest::ReadSizeIsNotNegative { 2934 + size: segment_size.clone(), 2935 + }); 2927 2936 } 2928 2937 2929 2938 // All segments but the last will require the original bit array to
+4 -2
compiler-core/src/javascript/decision.rs
··· 993 993 } 994 994 } 995 995 996 - BitArrayTest::VariableIsNotNegative { variable } => { 997 - docvec![self.local_var(variable.name()), " >= 0"] 996 + BitArrayTest::ReadSizeIsNotNegative { size } => { 997 + docvec![self.read_size_to_doc(size), " >= 0"] 998 998 } 999 999 1000 1000 BitArrayTest::SegmentIsFiniteFloat { ··· 1233 1233 /// remaining bits/bytes") this returns a document representing that size. 1234 1234 /// 1235 1235 fn read_size_to_doc(&mut self, size: &ReadSize) -> Option<Document<'a>> { 1236 + dbg!(size); 1237 + 1236 1238 match size { 1237 1239 ReadSize::ConstantBits(value) => Some(value.clone().to_doc()), 1238 1240 ReadSize::VariableBits { variable, unit } => {
+1 -3
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__bit_arrays__case_dynamic_size_float_pattern_with_unit.snap
··· 1 1 --- 2 2 source: compiler-core/src/javascript/tests/bit_arrays.rs 3 - assertion_line: 1845 4 3 expression: "\npub fn go(x) {\n let size = 3\n case x {\n <<1.3:size(size)-unit(2)>> -> 1\n _ -> 2\n }\n}\n" 5 - snapshot_kind: text 6 4 --- 7 5 ----- SOURCE CODE 8 6 ··· 21 19 export function go(x) { 22 20 let size = 3; 23 21 if ( 24 - size >= 0 && 22 + size * 2 >= 0 && 25 23 x.bitSize === size * 2 && 26 24 bitArraySliceToFloat(x, 0, size * 2, true) === 1.3 27 25 ) {
+1 -3
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__bit_arrays__case_dynamic_size_pattern_with_unit.snap
··· 1 1 --- 2 2 source: compiler-core/src/javascript/tests/bit_arrays.rs 3 - assertion_line: 1830 4 3 expression: "\npub fn go(x) {\n let size = 3\n case x {\n <<1:size(size)-unit(2)>> -> 1\n _ -> 2\n }\n}\n" 5 - snapshot_kind: text 6 4 --- 7 5 ----- SOURCE CODE 8 6 ··· 21 19 export function go(x) { 22 20 let size = 3; 23 21 if ( 24 - size >= 0 && 22 + size * 2 >= 0 && 25 23 x.bitSize === size * 2 && 26 24 bitArraySliceToInt(x, 0, size * 2, true, false) === 1 27 25 ) {
+1 -3
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__bit_arrays__case_match_dynamic_bytes_size.snap
··· 1 1 --- 2 2 source: compiler-core/src/javascript/tests/bit_arrays.rs 3 - assertion_line: 1090 4 3 expression: "\npub fn go(x) {\n let n = 3\n case x {\n <<a:bytes-size(n)>> -> a\n _ -> x\n }\n}\n" 5 - snapshot_kind: text 6 4 --- 7 5 ----- SOURCE CODE 8 6 ··· 20 18 21 19 export function go(x) { 22 20 let n = 3; 23 - if (n >= 0 && x.bitSize === n * 8) { 21 + if (n * 8 >= 0 && x.bitSize === n * 8) { 24 22 let a = bitArraySlice(x, 0, n * 8); 25 23 return a; 26 24 } else {
+1 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__bit_arrays__non_byte_aligned_size_calculation.snap
··· 19 19 let $ = toBitArray([]); 20 20 if ($.bitSize >= 1 && $.bitSize >= 4) { 21 21 let b = bitArraySliceToInt($, 1, 4, true, false); 22 - if ($.bitSize === 4 + b - 2) { 22 + if (b - 2 >= 0 && $.bitSize === 4 + b - 2) { 23 23 let a = bitArraySliceToInt($, 0, 1, true, false); 24 24 let b$1 = b; 25 25 let c = bitArraySliceToInt($, 4, 4 + b$1 - 2, true, false);