-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ty_check: make
expr
module to isolate logics for expression type ch…
…ecking
- Loading branch information
Showing
5 changed files
with
175 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use hir::hir_def::{Expr, ExprId, Partial}; | ||
|
||
use crate::ty::{ | ||
ty_check::TyChecker, | ||
ty_def::{InvalidCause, TyId}, | ||
}; | ||
|
||
impl<'db> TyChecker<'db> { | ||
pub(super) fn check_expr(&mut self, expr: ExprId, expected: TyId) -> TyId { | ||
let Partial::Present(expr_data) = self.env.expr_data(expr) else { | ||
let ty = TyId::invalid(self.db, InvalidCause::Other); | ||
self.env.type_expr(expr, ty); | ||
return ty; | ||
}; | ||
|
||
let actual = match expr_data { | ||
Expr::Lit(lit) => self.lit_ty(lit), | ||
Expr::Block(..) => self.check_block(expr, expr_data, expected), | ||
|
||
Expr::Bin(..) => todo!(), | ||
Expr::Un(..) => todo!(), | ||
Expr::Call(..) => todo!(), | ||
Expr::MethodCall(..) => todo!(), | ||
Expr::Path(..) => todo!(), | ||
Expr::RecordInit(..) => todo!(), | ||
Expr::Field(..) => todo!(), | ||
Expr::Tuple(..) => todo!(), | ||
Expr::Index(..) => todo!(), | ||
Expr::Array(..) => todo!(), | ||
Expr::ArrayRep(..) => todo!(), | ||
Expr::If(..) => todo!(), | ||
Expr::Match(..) => todo!(), | ||
Expr::Assign(..) => todo!(), | ||
Expr::AugAssign(..) => todo!(), | ||
}; | ||
|
||
self.unify_ty(expr, actual, expected) | ||
} | ||
|
||
fn check_block(&mut self, expr: ExprId, expr_data: &Expr, expected: TyId) -> TyId { | ||
let Expr::Block(stmts) = expr_data else { | ||
unreachable!() | ||
}; | ||
|
||
if stmts.is_empty() { | ||
TyId::unit(self.db) | ||
} else { | ||
self.env.enter_block(expr).ok(); | ||
for &stmt in stmts[..stmts.len() - 1].iter() { | ||
self.check_stmt(stmt, TyId::bot(self.db)); | ||
} | ||
|
||
let last_stmt = stmts[stmts.len() - 1]; | ||
let res = self.check_stmt(last_stmt, expected); | ||
self.env.leave_block(); | ||
res | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.