Skip to content

Commit

Permalink
Merge pull request #154 from baszalmstra/feature/number_type_inferencing
Browse files Browse the repository at this point in the history
feat: implements number type inferencing
  • Loading branch information
baszalmstra authored May 1, 2020
2 parents 8ab15c9 + 286c662 commit 29e3b79
Show file tree
Hide file tree
Showing 13 changed files with 426 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ error: mismatched type
--> main.mun:6:14
|
6 | let b: bool = 22;
| ^^ expected `bool`, found `int`
| ^^ expected `bool`, found `{integer}`
|

2 changes: 1 addition & 1 deletion crates/mun_hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mun_target={path="../mun_target"}
rustc-hash = "1.1"
once_cell = "0.2"
relative-path = "0.4.0"
ena = "0.13"
ena = "0.14"
drop_bomb = "0.1.4"
either = "1.5.3"

Expand Down
1 change: 1 addition & 0 deletions crates/mun_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod resolve;
mod source_id;
mod ty;
mod type_ref;
mod utils;

#[cfg(test)]
mod mock;
Expand Down
53 changes: 49 additions & 4 deletions crates/mun_hir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ mod primitives;
mod resolve;

use crate::display::{HirDisplay, HirFormatter};
use crate::ty::infer::TypeVarId;
use crate::ty::infer::InferTy;
use crate::ty::lower::fn_sig_for_struct_constructor;
use crate::utils::make_mut_slice;
use crate::{HirDatabase, Struct, StructMemoryKind};
pub(crate) use infer::infer_query;
pub use infer::InferenceResult;
pub(crate) use lower::{callable_item_sig, fn_sig_for_fn, type_for_def, CallableDef, TypableDef};
pub use primitives::{FloatTy, IntTy};
pub use resolve::ResolveBitness;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use std::{fmt, mem};

#[cfg(test)]
mod tests;
Expand All @@ -27,7 +29,7 @@ pub enum Ty {
Apply(ApplicationTy),

/// A type variable used during type checking. Not to be confused with a type parameter.
Infer(TypeVarId),
Infer(InferTy),

/// A placeholder for a type which could not be computed; this is propagated to avoid useless
/// error messages. Doubles as a placeholder where type variables are inserted before type
Expand Down Expand Up @@ -167,6 +169,20 @@ impl Substs {
}
}

impl Deref for Substs {
type Target = [Ty];

fn deref(&self) -> &[Ty] {
&self.0
}
}

impl DerefMut for Substs {
fn deref_mut(&mut self) -> &mut [Ty] {
make_mut_slice(&mut self.0)
}
}

/// A function signature as seen by type inference: Several parameter types and
/// one return type.
#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -208,7 +224,11 @@ impl HirDisplay for Ty {
Ty::Apply(a_ty) => a_ty.hir_fmt(f),
Ty::Unknown => write!(f, "{{unknown}}"),
Ty::Empty => write!(f, "nothing"),
Ty::Infer(tv) => write!(f, "'{}", tv.0),
Ty::Infer(tv) => match tv {
InferTy::TypeVar(tv) => write!(f, "'{}", tv.0),
InferTy::IntVar(_) => write!(f, "{{integer}}"),
InferTy::FloatVar(_) => write!(f, "{{float}}"),
},
}
}
}
Expand Down Expand Up @@ -246,3 +266,28 @@ impl HirDisplay for &Ty {
HirDisplay::hir_fmt(*self, f)
}
}

impl Ty {
fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
match self {
Ty::Apply(ty) => {
for t in ty.parameters.iter_mut() {
t.walk_mut(f);
}
}
Ty::Empty | Ty::Infer(_) | Ty::Unknown => {}
}
f(self)
}

fn fold(mut self, f: &mut impl FnMut(Ty) -> Ty) -> Self
where
Self: Sized,
{
self.walk_mut(&mut |ty_mut| {
let ty = mem::replace(ty_mut, Ty::Unknown);
*ty_mut = f(ty);
});
self
}
}
Loading

0 comments on commit 29e3b79

Please sign in to comment.