Skip to content

Commit

Permalink
Auto merge of #85305 - MarcusDunn:master, r=pnkfelix
Browse files Browse the repository at this point in the history
Stabilize bindings_after_at

attempting to stabilze bindings_after_at [#65490](#65490), im pretty new to the whole thing so any pointers are greatly appreciated.
  • Loading branch information
bors committed Jul 27, 2021
2 parents 7d6bf86 + c2af4cb commit 998cfe5
Show file tree
Hide file tree
Showing 56 changed files with 228 additions and 335 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! The crate also contains other misc AST visitors, e.g. `node_count` and `show_span`.

#![feature(bindings_after_at)]
#![cfg_attr(bootstrap, feature(bindings_after_at))]
#![feature(iter_is_partitioned)]
#![feature(box_patterns)]
#![recursion_limit = "256"]
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ declare_features! (
(accepted, const_fn_unsize, "1.54.0", Some(64992), None),
/// Allows `impl Trait` with multiple unrelated lifetimes.
(accepted, member_constraints, "1.54.0", Some(61997), None),
/// Allows bindings in the subpattern of a binding pattern.
/// For example, you can write `x @ Some(y)`.
(accepted, bindings_after_at, "1.54.0", Some(65490), None),

// -------------------------------------------------------------------------
// feature-group-end: accepted features
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,6 @@ declare_features! (
/// Allows using `&mut` in constant functions.
(active, const_mut_refs, "1.41.0", Some(57349), 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),

/// Allows `impl const Trait for T` syntax.
(active, const_trait_impl, "1.42.0", Some(67792), None),

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Rust MIR: a lowered representation of Rust.
#![feature(in_band_lifetimes)]
#![feature(array_windows)]
#![feature(assert_matches)]
#![feature(bindings_after_at)]
#![cfg_attr(bootstrap, feature(bindings_after_at))]
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
Expand Down
49 changes: 1 addition & 48 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use rustc_middle::thir::PatKind;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::lint::builtin::BINDINGS_WITH_VARIANT_NAME;
use rustc_session::lint::builtin::{IRREFUTABLE_LET_PATTERNS, UNREACHABLE_PATTERNS};
use rustc_session::parse::feature_err;
use rustc_session::Session;
use rustc_span::{sym, Span};
use rustc_span::Span;
use std::slice;

crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
Expand Down Expand Up @@ -115,9 +114,6 @@ impl PatCtxt<'_, '_> {
impl<'tcx> MatchVisitor<'_, 'tcx> {
fn check_patterns(&mut self, pat: &Pat<'_>) {
pat.walk_always(|pat| check_borrow_conflicts_in_at_patterns(self, pat));
if !self.tcx.features().bindings_after_at {
check_legality_of_bindings_in_at_patterns(self, pat);
}
check_for_bindings_named_same_as_variants(self, pat);
}

Expand Down Expand Up @@ -732,46 +728,3 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
err.emit();
}
}

/// Forbids bindings in `@` patterns. This used to be is necessary for memory safety,
/// because of the way rvalues were handled in the borrow check. (See issue #14587.)
fn check_legality_of_bindings_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_>) {
AtBindingPatternVisitor { cx, bindings_allowed: true }.visit_pat(pat);

struct AtBindingPatternVisitor<'a, 'b, 'tcx> {
cx: &'a MatchVisitor<'b, 'tcx>,
bindings_allowed: bool,
}

impl<'v> Visitor<'v> for AtBindingPatternVisitor<'_, '_, '_> {
type Map = intravisit::ErasedMap<'v>;

fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}

