-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for logical operators. #3323
Conversation
27c39c8
to
83dd5f4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 10 files at r1, all commit messages.
Reviewable status: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)
crates/cairo-lang-lowering/src/lower/mod.rs
line 455 at r1 (raw file):
semantic::Expr::Desnap(expr) => lower_expr_desnap(ctx, expr, builder), semantic::Expr::Assignment(expr) => lower_expr_assignment(ctx, expr, builder), semantic::Expr::BinaryOperator(expr) => lower_binary_op(ctx, builder, expr),
Isn't it kinda wierd that binary op is only && and ||?
crates/cairo-lang-semantic/src/expr/compute.rs
line 379 at r1 (raw file):
let bool_ty = core_bool_ty(db); if lexpr.expr.ty() != bool_ty {
If we always expect bool, conform_ty instead (look at if lowering)
crates/cairo-lang-semantic/src/expr/compute.rs
line 386 at r1 (raw file):
} if rexpr.expr.ty() != bool_ty {
Here as well
Previously, orizi wrote…
I can rename it, but I think (and shachar agreed) that long term we want to use it for all binary ops. |
Previously, ilyalesokhin-starkware wrote…
Though I'm not sure how it will work with the inference in the semantic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @orizi and @spapinistarkware)
crates/cairo-lang-lowering/src/lower/mod.rs
line 455 at r1 (raw file):
Previously, ilyalesokhin-starkware wrote…
Though I'm not sure how it will work with the inference in the semantic.
Do you want me to rename it to LogicalOperator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)
crates/cairo-lang-lowering/src/lower/mod.rs
line 455 at r1 (raw file):
Previously, ilyalesokhin-starkware wrote…
Do you want me to rename it to LogicalOperator?
i think we should rename - we might want to add the Call
function some information about this being an operator for better diagnostics - but && and || are very different from other operators IMO (as these actually control the flow of the run)
83dd5f4
to
2d882f3
Compare
Previously, orizi wrote…
Done, thanks. |
2d882f3
to
f41ba5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @orizi and @spapinistarkware)
crates/cairo-lang-lowering/src/lower/mod.rs
line 455 at r1 (raw file):
Previously, orizi wrote…
i think we should rename - we might want to add the
Call
function some information about this being an operator for better diagnostics - but && and || are very different from other operators IMO (as these actually control the flow of the run)
Done.
crates/cairo-lang-semantic/src/expr/compute.rs
line 386 at r1 (raw file):
Previously, orizi wrote…
Here as well
Done.
17e1c0c
to
fb40b13
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 10 files at r1, 1 of 2 files at r2, 1 of 7 files at r3, 1 of 5 files at r4.
Reviewable status: 5 of 11 files reviewed, 6 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)
crates/cairo-lang-semantic/src/expr/objects.rs
line 356 at r4 (raw file):
pub op: LogicalOperator, pub rhs: semantic::ExprId, pub ty: semantic::TypeId,
Suggestion:
// ExprLogicalOperator is always of bool type.
pub ty: semantic::TypeId,
crates/cairo-lang-semantic/src/expr/test_data/logical_operator
line 41 at r2 (raw file):
//! > function fn foo(a: u128, b: bool, c: u128) -> bool { bar(a.into(), b, c.into());
why the into
s?
Code quote:
bar(a.into(), b, c.into());
crates/cairo-lang-lowering/src/diagnostic.rs
line 122 at r2 (raw file):
UnsupportedMatchArmNotAVariant, UnsupportedMatchArmOutOfOrder, LogicalOperatorsNotSupported,
you add it but not use it.
Code quote:
LogicalOperatorsNotSupported,
crates/cairo-lang-lowering/src/test_data/lazy_bool_expr
line 24 at r3 (raw file):
--> lib.cairo:2:10 if a && (x == 0) { ^^
why?
Code quote:
error: Unknown binary operator.
--> lib.cairo:2:10
if a && (x == 0) {
^^
Previously, orizi wrote…
sorry this file is not relevent. |
fb40b13
to
f9da2ce
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 5 of 11 files reviewed, 6 unresolved discussions (waiting on @orizi and @spapinistarkware)
crates/cairo-lang-lowering/src/diagnostic.rs
line 122 at r2 (raw file):
Previously, orizi wrote…
you add it but not use it.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 5 of 11 files reviewed, 6 unresolved discussions (waiting on @orizi and @spapinistarkware)
crates/cairo-lang-semantic/src/expr/test_data/logical_operator
line 41 at r2 (raw file):
Previously, orizi wrote…
why the
into
s?
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 10 files at r1, 1 of 2 files at r2, 2 of 7 files at r3, all commit messages.
Reviewable status: 5 of 11 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)
crates/cairo-lang-lowering/src/lower/binary_op.rs
line 138 at r2 (raw file):
semantic::BinaryOperator::OrOr => lower_or_or(ctx, builder, location, expr.lhs, expr.rhs), } }
or something similar?
this seems very duplicated
Suggestion:
/// Lowers an expression of type [semantic::ExprBinaryOperator].
pub fn lower_binary_op(
ctx: &mut LoweringContext<'_, '_>,
builder: &mut BlockBuilder,
expr: &semantic::ExprBinaryOperator,
) -> LoweringResult<LoweredExpr> {
let location = ctx.get_location(expr.stable_ptr.untyped()); let semantic_db = ctx.db.upcast();
let unit_ty = corelib::unit_ty(semantic_db);
let lhs_var = lower_expr(ctx, builder, lhs)?.var(ctx, builder)?;
let mut subscope_lhs_true = create_subscope_with_bound_refs(ctx, builder);
let lhs_true_block_id = subscope_lhs_true.block_id;
let rhs_var = lower_expr(ctx, &mut subscope_lhs_true, rhs)?.var(ctx, &mut subscope_lhs_true)?;
let mut subscope_lhs_true = create_subscope_with_bound_refs(ctx, builder);
let lhs_true_block_id = subscope_lhs_true.block_id;
let (sealed_block_lhs_true, lhs_false_block_id, sealed_block_lhs_false) = match expr.op {
// Lowers `lhs && rhs` to `if lhs { rhs } else { false }`.
semantic::BinaryOperator::AndAnd => {
let rhs_var = lower_expr(ctx, &mut subscope_lhs_true, rhs)?.var(ctx, &mut subscope_lhs_true)?;
let sealed_block_lhs_true = subscope_lhs_true.goto_callsite(Some(rhs_var));
let mut subscope_lhs_false = create_subscope_with_bound_refs(ctx, builder);
let lhs_false_block_id = subscope_lhs_false.block_id;
let false_var =
create_bool(ctx, &mut subscope_lhs_false, corelib::false_variant(semantic_db), location);
let sealed_block_lhs_false = subscope_lhs_false.goto_callsite(Some(false_var));
(sealed_block_lhs_true, lhs_false_block_id, sealed_block_lhs_false)
}
// Lowers `lhs || rhs` to `if lhs { true } else { rhs }`.
semantic::BinaryOperator::OrOr => {
let true_var =
create_bool(ctx, &mut subscope_lhs_true, corelib::true_variant(semantic_db), location);
let sealed_block_lhs_true = subscope_lhs_true.goto_callsite(Some(true_var));
let mut subscope_lhs_false = create_subscope_with_bound_refs(ctx, builder);
let lhs_false_block_id = subscope_lhs_false.block_id;
let rhs_var =
lower_expr(ctx, &mut subscope_lhs_false, rhs)?.var(ctx, &mut subscope_lhs_false)?;
let sealed_block_lhs_false = subscope_lhs_false.goto_callsite(Some(rhs_var));
(sealed_block_lhs_true, lhs_false_block_id, sealed_block_lhs_false)or
},
}
let match_info = MatchInfo::Enum(MatchEnumInfo {
concrete_enum_id: corelib::core_bool_enum(semantic_db),
input: lhs_var,
arms: vec![
MatchArm {
variant_id: corelib::false_variant(semantic_db),
block_id: lhs_true_block_id,
var_ids: vec![ctx.new_var(VarRequest { ty: unit_ty, location })],
},
MatchArm {
variant_id: corelib::true_variant(semantic_db),
block_id: lhs_false_block_id,
var_ids: vec![ctx.new_var(VarRequest { ty: unit_ty, location })],
},
],
});
builder.merge_and_end_with_match(
ctx,
match_info,
vec![sealed_block_lhs_true, sealed_block_lhs_false],
location,
)
}
crates/cairo-lang-lowering/src/lower/mod.rs
line 455 at r5 (raw file):
semantic::Expr::Desnap(expr) => lower_expr_desnap(ctx, expr, builder), semantic::Expr::Assignment(expr) => lower_expr_assignment(ctx, expr, builder), semantic::Expr::LogicalOperator(expr) => lower_binary_op(ctx, builder, expr),
Suggestion:
ower_logical_op
crates/cairo-lang-semantic/src/expr/compute.rs
line 379 at r1 (raw file):
Previously, ilyalesokhin-starkware wrote…
Done, thanks.
Not sure how to test it though.
this already tests if it is the correct type for now - so sgtm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 2 files at r5.
Reviewable status: 6 of 11 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)
Is this supposed to work? Code quote:
|
f9da2ce
to
d07ea6e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 7 files at r3, 2 of 5 files at r4, 2 of 3 files at r6, all commit messages.
Reviewable status: 9 of 11 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)
d07ea6e
to
a59b985
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 8 of 11 files reviewed, 3 unresolved discussions (waiting on @orizi and @spapinistarkware)
crates/cairo-lang-lowering/src/lower/binary_op.rs
line 138 at r2 (raw file):
Previously, orizi wrote…
or something similar?
this seems very duplicated
Done.
crates/cairo-lang-lowering/src/lower/mod.rs
line 455 at r5 (raw file):
semantic::Expr::Desnap(expr) => lower_expr_desnap(ctx, expr, builder), semantic::Expr::Assignment(expr) => lower_expr_assignment(ctx, expr, builder), semantic::Expr::LogicalOperator(expr) => lower_binary_op(ctx, builder, expr),
Done.
crates/cairo-lang-semantic/src/expr/compute.rs
line 379 at r1 (raw file):
Previously, orizi wrote…
this already tests if it is the correct type for now - so sgtm.
Done.
crates/cairo-lang-semantic/src/expr/objects.rs
line 356 at r4 (raw file):
pub op: LogicalOperator, pub rhs: semantic::ExprId, pub ty: semantic::TypeId,
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 3 files at r6, 2 of 2 files at r7, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @spapinistarkware)
This change is