Skip to content

Commit a7db1a4

Browse files
committed
or-patterns: address review comments.
1 parent d70b0c5 commit a7db1a4

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/librustc_resolve/late.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
11091109
}
11101110

11111111
fn resolve_params(&mut self, params: &[Param]) {
1112-
let mut bindings = smallvec![(false, <_>::default())];
1112+
let mut bindings = smallvec![(false, Default::default())];
11131113
for Param { pat, ty, .. } in params {
11141114
self.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
11151115
self.visit_ty(ty);
@@ -1255,7 +1255,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
12551255

12561256
/// Arising from `source`, resolve a top level pattern.
12571257
fn resolve_pattern_top(&mut self, pat: &Pat, pat_src: PatternSource) {
1258-
self.resolve_pattern(pat, pat_src, &mut smallvec![(false, <_>::default())]);
1258+
self.resolve_pattern(pat, pat_src, &mut smallvec![(false, Default::default())]);
12591259
}
12601260

12611261
fn resolve_pattern(
@@ -1270,6 +1270,25 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
12701270
visit::walk_pat(self, pat);
12711271
}
12721272

1273+
/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
1274+
///
1275+
/// ### `bindings`
1276+
///
1277+
/// A stack of sets of bindings accumulated.
1278+
///
1279+
/// In each set, `false` denotes that a found binding in it should be interpreted as
1280+
/// re-binding an already bound binding. This results in an error. Meanwhile, `true`
1281+
/// denotes that a found binding in the set should result in reusing this binding
1282+
/// rather than creating a fresh one. In other words, `false` and `true` correspond
1283+
/// to product (e.g., `(a, b)`) and sum/or contexts (e.g., `p_0 | ... | p_i`) respectively.
1284+
///
1285+
/// When called at the top level, the stack should have a single element with `false`.
1286+
/// Otherwise, pushing to the stack happens as or-patterns are encountered and the
1287+
/// context needs to be switched to `true` and then `false` for each `p_i.
1288+
/// When each `p_i` has been dealt with, the top set is merged with its parent.
1289+
/// When a whole or-pattern has been dealt with, the thing happens.
1290+
///
1291+
/// See the implementation and `fresh_binding` for more details.
12731292
fn resolve_pattern_inner(
12741293
&mut self,
12751294
pat: &Pat,
@@ -1301,12 +1320,12 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
13011320
// Add a new set of bindings to the stack. `true` here records that when a
13021321
// binding already exists in this set, it should not result in an error because
13031322
// `V1(a) | V2(a)` must be allowed and are checked for consistency later.
1304-
bindings.push((true, <_>::default()));
1323+
bindings.push((true, Default::default()));
13051324
for p in ps {
13061325
// Now we need to switch back to a product context so that each
13071326
// part of the or-pattern internally rejects already bound names.
13081327
// For example, `V1(a) | V2(a, a)` and `V1(a, a) | V2(a)` are bad.
1309-
bindings.push((false, <_>::default()));
1328+
bindings.push((false, Default::default()));
13101329
self.resolve_pattern_inner(p, pat_src, bindings);
13111330
// Move up the non-overlapping bindings to the or-pattern.
13121331
// Existing bindings just get "merged".

src/libsyntax/ast.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ impl Pat {
563563

564564
/// Walk top-down and call `it` in each place where a pattern occurs
565565
/// starting with the root pattern `walk` is called on. If `it` returns
566-
/// false then we will decend no further but siblings will be processed.
566+
/// false then we will descend no further but siblings will be processed.
567567
pub fn walk(&self, it: &mut impl FnMut(&Pat) -> bool) {
568568
if !it(self) {
569569
return;
@@ -1150,9 +1150,6 @@ pub enum ExprKind {
11501150
Type(P<Expr>, P<Ty>),
11511151
/// A `let pat = expr` expression that is only semantically allowed in the condition
11521152
/// of `if` / `while` expressions. (e.g., `if let 0 = x { .. }`).
1153-
///
1154-
/// The `Vec<P<Pat>>` is for or-patterns at the top level.
1155-
/// FIXME(54883): Change this to just `P<Pat>`.
11561153
Let(P<Pat>, P<Expr>),
11571154
/// An `if` block, with an optional `else` block.
11581155
///

src/libsyntax/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
831831

832832
pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
833833
visitor.visit_pat(&arm.pat);
834-
// HACK(or_patterns; Centril | dlrobertson):
834+
// NOTE(or_patterns; Centril | dlrobertson):
835835
// If you change this, also change the hack in `lowering.rs`.
836836
walk_list!(visitor, visit_expr, &arm.guard);
837837
visitor.visit_expr(&arm.body);

0 commit comments

Comments
 (0)