From a7292f47191f207d337cd35271554587b139c193 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 7 Jul 2025 23:31:32 +0300 Subject: [PATCH] Fix comparison return type in typechecking --- reid/examples/reid/fibonacci.reid | 2 +- reid/src/mir/typecheck.rs | 32 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/reid/examples/reid/fibonacci.reid b/reid/examples/reid/fibonacci.reid index a7f597f..b287c6f 100644 --- a/reid/examples/reid/fibonacci.reid +++ b/reid/examples/reid/fibonacci.reid @@ -1,6 +1,6 @@ // Main fn main() -> i32 { - return (fibonacci(3) < 5) + 5; + return fibonacci(3); } // Fibonacci diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 65aa18a..27d52e5 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -142,12 +142,15 @@ impl Module { let mut scope = Scope::default(); for function in &self.functions { - let r = scope.function_returns.set( - function.name.clone(), - ScopeFunction { - ret: function.return_type, - params: function.parameters.iter().map(|v| v.1).collect(), - }, + state.ok( + scope.function_returns.set( + function.name.clone(), + ScopeFunction { + ret: function.return_type, + params: function.parameters.iter().map(|v| v.1).collect(), + }, + ), + function.signature(), ); } @@ -251,14 +254,14 @@ impl Expression { )) } ExprKind::Literal(literal) => Ok(literal.as_type()), - ExprKind::BinOp(_, lhs, rhs) => { + ExprKind::BinOp(op, lhs, rhs) => { // TODO make sure lhs and rhs can actually do this binary // operation once relevant let lhs_res = lhs.typecheck(state, scope); let rhs_res = rhs.typecheck(state, scope); let lhs_type = state.or_else(lhs_res, Vague(Unknown), lhs.1); let rhs_type = state.or_else(rhs_res, Vague(Unknown), rhs.1); - lhs_type.collapse_into(&rhs_type) + lhs_type.binop_type(op, &rhs_type) } ExprKind::FunctionCall(function_call) => { let true_function = scope @@ -315,6 +318,17 @@ impl TypeKind { fn assert_known(&self) -> Result { self.is_known().map_err(ErrorKind::TypeIsVague) } + + fn binop_type(&self, op: &BinaryOperator, other: &TypeKind) -> Result { + let res = self.collapse_into(other)?; + Ok(match op { + BinaryOperator::Add => res, + BinaryOperator::Minus => res, + BinaryOperator::Mult => res, + BinaryOperator::And => res, + BinaryOperator::Logic(_) => Bool, + }) + } } fn try_collapse(lhs: &TypeKind, rhs: &TypeKind) -> Result { @@ -323,7 +337,7 @@ fn try_collapse(lhs: &TypeKind, rhs: &TypeKind) -> Result { .or(Err(ErrorKind::TypesIncompatible(*lhs, *rhs))) } -trait Collapsable: Sized + Clone { +pub trait Collapsable: Sized + Clone { fn collapse_into(&self, other: &Self) -> Result; }