Skip to content

Commit

Permalink
Merge pull request #68 from Marwes/refactor_check
Browse files Browse the repository at this point in the history
Refactor parts of the check crate
  • Loading branch information
Marwes authored Jul 26, 2016
2 parents feb5ea2 + 89ab4ee commit e4f6f66
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 324 deletions.
23 changes: 12 additions & 11 deletions base/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Module containing types representing `gluon`'s type system
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::fmt;
use std::ops::Deref;
Expand Down Expand Up @@ -923,20 +924,20 @@ pub fn walk_move_type<F, I, T>(typ: T, f: &mut F) -> T

/// Merges two values using `f` if either or both them is `Some(..)`.
/// If both are `None`, `None` is returned.
pub fn merge<F, A, B, R>(a_original: &A,
a: Option<A>,
b_original: &B,
b: Option<B>,
f: F)
-> Option<R>
where A: Clone,
B: Clone,
F: FnOnce(A, B) -> R
pub fn merge<F, A: ?Sized, B: ?Sized, R>(a_original: &A,
a: Option<A::Owned>,
b_original: &B,
b: Option<B::Owned>,
f: F)
-> Option<R>
where A: ToOwned,
B: ToOwned,
F: FnOnce(A::Owned, B::Owned) -> R
{
match (a, b) {
(Some(a), Some(b)) => Some(f(a, b)),
(Some(a), None) => Some(f(a, b_original.clone())),
(None, Some(b)) => Some(f(a_original.clone(), b)),
(Some(a), None) => Some(f(a, b_original.to_owned())),
(None, Some(b)) => Some(f(a_original.to_owned(), b)),
(None, None) => None,
}
}
Expand Down
61 changes: 25 additions & 36 deletions check/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ impl OnFound for Suggest {

fn on_pattern(&mut self, pattern: &ast::LPattern<ast::TcIdent<Symbol>>) {
match pattern.value {
ast::Pattern::Record { ref id, ref fields, .. } => {
match *instantiate::remove_aliases(&(), id.typ.clone()) {
Type::Record { fields: ref field_types, .. } => {
for (field, field_type) in fields.iter().zip(field_types) {
let f = field.1.as_ref().unwrap_or(&field.0).clone();
self.stack.insert(f, field_type.typ.clone());
}
ast::Pattern::Record { ref id, fields: ref field_ids, .. } => {
let unaliased = instantiate::remove_aliases(&(), id.typ.clone());
if let Type::Record { ref fields, .. } = *unaliased {
for (field, field_type) in field_ids.iter().zip(fields) {
let f = field.1.as_ref().unwrap_or(&field.0).clone();
self.stack.insert(f, field_type.typ.clone());
}
_ => (),
}
}
ast::Pattern::Identifier(ref id) => {
Expand All @@ -71,41 +69,32 @@ impl OnFound for Suggest {
}

fn expr(&mut self, expr: &ast::LExpr<ast::TcIdent<Symbol>>) {
match expr.value {
ast::Expr::Identifier(ref ident) => {
for (k, typ) in self.stack.iter() {
if k.declared_name().starts_with(ident.name.declared_name()) {
self.result.push(ast::TcIdent {
name: k.clone(),
typ: typ.clone(),
});
}
if let ast::Expr::Identifier(ref ident) = expr.value {
for (k, typ) in self.stack.iter() {
if k.declared_name().starts_with(ident.name.declared_name()) {
self.result.push(ast::TcIdent {
name: k.clone(),
typ: typ.clone(),
});
}
}
_ => (),
}
}

fn ident(&mut self, context: &ast::LExpr<ast::TcIdent<Symbol>>, ident: &ast::TcIdent<Symbol>) {
match context.value {
ast::Expr::FieldAccess(ref expr, _) => {
let typ = expr.type_of();
match *instantiate::remove_aliases(&(), typ) {
Type::Record { ref fields, .. } => {
let id = ident.name.as_ref();
for field in fields {
if field.name.as_ref().starts_with(id) {
self.result.push(ast::TcIdent {
name: field.name.clone(),
typ: field.typ.clone(),
});
}
}
if let ast::Expr::FieldAccess(ref expr, _) = context.value {
let typ = expr.type_of();
if let Type::Record { ref fields, .. } = *instantiate::remove_aliases(&(), typ) {
let id = ident.name.as_ref();
for field in fields {
if field.name.as_ref().starts_with(id) {
self.result.push(ast::TcIdent {
name: field.name.clone(),
typ: field.typ.clone(),
});
}
_ => (),
}
}
_ => (),
}
}
}
Expand Down Expand Up @@ -189,7 +178,7 @@ impl<'a, F> FindVisitor<'a, F>
match self.select_spanned(bindings, |b| b.expression.span(self.env)) {
(false, Some(bind)) => {
for arg in &bind.arguments {
self.on_found.on_ident(&arg);
self.on_found.on_ident(arg);
}
self.visit_expr(&bind.expression)
}
Expand All @@ -213,7 +202,7 @@ impl<'a, F> FindVisitor<'a, F>
}
Lambda(ref lambda) => {
for arg in &lambda.arguments {
self.on_found.on_ident(&arg);
self.on_found.on_ident(arg);
}
self.visit_expr(&lambda.body)
}
Expand Down
10 changes: 3 additions & 7 deletions check/src/kindcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'a> KindCheck<'a> {
})
.or_else(|| self.info.find_kind(id))
.map_or_else(|| {
let id_str = self.idents.string(&id);
let id_str = self.idents.string(id);
if id_str.chars().next().map_or(false, |c| c.is_uppercase()) {
Err(UnifyError::Other(KindError::UndefinedType(id.clone())))
} else {
Expand Down Expand Up @@ -146,12 +146,8 @@ impl<'a> KindCheck<'a> {

fn builtin_kind(&self, typ: BuiltinType) -> RcKind {
match typ {
BuiltinType::String |
BuiltinType::Byte |
BuiltinType::Char |
BuiltinType::Int |
BuiltinType::Float |
BuiltinType::Unit => self.type_kind(),
BuiltinType::String | BuiltinType::Byte | BuiltinType::Char | BuiltinType::Int |
BuiltinType::Float | BuiltinType::Unit => self.type_kind(),
BuiltinType::Array => self.function1_kind(),
BuiltinType::Function => self.function2_kind(),
}
Expand Down
4 changes: 2 additions & 2 deletions check/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ pub fn rename(symbols: &mut SymbolModule,
let locals = self.env
.stack
.get_all(id);
let global = self.env.env.find_type(&id).map(|typ| (id, None, typ));
let global = self.env.env.find_type(id).map(|typ| (id, None, typ));
let candidates = || {
locals.iter()
.flat_map(|bindings| {
bindings.iter().rev().map(|bind| (&bind.0, Some(&bind.1), &bind.2))
})
.chain(global.clone())
.chain(global)
};
// If there is a single binding (or no binding in case of primitives such as #Int+)
// there is no need to check for equivalency as typechecker couldnt have infered a
Expand Down
Loading

0 comments on commit e4f6f66

Please sign in to comment.