Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

or-patterns: Uniformly use PatKind::Or in AST & Fix/Cleanup resolve #64111

Merged
merged 27 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
af06bfb
resolve: extract `resolve_params`.
Centril Aug 28, 2019
f8835ee
resolve: cleanup using `with_rib`, etc.
Centril Aug 28, 2019
6d537d4
resolve: simplify `resolve_arm`.
Centril Aug 28, 2019
219ddde
resolve: extract `try_resolve_as_non_binding`.
Centril Aug 28, 2019
f343e84
resolve: add `type IdentMap<T>` alias.
Centril Aug 28, 2019
fd3b441
resolve: move `fresh_binding`.
Centril Aug 28, 2019
dc91e22
resolve: move `resolve_block` to a better place.
Centril Aug 28, 2019
70cae78
resolve: already-bound-check: account for or-patterns.
Centril Aug 28, 2019
166a558
resolve: revamp already-bound check -- fix some bugs.
Centril Sep 1, 2019
498ec59
resolve: add tests for already-bound check.
Centril Sep 1, 2019
896a1c7
resolve: account for general or-patterns in consistency checking.
Centril Sep 1, 2019
33317c7
resolve: add test for missing bindings in or-patterns.
Centril Sep 1, 2019
aa7a02b
resolve: test binding mode consistency for or-patterns.
Centril Sep 1, 2019
dbe6873
resolve: test consistent or-patterns being allowed.
Centril Sep 3, 2019
ca968a1
or-patterns: syntax: simplify `Arm.pats` and `ExprKind::Let.0`.
Centril Aug 27, 2019
998060b
or-patterns: syntax: adjust `visit` and `mut_visit`.
Centril Aug 27, 2019
ad3db72
or-patterns: syntax: adjust parser removing a hack.
Centril Aug 27, 2019
424492a
or-patterns: syntax: adjust pretty printing.
Centril Aug 27, 2019
76625eb
or-patterns: syntax: adjust derive, format, and building.
Centril Aug 27, 2019
3fccbac
or-patterns: adjust librustc_lint.
Centril Aug 27, 2019
d8ef907
or-patterns: adjust lowering of `ast::Arm` & `ast::ExprKind::Let`.
Centril Aug 28, 2019
a867c5f
resolve: merge `resolve_pats` and `resolve_pattern_top`.
Centril Sep 1, 2019
be95dee
or-patterns: adjust save_analysis wrt. `process_var_decl{_multi}`.
Centril Sep 2, 2019
d70b0c5
or-patterns: fix pprust-expr-roundtrip due to AST change.
Centril Sep 3, 2019
a7db1a4
or-patterns: address review comments.
Centril Sep 4, 2019
0341b78
resolve: bool -> enum PatBoundCtx
Centril Sep 4, 2019
16ba502
or-patterns: fix fallout from #664128.
Centril Sep 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
or-patterns: address review comments.
  • Loading branch information
Centril committed Sep 5, 2019
commit a7db1a4861e33f89181511a2e19aaa38e37e7f7a
27 changes: 23 additions & 4 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
}

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

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

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

/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
///
/// ### `bindings`
///
/// A stack of sets of bindings accumulated.
///
/// In each set, `false` denotes that a found binding in it should be interpreted as
/// re-binding an already bound binding. This results in an error. Meanwhile, `true`
/// denotes that a found binding in the set should result in reusing this binding
/// rather than creating a fresh one. In other words, `false` and `true` correspond
/// to product (e.g., `(a, b)`) and sum/or contexts (e.g., `p_0 | ... | p_i`) respectively.
///
/// When called at the top level, the stack should have a single element with `false`.
/// Otherwise, pushing to the stack happens as or-patterns are encountered and the
/// context needs to be switched to `true` and then `false` for each `p_i.
/// When each `p_i` has been dealt with, the top set is merged with its parent.
/// When a whole or-pattern has been dealt with, the thing happens.
///
/// See the implementation and `fresh_binding` for more details.
fn resolve_pattern_inner(
&mut self,
pat: &Pat,
Expand Down Expand Up @@ -1301,12 +1320,12 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
// Add a new set of bindings to the stack. `true` here records that when a
// binding already exists in this set, it should not result in an error because
// `V1(a) | V2(a)` must be allowed and are checked for consistency later.
bindings.push((true, <_>::default()));
bindings.push((true, Default::default()));
for p in ps {
// Now we need to switch back to a product context so that each
// part of the or-pattern internally rejects already bound names.
// For example, `V1(a) | V2(a, a)` and `V1(a, a) | V2(a)` are bad.
bindings.push((false, <_>::default()));
bindings.push((false, Default::default()));
self.resolve_pattern_inner(p, pat_src, bindings);
// Move up the non-overlapping bindings to the or-pattern.
// Existing bindings just get "merged".
Expand Down
5 changes: 1 addition & 4 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ impl Pat {

/// Walk top-down and call `it` in each place where a pattern occurs
/// starting with the root pattern `walk` is called on. If `it` returns
/// false then we will decend no further but siblings will be processed.
/// false then we will descend no further but siblings will be processed.
pub fn walk(&self, it: &mut impl FnMut(&Pat) -> bool) {
if !it(self) {
return;
Expand Down Expand Up @@ -1150,9 +1150,6 @@ pub enum ExprKind {
Type(P<Expr>, P<Ty>),
/// A `let pat = expr` expression that is only semantically allowed in the condition
/// of `if` / `while` expressions. (e.g., `if let 0 = x { .. }`).
///
/// The `Vec<P<Pat>>` is for or-patterns at the top level.
/// FIXME(54883): Change this to just `P<Pat>`.
Let(P<Pat>, P<Expr>),
/// An `if` block, with an optional `else` block.
///
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {

pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
visitor.visit_pat(&arm.pat);
// HACK(or_patterns; Centril | dlrobertson):
// NOTE(or_patterns; Centril | dlrobertson):
// If you change this, also change the hack in `lowering.rs`.
walk_list!(visitor, visit_expr, &arm.guard);
Centril marked this conversation as resolved.
Show resolved Hide resolved
visitor.visit_expr(&arm.body);
Expand Down