Skip to content

Commit 0f1b56a

Browse files
committed
Fix discarded variable sized string segment patterns not being disallowed
1 parent 5288b74 commit 0f1b56a

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

compiler-core/src/erlang/tests/bit_arrays.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,6 @@ pub fn main() {
8989
);
9090
}
9191

92-
#[test]
93-
fn bit_array_discard() {
94-
// https://github.com/gleam-lang/gleam/issues/704
95-
96-
assert_erl!(
97-
r#"
98-
pub fn bit_array_discard(x) -> Bool {
99-
case x {
100-
<<_:utf8, rest:bytes>> -> True
101-
_ -> False
102-
}
103-
}
104-
"#
105-
);
106-
}
107-
108-
#[test]
109-
fn bit_array_discard1() {
110-
assert_erl!(
111-
r#"
112-
pub fn bit_array_discard(x) -> Bool {
113-
case x {
114-
<<_discardme:utf8, rest:bytes>> -> True
115-
_ -> False
116-
}
117-
}
118-
"#
119-
);
120-
}
121-
12292
#[test]
12393
fn bit_array_declare_and_use_var() {
12494
assert_erl!(
@@ -184,16 +154,6 @@ pub fn main() {
184154
);
185155
}
186156

187-
#[test]
188-
fn discard_utf8_pattern() {
189-
assert_erl!(
190-
r#"
191-
pub fn main() {
192-
let assert <<_:utf8, rest:bits>> = <<>>
193-
}"#
194-
);
195-
}
196-
197157
// https://github.com/gleam-lang/gleam/issues/3944
198158
#[test]
199159
fn pipe_size_segment() {

compiler-core/src/type_/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
521521
});
522522
self.environment.new_unbound_var()
523523
}
524-
Pattern::Variable { .. } if segment_type.is_string() => {
524+
Pattern::Variable { .. } | Pattern::Discard { .. } if segment_type.is_string() => {
525525
self.error(Error::BitArraySegmentError {
526526
error: bit_array::ErrorType::VariableUtfSegmentInPattern,
527527
location: segment.location,

compiler-core/src/type_/tests/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ fn bit_array_segment_type_does_not_allow_variable_string() {
170170
assert_error!("case <<>> { <<a:utf8>> -> 1 _ -> 2 }");
171171
}
172172

173+
#[test]
174+
fn bit_array_segment_type_does_not_allow_discarded_variable_string() {
175+
assert_error!("case <<>> { <<_:utf8>> -> 1 _ -> 2 }");
176+
}
177+
173178
#[test]
174179
fn bit_array_segment_type_does_not_allow_aliased_variable_string() {
175180
assert_error!("case <<>> { <<_ as a:utf8>> -> 1 _ -> 2 }");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: compiler-core/src/type_/tests/errors.rs
3+
assertion_line: 175
4+
expression: "case <<>> { <<_:utf8>> -> 1 _ -> 2 }"
5+
snapshot_kind: text
6+
---
7+
----- SOURCE CODE
8+
case <<>> { <<_:utf8>> -> 1 _ -> 2 }
9+
10+
----- ERROR
11+
error: Invalid bit array segment
12+
┌─ /src/one/two.gleam:1:15
13+
14+
1 │ case <<>> { <<_:utf8>> -> 1 _ -> 2 }
15+
│ ^^^^^^ This cannot be a variable
16+
17+
Hint: in patterns utf8, utf16, and utf32 must be an exact string.
18+
See: https://tour.gleam.run/data-types/bit-arrays/

0 commit comments

Comments
 (0)