Skip to content

Commit

Permalink
core: remove all Clone operations from the expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
clarus committed Dec 29, 2023
1 parent 72bf161 commit 7ce3caa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
25 changes: 11 additions & 14 deletions lib/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl FreshVars {

/// Struct [MatchArm] represents a pattern-matching branch: [pat] is the
/// matched pattern and [body] the expression on which it is mapped
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Debug, Eq, Hash, PartialEq)]
pub(crate) struct MatchArm {
pub(crate) pattern: Rc<Pattern>,
pub(crate) body: Rc<Expr>,
Expand All @@ -42,7 +42,7 @@ pub(crate) enum Purity {
Effectful,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Debug, Eq, Hash, PartialEq)]
pub(crate) enum Literal {
Bool(bool),
Integer { value: u128, neg: bool },
Expand All @@ -51,14 +51,14 @@ pub(crate) enum Literal {
Error,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Debug, Eq, Hash, PartialEq)]
pub(crate) struct Expr {
pub(crate) kind: Rc<ExprKind>,
pub(crate) ty: Option<Rc<CoqType>>,
}

/// Enum [ExprKind] represents the AST of rust terms.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Debug, Eq, Hash, PartialEq)]
pub(crate) enum ExprKind {
Pure(Rc<Expr>),
LocalVar(String),
Expand Down Expand Up @@ -122,7 +122,7 @@ pub(crate) enum ExprKind {
},
Match {
scrutinee: Rc<Expr>,
arms: Vec<MatchArm>,
arms: Vec<Rc<MatchArm>>,
},
#[allow(dead_code)]
IndexedField {
Expand Down Expand Up @@ -228,10 +228,7 @@ impl Expr {
} => condition.has_return() || success.has_return() || failure.has_return(),
ExprKind::Loop { body } => body.has_return(),
ExprKind::Match { scrutinee, arms } => {
scrutinee.has_return()
|| arms
.iter()
.any(|MatchArm { pattern: _, body }| body.has_return())
scrutinee.has_return() || arms.iter().any(|arm| arm.body.has_return())
}
ExprKind::IndexedField { base, index: _ } => base.has_return(),
ExprKind::NamedField { base, name: _ } => base.has_return(),
Expand Down Expand Up @@ -583,13 +580,13 @@ pub(crate) fn mt_expression(fresh_vars: FreshVars, expr: Rc<Expr>) -> (Rc<Expr>,
scrutinee,
arms: arms
.iter()
.map(|MatchArm { pattern, body }| {
.map(|arm| {
let (body, _fresh_vars) =
mt_expression(FreshVars::new(), body.clone());
MatchArm {
pattern: pattern.clone(),
mt_expression(FreshVars::new(), arm.body.clone());
Rc::new(MatchArm {
pattern: arm.pattern.clone(),
body,
}
})
})
.collect(),
}),
Expand Down
24 changes: 12 additions & 12 deletions lib/src/thir_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub(crate) fn allocate_bindings(bindings: &[String], body: Rc<Expr>) -> Rc<Expr>
}

fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc<Expr> {
let default_match_arm = MatchArm {
let default_match_arm = Rc::new(MatchArm {
pattern: Rc::new(Pattern::Wild),
body: Rc::new(Expr {
kind: Rc::new(ExprKind::Call {
Expand All @@ -220,7 +220,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
}),
ty: None,
}),
};
});

patterns
.into_iter()
Expand Down Expand Up @@ -265,7 +265,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
kind: Rc::new(ExprKind::Match {
scrutinee: Expr::local_var(&scrutinee).read(),
arms: [
vec![MatchArm {
vec![Rc::new(MatchArm {
pattern: Rc::new(Pattern::StructStruct(
path.clone(),
fields
Expand Down Expand Up @@ -312,7 +312,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
},
)
},
}],
})],
match struct_or_variant {
StructOrVariant::Struct => vec![],
StructOrVariant::Variant => vec![default_match_arm.clone()],
Expand All @@ -326,7 +326,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
kind: Rc::new(ExprKind::Match {
scrutinee: Expr::local_var(&scrutinee).read(),
arms: [
vec![MatchArm {
vec![Rc::new(MatchArm {
pattern: Rc::new(Pattern::StructTuple(
path.clone(),
patterns.iter().map(|_| Rc::new(Pattern::Wild)).collect(),
Expand Down Expand Up @@ -365,7 +365,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
})
})
},
}],
})],
match struct_or_variant {
StructOrVariant::Struct => vec![],
StructOrVariant::Variant => vec![default_match_arm.clone()],
Expand Down Expand Up @@ -396,7 +396,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
ty: body.ty.clone(),
kind: Rc::new(ExprKind::Match {
scrutinee: Expr::local_var(&scrutinee).read(),
arms: vec![MatchArm {
arms: vec![Rc::new(MatchArm {
pattern: Rc::new(Pattern::Tuple(
patterns.iter().map(|_| Rc::new(Pattern::Wild)).collect(),
)),
Expand Down Expand Up @@ -453,18 +453,18 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
})
})
},
}],
})],
}),
}),
Pattern::Lit(_) => Rc::new(Expr {
ty: body.ty.clone(),
kind: Rc::new(ExprKind::Match {
scrutinee: Expr::local_var(&scrutinee).read(),
arms: vec![
MatchArm {
Rc::new(MatchArm {
pattern: pattern.clone(),
body,
},
}),
default_match_arm.clone(),
],
}),
Expand All @@ -477,7 +477,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
kind: Rc::new(ExprKind::Match {
scrutinee: Expr::local_var(&scrutinee).read(),
arms: vec![
MatchArm {
Rc::new(MatchArm {
pattern: Rc::new(Pattern::Slice {
init_patterns: init_patterns
.iter()
Expand Down Expand Up @@ -551,7 +551,7 @@ fn build_inner_match(patterns: Vec<(String, Rc<Pattern>)>, body: Rc<Expr>) -> Rc
})
})
},
},
}),
default_match_arm.clone(),
],
}),
Expand Down

0 comments on commit 7ce3caa

Please sign in to comment.