Skip to content

Commit 111cc32

Browse files
giacomocavalierilpil
authored andcommitted
add a BlockNode variant to the ClauseGuard enum
allowing us to keep track of the blocks one has explicitly used when writing a clause guard
1 parent f09b097 commit 111cc32

File tree

7 files changed

+33
-5
lines changed

7 files changed

+33
-5
lines changed

compiler-core/src/ast.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,11 @@ pub type TypedClauseGuard = ClauseGuard<Arc<Type>, EcoString>;
19011901

19021902
#[derive(Debug, Clone, PartialEq, Eq)]
19031903
pub enum ClauseGuard<Type, RecordTag> {
1904+
Block {
1905+
location: SrcSpan,
1906+
value: Box<ClauseGuard<Type, RecordTag>>,
1907+
},
1908+
19041909
Equals {
19051910
location: SrcSpan,
19061911
left: Box<Self>,
@@ -2094,7 +2099,8 @@ impl<A, B> ClauseGuard<A, B> {
20942099
| ClauseGuard::DivFloat { location, .. }
20952100
| ClauseGuard::RemainderInt { location, .. }
20962101
| ClauseGuard::LtEqFloat { location, .. }
2097-
| ClauseGuard::ModuleSelect { location, .. } => *location,
2102+
| ClauseGuard::ModuleSelect { location, .. }
2103+
| ClauseGuard::Block { location, .. } => *location,
20982104
ClauseGuard::FieldAccess {
20992105
label_location,
21002106
container,
@@ -2140,7 +2146,8 @@ impl<A, B> ClauseGuard<A, B> {
21402146
| ClauseGuard::Not { .. }
21412147
| ClauseGuard::TupleIndex { .. }
21422148
| ClauseGuard::FieldAccess { .. }
2143-
| ClauseGuard::ModuleSelect { .. } => None,
2149+
| ClauseGuard::ModuleSelect { .. }
2150+
| ClauseGuard::Block { .. } => None,
21442151
}
21452152
}
21462153
}
@@ -2153,6 +2160,7 @@ impl TypedClauseGuard {
21532160
ClauseGuard::FieldAccess { type_, .. } => type_.clone(),
21542161
ClauseGuard::ModuleSelect { type_, .. } => type_.clone(),
21552162
ClauseGuard::Constant(constant) => constant.type_(),
2163+
ClauseGuard::Block { value, .. } => value.type_(),
21562164

21572165
ClauseGuard::AddInt { .. }
21582166
| ClauseGuard::SubInt { .. }
@@ -2185,6 +2193,7 @@ impl TypedClauseGuard {
21852193
match self {
21862194
ClauseGuard::Var { name, .. } => im::hashset![name],
21872195

2196+
ClauseGuard::Block { value, .. } => value.referenced_variables(),
21882197
ClauseGuard::Not { expression, .. } => expression.referenced_variables(),
21892198
ClauseGuard::TupleIndex { tuple, .. } => tuple.referenced_variables(),
21902199
ClauseGuard::FieldAccess { container, .. } => container.referenced_variables(),

compiler-core/src/ast/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,7 @@ where
13671367
v.visit_typed_clause_guard(left);
13681368
v.visit_typed_clause_guard(right);
13691369
}
1370+
super::ClauseGuard::Block { location: _, value } => v.visit_typed_clause_guard(value),
13701371
super::ClauseGuard::Not {
13711372
location: _,
13721373
expression,

compiler-core/src/call_graph.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ impl<'a> CallGraphBuilder<'a> {
457457
self.guard(right);
458458
}
459459

460+
ClauseGuard::Block { value, .. } => self.guard(value),
461+
460462
ClauseGuard::Not { expression, .. } => self.guard(expression),
461463

462464
ClauseGuard::Var { name, .. } => self.referenced(name),

compiler-core/src/erlang.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,8 @@ fn optional_clause_guard<'a>(
16141614

16151615
fn bare_clause_guard<'a>(guard: &'a TypedClauseGuard, env: &mut Env<'a>) -> Document<'a> {
16161616
match guard {
1617+
ClauseGuard::Block { value, .. } => bare_clause_guard(value, env).surround("(", ")"),
1618+
16171619
ClauseGuard::Not { expression, .. } => docvec!["not ", bare_clause_guard(expression, env)],
16181620

16191621
ClauseGuard::Or { left, right, .. } => clause_guard(left, env)
@@ -1762,7 +1764,8 @@ fn clause_guard<'a>(guard: &'a TypedClauseGuard, env: &mut Env<'a>) -> Document<
17621764
| ClauseGuard::Var { .. }
17631765
| ClauseGuard::TupleIndex { .. }
17641766
| ClauseGuard::FieldAccess { .. }
1765-
| ClauseGuard::ModuleSelect { .. } => bare_clause_guard(guard, env),
1767+
| ClauseGuard::ModuleSelect { .. }
1768+
| ClauseGuard::Block { .. } => bare_clause_guard(guard, env),
17661769
}
17671770
}
17681771

compiler-core/src/format.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,8 @@ impl<'comments> Formatter<'comments> {
25142514
ClauseGuard::Constant(constant) => self.const_expr(constant),
25152515

25162516
ClauseGuard::Not { expression, .. } => docvec!["!", self.clause_guard(expression)],
2517+
2518+
ClauseGuard::Block { value, .. } => wrap_block(self.clause_guard(value)).group(),
25172519
}
25182520
}
25192521

compiler-core/src/javascript/expression.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,8 @@ impl<'module, 'a> Generator<'module, 'a> {
19361936

19371937
pub(crate) fn guard(&mut self, guard: &'a TypedClauseGuard) -> Document<'a> {
19381938
match guard {
1939+
ClauseGuard::Block { value, .. } => self.guard(value).surround("(", ")"),
1940+
19391941
ClauseGuard::Equals { left, right, .. } if is_js_scalar(left.type_()) => {
19401942
let left = self.wrapped_guard(left);
19411943
let right = self.wrapped_guard(right);
@@ -2066,7 +2068,8 @@ impl<'module, 'a> Generator<'module, 'a> {
20662068
| ClauseGuard::TupleIndex { .. }
20672069
| ClauseGuard::Constant(_)
20682070
| ClauseGuard::Not { .. }
2069-
| ClauseGuard::FieldAccess { .. } => self.guard(guard),
2071+
| ClauseGuard::FieldAccess { .. }
2072+
| ClauseGuard::Block { .. } => self.guard(guard),
20702073

20712074
ClauseGuard::Equals { .. }
20722075
| ClauseGuard::NotEquals { .. }
@@ -2089,7 +2092,7 @@ impl<'module, 'a> Generator<'module, 'a> {
20892092
| ClauseGuard::RemainderInt { .. }
20902093
| ClauseGuard::Or { .. }
20912094
| ClauseGuard::And { .. }
2092-
| ClauseGuard::ModuleSelect { .. } => docvec!["(", self.guard(guard,), ")"],
2095+
| ClauseGuard::ModuleSelect { .. } => docvec!["(", self.guard(guard), ")"],
20932096
}
20942097
}
20952098

compiler-core/src/type_/expression.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,14 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
28002800
ClauseGuard::Constant(constant) => {
28012801
Ok(ClauseGuard::Constant(self.infer_const(&None, constant)))
28022802
}
2803+
2804+
ClauseGuard::Block { value, location } => {
2805+
let value = self.infer_clause_guard(*value)?;
2806+
Ok(ClauseGuard::Block {
2807+
location,
2808+
value: Box::new(value),
2809+
})
2810+
}
28032811
}
28042812
}
28052813

0 commit comments

Comments
 (0)