Fix default for arrays

This commit is contained in:
Sofia 2025-07-23 23:11:20 +03:00
parent f6ed39d4e5
commit 7027ee3645
2 changed files with 21 additions and 7 deletions

View File

@ -596,15 +596,25 @@ impl TypeKind {
/// Try to collapse a type on itself producing a default type if one exists,
/// Error if not.
pub fn or_default(&self) -> Result<TypeKind, ErrorKind> {
match self {
Ok(match self {
TypeKind::Vague(vague_type) => match &vague_type {
Vague::Unknown => Err(ErrorKind::TypeIsVague(*vague_type)),
Vague::Integer => Ok(TypeKind::I32),
Vague::Unknown => Err(ErrorKind::TypeIsVague(*vague_type))?,
Vague::Integer => TypeKind::I32,
Vague::TypeRef(_) => panic!("Hinted default!"),
VagueType::Decimal => Ok(TypeKind::F32),
VagueType::Decimal => TypeKind::F32,
},
_ => Ok(self.clone()),
}
TypeKind::Array(type_kind, len) => {
TypeKind::Array(Box::new(type_kind.or_default()?), *len)
}
TypeKind::Borrow(type_kind, mutable) => {
TypeKind::Borrow(Box::new(type_kind.or_default()?), *mutable)
}
TypeKind::UserPtr(type_kind) => TypeKind::UserPtr(Box::new(type_kind.or_default()?)),
TypeKind::CodegenPtr(type_kind) => {
TypeKind::CodegenPtr(Box::new(type_kind.or_default()?))
}
_ => self.clone(),
})
}
pub fn resolve_weak(&self, refs: &TypeRefs) -> TypeKind {

View File

@ -222,7 +222,11 @@ impl Block {
let ret = match &mut statement.0 {
StmtKind::Let(variable_reference, mutable, expression) => {
// Resolve possible hint in var reference
let var_t_resolved = variable_reference.0.resolve_ref(&typerefs);
let var_t_resolved = state.or_else(
variable_reference.0.resolve_ref(&typerefs).or_default(),
TypeKind::Vague(VagueType::Unknown),
variable_reference.2,
);
// Typecheck (and coerce) expression with said type
let res = expression.typecheck(&mut state, &typerefs, Some(&var_t_resolved));