Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(check): Reject programs which misspecifies the number of patterns #809

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 18 additions & 8 deletions check/src/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ impl<'a> Typecheck<'a> {
| Message(_) => (),
NotAFunction(ref mut typ)
| UndefinedField(ref mut typ, _)
| PatternError(ref mut typ, _)
| PatternError {
constructor_type: ref mut typ,
..
}
| InvalidProjection(ref mut typ)
| TypeConstructorReturnsWrongType {
actual: ref mut typ,
Expand Down Expand Up @@ -1721,16 +1724,23 @@ impl<'a> Typecheck<'a> {
fn typecheck_pattern_rec(
&mut self,
args: &mut [SpannedPattern<Symbol>],
mut typ: &RcType,
typ: &RcType,
) -> TcResult<RcType> {
let len = args.len();
for arg_pattern in args {
match typ.as_function() {
Some((arg, ret)) => {
let pattern_args = args.len();
let mut pattern_iter = args.iter_mut();
let mut type_iter = typ.arg_iter();
loop {
match (pattern_iter.next(), type_iter.next()) {
(Some(arg_pattern), Some(arg)) => {
self.typecheck_pattern(arg_pattern, ModType::wobbly(arg.clone()), arg.clone());
typ = ret;
}
None => return Err(TypeError::PatternError(typ.clone(), len)),
(None, Some(_)) | (Some(_), None) => {
return Err(TypeError::PatternError {
constructor_type: typ.clone(),
pattern_args,
})
}
(None, None) => break,
}
}
Ok(typ.clone())
Expand Down
15 changes: 12 additions & 3 deletions check/src/typecheck/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub enum TypeError<I, T> {
/// Type were expected to have a certain field
UndefinedField(T, I),
/// Constructor type was found in a pattern but did not have the expected number of arguments
PatternError(T, usize),
PatternError {
constructor_type: T,
pattern_args: usize,
},
/// Errors found when trying to unify two types
Unification(T, T, Vec<UnifyTypeError<I, T>>),
/// Error were found when trying to unify the kinds of two types
Expand Down Expand Up @@ -176,8 +179,14 @@ where
}
write!(f, "{}", errors.last().unwrap())
}
PatternError(typ, expected_len) => {
write!(f, "Type {} has {} to few arguments", typ, expected_len)
PatternError { constructor_type, pattern_args } => {
write!(
f,
"Matching on constructor `{}` requires `{}` arguments but the pattern specifies `{}`",
constructor_type,
constructor_type.arg_iter().count(),
pattern_args
)
}
KindError(err) => kindcheck::fmt_kind_error(err, f),
RecursionCheck(err) => write!(f, "{}", err),
Expand Down
11 changes: 11 additions & 0 deletions check/tests/fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,3 +836,14 @@ let alternative : Alternative (Eff (HttpEffect r)) = alt.alternative
"#,
UndefinedField(..)
}

test_check_err! {
issue_807_pattern_match_arg_mismatch,
r#"
type Test = | Test Int

match Test 0 with
| Test -> ()
"#,
PatternError { .. }
}