Skip to content

Commit

Permalink
Auto merge of #66296 - Centril:bindings_after_at-init, r=pnkfelix
Browse files Browse the repository at this point in the history
Initial implementation of `#![feature(bindings_after_at)]`

Following up on #16053, under the gate `#![feature(bindings_after_at)]`, `x @ Some(y)` is allowed subject to restrictions necessary for soundness.

The implementation and test suite should be fairly complete now.

One aspect that is not covered is the interaction with nested `#![feature(or_patterns)]`.
This is not possible to test at the moment in a good way because that feature has not progressed sufficiently and has fatal errors in MIR building. We should make sure to add such tests before we stabilize both features (but shipping one of them is fine).

r? @pnkfelix
cc @nikomatsakis @matthewjasper @pcwalton
cc #65490
  • Loading branch information
bors committed Dec 23, 2019
2 parents 9ae6ced + acfe582 commit a4cd03d
Show file tree
Hide file tree
Showing 53 changed files with 2,412 additions and 187 deletions.
10 changes: 10 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,16 @@ impl Pat {
pub fn walk(&self, mut it: impl FnMut(&Pat) -> bool) {
self.walk_(&mut it)
}

/// Walk the pattern in left-to-right order.
///
/// If you always want to recurse, prefer this method over `walk`.
pub fn walk_always(&self, mut it: impl FnMut(&Pat)) {
self.walk(|p| {
it(p);
true
})
}
}

/// A single field in a struct pattern.
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/hir/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ impl hir::Pat {
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
/// `match foo() { Some(a) => (), None => () }`
pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident)) {
self.walk(|p| {
self.walk_always(|p| {
if let PatKind::Binding(binding_mode, _, ident, _) = p.kind {
f(binding_mode, p.hir_id, p.span, ident);
}
true
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,13 @@ impl<'tcx> TypeckTables<'tcx> {
}
}

pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> {
self.pat_binding_modes().get(id).copied().or_else(|| {
s.delay_span_bug(sp, "missing binding mode");
None
})
}

pub fn pat_binding_modes(&self) -> LocalTableInContext<'_, BindingMode> {
LocalTableInContext {
local_id_root: self.local_id_root,
Expand Down
10 changes: 9 additions & 1 deletion src/librustc_error_codes/error_codes/E0303.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#### Note: this error code is no longer emitted by the compiler.

Sub-bindings, e.g. `ref x @ Some(ref y)` are now allowed under
`#![feature(bindings_after_at)]` and checked to make sure that
memory safety is upheld.

--------------

In certain cases it is possible for sub-bindings to violate memory safety.
Updates to the borrow checker in a future version of Rust may remove this
restriction, but for now patterns must be rewritten without sub-bindings.

Before:

```compile_fail,E0303
```compile_fail
match Some("hi".to_string()) {
ref op_string_ref @ Some(s) => {},
None => {},
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_feature/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ declare_features! (
/// Allows the use of `loop` and `while` in constants.
(active, const_loop, "1.41.0", Some(52000), None),

/// Allows bindings in the subpattern of a binding pattern.
/// For example, you can write `x @ Some(y)`.
(active, bindings_after_at, "1.41.0", Some(65490), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
11 changes: 4 additions & 7 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
name = ident.name;

if let Some(&bm) = hir_tables.pat_binding_modes().get(pat.hir_id) {
if bm == ty::BindByValue(hir::Mutability::Mut) {
match hir_tables.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) {
Some(ty::BindByValue(hir::Mutability::Mut)) => {
mutability = Mutability::Mut;
} else {
mutability = Mutability::Not;
}
} else {
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
Some(_) => mutability = Mutability::Not,
_ => {}
}
}
}
Expand Down
Loading

0 comments on commit a4cd03d

Please sign in to comment.