From 1a59dafe1198b9f36d7c72e536f5d57495de1b9d Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 25 Jul 2018 16:59:03 +0200 Subject: [PATCH] Bug fix: `#![feature(nll)]` takes precedence over `-Z borrowck=migrate`. (Includes test illustrating desired behavior; compare its diagnostic output to that of the file `borrowck-migreate-to-nll.rs`.) --- src/librustc/ty/context.rs | 17 ++++++++- .../borrowck-feature-nll-overrides-migrate.rs | 37 +++++++++++++++++++ ...rowck-feature-nll-overrides-migrate.stderr | 9 +++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/borrowck/borrowck-feature-nll-overrides-migrate.rs create mode 100644 src/test/ui/borrowck/borrowck-feature-nll-overrides-migrate.stderr diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index d30f656098dc1..94dc8e2901577 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1405,9 +1405,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { pub fn borrowck_mode(&self) -> BorrowckMode { match self.sess.opts.borrowck_mode { mode @ BorrowckMode::Mir | - mode @ BorrowckMode::Migrate | mode @ BorrowckMode::Compare => mode, + // `BorrowckMode::Ast` is synonymous with no `-Z + // borrowck=...` flag at all. Therefore, we definitely + // want `#![feature(nll)]` to override it. mode @ BorrowckMode::Ast => { if self.features().nll { BorrowckMode::Mir @@ -1416,6 +1418,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } } + // `BorrowckMode::Migrate` is modelling the behavior one + // will eventually specify via `--edition 2018`. We want + // to allow developers on the Nightly channel to opt back + // into the "hard error" mode for NLL, which they can do + // via specifying `#![feature(nll)]` explicitly in their + // crate. + mode @ BorrowckMode::Migrate => { + if self.features().nll { + BorrowckMode::Mir + } else { + mode + } + } } } diff --git a/src/test/ui/borrowck/borrowck-feature-nll-overrides-migrate.rs b/src/test/ui/borrowck/borrowck-feature-nll-overrides-migrate.rs new file mode 100644 index 0000000000000..b9cb97eeec1a1 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-feature-nll-overrides-migrate.rs @@ -0,0 +1,37 @@ +// 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 that the `#![feature(nll)]` opt-in overrides the +// migration mode. The intention here is to emulate the goal behavior +// that `--edition 2018` effects on borrowck (modeled here by `-Z +// borrowck=migrate`) are themselves overridden by the +// `#![feature(nll)]` opt-in. +// +// Therefore, for developer convenience, under `#[feature(nll)]` the +// NLL checks will be emitted as errors *even* in the presence of `-Z +// borrowck=migrate`. + +// compile-flags: -Z borrowck=migrate + +#![feature(nll)] + +fn main() { + match Some(&4) { + None => {}, + ref mut foo + if { + (|| { let bar = foo; bar.take() })(); + //~^ ERROR cannot move out of borrowed content [E0507] + 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-feature-nll-overrides-migrate.stderr b/src/test/ui/borrowck/borrowck-feature-nll-overrides-migrate.stderr new file mode 100644 index 0000000000000..f12da562f1b40 --- /dev/null +++ b/src/test/ui/borrowck/borrowck-feature-nll-overrides-migrate.stderr @@ -0,0 +1,9 @@ +error[E0507]: cannot move out of borrowed content + --> $DIR/borrowck-feature-nll-overrides-migrate.rs:30:17 + | +LL | (|| { let bar = foo; bar.take() })(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of borrowed content + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0507`.