Skip to content

Commit

Permalink
fix(repl): Don't panic on solo array literals
Browse files Browse the repository at this point in the history
As the repl invokes the typechecker without an expected type any array
in the tail expression would not get a type assigned to them.

Fixes #555
  • Loading branch information
Marwes committed Jun 26, 2018
1 parent b7c88ed commit 0576b11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion check/src/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,8 @@ impl<'a> Typecheck<'a> {
Expr::Array(ref mut array) => {
let mut expected_element_type = self.subs.new_var();

array.typ = self.type_cache.array(expected_element_type.clone());
if let Some(expected_type) = expected_type.take() {
array.typ = self.type_cache.array(expected_element_type.clone());
self.unify_span(expr.span, &expected_type, array.typ.clone());
}

Expand Down
13 changes: 13 additions & 0 deletions check/tests/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,16 @@ A 1

assert!(result.is_ok(), "{}", result.unwrap_err());
}

#[test]
fn array_expr_gets_type_assigned_without_expected_type_issue_555() {
let _ = env_logger::try_init();

let text = r#"
[1]
"#;
let (expr, result) = support::typecheck_expr(text);

assert!(result.is_ok(), "{}", result.unwrap_err());
assert_eq!(expr.env_type_of(&MockEnv::new()).to_string(), "Array Int");
}

0 comments on commit 0576b11

Please sign in to comment.