Skip to content

Commit 00abee8

Browse files
authored
fix(compiler): Properly apply well-formedness checks on patterns (#1989)
1 parent 8a335ae commit 00abee8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

compiler/src/parsing/well_formedness.re

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,23 @@ let malformed_strings = (errs, super) => {
131131
super.enter_expression(e);
132132
};
133133

134+
let enter_pattern = ({ppat_desc: desc, ppat_loc: loc} as p) => {
135+
switch (desc) {
136+
| PPatConstant(PConstString(s)) =>
137+
if (!Utf8.validString(s)) {
138+
errs := [MalformedString(loc), ...errs^];
139+
}
140+
| _ => ()
141+
};
142+
super.enter_pattern(p);
143+
};
144+
134145
{
135146
errs,
136147
iter_hooks: {
137148
...super,
138149
enter_expression,
150+
enter_pattern,
139151
},
140152
};
141153
};
@@ -157,11 +169,28 @@ let malformed_characters = (errs, super) => {
157169
super.enter_expression(e);
158170
};
159171

172+
let enter_pattern = ({ppat_desc: desc, ppat_loc: loc} as p) => {
173+
switch (desc) {
174+
| PPatConstant(PConstChar(c)) =>
175+
switch (
176+
String_utils.Utf8.utf_length_at_offset(c, 0) == String.length(c)
177+
) {
178+
| true => ()
179+
| false
180+
| exception (Invalid_argument(_)) =>
181+
errs := [IllegalCharacterLiteral(c, loc), ...errs^]
182+
}
183+
| _ => ()
184+
};
185+
super.enter_pattern(p);
186+
};
187+
160188
{
161189
errs,
162190
iter_hooks: {
163191
...super,
164192
enter_expression,
193+
enter_pattern,
165194
},
166195
};
167196
};

compiler/test/suites/chars.re

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ Did you mean to create the string "\{\\"test\\": 1\}" instead?|},
8484
"''",
8585
"This character literal contains no character. Did you mean to create an empty string \"\" instead?",
8686
);
87+
assertCompileError(
88+
"char_illegal_pattern",
89+
"match ('a') { 'abc' => void, _ => void }",
90+
"This character literal contains multiple characters: 'abc'\nDid you mean to create the string \"abc\" instead?",
91+
);
8792
assertCompileError(
8893
"unicode_err1",
8994
"let x = '\\u{d800}'",

0 commit comments

Comments
 (0)