Nearly get typechecking working for deref/borrow
This commit is contained in:
parent
62d73b19a2
commit
47fa5f342f
@ -311,13 +311,3 @@ fn write_access(f: &mut std::fmt::Formatter<'_>, name: &String) -> std::fmt::Res
|
|||||||
f.write_char('.')?;
|
f.write_char('.')?;
|
||||||
Display::fmt(name, f)
|
Display::fmt(name, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for VagueType {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
VagueType::Unknown => write!(f, "Unknown"),
|
|
||||||
VagueType::Number => write!(f, "Number"),
|
|
||||||
VagueType::TypeRef(_) => write!(f, "{{unknown}}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -339,6 +339,7 @@ impl TypeKind {
|
|||||||
let resolved = self.resolve_weak(refs);
|
let resolved = self.resolve_weak(refs);
|
||||||
match resolved {
|
match resolved {
|
||||||
TypeKind::Array(t, len) => TypeKind::Array(Box::new(t.resolve_ref(refs)), len),
|
TypeKind::Array(t, len) => TypeKind::Array(Box::new(t.resolve_ref(refs)), len),
|
||||||
|
TypeKind::Borrow(inner) => TypeKind::Borrow(Box::new(inner.resolve_ref(refs))),
|
||||||
_ => resolved,
|
_ => resolved,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,18 +109,21 @@ pub enum TypeKind {
|
|||||||
Array(Box<TypeKind>, u64),
|
Array(Box<TypeKind>, u64),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
CustomType(String),
|
CustomType(String),
|
||||||
#[error("Ptr({0})")]
|
#[error("Borrow({0})")]
|
||||||
Borrow(Box<TypeKind>),
|
Borrow(Box<TypeKind>),
|
||||||
#[error("Ptr({0})")]
|
#[error("Ptr({0})")]
|
||||||
Ptr(Box<TypeKind>),
|
Ptr(Box<TypeKind>),
|
||||||
#[error("{0}")]
|
#[error(transparent)]
|
||||||
Vague(VagueType),
|
Vague(#[from] VagueType),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error, PartialOrd, Ord, Hash)]
|
||||||
pub enum VagueType {
|
pub enum VagueType {
|
||||||
|
#[error("Unknown")]
|
||||||
Unknown,
|
Unknown,
|
||||||
|
#[error("Number")]
|
||||||
Number,
|
Number,
|
||||||
|
#[error("TypeRef({0})")]
|
||||||
TypeRef(usize),
|
TypeRef(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -669,16 +669,15 @@ impl Expression {
|
|||||||
.resolve_ref(typerefs);
|
.resolve_ref(typerefs);
|
||||||
|
|
||||||
// Update typing to be more accurate
|
// Update typing to be more accurate
|
||||||
var_ref.0 = state.or_else(
|
let TypeKind::Borrow(inner) = state.or_else(
|
||||||
var_ref.0.resolve_ref(typerefs).collapse_into(&existing),
|
var_ref.0.resolve_ref(typerefs).collapse_into(&existing),
|
||||||
TypeKind::Vague(Vague::Unknown),
|
TypeKind::Vague(Vague::Unknown),
|
||||||
var_ref.2,
|
var_ref.2,
|
||||||
);
|
) else {
|
||||||
|
return Err(ErrorKind::AttemptedDerefNonBorrow(var_ref.1.clone()));
|
||||||
|
};
|
||||||
|
|
||||||
match &var_ref.0 {
|
Ok(*inner)
|
||||||
TypeKind::Borrow(type_kind) => Ok(*type_kind.clone()),
|
|
||||||
_ => Err(ErrorKind::AttemptedDerefNonBorrow(var_ref.1.clone())),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -760,6 +759,9 @@ impl Collapsable for TypeKind {
|
|||||||
(TypeKind::Vague(Vague::Unknown), other) | (other, TypeKind::Vague(Vague::Unknown)) => {
|
(TypeKind::Vague(Vague::Unknown), other) | (other, TypeKind::Vague(Vague::Unknown)) => {
|
||||||
Ok(other.clone())
|
Ok(other.clone())
|
||||||
}
|
}
|
||||||
|
(TypeKind::Borrow(val1), TypeKind::Borrow(val2)) => {
|
||||||
|
Ok(TypeKind::Borrow(Box::new(val1.collapse_into(val2)?)))
|
||||||
|
}
|
||||||
_ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())),
|
_ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,6 +163,11 @@ impl<'outer> ScopeTypeRefs<'outer> {
|
|||||||
self.types
|
self.types
|
||||||
.new(&TypeKind::Array(Box::new(elem_ty.as_type()), *length))
|
.new(&TypeKind::Array(Box::new(elem_ty.as_type()), *length))
|
||||||
}
|
}
|
||||||
|
TypeKind::Borrow(ty) => {
|
||||||
|
let inner_ty = self.from_type(ty)?;
|
||||||
|
self.types
|
||||||
|
.new(&&TypeKind::Borrow(Box::new(inner_ty.as_type())))
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(ty_ref) = self.types.find(ty) {
|
if let Some(ty_ref) = self.types.find(ty) {
|
||||||
ty_ref
|
ty_ref
|
||||||
|
@ -7,5 +7,5 @@ fn main() -> u32 {
|
|||||||
let mut borrow = &value;
|
let mut borrow = &value;
|
||||||
*borrow = 17;
|
*borrow = 17;
|
||||||
|
|
||||||
return borrow;
|
return *borrow;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user