From f8084053280d4557c603f3cfeb6b0f12cffb46e3 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 24 Jul 2018 13:58:19 +0200 Subject: [PATCH] Test for `-Z borrowck=migrate`. Note that this test is carefully crafted to *try* to not segfault during its run. Howver, it really is representing unsound code that should be rejected after we manage to remove the AST-borrowck entirely from the compiler. --- .../ui/borrowck/borrowck-migrate-to-nll.rs | 32 +++++++++++++++++++ .../borrowck/borrowck-migrate-to-nll.stderr | 24 ++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/test/ui/borrowck/borrowck-migrate-to-nll.rs create mode 100644 src/test/ui/borrowck/borrowck-migrate-to-nll.stderr diff --git a/src/test/ui/borrowck/borrowck-migrate-to-nll.rs b/src/test/ui/borrowck/borrowck-migrate-to-nll.rs new file mode 100644 index 000000000000..cac595e6ae5c --- /dev/null +++ b/src/test/ui/borrowck/borrowck-migrate-to-nll.rs @@ -0,0 +1,32 @@ +// Copyright 2017 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. + +// This is a test of the borrowck migrate mode. It leverages #27282, a +// bug that is fixed by NLL: this code is (unsoundly) accepted by +// AST-borrowck, but is correctly rejected by the NLL borrowck. +// +// Therefore, for backwards-compatiblity, under borrowck=migrate the +// NLL checks will be emitted as *warnings*. + +// compile-flags: -Z borrowck=migrate +// run-pass + +fn main() { + match Some(&4) { + None => {}, + ref mut foo + if { + (|| { let bar = foo; bar.take() })(); + false + } => {}, + Some(ref _s) => println!("Note this arm is bogus; the `Some` became `None` in the guard."), + _ => println!("Here is some supposedly unreachable code."), + } +} diff --git a/src/test/ui/borrowck/borrowck-migrate-to-nll.stderr b/src/test/ui/borrowck/borrowck-migrate-to-nll.stderr new file mode 100644 index 000000000000..115b09466735 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-migrate-to-nll.stderr @@ -0,0 +1,24 @@ +warning[E0507]: cannot move out of borrowed content + --> $DIR/borrowck-migrate-to-nll.rs:26:17 + | +LL | (|| { let bar = foo; bar.take() })(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of borrowed content + | + = warning: This error has been downgraded to a warning for backwards compatibility with previous releases. + It represents potential unsoundness in your code. + This warning will become a hard error in the future. + +warning[E0507]: cannot move out of `foo`, as it is immutable for the pattern guard + --> $DIR/borrowck-migrate-to-nll.rs:26:17 + | +LL | (|| { let bar = foo; bar.take() })(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | cannot move out of `foo`, as it is immutable for the pattern guard + | cannot move + | + = note: variables bound in patterns are immutable until the end of the pattern guard + = warning: This error has been downgraded to a warning for backwards compatibility with previous releases. + It represents potential unsoundness in your code. + This warning will become a hard error in the future. +