Skip to content

Commit 04139f8

Browse files
GearsDatapackslpil
authored andcommitted
Error for UTF-16 and UTF-32 assignments
1 parent aff0e2f commit 04139f8

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

compiler-core/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2401,7 +2401,7 @@ impl<Type> BitArraySegment<Pattern<Type>, Type> {
24012401
}
24022402
}
24032403

2404-
impl<Value> BitArraySegment<Value, Arc<Type>> {
2404+
impl<Value, Type> BitArraySegment<Value, Type> {
24052405
#[must_use]
24062406
pub fn has_native_option(&self) -> bool {
24072407
self.options

compiler-core/src/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,6 +3684,25 @@ at once, which is not possible in bit arrays."),
36843684
extra_labels: vec![],
36853685
}),
36863686
},
3687+
3688+
TypeError::NonUtf8StringAssignmentInBitArray { location } => Diagnostic {
3689+
title: "Non UTF-8 string assignment".to_string(),
3690+
text:wrap("This pattern assigns a non UTF-8 string to a \
3691+
variable in a bit array. This is planned to be supported in the future, but we are \
3692+
unsure of the desired behaviour. Please go to https://github.com/gleam-lang/gleam/issues/4566 \
3693+
and explain your usecase for this pattern, and how you would expect it to behave."),
3694+
hint: None,
3695+
level: Level::Error,
3696+
location: Some(Location {
3697+
label: Label {
3698+
text: None,
3699+
span: *location,
3700+
},
3701+
path: path.clone(),
3702+
src: src.clone(),
3703+
extra_labels: vec![],
3704+
}),
3705+
},
36873706
}
36883707
}).collect_vec(),
36893708

compiler-core/src/type_/error.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ pub enum Error {
633633
DoubleVariableAssignmentInBitArray {
634634
location: SrcSpan,
635635
},
636+
637+
NonUtf8StringAssignmentInBitArray {
638+
location: SrcSpan,
639+
},
636640
}
637641

638642
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -1136,7 +1140,8 @@ impl Error {
11361140
| Error::FloatOperatorOnInts { location, .. }
11371141
| Error::IntOperatorOnFloats { location, .. }
11381142
| Error::StringConcatenationWithAddInt { location }
1139-
| Error::DoubleVariableAssignmentInBitArray { location } => location.start,
1143+
| Error::DoubleVariableAssignmentInBitArray { location }
1144+
| Error::NonUtf8StringAssignmentInBitArray { location } => location.start,
11401145

11411146
Error::UnknownLabels { unknown, .. } => {
11421147
unknown.iter().map(|(_, s)| s.start).min().unwrap_or(0)

compiler-core/src/type_/pattern.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
423423
}
424424
}
425425

426+
let has_non_utf8_string_option = segment.has_utf16_option() || segment.has_utf32_option();
427+
426428
let options: Vec<_> = segment
427429
.options
428430
.into_iter()
@@ -517,6 +519,11 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
517519
location: *location,
518520
});
519521
}
522+
Pattern::Assign { location, .. } if has_non_utf8_string_option => {
523+
self.error(Error::NonUtf8StringAssignmentInBitArray {
524+
location: *location,
525+
});
526+
}
520527
_ => {}
521528
};
522529

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,3 +3120,8 @@ fn bit_array_using_pattern_variables() {
31203120
fn bit_array_using_pattern_variables_from_other_bit_array() {
31213121
assert_error!("let assert #(<<a>>, <<b:size(a)>>) = #(<<2>>, <<2:2>>)");
31223122
}
3123+
3124+
#[test]
3125+
fn non_utf8_string_assignment() {
3126+
assert_error!(r#"let assert <<"Hello" as message:utf16>> = <<>>"#);
3127+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: compiler-core/src/type_/tests/errors.rs
3+
expression: "let assert <<\"Hello\" as message:utf16>> = <<>>"
4+
---
5+
----- SOURCE CODE
6+
let assert <<"Hello" as message:utf16>> = <<>>
7+
8+
----- ERROR
9+
error: Non UTF-8 string assignment
10+
┌─ /src/one/two.gleam:1:25
11+
12+
1let assert <<"Hello" as message:utf16>> = <<>>
13+
^^^^^^^
14+
15+
This pattern assigns a non UTF-8 string to a variable in a bit array. This
16+
is planned to be supported in the future, but we are unsure of the desired
17+
behaviour. Please go to https://github.com/gleam-lang/gleam/issues/4566 and
18+
explain your usecase for this pattern, and how you would expect it to
19+
behave.

0 commit comments

Comments
 (0)