fn visit_pat(&mut self, pat: &Pat<'_>) {
match pat.kind {
hir::PatKind::Binding(.., ref subpat) => {
if !self.bindings_allowed {
feature_err(
&self.cx.tcx.sess.parse_sess,
sym::bindings_after_at,
pat.span,
"pattern bindings after an `@` are unstable",
)
.emit();
}

if subpat.is_some() {
let bindings_were_allowed = self.bindings_allowed;
self.bindings_allowed = false;
intravisit::walk_pat(self, pat);
self.bindings_allowed = bindings_were_allowed;
}
}
_ => intravisit::walk_pat(self, pat),
}
}
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#![feature(array_windows)]
#![feature(crate_visibility_modifier)]
#![feature(bindings_after_at)]
#![cfg_attr(bootstrap, feature(bindings_after_at))]
#![feature(box_syntax)]
#![feature(box_patterns)]
#![recursion_limit = "256"]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ This API is completely unstable and subject to change.
*/

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(bindings_after_at)]
#![cfg_attr(bootstrap, feature(bindings_after_at))]
#![feature(bool_to_option)]
#![feature(box_syntax)]
#![feature(crate_visibility_modifier)]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#![feature(allow_internal_unstable)]
#![feature(arbitrary_self_types)]
#![feature(async_stream)]
#![cfg_attr(bootstrap, feature(bindings_after_at))]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(cfg_sanitize)]
Expand Down Expand Up @@ -145,7 +146,6 @@
#![feature(associated_type_bounds)]
#![feature(slice_group_by)]
#![feature(decl_macro)]
#![feature(bindings_after_at)]
// Allow testing this library

#[cfg(test)]
Expand Down
Empty file modified src/etc/pre-commit.sh
100755 → 100644
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Tests using a combination of pattern features has the expected borrow checking behavior
#![feature(bindings_after_at)]
#![feature(box_patterns)]

enum Test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: cannot borrow value as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:37:9
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:36:9
|
LL | ref foo @ [.., ref mut bar] => (),
| -------^^^^^^^^-----------^
Expand All @@ -8,7 +8,7 @@ LL | ref foo @ [.., ref mut bar] => (),
| immutable borrow, by `foo`, occurs here

error: cannot borrow value as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:121:9
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:120:9
|
LL | ref foo @ Some(box ref mut s) => (),
| -------^^^^^^^^^^^^---------^
Expand All @@ -17,7 +17,7 @@ LL | ref foo @ Some(box ref mut s) => (),
| immutable borrow, by `foo`, occurs here

