Skip to content

Commit 94b7ea9

Browse files
committed
resolve: more declarative fresh_binding
1 parent 083b5a0 commit 94b7ea9

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

src/librustc_resolve/late.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl PatternSource {
6969
/// Denotes whether the context for the set of already bound bindings is a `Product`
7070
/// or `Or` context. This is used in e.g., `fresh_binding` and `resolve_pattern_inner`.
7171
/// See those functions for more information.
72+
#[derive(PartialEq)]
7273
enum PatBoundCtx {
7374
/// A product pattern context, e.g., `Variant(a, b)`.
7475
Product,
@@ -1417,21 +1418,12 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
14171418
// later passes make about or-patterns.)
14181419
let ident = ident.modern_and_legacy();
14191420

1420-
// Walk outwards the stack of products / or-patterns and
1421-
// find out if the identifier has been bound in any of these.
1422-
let mut already_bound_and = false;
1423-
let mut already_bound_or = false;
1424-
for (is_sum, set) in bindings.iter_mut().rev() {
1425-
match (is_sum, set.get(&ident).cloned()) {
1426-
// Already bound in a product pattern, e.g. `(a, a)` which is not allowed.
1427-
(PatBoundCtx::Product, Some(..)) => already_bound_and = true,
1428-
// Already bound in an or-pattern, e.g. `V1(a) | V2(a)`.
1429-
// This is *required* for consistency which is checked later.
1430-
(PatBoundCtx::Or, Some(..)) => already_bound_or = true,
1431-
// Not already bound here.
1432-
_ => {}
1433-
}
1434-
}
1421+
let mut bound_iter = bindings.iter().filter(|(_, set)| set.contains(&ident));
1422+
// Already bound in a product pattern? e.g. `(a, a)` which is not allowed.
1423+
let already_bound_and = bound_iter.clone().any(|(ctx, _)| *ctx == PatBoundCtx::Product);
1424+
// Already bound in an or-pattern? e.g. `V1(a) | V2(a)`.
1425+
// This is *required* for consistency which is checked later.
1426+
let already_bound_or = bound_iter.any(|(ctx, _)| *ctx == PatBoundCtx::Or);
14351427

14361428
if already_bound_and {
14371429
// Overlap in a product pattern somewhere; report an error.

0 commit comments

Comments
 (0)