@@ -1537,24 +1537,44 @@ TypeCheckExpr::visit (HIR::LoopExpr &expr)
15371537 : TyTy::TupleType::get_unit_type ();
15381538}
15391539
1540- void
1541- TypeCheckExpr::visit (HIR::WhileLoopExpr &expr)
1542- {
1543- context->push_new_while_loop_context (expr.get_mappings ().get_hirid ());
1544-
1545- TypeCheckExpr::Resolve (expr.get_predicate_expr ());
1546- TyTy::BaseType *block_expr = TypeCheckExpr::Resolve (expr.get_loop_block ());
1547-
1548- if (!block_expr->is_unit ())
1549- {
1550- rust_error_at (expr.get_loop_block ().get_locus (),
1551- " expected %<()%> got %s" ,
1552- block_expr->as_string ().c_str ());
1540+ void TypeCheckExpr::visit (HIR::WhileLoopExpr &expr) {
1541+ context->push_new_while_loop_context (expr.get_mappings ().get_hirid ());
1542+ TyTy::BaseType *predicate_type =
1543+ TypeCheckExpr::Resolve (expr.get_predicate_expr ());
1544+ if (predicate_type->get_kind () == TyTy::TypeKind::ERROR) {
1545+ infered = TyTy::TupleType::get_unit_type ();
1546+ context->pop_loop_context ();
15531547 return ;
1554- }
1555-
1556- context->pop_loop_context ();
1557- infered = TyTy::TupleType::get_unit_type ();
1548+ }
1549+ if (predicate_type->get_kind () == TyTy::TypeKind::NEVER) {
1550+ rust_error_at (expr.get_predicate_expr ().get_locus (),
1551+ " expected boolean expression in %<while%> condition" );
1552+ infered = TyTy::TupleType::get_unit_type ();
1553+ context->pop_loop_context ();
1554+ return ;
1555+ }
1556+ if (predicate_type->get_kind () != TyTy::TypeKind::BOOL) {
1557+ rust_error_at (expr.get_predicate_expr ().get_locus (),
1558+ " expected boolean expression in %<while%> condition" );
1559+ infered = TyTy::TupleType::get_unit_type ();
1560+ context->pop_loop_context ();
1561+ return ;
1562+ }
1563+ TyTy::BaseType *block_expr = TypeCheckExpr::Resolve (expr.get_loop_block ());
1564+ if (!block_expr->is_unit ()) {
1565+ rust_error_at (expr.get_loop_block ().get_locus (), " expected %<()%> got %s" ,
1566+ block_expr->as_string ().c_str ());
1567+ infered = TyTy::TupleType::get_unit_type ();
1568+ context->pop_loop_context ();
1569+ return ;
1570+ }
1571+ if (block_expr->get_kind () == TyTy::TypeKind::ERROR) {
1572+ infered = TyTy::TupleType::get_unit_type ();
1573+ context->pop_loop_context ();
1574+ return ;
1575+ }
1576+ context->pop_loop_context ();
1577+ infered = TyTy::TupleType::get_unit_type ();
15581578}
15591579
15601580void
0 commit comments