Skip to content

Commit bb55bd4

Browse files
committed
Auto merge of #95565 - jackh726:remove-borrowck-mode, r=nikomatsakis
Remove migrate borrowck mode Closes #58781 Closes #43234 # Stabilization proposal This PR proposes the stabilization of `#![feature(nll)]` and the removal of `-Z borrowck`. Current borrow checking behavior of item bodies is currently done by first infering regions *lexically* and reporting any errors during HIR type checking. If there *are* any errors, then MIR borrowck (NLL) never occurs. If there *aren't* any errors, then MIR borrowck happens and any errors there would be reported. This PR removes the lexical region check of item bodies entirely and only uses MIR borrowck. Because MIR borrowck could never *not* be run for a compiled program, this should not break any programs. It does, however, change diagnostics significantly and allows a slightly larger set of programs to compile. Tracking issue: #43234 RFC: https://github.com/rust-lang/rfcs/blob/master/text/2094-nll.md Version: 1.63 (2022-06-30 => beta, 2022-08-11 => stable). ## Motivation Over time, the Rust borrow checker has become "smarter" and thus allowed more programs to compile. There have been three different implementations: AST borrowck, MIR borrowck, and polonius (well, in progress). Additionally, there is the "lexical region resolver", which (roughly) solves the constraints generated through HIR typeck. It is not a full borrow checker, but does emit some errors. The AST borrowck was the original implementation of the borrow checker and was part of the initially stabilized Rust 1.0. In mid 2017, work began to implement the current MIR borrow checker and that effort ompleted by the end of 2017, for the most part. During 2018, efforts were made to migrate away from the AST borrow checker to the MIR borrow checker - eventually culminating into "migrate" mode - where HIR typeck with lexical region resolving following by MIR borrow checking - being active by default in the 2018 edition. In early 2019, migrate mode was turned on by default in the 2015 edition as well, but with MIR borrowck errors emitted as warnings. By late 2019, these warnings were upgraded to full errors. This was followed by the complete removal of the AST borrow checker. In the period since, various errors emitted by the MIR borrow checker have been improved to the point that they are mostly the same or better than those emitted by the lexical region resolver. While there do remain some degradations in errors (tracked under the [NLL-diagnostics tag](https://github.com/rust-lang/rust/issues?q=is%3Aopen+is%3Aissue+label%3ANLL-diagnostics), those are sufficiently small and rare enough that increased flexibility of MIR borrow check-only is now a worthwhile tradeoff. ## What is stabilized As said previously, this does not fundamentally change the landscape of accepted programs. However, there are a [few](https://github.com/rust-lang/rust/issues?q=is%3Aopen+is%3Aissue+label%3ANLL-fixed-by-NLL) cases where programs can compile under `feature(nll)`, but not otherwise. There are two notable patterns that are "fixed" by this stabilization. First, the `scoped_threads` feature, which is a continutation of a pre-1.0 API, can sometimes emit a [weird lifetime error](#95527) without NLL. Second, actually seen in the standard library. In the `Extend` impl for `HashMap`, there is an implied bound of `K: 'a` that is available with NLL on but not without - this is utilized in the impl. As mentioned before, there are a large number of diagnostic differences. Most of them are better, but some are worse. None are serious or happen often enough to need to block this PR. The biggest change is the loss of error code for a number of lifetime errors in favor of more general "lifetime may not live long enough" error. While this may *seem* bad, the former error codes were just attempts to somewhat-arbitrarily bin together lifetime errors of the same type; however, on paper, they end up being roughly the same with roughly the same kinds of solutions. ## What isn't stabilized This PR does not completely remove the lexical region resolver. In the future, it may be possible to remove that (while still keeping HIR typeck) or to remove it together with HIR typeck. ## Tests Many test outputs get updated by this PR. However, there are number of tests specifically geared towards NLL under `src/test/ui/nll` ## History * On 2017-07-14, [tracking issue opened](#43234) * On 2017-07-20, [initial empty MIR pass added](#43271) * On 2017-08-29, [RFC opened](rust-lang/rfcs#2094) * On 2017-11-16, [Integrate MIR type-checker with NLL](#45825) * On 2017-12-20, [NLL feature complete](#46862) * On 2018-07-07, [Don't run AST borrowck on mir mode](#52083) * On 2018-07-27, [Add migrate mode](#52681) * On 2019-04-22, [Enable migrate mode on 2015 edition](#59114) * On 2019-08-26, [Don't downgrade errors on 2015 edition](#64221) * On 2019-08-27, [Remove AST borrowck](#64790)
2 parents 9f7e997 + 410dcc9 commit bb55bd4

