From 4b8dc182944063d55b8642f5e428f2f5f21db7cb Mon Sep 17 00:00:00 2001 From: Andreas Jonson Date: Thu, 18 May 2017 20:08:16 +0200 Subject: [PATCH 1/2] implement feature gate bind_by_move_pattern_guards implementation of issue #15287 --- src/doc/unstable-book/src/SUMMARY.md | 1 + .../bind_by_move_pattern_guards.md | 10 ++++++++ src/librustc_const_eval/check_match.rs | 2 +- src/libsyntax/feature_gate.rs | 3 +++ .../bind-by-move-move-in-guard.rs | 24 +++++++++++++++++++ .../compile-fail/bind-by-move-no-guards.rs | 1 + src/test/run-pass/bind-by-move-with-guard.rs | 23 ++++++++++++++++++ 7 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/doc/unstable-book/src/language-features/bind_by_move_pattern_guards.md create mode 100644 src/test/compile-fail/bind-by-move-move-in-guard.rs create mode 100644 src/test/run-pass/bind-by-move-with-guard.rs diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index 39f800591483b..eb0771bcdb27a 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -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) diff --git a/src/doc/unstable-book/src/language-features/bind_by_move_pattern_guards.md b/src/doc/unstable-book/src/language-features/bind_by_move_pattern_guards.md new file mode 100644 index 0000000000000..01dcd4c589e87 --- /dev/null +++ b/src/doc/unstable-book/src/language-features/bind_by_move_pattern_guards.md @@ -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 + +------------------------ + + + diff --git a/src/librustc_const_eval/check_match.rs b/src/librustc_const_eval/check_match.rs index a18f91a9ee391..830630c0b4e48 100644 --- a/src/librustc_const_eval/check_match.rs +++ b/src/librustc_const_eval/check_match.rs @@ -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 { 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") diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 09090ab873130..1e118832b8823 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -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! ( diff --git a/src/test/compile-fail/bind-by-move-move-in-guard.rs b/src/test/compile-fail/bind-by-move-move-in-guard.rs new file mode 100644 index 0000000000000..03284f31a9e50 --- /dev/null +++ b/src/test/compile-fail/bind-by-move-move-in-guard.rs @@ -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 or the MIT license +// , 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) { } + +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!() + } +} + diff --git a/src/test/compile-fail/bind-by-move-no-guards.rs b/src/test/compile-fail/bind-by-move-no-guards.rs index bb6060f2543e2..3869dedf7b592 100644 --- a/src/test/compile-fail/bind-by-move-no-guards.rs +++ b/src/test/compile-fail/bind-by-move-no-guards.rs @@ -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() { diff --git a/src/test/run-pass/bind-by-move-with-guard.rs b/src/test/run-pass/bind-by-move-with-guard.rs new file mode 100644 index 0000000000000..524272ab70a17 --- /dev/null +++ b/src/test/run-pass/bind-by-move-with-guard.rs @@ -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 or the MIT license +// , 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) { } + +pub fn main() { + let p = Arc::new(true); + let x = Some(p); + match x { + Some(z) if z == true => { dispose(z); }, + None => panic!() + } +} From 6029d4c12e21ff0f4836b7d26b6eeac0efada65c Mon Sep 17 00:00:00 2001 From: Andreas Jonson Date: Thu, 18 May 2017 22:32:57 +0200 Subject: [PATCH 2/2] fix failing testcase --- src/test/run-pass/bind-by-move-with-guard.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/run-pass/bind-by-move-with-guard.rs b/src/test/run-pass/bind-by-move-with-guard.rs index 524272ab70a17..818969eb38c1c 100644 --- a/src/test/run-pass/bind-by-move-with-guard.rs +++ b/src/test/run-pass/bind-by-move-with-guard.rs @@ -17,7 +17,7 @@ pub fn main() { let p = Arc::new(true); let x = Some(p); match x { - Some(z) if z == true => { dispose(z); }, - None => panic!() + Some(z) if z == Arc::new(true) => { dispose(z); }, + _ => panic!() } }