Skip to content

Commit

Permalink
Bug fix: #![feature(nll)] takes precedence over -Z borrowck=migrate.
Browse files Browse the repository at this point in the history
(Includes test illustrating desired behavior; compare its diagnostic
output to that of the file `borrowck-migreate-to-nll.rs`.)
  • Loading branch information
pnkfelix committed Jul 26, 2018
1 parent f808405 commit 1a59daf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/librustc/ty/context.rs
Expand Up @@ -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
Expand All @@ -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
}
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions 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 <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.

// 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."),
}
}
@@ -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`.

0 comments on commit 1a59daf

Please sign in to comment.