Skip to content

Commit 640d672

Browse files
authored
Merge pull request #1950 from JuanCoRo/remove-binops-resugaring
Remove `BinOp` resugaring
2 parents 997950d + a033c48 commit 640d672

5 files changed

Lines changed: 44 additions & 114 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
Changes to the Rust Engine:
11+
- Remove `BinOp` resugaring (#1950)
1112

1213
Changes to the frontend:
1314

rust-engine/src/ast/resugared.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@ pub enum ResugaredItemKind {
5050
// TODO: drop `clippy::large_enum_variant` when https://github.com/cryspen/hax/issues/1666 is addressed.
5151
#[allow(clippy::large_enum_variant)]
5252
pub enum ResugaredExprKind {
53-
/// Binary operations (identified by resugaring) of the form `f(e1, e2)`
54-
BinOp {
55-
/// The identifier of the operation (`f`)
56-
op: GlobalId,
57-
/// The left-hand side of the operation (`e1`)
58-
lhs: Expr,
59-
/// The right-hand side of the operation (`e2`)
60-
rhs: Expr,
61-
/// The generic arguments applied to the function.
62-
generic_args: Vec<GenericValue>,
63-
/// If the function requires generic bounds to be called, `bounds_impls`
64-
/// is a vector of impl. expressions for those bounds.
65-
bounds_impls: Vec<ImplExpr>,
66-
/// If we apply an associated function, contains the impl. expr used.
67-
trait_: Option<(ImplExpr, Vec<GenericValue>)>,
68-
},
6953
/// A tuple constructor.
7054
///
7155
/// # Example:

rust-engine/src/backends/lean.rs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,6 @@ impl Printer for LeanPrinter {
144144
fn resugaring_phases() -> Vec<Box<dyn Resugaring>> {
145145
vec![
146146
Box::new(RecursiveFunctions),
147-
Box::new(BinOp::new(&[
148-
binops::add,
149-
binops::sub,
150-
binops::mul,
151-
binops::rem,
152-
binops::div,
153-
binops::shr,
154-
binops::shl,
155-
binops::bitand,
156-
binops::bitxor,
157-
binops::logical_op_and,
158-
binops::logical_op_or,
159-
binops::Index::index,
160-
])),
161147
Box::new(FunctionsToConstants),
162148
Box::new(LetPure),
163149
]
@@ -901,6 +887,49 @@ const _: () = {
901887
([arg], [], ExprKind::GlobalId(binops::neg)) => {
902888
docs!["-?", softline!(), arg].parens()
903889
}
890+
([lhs, rhs], [], ExprKind::GlobalId(binops::Index::index)) => {
891+
docs![lhs, "[", line_!(), rhs, line_!(), "]_?"]
892+
.nest(INDENT)
893+
.group()
894+
}
895+
// TODO: Replace this match pattern with an `if let` guard when the feature stabilizes
896+
// Tracking PR: https://github.com/rust-lang/rust/pull/141295
897+
(
898+
[lhs, rhs],
899+
[],
900+
ExprKind::GlobalId(
901+
op @ (binops::add
902+
| binops::sub
903+
| binops::mul
904+
| binops::div
905+
| binops::rem
906+
| binops::shr
907+
| binops::shl
908+
| binops::bitand
909+
| binops::bitxor
910+
| binops::logical_op_and
911+
| binops::logical_op_or),
912+
),
913+
) => {
914+
let symbol = match *op {
915+
binops::add => "+?",
916+
binops::sub => "-?",
917+
binops::mul => "*?",
918+
binops::div => "/?",
919+
binops::rem => "%?",
920+
binops::shr => ">>>?",
921+
binops::shl => "<<<?",
922+
binops::bitand => "&&&?",
923+
binops::bitxor => "^^^?",
924+
binops::logical_op_and => "&&?",
925+
binops::logical_op_or => "||?",
926+
_ => unreachable!(),
927+
};
928+
docs![lhs, line!(), docs![symbol, softline!(), rhs].group()]
929+
.group()
930+
.nest(INDENT)
931+
.parens()
932+
}
904933
_ => {
905934
// Fallback for any application
906935
docs![
@@ -1015,33 +1044,6 @@ const _: () = {
10151044
.group()
10161045
.nest(INDENT),
10171046

1018-
ExprKind::Resugared(ResugaredExprKind::BinOp { op, lhs, rhs, .. }) => {
1019-
// TODO : refactor this, moving this code directly in the `App` node (see
1020-
// https://github.com/cryspen/hax/issues/1705)
1021-
if *op == binops::Index::index {
1022-
return docs![lhs, "[", line_!(), rhs, line_!(), "]_?"]
1023-
.nest(INDENT)
1024-
.group();
1025-
}
1026-
let symbol = match *op {
1027-
binops::add => "+?",
1028-
binops::sub => "-?",
1029-
binops::mul => "*?",
1030-
binops::div => "/?",
1031-
binops::rem => "%?",
1032-
binops::shr => ">>>?",
1033-
binops::shl => "<<<?",
1034-
binops::bitand => "&&&?",
1035-
binops::bitxor => "^^^?",
1036-
binops::logical_op_and => "&&?",
1037-
binops::logical_op_or => "||?",
1038-
_ => unreachable!(),
1039-
};
1040-
docs![lhs, line!(), docs![symbol, softline!(), rhs].group()]
1041-
.group()
1042-
.nest(INDENT)
1043-
.parens()
1044-
}
10451047
ExprKind::Resugared(ResugaredExprKind::Tuple { .. }) => {
10461048
unreachable!("This printer doesn't use the tuple resugaring")
10471049
}

rust-engine/src/backends/rust.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ const _: () = {
590590
}
591591
fn resugared_expr_kind(&self, resugared_expr_kind: &ResugaredExprKind) -> DocBuilder<A> {
592592
match resugared_expr_kind {
593-
ResugaredExprKind::BinOp { .. } => unreachable!("BinOp resugaring not active"),
594593
ResugaredExprKind::Tuple(values) => print_tuple!(values),
595594
ResugaredExprKind::LetPure { .. } => unreachable!("LetPure resugaring not active"),
596595
}

rust-engine/src/resugarings.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::ast::resugared::*;
99
use crate::ast::visitors::*;
1010
use crate::ast::*;
1111
use crate::printer::*;
12-
use std::collections::HashSet;
1312

1413
/// Transforms [`ItemKind::Fn`] of arity zero into [`ResugaredItemKind::Constant`].
1514
/// Rust `const` items are encoded by the `ImportThir` phase of the hax engine as function of arity zero.
@@ -54,61 +53,6 @@ impl Resugaring for FunctionsToConstants {
5453
}
5554
}
5655

57-
/// Binop resugaring. Used to identify expressions of the form `(f e1 e2)` where
58-
/// `f` is a known identifier.
59-
pub struct BinOp {
60-
/// Stores a set of identifiers that should be resugared as binary
61-
/// operations. Usually, those identifiers come from the hax encoding. Each
62-
/// backend can select its own set of identifiers Typically, if the backend
63-
/// has a special support for addition, `known_ops` will contain
64-
/// `hax::machine::int::add`
65-
pub known_ops: HashSet<GlobalId>,
66-
}
67-
68-
impl BinOp {
69-
/// Adds a new binary operation from a list of (hax-introduced) names
70-
pub fn new(known_ops: &[GlobalId]) -> Self {
71-
Self {
72-
known_ops: HashSet::from_iter(known_ops.iter().cloned()),
73-
}
74-
}
75-
}
76-
77-
impl AstVisitorMut for BinOp {
78-
fn enter_expr_kind(&mut self, x: &mut ExprKind) {
79-
let ExprKind::App {
80-
head,
81-
args,
82-
generic_args,
83-
bounds_impls,
84-
trait_,
85-
}: &mut ExprKind = x
86-
else {
87-
return;
88-
};
89-
let ExprKind::GlobalId(id) = &*head.kind else {
90-
return;
91-
};
92-
let [lhs, rhs] = &args[..] else { return };
93-
if self.known_ops.iter().any(|defid| id == defid) {
94-
*x = ExprKind::Resugared(ResugaredExprKind::BinOp {
95-
op: *id,
96-
lhs: lhs.clone(),
97-
rhs: rhs.clone(),
98-
generic_args: generic_args.clone(),
99-
bounds_impls: bounds_impls.clone(),
100-
trait_: trait_.clone(),
101-
});
102-
}
103-
}
104-
}
105-
106-
impl Resugaring for BinOp {
107-
fn name(&self) -> String {
108-
"binop".to_string()
109-
}
110-
}
111-
11256
/// Tuples resugaring. Resugars tuple constructors to the dedicated expression variant [`ResugaredExprKind::Tuple`],
11357
/// and tuple types to the dedicated type variant [`ResugaredTyKind::Tuple`].
11458
pub struct Tuples;

0 commit comments

Comments
 (0)