Skip to content

Commit f960226

Browse files
GearsDatapackslpil
authored andcommitted
Use enum instead of bool
1 parent 6195be4 commit f960226

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

compiler-core/src/type_/pattern.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,36 @@ struct LocalVariable {
5252
location: SrcSpan,
5353
origin: VariableOrigin,
5454
type_: Arc<Type>,
55-
used_in_pattern: bool,
56-
in_scope: bool,
55+
usage: Usage,
56+
scope: Scope,
57+
}
58+
59+
impl LocalVariable {
60+
fn in_scope(&self) -> bool {
61+
match self.scope {
62+
Scope::CurrentBitArrayPattern => true,
63+
Scope::OtherPattern => false,
64+
}
65+
}
66+
67+
fn was_used(&self) -> bool {
68+
match self.usage {
69+
Usage::UsedInPattern => true,
70+
Usage::UnusedSoFar => false,
71+
}
72+
}
73+
}
74+
75+
#[derive(Debug, Clone, Copy)]
76+
enum Usage {
77+
UsedInPattern,
78+
UnusedSoFar,
79+
}
80+
81+
#[derive(Debug, Clone, Copy)]
82+
enum Scope {
83+
CurrentBitArrayPattern,
84+
OtherPattern,
5785
}
5886

5987
enum PatternMode {
@@ -117,8 +145,8 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
117145
location,
118146
origin: origin.clone(),
119147
type_: type_.clone(),
120-
used_in_pattern: false,
121-
in_scope: true,
148+
usage: Usage::UnusedSoFar,
149+
scope: Scope::CurrentBitArrayPattern,
122150
},
123151
);
124152
}
@@ -314,18 +342,20 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
314342
/// Register the variables bound in this pattern in the environment
315343
fn register_variables(&mut self) {
316344
for (name, variable) in std::mem::take(&mut self.variables) {
345+
let was_used = variable.was_used();
346+
317347
let LocalVariable {
318348
location,
319349
origin,
320350
type_,
321-
used_in_pattern,
322-
in_scope: _,
351+
usage: _,
352+
scope: _,
323353
} = variable;
324354

325355
// If this variable has already been referenced in another part of
326356
// the pattern, we don't need to register it for usage tracking as
327357
// it has already been used.
328-
if !used_in_pattern {
358+
if !was_used {
329359
self.environment
330360
.init_usage(name.clone(), origin.clone(), location, self.problems);
331361
}
@@ -343,7 +373,7 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
343373
// Any variables from other parts of the pattern are no longer in scope.
344374
// Only variables from the bit array pattern itself can be used.
345375
for (_, variable) in self.variables.iter_mut() {
346-
variable.in_scope = false;
376+
variable.scope = Scope::OtherPattern;
347377
}
348378

349379
let last_segment = segments.pop();
@@ -555,8 +585,8 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
555585
let constructor = match self.variables.get_mut(&name) {
556586
// If we've bound a variable in the current bit array pattern, we
557587
// want to use that.
558-
Some(variable) if variable.in_scope => {
559-
variable.used_in_pattern = true;
588+
Some(variable) if variable.in_scope() => {
589+
variable.usage = Usage::UsedInPattern;
560590
ValueConstructor::local_variable(
561591
variable.location,
562592
variable.origin.clone(),

0 commit comments

Comments
 (0)