File tree

985 files changed

+1815
-12128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

985 files changed

+1815
-12128
lines changed

compiler/rustc_apfloat/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
3434
#![no_std]
3535
#![forbid(unsafe_code)]
36-
#![feature(nll)]
3736

3837
#[macro_use]
3938
extern crate alloc;

compiler/rustc_ast/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#![feature(let_chains)]
1818
#![feature(min_specialization)]
1919
#![feature(negative_impls)]
20-
#![feature(nll)]
2120
#![feature(slice_internals)]
2221
#![feature(stmt_expr_attributes)]
2322
#![recursion_limit = "256"]

compiler/rustc_builtin_macros/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#![feature(is_sorted)]
1010
#![feature(let_chains)]
1111
#![feature(let_else)]
12-
#![feature(nll)]
1312
#![feature(proc_macro_internals)]
1413
#![feature(proc_macro_quote)]
1514
#![recursion_limit = "256"]

compiler/rustc_codegen_llvm/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#![feature(let_else)]
1010
#![feature(extern_types)]
1111
#![feature(once_cell)]
12-
#![feature(nll)]
1312
#![feature(iter_intersperse)]
1413
#![recursion_limit = "256"]
1514
#![allow(rustc::potential_query_instability)]

compiler/rustc_codegen_ssa/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![feature(try_blocks)]
44
#![feature(let_else)]
55
#![feature(once_cell)]
6-
#![feature(nll)]
76
#![feature(associated_type_bounds)]
87
#![feature(strict_provenance)]
98
#![feature(int_roundings)]

compiler/rustc_driver/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//! This API is completely unstable and subject to change.
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8-
#![feature(nll)]
98
#![feature(let_else)]
109
#![feature(once_cell)]
1110
#![recursion_limit = "256"]

compiler/rustc_error_codes/src/error_codes/E0312.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
Reference's lifetime of borrowed content doesn't match the expected lifetime.
24

35
Erroneous code example:
46

