Skip to content

implement feature gate bind_by_move_pattern_guards #42088

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [associated_consts](language-features/associated-consts.md)
- [associated_type_defaults](language-features/associated-type-defaults.md)
- [attr_literals](language-features/attr-literals.md)
- [bind_by_move_pattern_guards](language-features/bind_by_move_pattern_guards.md)
- [box_patterns](language-features/box-patterns.md)
- [box_syntax](language-features/box-syntax.md)
- [catch_expr](language-features/catch-expr.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `bind_by_move_pattern_guards`

The tracking issue for this feature is: [#15287]

[#15287]: https://github.com/rust-lang/rust/issues/15287

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



2 changes: 1 addition & 1 deletion src/librustc_const_eval/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
"cannot bind by-move with sub-bindings")
.span_label(p.span, "binds an already bound by-move value by moving it")
.emit();
} else if has_guard {
} else if has_guard && !cx.tcx.sess.features.borrow().bind_by_move_pattern_guards {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think that this doesn't suffice. Specifically, there are cases we want to disallow, such as this:

let x = Some(vec![1, 2, 3]);
match x {
    Some(v) if { mem::drop(v); true } => { println!("empty"); }
    Some(v) => { println!("not-empty: {:?}", v); }
    None => { println!("None!"); }
}

The danger is that v the vector will get freed after the first guard is executed.

I think that the RFC specifically stated that we should allow this but only if the guard only uses the value "by reference" (or if it only copies).

One way to do this analysis would be to deploy the ExprUseVisitor. I think the option I prefer at this point is to say that, when the guard executes, we treat the bound values as if there was an implicit shared reference (much like how a closure works). This probably requires an RFC, though I think it's backwards compatible.

In the example above, that would mean that references to v in the pattern guard would be treated as equivalent to *v0 where v0 is a synthetic variable with type &Vec<i32>; hence the mem::drop(v) guard would be equivalent to mem::drop(*v0), which would mean that it'd be an error (since you are moving through a borrowed reference).

struct_span_err!(cx.tcx.sess, p.span, E0008,
"cannot bind by-move into a pattern guard")
.span_label(p.span, "moves value into pattern guard")
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ declare_features! (

// Allows use of the :vis macro fragment specifier
(active, macro_vis_matcher, "1.18.0", Some(41022)),

// Allows use of pattern guards with Bind-By-Move
(active, bind_by_move_pattern_guards, "1.18.0", Some(15287)),
);

declare_features! (
Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/bind-by-move-move-in-guard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(bind_by_move_pattern_guards)]

use std::sync::Arc;
fn dispose(_x: Arc<bool>) { }

pub fn main() {
let p = Arc::new(true);
let x = Some(p);
match x {
Some(z) if {dispose(z); true} => { dispose(z); },//~ ERROR use of moved value: `z`
_ => panic!()
}
}

1 change: 1 addition & 0 deletions src/test/compile-fail/bind-by-move-no-guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// gate-test-bind_by_move_pattern_guards
use std::sync::mpsc::channel;

fn main() {
Expand Down
23 changes: 23 additions & 0 deletions src/test/run-pass/bind-by-move-with-guard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(bind_by_move_pattern_guards)]

use std::sync::Arc;
fn dispose(_x: Arc<bool>) { }

pub fn main() {
let p = Arc::new(true);
let x = Some(p);
match x {
Some(z) if z == Arc::new(true) => { dispose(z); },
_ => panic!()
}
}