Skip to content

Commit

Permalink
feat(base): Use quick-error for instantiate::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jul 24, 2016
1 parent 2a29950 commit 96a8c63
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ documentation = "https://marwes.github.io/gluon/gluon/index.html"

[dependencies]
log = "0.3.6"
quick-error = "1.0.0"
19 changes: 14 additions & 5 deletions base/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ use types;
use types::{AliasData, BuiltinType, Type, Generic, TcType, TypeEnv, merge};
use symbol::Symbol;

pub enum Error<I> {
SelfRecursive(I),
UndefinedType(I),
quick_error! {
#[derive(Debug)]
pub enum Error {
SelfRecursive(id: Symbol) {
description("self recursive")
display("The use of self recursion in type `{}` could not be unified.", id)
}
UndefinedType(id: Symbol) {
description("undefined type")
display("Type `{}` does not exist.", id)
}
}
}

/// Removes type aliases from `typ` until it is an actual type
Expand All @@ -36,7 +45,7 @@ pub fn remove_aliases_cow<'t>(env: &TypeEnv, typ: &'t TcType) -> Cow<'t, TcType>
pub fn remove_aliases_checked(reduced_aliases: &mut Vec<Symbol>,
env: &TypeEnv,
typ: &TcType)
-> Result<Option<TcType>, Error<Symbol>> {
-> Result<Option<TcType>, Error> {
if let Some((alias_id, _)) = typ.as_alias() {
if reduced_aliases.iter().any(|name| name == alias_id) {
return Err(Error::SelfRecursive(alias_id.clone()));
Expand Down Expand Up @@ -66,7 +75,7 @@ pub fn remove_alias(env: &TypeEnv, typ: TcType) -> TcType {
maybe_remove_alias(env, &typ).unwrap_or(None).unwrap_or(typ)
}

pub fn maybe_remove_alias(env: &TypeEnv, typ: &TcType) -> Result<Option<TcType>, Error<Symbol>> {
pub fn maybe_remove_alias(env: &TypeEnv, typ: &TcType) -> Result<Option<TcType>, Error> {
let maybe_alias = match **typ {
Type::Alias(ref alias) if alias.args.is_empty() => Some(alias),
Type::App(ref alias, ref args) => {
Expand Down
2 changes: 2 additions & 0 deletions base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#[macro_use]
extern crate log;
#[macro_use]
extern crate quick_error;

pub mod ast;
pub mod fixed;
Expand Down
4 changes: 2 additions & 2 deletions check/src/unify_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub enum TypeError<I> {
SelfRecursive(I),
}

impl<I> From<instantiate::Error<I>> for Error<I> {
fn from(error: instantiate::Error<I>) -> Error<I> {
impl From<instantiate::Error> for Error<Symbol> {
fn from(error: instantiate::Error) -> Error<Symbol> {
UnifyError::Other(match error {
instantiate::Error::UndefinedType(id) => TypeError::UndefinedType(id),
instantiate::Error::SelfRecursive(id) => TypeError::SelfRecursive(id),
Expand Down

0 comments on commit 96a8c63

Please sign in to comment.