error[E0382]: borrow of moved value: `x`
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:19:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:18:5
|
LL | fn bindings_after_at_slice_patterns_move_binding(x: [String; 4]) {
| - move occurs because `x` has type `[String; 4]`, which does not implement the `Copy` trait
Expand All @@ -29,7 +29,7 @@ LL | &x;
| ^^ value borrowed here after move

error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:29:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:28:5
|
LL | ref mut foo @ [.., _] => Some(foo),
| --------------------- mutable borrow occurs here
Expand All @@ -41,7 +41,7 @@ LL | drop(r);
| - mutable borrow later used here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:51:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:50:5
|
LL | [ref foo @ .., ref bar] => Some(foo),
| ------------ immutable borrow occurs here
Expand All @@ -53,7 +53,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:63:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:62:5
|
LL | ref foo @ [.., ref bar] => Some(foo),
| ----------------------- immutable borrow occurs here
Expand All @@ -65,7 +65,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0382]: borrow of moved value: `x`
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:77:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:76:5
|
LL | fn bindings_after_at_or_patterns_move(x: Option<Test>) {
| - move occurs because `x` has type `Option<Test>`, which does not implement the `Copy` trait
Expand All @@ -80,7 +80,7 @@ LL | &x;
| ^^ value borrowed here after move

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:87:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:86:5
|
LL | ref foo @ Some(Test::Foo | Test::Bar) => Some(foo),
| ------------------------------------- immutable borrow occurs here
Expand All @@ -92,7 +92,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:99:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:98:5
|
LL | ref mut foo @ Some(Test::Foo | Test::Bar) => Some(foo),
| ----------------------------------------- mutable borrow occurs here
Expand All @@ -104,7 +104,7 @@ LL | drop(r);
| - mutable borrow later used here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:113:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:112:5
|
LL | ref foo @ Some(box ref s) => Some(foo),
| ------------------------- immutable borrow occurs here
Expand All @@ -116,7 +116,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0382]: borrow of moved value: `x`
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:135:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:134:5
|
LL | fn bindings_after_at_slice_patterns_or_patterns_moves(x: [Option<Test>; 4]) {
| - move occurs because `x` has type `[Option<Test>; 4]`, which does not implement the `Copy` trait
Expand All @@ -131,7 +131,7 @@ LL | &x;
| ^^ value borrowed here after move

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:145:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:144:5
|
LL | ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(a),
| ------------------------------------------------- immutable borrow occurs here
Expand All @@ -143,7 +143,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:157:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:156:5
|
LL | ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(b),
| ---------- immutable borrow occurs here
Expand All @@ -155,7 +155,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:171:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:170:5
|
LL | [_, ref a @ Some(box ref b), ..] => Some(a),
| ----------------------- immutable borrow occurs here
Expand All @@ -167,7 +167,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:187:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:186:5
|
LL | [_, ref a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
| ------------------------------------------- immutable borrow occurs here
Expand All @@ -179,7 +179,7 @@ LL | drop(r);
| - immutable borrow later used here

error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:201:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:200:5
|
LL | [_, ref mut a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
| ----------------------------------------------- mutable borrow occurs here
Expand All @@ -191,7 +191,7 @@ LL | drop(r);
| - mutable borrow later used here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:215:5
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:214:5
|
LL | ref a @ [_, ref b @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
| ------------------------------------------------------------ immutable borrow occurs here
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/drop/dynamic-drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// ignore-wasm32-bare compiled with panic=abort by default

#![feature(generators, generator_trait)]
#![feature(bindings_after_at)]

#![allow(unused_assignments)]
#![allow(unused_variables)]
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/pattern/bindings-after-at/bind-by-copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

// Test copy

#![feature(bindings_after_at)]

struct A { a: i32, b: i32 }
struct B { a: i32, b: C }
struct D { a: i32, d: C }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// It checks that you cannot use an AND-pattern (`binding @ pat`)
// where one side is by-ref and the other is by-move.

#![feature(bindings_after_at)]

struct X {
x: (),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: cannot move out of value because it is borrowed
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:14:14
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:12:14
|
LL | Some(ref _y @ _z) => {}
| ------^^^--
Expand All @@ -8,7 +8,7 @@ LL | Some(ref _y @ _z) => {}
| value borrowed, by `_y`, here

error: borrow of moved value
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:21:14
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:19:14
|
LL | Some(_z @ ref _y) => {}
| --^^^------
Expand All @@ -18,7 +18,7 @@ LL | Some(_z @ ref _y) => {}
| move occurs because `_z` has type `X` which does not implement the `Copy` trait

error: cannot move out of value because it is borrowed
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:28:14
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:26:14
|
LL | Some(ref mut _y @ _z) => {}
| ----------^^^--
Expand All @@ -27,7 +27,7 @@ LL | Some(ref mut _y @ _z) => {}
| value borrowed, by `_y`, here

error: borrow of moved value
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:35:14
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:33:14
|
LL | Some(_z @ ref mut _y) => {}
| --^^^----------
Expand All @@ -37,7 +37,7 @@ LL | Some(_z @ ref mut _y) => {}
| move occurs because `_z` has type `X` which does not implement the `Copy` trait

error[E0382]: borrow of moved value
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:14:14
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:12:14
|
LL | Some(ref _y @ _z) => {}
| ^^^^^^^^^--
Expand All @@ -52,7 +52,7 @@ LL | Some(ref _y @ ref _z) => {}
| ^^^

error[E0382]: borrow of moved value
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:28:14
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:26:14
|
LL | Some(ref mut _y @ _z) => {}
| ^^^^^^^^^^^^^--
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// See issue #12534.

#![feature(bindings_after_at)]

fn main() {}

struct A(Box<u8>);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0382]: use of partially moved value
--> $DIR/bind-by-move-no-subbindings-fun-param.rs:9:6
--> $DIR/bind-by-move-no-subbindings-fun-param.rs:7:6
|
LL | fn f(a @ A(u): A) -> Box<u8> {
| ^^^^^^-^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Test that moving on both sides of an `@` pattern is not allowed.

#![feature(bindings_after_at)]

fn main() {
struct U; // Not copy!

Expand Down
Loading

0 comments on commit 998cfe5

Please sign in to comment.