Skip to content

Commit

Permalink
fix(check): Don't panic if a type is undefined in a variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Dec 19, 2017
1 parent 923f5cc commit e476b1e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 39 deletions.
2 changes: 2 additions & 0 deletions base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#[macro_use]
extern crate collect_mac;
extern crate itertools;
#[macro_use]
extern crate log;
extern crate pretty;
#[macro_use]
extern crate quick_error;
Expand Down
1 change: 0 additions & 1 deletion base/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[macro_export]
macro_rules! ice {
() => ({
Expand Down
4 changes: 1 addition & 3 deletions base/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ where
Ok(Some(alias)) if !canonical(alias) => alias
.typ()
.apply_args(&typ.unapplied_args())
.map(|typ| {
Cow::Owned(canonical_alias(env, &typ, canonical).into_owned())
})
.map(|typ| Cow::Owned(canonical_alias(env, &typ, canonical).into_owned()))
.unwrap_or(Cow::Borrowed(typ)),
_ => Cow::Borrowed(typ),
}
Expand Down
74 changes: 39 additions & 35 deletions base/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,18 @@ where
Type::Ident(ref id) => {
// Replace `Ident` with the alias it resolves to so that a `TypeEnv` is not
// needed to resolve the type later on
let index = self.group
.iter()
.position(|alias| alias.name == *id)
.expect("ICE: Alias group were not able to resolve an identifier");
Some(T::from(Type::Alias(AliasRef {
index: index,
group: self.group.clone(),
})))
let replacement = self.group.iter().position(|alias| alias.name == *id).map(
|index| {
T::from(Type::Alias(AliasRef {
index: index,
group: self.group.clone(),
}))
},
);
if replacement.is_none() {
info!("Alias group were not able to resolve an identifier");
}
replacement
}
_ => None,
}
Expand Down Expand Up @@ -1885,15 +1889,15 @@ where
}
f.walk(rest);
}
Type::Hole |
Type::Opaque |
Type::Builtin(_) |
Type::Variable(_) |
Type::Generic(_) |
Type::Skolem(_) |
Type::Ident(_) |
Type::Alias(_) |
Type::EmptyRow => (),
Type::Hole
| Type::Opaque
| Type::Builtin(_)
| Type::Variable(_)
| Type::Generic(_)
| Type::Skolem(_)
| Type::Ident(_)
| Type::Alias(_)
| Type::EmptyRow => (),
}
}

Expand Down Expand Up @@ -1922,15 +1926,15 @@ where
}
f.walk_mut(rest);
}
Type::Hole |
Type::Opaque |
Type::Builtin(_) |
Type::Variable(_) |
Type::Generic(_) |
Type::Skolem(_) |
Type::Ident(_) |
Type::Alias(_) |
Type::EmptyRow => (),
Type::Hole
| Type::Opaque
| Type::Builtin(_)
| Type::Variable(_)
| Type::Generic(_)
| Type::Skolem(_)
| Type::Ident(_)
| Type::Alias(_)
| Type::EmptyRow => (),
}
}

Expand Down Expand Up @@ -2077,15 +2081,15 @@ where
Type::extend_row(types.clone(), fields, rest)
})
}
Type::Hole |
Type::Opaque |
Type::Builtin(_) |
Type::Variable(_) |
Type::Skolem(_) |
Type::Generic(_) |
Type::Ident(_) |
Type::Alias(_) |
Type::EmptyRow => None,
Type::Hole
| Type::Opaque
| Type::Builtin(_)
| Type::Variable(_)
| Type::Skolem(_)
| Type::Generic(_)
| Type::Ident(_)
| Type::Alias(_)
| Type::EmptyRow => None,
}
}

Expand Down
13 changes: 13 additions & 0 deletions check/tests/fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,16 @@ do x = 1

assert_unify_err!(result, TypeMismatch(..));
}

#[test]
fn undefined_type_in_variant() {
let _ = ::env_logger::init();

let text = r#"
type Test = | Test In
2
"#;
let result = support::typecheck(text);

assert_err!(result, UndefinedType(..));
}

0 comments on commit e476b1e

Please sign in to comment.