diff --git a/CHANGELOG.md b/CHANGELOG.md index c65e43a6716..5b2e252e626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -276,3 +276,7 @@ - Fixed a bug where renaming a variable used in a record update would produce invalid code in certain situations. ([Surya Rose](https://github.com/GearsDatapacks)) + +- Fixed a bug where variable sized string segment patterns that are discarded + are not disallowed by the compiler. + ([Lily Rose](https://github.com/LilyRose2798)) diff --git a/compiler-core/src/erlang/tests/bit_arrays.rs b/compiler-core/src/erlang/tests/bit_arrays.rs index c29eab740b8..394f4f658c3 100644 --- a/compiler-core/src/erlang/tests/bit_arrays.rs +++ b/compiler-core/src/erlang/tests/bit_arrays.rs @@ -89,36 +89,6 @@ pub fn main() { ); } -#[test] -fn bit_array_discard() { - // https://github.com/gleam-lang/gleam/issues/704 - - assert_erl!( - r#" -pub fn bit_array_discard(x) -> Bool { - case x { - <<_:utf8, rest:bytes>> -> True - _ -> False - } -} - "# - ); -} - -#[test] -fn bit_array_discard1() { - assert_erl!( - r#" -pub fn bit_array_discard(x) -> Bool { - case x { - <<_discardme:utf8, rest:bytes>> -> True - _ -> False - } -} -"# - ); -} - #[test] fn bit_array_declare_and_use_var() { assert_erl!( @@ -184,16 +154,6 @@ pub fn main() { ); } -#[test] -fn discard_utf8_pattern() { - assert_erl!( - r#" -pub fn main() { - let assert <<_:utf8, rest:bits>> = <<>> -}"# - ); -} - // https://github.com/gleam-lang/gleam/issues/3944 #[test] fn pipe_size_segment() { diff --git a/compiler-core/src/type_/pattern.rs b/compiler-core/src/type_/pattern.rs index 915296ed804..825d5b298dc 100644 --- a/compiler-core/src/type_/pattern.rs +++ b/compiler-core/src/type_/pattern.rs @@ -521,7 +521,7 @@ impl<'a, 'b> PatternTyper<'a, 'b> { }); self.environment.new_unbound_var() } - Pattern::Variable { .. } if segment_type.is_string() => { + Pattern::Variable { .. } | Pattern::Discard { .. } if segment_type.is_string() => { self.error(Error::BitArraySegmentError { error: bit_array::ErrorType::VariableUtfSegmentInPattern, location: segment.location, diff --git a/compiler-core/src/type_/tests/errors.rs b/compiler-core/src/type_/tests/errors.rs index 2ba6d7a5534..1b041ca6414 100644 --- a/compiler-core/src/type_/tests/errors.rs +++ b/compiler-core/src/type_/tests/errors.rs @@ -170,6 +170,11 @@ fn bit_array_segment_type_does_not_allow_variable_string() { assert_error!("case <<>> { <> -> 1 _ -> 2 }"); } +#[test] +fn bit_array_segment_type_does_not_allow_discarded_variable_string() { + assert_error!("case <<>> { <<_:utf8>> -> 1 _ -> 2 }"); +} + #[test] fn bit_array_segment_type_does_not_allow_aliased_variable_string() { assert_error!("case <<>> { <<_ as a:utf8>> -> 1 _ -> 2 }"); diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__bit_array_segment_type_does_not_allow_discarded_variable_string.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__bit_array_segment_type_does_not_allow_discarded_variable_string.snap new file mode 100644 index 00000000000..afcec1a7ae0 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__bit_array_segment_type_does_not_allow_discarded_variable_string.snap @@ -0,0 +1,16 @@ +--- +source: compiler-core/src/type_/tests/errors.rs +expression: "case <<>> { <<_:utf8>> -> 1 _ -> 2 }" +--- +----- SOURCE CODE +case <<>> { <<_:utf8>> -> 1 _ -> 2 } + +----- ERROR +error: Invalid bit array segment + ┌─ /src/one/two.gleam:1:15 + │ +1 │ case <<>> { <<_:utf8>> -> 1 _ -> 2 } + │ ^^^^^^ This cannot be a variable + +Hint: in patterns utf8, utf16, and utf32 must be an exact string. +See: https://tour.gleam.run/data-types/bit-arrays/