5-
```compile_fail,E0312
7+
```compile_fail
68
pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'static str {
79
if maybestr.is_none() {
810
"(none)"

compiler/rustc_error_codes/src/error_codes/E0477.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
The type does not fulfill the required lifetime.
24

35
Erroneous code example:
46

5-
```compile_fail,E0477
7+
```compile_fail
68
use std::sync::Mutex;
79
810
struct MyString<'a> {

compiler/rustc_error_codes/src/error_codes/E0495.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
A lifetime cannot be determined in the given situation.
24

35
Erroneous code example:
46

5-
```compile_fail,E0495
7+
```compile_fail
68
fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T {
79
match (&t,) { // error!
810
((u,),) => u,

compiler/rustc_error_codes/src/error_codes/E0623.md

+48-17
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,70 @@ A lifetime didn't match what was expected.
33
Erroneous code example:
44

55
```compile_fail,E0623
6-
struct Foo<'a> {
7-
x: &'a isize,
8-
}
6+
struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
7+
where
8+
T: Convert<'a, 'b>;
99
10-
fn bar<'short, 'long>(c: Foo<'short>, l: &'long isize) {
11-
let _: Foo<'long> = c; // error!
10+
trait Convert<'a, 'b>: Sized {
11+
fn cast(&'a self) -> &'b Self;
12+
}
13+
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
14+
fn cast(&'long self) -> &'short T {
15+
self
16+
}
17+
}
18+
// error
19+
fn badboi<'in_, 'out, T>(
20+
x: Foo<'in_, 'out, T>,
21+
sadness: &'in_ T
22+
) -> &'out T {
23+
sadness.cast()
1224
}
1325
```
1426

1527
In this example, we tried to set a value with an incompatible lifetime to
16-
another one (`'long` is unrelated to `'short`). We can solve this issue in
28+
another one (`'in_` is unrelated to `'out`). We can solve this issue in
1729
two different ways:
1830

19-
Either we make `'short` live at least as long as `'long`:
31+
Either we make `'in_` live at least as long as `'out`:
2032

2133
```
22-
struct Foo<'a> {
23-
x: &'a isize,
24-
}
34+
struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
35+
where
36+
T: Convert<'a, 'b>;
2537
26-
// we set 'short to live at least as long as 'long
27-
fn bar<'short: 'long, 'long>(c: Foo<'short>, l: &'long isize) {
28-
let _: Foo<'long> = c; // ok!
38+
trait Convert<'a, 'b>: Sized {
39+
fn cast(&'a self) -> &'b Self;
40+
}
41+
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
42+
fn cast(&'long self) -> &'short T {
43+
self
44+
}
45+
}
46+
fn badboi<'in_: 'out, 'out, T>(
47+
x: Foo<'in_, 'out, T>,
48+
sadness: &'in_ T
49+
) -> &'out T {
50+
sadness.cast()
2951
}
3052
```
3153

3254
Or we use only one lifetime:
3355

3456
```
35-
struct Foo<'a> {
36-
x: &'a isize,
57+
struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
58+
where
59+
T: Convert<'a, 'b>;
60+
61+
trait Convert<'a, 'b>: Sized {
62+
fn cast(&'a self) -> &'b Self;
63+
}
64+
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
65+
fn cast(&'long self) -> &'short T {
66+
self
67+
}
3768
}
38-
fn bar<'short>(c: Foo<'short>, l: &'short isize) {
39-
let _: Foo<'short> = c; // ok!
69+
fn badboi<'out, T>(x: Foo<'out, 'out, T>, sadness: &'out T) -> &'out T {
70+
sadness.cast()
4071
}
4172
```

compiler/rustc_error_codes/src/error_codes/E0713.md

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ lifetime of a type that implements the `Drop` trait.
44
Erroneous code example:
55

66
```compile_fail,E0713
7-
#![feature(nll)]
8-
97
pub struct S<'a> { data: &'a mut String }
108
119
impl<'a> Drop for S<'a> {

compiler/rustc_error_codes/src/error_codes/E0759.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
Return type involving a trait did not require `'static` lifetime.
24

35
Erroneous code examples:
46

5-
```compile_fail,E0759
7+
```compile_fail
68
use std::fmt::Debug;
79
810
fn foo(x: &i32) -> impl Debug { // error!

compiler/rustc_error_codes/src/error_codes/E0772.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
A trait object has some specific lifetime `'1`, but it was used in a way that
24
requires it to have a `'static` lifetime.
35

46
Example of erroneous code:
57

6-
```compile_fail,E0772
8+
```compile_fail
79
trait BooleanLike {}
810
trait Person {}
911

compiler/rustc_errors/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#![feature(if_let_guard)]
99
#![feature(let_else)]
1010
#![feature(never_type)]
11-
#![feature(nll)]
1211
#![feature(adt_const_params)]
1312
#![allow(incomplete_features)]
1413
#![allow(rustc::potential_query_instability)]

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ declare_features! (
221221
(accepted, native_link_modifiers, "1.61.0", Some(81490), None),
222222
/// Allows specifying the whole-archive link modifier
223223
(accepted, native_link_modifiers_whole_archive, "1.61.0", Some(81490), None),
224+
/// Allows using non lexical lifetimes (RFC 2094).
225+
(accepted, nll, "1.63.0", Some(43234), None),
224226
/// Allows using `#![no_std]`.
225227
(accepted, no_std, "1.6.0", None, None),
226228
/// Allows defining identifiers beyond ASCII.

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,6 @@ declare_features! (
461461
(active, never_type, "1.13.0", Some(35121), None),
462462
/// Allows diverging expressions to fall back to `!` rather than `()`.
463463
(active, never_type_fallback, "1.41.0", Some(65992), None),
464-
/// Allows using non lexical lifetimes (RFC 2094).
465-
(active, nll, "1.0.0", Some(43234), None),
466464
/// Allows `#![no_core]`.
467465
(active, no_core, "1.3.0", Some(29639), None),
468466
/// Allows function attribute `#[no_coverage]`, to bypass coverage

compiler/rustc_graphviz/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@
273273
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
274274
test(attr(allow(unused_variables), deny(warnings)))
275275
)]
276-
#![feature(nll)]
277276

278277
use LabelText::*;
279278

compiler/rustc_incremental/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![deny(missing_docs)]
44
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
55
#![feature(let_else)]
6-
#![feature(nll)]
76
#![recursion_limit = "256"]
87
#![allow(rustc::potential_query_instability)]
98

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,7 @@ pub(crate) fn resolve<'tcx>(
4242
let values = resolver.infer_variable_values(&mut errors);
4343
(values, errors)
4444
}
45-
RegionckMode::Erase { suppress_errors: false } => {
46-
// Do real inference to get errors, then erase the results.
47-
let mut values = resolver.infer_variable_values(&mut errors);
48-
let re_erased = region_rels.tcx.lifetimes.re_erased;
49-
50-
values.values.iter_mut().for_each(|v| match *v {
51-
VarValue::Value(ref mut r) => *r = re_erased,
52-
VarValue::ErrorValue => {}
53-
});
54-
(values, errors)
55-
}
56-
RegionckMode::Erase { suppress_errors: true } => {
45+
RegionckMode::Erase => {
5746
// Skip region inference entirely.
5847
(resolver.erased_data(region_rels.tcx), Vec::new())
5948
}

compiler/rustc_infer/src/infer/mod.rs

+1-24
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Substs
2929
pub use rustc_middle::ty::IntVarValue;
3030
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
3131
use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
32-
use rustc_session::config::BorrowckMode;
3332
use rustc_span::symbol::Symbol;
3433
use rustc_span::Span;
3534

@@ -97,29 +96,7 @@ pub enum RegionckMode {
9796
#[default]
9897
Solve,
9998
/// Erase the results of region after solving.
100-
Erase {
101-
/// A flag that is used to suppress region errors, when we are doing
102-
/// region checks that the NLL borrow checker will also do -- it might
103-
/// be set to true.
104-
suppress_errors: bool,
105-
},
106-
}
107-
108-
impl RegionckMode {
109-
/// Indicates that the MIR borrowck will repeat these region
110-
/// checks, so we should ignore errors if NLL is (unconditionally)
111-
/// enabled.
112-
pub fn for_item_body(tcx: TyCtxt<'_>) -> Self {
113-
// FIXME(Centril): Once we actually remove `::Migrate` also make
114-
// this always `true` and then proceed to eliminate the dead code.
115-
match tcx.borrowck_mode() {
116-
// If we're on Migrate mode, report AST region errors
117-
BorrowckMode::Migrate => RegionckMode::Erase { suppress_errors: false },
118-
119-
// If we're on MIR, don't report AST region errors as they should be reported by NLL
120-
BorrowckMode::Mir => RegionckMode::Erase { suppress_errors: true },
121-
}
122-
}
99+
Erase,
123100
}
124101

125102
/// This type contains all the things within `InferCtxt` that sit within a

compiler/rustc_interface/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![feature(let_else)]
33
#![feature(internal_output_capture)]
44
#![feature(thread_spawn_unchecked)]
5-
#![feature(nll)]
65
#![feature(once_cell)]
76
#![recursion_limit = "256"]
87
#![allow(rustc::potential_query_instability)]

compiler/rustc_interface/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,6 @@ fn test_debugging_options_tracking_hash() {
644644
// Make sure that changing an [UNTRACKED] option leaves the hash unchanged.
645645
// This list is in alphabetical order.
646646
untracked!(assert_incr_state, Some(String::from("loaded")));
647-
untracked!(borrowck, String::from("other"));
648647
untracked!(deduplicate_diagnostics, false);
649648
untracked!(dep_tasks, true);
650649
untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));

compiler/rustc_lint/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#![feature(let_chains)]
3737
#![feature(let_else)]
3838
#![feature(never_type)]
39-
#![feature(nll)]
4039
#![recursion_limit = "256"]
4140

4241
#[macro_use]

compiler/rustc_llvm/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(nll)]
21
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
32

43
// NOTE: This crate only exists to allow linking on mingw targets.

compiler/rustc_metadata/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#![feature(iter_from_generator)]
77
#![feature(let_chains)]
88
#![feature(let_else)]
9-
#![feature(nll)]
109
#![feature(once_cell)]
1110
#![feature(proc_macro_internals)]
1211
#![feature(macro_metavar_expr)]

compiler/rustc_middle/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#![feature(never_type)]
4040
#![feature(extern_types)]
4141
#![feature(new_uninit)]
42-
#![feature(nll)]
4342
#![feature(once_cell)]
4443
#![feature(let_chains)]
4544
#![feature(let_else)]

0 commit comments

Comments
 (0)