From 2f0319be25dc6850fc3b2f1191c5af8292c7456a Mon Sep 17 00:00:00 2001 From: mertcandav Date: Sun, 17 Mar 2024 21:11:13 +0300 Subject: [PATCH] compiler: improve --opt-cond --- src/julec/optimizing/scope.jule | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/julec/optimizing/scope.jule b/src/julec/optimizing/scope.jule index 0ee56a768..5f76978f1 100644 --- a/src/julec/optimizing/scope.jule +++ b/src/julec/optimizing/scope.jule @@ -13,6 +13,7 @@ use std::jule::sema::{ Data, Scope, St, + ExprModel, FnCallExprModel, Conditional, If, @@ -327,6 +328,20 @@ fn is_constant_valid_conditional_case(&i: &If): bool { ret false } +fn is_unreachable_expr(&expr: ExprModel): bool { + match type expr { + | &Const: + let c = (&Const)(expr) + ret c.is_bool() && !c.read_bool() + | &BinopExprModel: + let m = (&BinopExprModel)(expr) + if m.op.kind == TokenKind.DblAmper { + ret is_unreachable_expr(m.left.model) || is_unreachable_expr(m.right.model) + } + } + ret false +} + fn is_constant_valid_match_case(&case: &Case): bool { for _, expr in case.exprs { if expr.is_const() && expr.constant.is_bool() && expr.constant.read_bool() { @@ -337,17 +352,12 @@ fn is_constant_valid_match_case(&case: &Case): bool { } fn is_unreachable_conditional_case(&i: &If): bool { - match type i.expr { - | &Const: - let c = (&Const)(i.expr) - ret c.is_bool() && !c.read_bool() - } - ret false + ret is_unreachable_expr(i.expr) } fn is_unreachable_match_case(&case: &Case): bool { for _, expr in case.exprs { - if !expr.is_const() || !expr.constant.is_bool() || expr.constant.read_bool() { + if !is_unreachable_expr(expr.model) { ret false } }