+8
-5
compiler-core/src/type_/pattern.rs
+8
-5
compiler-core/src/type_/pattern.rs
···
926
926
}
927
927
};
928
928
929
+
let mut incorrect_arity_error = false;
929
930
match constructor.field_map() {
930
931
// The fun has a field map so labelled arguments may be present and need to be reordered.
931
932
Some(field_map) => {
···
1020
1021
location,
1021
1022
IncorrectArityContext::Pattern,
1022
1023
) {
1023
-
{
1024
-
self.problems.error(error);
1025
-
self.error_encountered = true;
1026
-
};
1024
+
incorrect_arity_error = true;
1025
+
self.problems.error(error);
1026
+
self.error_encountered = true;
1027
1027
}
1028
1028
}
1029
1029
···
1132
1132
self.set_subject_variable_variant(variable_to_infer, inferred_variant);
1133
1133
}
1134
1134
1135
-
if args.len() != pattern_args.len() {
1135
+
// We're emitting the incorrect arity error only if we haven't emitted
1136
+
// one already. This might happen when we can't reorder the field map
1137
+
// of a constructor because there's not enough labels.
1138
+
if args.len() != pattern_args.len() && !incorrect_arity_error {
1136
1139
self.error(Error::IncorrectArity {
1137
1140
labels: vec![],
1138
1141
location,
+16
compiler-core/src/type_/tests/errors.rs
+16
compiler-core/src/type_/tests/errors.rs
···
3192
3192
"
3193
3193
);
3194
3194
}
3195
+
3196
+
// https://github.com/gleam-lang/gleam/issues/4693
3197
+
#[test]
3198
+
fn pattern_with_incorrect_arity() {
3199
+
assert_module_error!(
3200
+
"
3201
+
pub type Pokemon { Pokemon(name: String, id: Int) }
3202
+
3203
+
pub fn main() {
3204
+
case todo {
3205
+
Pokemon(name:) -> todo
3206
+
}
3207
+
}
3208
+
"
3209
+
);
3210
+
}
+25
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__pattern_with_incorrect_arity.snap
+25
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__pattern_with_incorrect_arity.snap
···
1
+
---
2
+
source: compiler-core/src/type_/tests/errors.rs
3
+
expression: "\npub type Pokemon { Pokemon(name: String, id: Int) }\n\npub fn main() {\n case todo {\n Pokemon(name:) -> todo\n }\n}\n"
4
+
---
5
+
----- SOURCE CODE
6
+
7
+
pub type Pokemon { Pokemon(name: String, id: Int) }
8
+
9
+
pub fn main() {
10
+
case todo {
11
+
Pokemon(name:) -> todo
12
+
}
13
+
}
14
+
15
+
16
+
----- ERROR
17
+
error: Incorrect arity
18
+
┌─ /src/one/two.gleam:6:5
19
+
│
20
+
6 │ Pokemon(name:) -> todo
21
+
│ ^^^^^^^^^^^^^^ Expected 2 arguments, got 1
22
+
23
+
This pattern accepts these additional labelled arguments:
24
+
25
+
- id