Update where default value is calculated, fix load type

This commit is contained in:
Sofia 2025-07-12 23:59:16 +03:00
parent edb2784f4a
commit e79a0fe458
3 changed files with 17 additions and 8 deletions

View File

@ -251,10 +251,14 @@ impl Builder {
Ok(())
}
Alloca(_, _) => Ok(()),
Load(ptr, _) => {
if let Ok(ty) = ptr.get_type(&self) {
if let Type::Ptr(_) = ty {
Ok(())
Load(ptr, load_ty) => {
if let Ok(ptr_ty) = ptr.get_type(&self) {
if let Type::Ptr(ptr_ty_inner) = ptr_ty {
if *ptr_ty_inner == load_ty {
Ok(())
} else {
Err(())
}
} else {
Err(())
}
@ -332,7 +336,7 @@ impl InstructionValue {
FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret),
Phi(values) => values.first().ok_or(()).and_then(|v| v.get_type(&builder)),
Alloca(_, ty) => Ok(Type::Ptr(Box::new(ty.clone()))),
Load(_, ty) => Ok(Type::Ptr(Box::new(ty.clone()))),
Load(_, ty) => Ok(ty.clone()),
Store(_, value) => value.get_type(builder),
}
}

View File

@ -6,10 +6,11 @@ fn indirection() -> bool {
fn main() -> u16 {
let mut test = 5;
let heehoo = 10;
if indirection() {
test = 11;
}
return test;
return test + heehoo;
}

View File

@ -38,6 +38,8 @@ pub enum ErrorKind {
VariableNotMutable(String),
#[error("Function {0} was given {1} parameters, but {2} were expected")]
InvalidAmountParameters(String, usize, usize),
#[error("Unable to infer type {0}")]
TypeNotInferrable(TypeKind),
}
/// Struct used to implement a type-checking pass that can be performed on the
@ -194,6 +196,10 @@ impl Block {
);
let res_t = if res_t.known().is_err() {
// state.ok::<_, Infallible>(
// Err(ErrorKind::TypeNotInferrable(res_t)),
// variable_reference.2 + expression.1,
// );
// Unable to infer variable type even from expression! Default it
let res_t =
state.or_else(res_t.or_default(), Vague(Unknown), variable_reference.2);
@ -552,8 +558,6 @@ impl Literal {
(L::Vague(VagueL::Number(v)), U32) => L::U32(v as u32),
(L::Vague(VagueL::Number(v)), U64) => L::U64(v as u64),
(L::Vague(VagueL::Number(v)), U128) => L::U128(v as u128),
// Default type for number literal if unable to find true type.
(L::Vague(VagueL::Number(v)), Vague(Number)) => L::I32(v as i32),
(_, Vague(_)) => self,
_ => Err(ErrorKind::LiteralIncompatible(self, hint))?,
})