Skip to content

Commit 1a59daf

Browse files
committed
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`.)
1 parent f808405 commit 1a59daf

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/librustc/ty/context.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,9 +1405,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
14051405
pub fn borrowck_mode(&self) -> BorrowckMode {
14061406
match self.sess.opts.borrowck_mode {
14071407
mode @ BorrowckMode::Mir |
1408-
mode @ BorrowckMode::Migrate |
14091408
mode @ BorrowckMode::Compare => mode,
14101409

1410+
// `BorrowckMode::Ast` is synonymous with no `-Z
1411+
// borrowck=...` flag at all. Therefore, we definitely
1412+
// want `#![feature(nll)]` to override it.
14111413
mode @ BorrowckMode::Ast => {
14121414
if self.features().nll {
14131415
BorrowckMode::Mir
@@ -1416,6 +1418,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
14161418
}
14171419
}
14181420

1421+
// `BorrowckMode::Migrate` is modelling the behavior one
1422+
// will eventually specify via `--edition 2018`. We want
1423+
// to allow developers on the Nightly channel to opt back
1424+
// into the "hard error" mode for NLL, which they can do
1425+
// via specifying `#![feature(nll)]` explicitly in their
1426+
// crate.
1427+
mode @ BorrowckMode::Migrate => {
1428+
if self.features().nll {
1429+
BorrowckMode::Mir
1430+
} else {
1431+
mode
1432+
}
1433+
}
14191434
}
14201435
}
14211436

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This is a test that the `#![feature(nll)]` opt-in overrides the
12+
// migration mode. The intention here is to emulate the goal behavior
13+
// that `--edition 2018` effects on borrowck (modeled here by `-Z
14+
// borrowck=migrate`) are themselves overridden by the
15+
// `#![feature(nll)]` opt-in.
16+
//
17+
// Therefore, for developer convenience, under `#[feature(nll)]` the
18+
// NLL checks will be emitted as errors *even* in the presence of `-Z
19+
// borrowck=migrate`.
20+
21+
// compile-flags: -Z borrowck=migrate
22+
23+
#![feature(nll)]
24+
25+
fn main() {
26+
match Some(&4) {
27+
None => {},
28+
ref mut foo
29+
if {
30+
(|| { let bar = foo; bar.take() })();
31+
//~^ ERROR cannot move out of borrowed content [E0507]
32+
false
33+
} => {},
34+
Some(ref _s) => println!("Note this arm is bogus; the `Some` became `None` in the guard."),
35+
_ => println!("Here is some supposedly unreachable code."),
36+
}
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0507]: cannot move out of borrowed content
2+
--> $DIR/borrowck-feature-nll-overrides-migrate.rs:30:17
3+
|
4+
LL | (|| { let bar = foo; bar.take() })();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of borrowed content
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)