Skip to content

Commit 9b5e8b1

Browse files
committed
Auto merge of #134523 - dingxiangfei2009:issue-130836-attempt-2, r=<try>
Run borrowck tests on BIDs and emit tail-expr-drop-order lints for violations Fix #132861 r? `@nikomatsakis` cc `@compiler-errors` This patch enlarges the scope where the `tail-expr-drop-order` lint applies, so that all locals involved in tail expressions are inspected. This is necessary to run borrow-checking to capture the cases where it used to compile under Edition 2021 but is not going to pass borrow-checking from Edition 2024 onwards. The way it works is to inspect each BID against the set of borrows that are still live. If the local involved in BID has a borrow index which happens to be live as well at the location of this BID statement, in the future this will be a borrow-checking violation. The lint will fire in this case.
2 parents 11663cd + 0a77622 commit 9b5e8b1

File tree

6 files changed

+155
-19
lines changed

6 files changed

+155
-19
lines changed

compiler/rustc_borrowck/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ borrowck_suggest_create_fresh_reborrow =
213213
borrowck_suggest_iterate_over_slice =
214214
consider iterating over a slice of the `{$ty}`'s content to avoid moving into the `for` loop
215215
216+
borrowck_tail_expr_drop_order = a temporary value will be dropped here before the execution exits the block in Edition 2024, which will raise borrow checking error
217+
.label = consider using a `let` binding to create a longer lived value; or replacing the `{"{"} .. {"}"}` block with curly brackets `( .. )`; or folding the rest of the expression into the surrounding `unsafe {"{"} .. {"}"}`
218+
216219
borrowck_ty_no_impl_copy =
217220
{$is_partial_move ->
218221
[true] partial move

compiler/rustc_borrowck/src/lib.rs

+65-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![warn(unreachable_pub)]
1616
// tidy-alphabetical-end
1717

18+
use std::borrow::Cow;
1819
use std::cell::RefCell;
1920
use std::collections::BTreeMap;
2021
use std::marker::PhantomData;
@@ -24,8 +25,8 @@ use rustc_abi::FieldIdx;
2425
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2526
use rustc_data_structures::graph::dominators::Dominators;
2627
use rustc_errors::Diag;
27-
use rustc_hir as hir;
2828
use rustc_hir::def_id::LocalDefId;
29+
use rustc_hir::{self as hir, CRATE_HIR_ID};
2930
use rustc_index::bit_set::{BitSet, MixedBitSet};
3031
use rustc_index::{IndexSlice, IndexVec};
3132
use rustc_infer::infer::{
@@ -44,7 +45,7 @@ use rustc_mir_dataflow::move_paths::{
4445
InitIndex, InitLocation, LookupResult, MoveData, MoveOutIndex, MovePathIndex,
4546
};
4647
use rustc_mir_dataflow::{Analysis, EntryStates, Results, ResultsVisitor, visit_results};
47-
use rustc_session::lint::builtin::UNUSED_MUT;
48+
use rustc_session::lint::builtin::{TAIL_EXPR_DROP_ORDER, UNUSED_MUT};
4849
use rustc_span::{Span, Symbol};
4950
use smallvec::SmallVec;
5051
use tracing::{debug, instrument};
@@ -622,9 +623,11 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
622623
| StatementKind::Coverage(..)
623624
// These do not actually affect borrowck
624625
| StatementKind::ConstEvalCounter
625-
// This do not affect borrowck
626-
| StatementKind::BackwardIncompatibleDropHint { .. }
627626
| StatementKind::StorageLive(..) => {}
627+
// This does not affect borrowck
628+
StatementKind::BackwardIncompatibleDropHint { place, reason: BackwardIncompatibleDropReason::Edition2024 } => {
629+
self.check_backward_incompatible_drop(location, (**place, span), state);
630+
}
628631
StatementKind::StorageDead(local) => {
629632
self.access_place(
630633
location,
@@ -994,6 +997,24 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
994997
}
995998
}
996999

1000+
fn borrows_in_scope<'s>(
1001+
&self,
1002+
location: Location,
1003+
state: &'s BorrowckDomain,
1004+
) -> Cow<'s, BitSet<BorrowIndex>> {
1005+
if let Some(polonius) = &self.polonius_output {
1006+
// Use polonius output if it has been enabled.
1007+
let location = self.location_table.start_index(location);
1008+
let mut polonius_output = BitSet::new_empty(self.borrow_set.len());
1009+
for &idx in polonius.errors_at(location) {
1010+
polonius_output.insert(idx);
1011+
}
1012+
Cow::Owned(polonius_output)
1013+
} else {
1014+
Cow::Borrowed(&state.borrows)
1015+
}
1016+
}
1017+
9971018
#[instrument(level = "debug", skip(self, state))]
9981019
fn check_access_for_conflict(
9991020
&mut self,
@@ -1005,18 +1026,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
10051026
) -> bool {
10061027
let mut error_reported = false;
10071028

1008-
// Use polonius output if it has been enabled.
1009-
let mut polonius_output;
1010-
let borrows_in_scope = if let Some(polonius) = &self.polonius_output {
1011-
let location = self.location_table.start_index(location);
1012-
polonius_output = BitSet::new_empty(self.borrow_set.len());
1013-
for &idx in polonius.errors_at(location) {
1014-
polonius_output.insert(idx);
1015-
}
1016-
&polonius_output
1017-
} else {
1018-
&state.borrows
1019-
};
1029+
let borrows_in_scope = self.borrows_in_scope(location, state);
10201030

10211031
each_borrow_involving_path(
10221032
self,
@@ -1136,6 +1146,45 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
11361146
error_reported
11371147
}
11381148

1149+
/// Through #123739, backward incompatible drops (BIDs) are introduced.
1150+
/// We would like to emit lints whether borrow checking fails at these future drop locations.
1151+
#[instrument(level = "debug", skip(self, state))]
1152+
fn check_backward_incompatible_drop(
1153+
&mut self,
1154+
location: Location,
1155+
place_span: (Place<'tcx>, Span),
1156+
state: &BorrowckDomain,
1157+
) {
1158+
let sd = AccessDepth::Drop;
1159+
1160+
let borrows_in_scope = self.borrows_in_scope(location, state);
1161+
1162+
// This is a very simplified version of `Self::check_access_for_conflict`.
1163+
// We are here checking on BIDs and specifically still-live borrows of data involving the BIDs.
1164+
each_borrow_involving_path(
1165+
self,
1166+
self.infcx.tcx,
1167+
self.body,
1168+
(sd, place_span.0),
1169+
self.borrow_set,
1170+
|borrow_index| borrows_in_scope.contains(borrow_index),
1171+
|this, _borrow_index, borrow| {
1172+
if matches!(borrow.kind, BorrowKind::Fake(_)) {
1173+
return Control::Continue;
1174+
}
1175+
let borrowed = this.retrieve_borrow_spans(borrow).var_or_use_path_span();
1176+
this.infcx.tcx.emit_node_span_lint(
1177+
TAIL_EXPR_DROP_ORDER,
1178+
CRATE_HIR_ID,
1179+
place_span.1,
1180+
session_diagnostics::TailExprDropOrder { borrowed },
1181+
);
1182+
// We may stop at the first case
1183+
Control::Break
1184+
},
1185+
);
1186+
}
1187+
11391188
fn mutate_place(
11401189
&mut self,
11411190
location: Location,

compiler/rustc_borrowck/src/session_diagnostics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,10 @@ pub(crate) struct SimdIntrinsicArgConst {
480480
pub arg: usize,
481481
pub intrinsic: String,
482482
}
483+
484+
#[derive(LintDiagnostic)]
485+
#[diag(borrowck_tail_expr_drop_order)]
486+
pub(crate) struct TailExprDropOrder {
487+
#[label]
488+
pub borrowed: Span,
489+
}

compiler/rustc_mir_build/src/builder/scope.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1131,15 +1131,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11311131

11321132
/// Schedule emission of a backwards incompatible drop lint hint.
11331133
/// Applicable only to temporary values for now.
1134+
#[instrument(level = "debug", skip(self))]
11341135
pub(crate) fn schedule_backwards_incompatible_drop(
11351136
&mut self,
11361137
span: Span,
11371138
region_scope: region::Scope,
11381139
local: Local,
11391140
) {
1140-
if !self.local_decls[local].ty.has_significant_drop(self.tcx, self.typing_env()) {
1141-
return;
1142-
}
1141+
// Note that we are *not* gating BIDs here on whether they have significant destructor.
1142+
// We need to know all of them so that we can capture potential borrow-checking errors.
11431143
for scope in self.scopes.scopes.iter_mut().rev() {
11441144
// Since we are inserting linting MIR statement, we have to invalidate the caches
11451145
scope.invalidate_cache();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Edition 2024 lint for change in drop order at tail expression
2+
// This lint is to capture potential borrow-checking errors
3+
// due to implementation of RFC 3606 <https://github.com/rust-lang/rfcs/pull/3606>
4+
//@ edition: 2021
5+
6+
#![deny(tail_expr_drop_order)] //~ NOTE: the lint level is defined here
7+
8+
fn should_lint_with_potential_borrowck_err() {
9+
let _ = { String::new().as_str() }.len();
10+
//~^ ERROR: a temporary value will be dropped here
11+
//~| WARN: this changes meaning in Rust 2024
12+
//~| NOTE: consider using a `let` binding
13+
//~| NOTE: for more information, see
14+
}
15+
16+
fn should_lint_with_unsafe_block() {
17+
fn f(_: usize) {}
18+
f(unsafe { String::new().as_str() }.len());
19+
//~^ ERROR: a temporary value will be dropped here
20+
//~| WARN: this changes meaning in Rust 2024
21+
//~| NOTE: consider using a `let` binding
22+
//~| NOTE: for more information, see
23+
}
24+
25+
#[rustfmt::skip]
26+
fn should_lint_with_big_block() {
27+
fn f<T>(_: T) {}
28+
f({
29+
&mut || 0
30+
//~^ ERROR: a temporary value will be dropped here
31+
//~| WARN: this changes meaning in Rust 2024
32+
//~| NOTE: consider using a `let` binding
33+
//~| NOTE: for more information, see
34+
})
35+
}
36+
37+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: a temporary value will be dropped here before the execution exits the block in Edition 2024, which will raise borrow checking error
2+
--> $DIR/lint-tail-expr-drop-order-borrowck.rs:9:36
3+
|
4+
LL | let _ = { String::new().as_str() }.len();
5+
| ------------- ^
6+
| |
7+
| consider using a `let` binding to create a longer lived value; or replacing the `{ .. }` block with curly brackets `( .. )`; or folding the rest of the expression into the surrounding `unsafe { .. }`
8+
|
9+
= warning: this changes meaning in Rust 2024
10+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-tail-expr-scope.html>
11+
note: the lint level is defined here
12+
--> $DIR/lint-tail-expr-drop-order-borrowck.rs:6:9
13+
|
14+
LL | #![deny(tail_expr_drop_order)]
15+
| ^^^^^^^^^^^^^^^^^^^^
16+
17+
error: a temporary value will be dropped here before the execution exits the block in Edition 2024, which will raise borrow checking error
18+
--> $DIR/lint-tail-expr-drop-order-borrowck.rs:18:37
19+
|
20+
LL | f(unsafe { String::new().as_str() }.len());
21+
| ------------- ^
22+
| |
23+
| consider using a `let` binding to create a longer lived value; or replacing the `{ .. }` block with curly brackets `( .. )`; or folding the rest of the expression into the surrounding `unsafe { .. }`
24+
|
25+
= warning: this changes meaning in Rust 2024
26+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-tail-expr-scope.html>
27+
28+
error: a temporary value will be dropped here before the execution exits the block in Edition 2024, which will raise borrow checking error
29+
--> $DIR/lint-tail-expr-drop-order-borrowck.rs:29:17
30+
|
31+
LL | &mut || 0
32+
| --------^
33+
| |
34+
| consider using a `let` binding to create a longer lived value; or replacing the `{ .. }` block with curly brackets `( .. )`; or folding the rest of the expression into the surrounding `unsafe { .. }`
35+
|
36+
= warning: this changes meaning in Rust 2024
37+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-tail-expr-scope.html>
38+
39+
error: aborting due to 3 previous errors
40+

0 commit comments

Comments
 (0)