Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
40 changes: 0 additions & 40 deletions compiler-core/src/erlang/tests/bit_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/type_/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions compiler-core/src/type_/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ fn bit_array_segment_type_does_not_allow_variable_string() {
assert_error!("case <<>> { <<a:utf8>> -> 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 }");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: compiler-core/src/type_/tests/errors.rs
assertion_line: 175
expression: "case <<>> { <<_:utf8>> -> 1 _ -> 2 }"
snapshot_kind: text
---
----- 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/
Loading