Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3bb52fb

Browse files
committed
Auto merge of rust-lang#134523 - dingxiangfei2009:issue-130836-attempt-2, r=nikomatsakis
Run borrowck tests on BIDs and emit tail-expr-drop-order lints for violations Fix rust-lang#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 fb546ee + 1c69947 commit 3bb52fb

17 files changed

+310
-48
lines changed

compiler/rustc_borrowck/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ 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 = relative drop order changing in Rust 2024
217+
.label = this temporary value will be dropped at the end of the block
218+
.note = consider using a `let` binding to ensure the value will live long enough
219+
216220
borrowck_ty_no_impl_copy =
217221
{$is_partial_move ->
218222
[true] partial move

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use std::assert_matches::assert_matches;
77

8-
use rustc_errors::{Applicability, Diag};
8+
use rustc_errors::{Applicability, Diag, EmissionGuarantee};
99
use rustc_hir as hir;
1010
use rustc_hir::intravisit::Visitor;
1111
use rustc_index::IndexSlice;
@@ -61,12 +61,12 @@ impl<'tcx> BorrowExplanation<'tcx> {
6161
pub(crate) fn is_explained(&self) -> bool {
6262
!matches!(self, BorrowExplanation::Unexplained)
6363
}
64-
pub(crate) fn add_explanation_to_diagnostic(
64+
pub(crate) fn add_explanation_to_diagnostic<G: EmissionGuarantee>(
6565
&self,
6666
tcx: TyCtxt<'tcx>,
6767
body: &Body<'tcx>,
6868
local_names: &IndexSlice<Local, Option<Symbol>>,
69-
err: &mut Diag<'_>,
69+
err: &mut Diag<'_, G>,
7070
borrow_desc: &str,
7171
borrow_span: Option<Span>,
7272
multiple_borrow_span: Option<(Span, Span)>,
@@ -349,10 +349,10 @@ impl<'tcx> BorrowExplanation<'tcx> {
349349
}
350350
}
351351

352-
fn add_object_lifetime_default_note(
352+
fn add_object_lifetime_default_note<G: EmissionGuarantee>(
353353
&self,
354354
tcx: TyCtxt<'tcx>,
355-
err: &mut Diag<'_>,
355+
err: &mut Diag<'_, G>,
356356
unsize_ty: Ty<'tcx>,
357357
) {
358358
if let ty::Adt(def, args) = unsize_ty.kind() {
@@ -406,9 +406,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
406406
}
407407
}
408408

409-
fn add_lifetime_bound_suggestion_to_diagnostic(
409+
fn add_lifetime_bound_suggestion_to_diagnostic<G: EmissionGuarantee>(
410410
&self,
411-
err: &mut Diag<'_>,
411+
err: &mut Diag<'_, G>,
412412
category: &ConstraintCategory<'tcx>,
413413
span: Span,
414414
region_name: &RegionName,
@@ -435,14 +435,14 @@ impl<'tcx> BorrowExplanation<'tcx> {
435435
}
436436
}
437437

438-
fn suggest_rewrite_if_let(
438+
fn suggest_rewrite_if_let<G: EmissionGuarantee>(
439439
tcx: TyCtxt<'_>,
440440
expr: &hir::Expr<'_>,
441441
pat: &str,
442442
init: &hir::Expr<'_>,
443443
conseq: &hir::Expr<'_>,
444444
alt: Option<&hir::Expr<'_>>,
445-
err: &mut Diag<'_>,
445+
err: &mut Diag<'_, G>,
446446
) {
447447
let source_map = tcx.sess.source_map();
448448
err.span_note(

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
992992
kind,
993993
};
994994
}
995+
995996
normal_ret
996997
}
997998

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt::{self, Display};
55
use std::iter;
66

77
use rustc_data_structures::fx::IndexEntry;
8-
use rustc_errors::Diag;
8+
use rustc_errors::{Diag, EmissionGuarantee};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{DefKind, Res};
1111
use rustc_middle::ty::print::RegionHighlightMode;
@@ -108,7 +108,7 @@ impl RegionName {
108108
}
109109
}
110110

111-
pub(crate) fn highlight_region_name(&self, diag: &mut Diag<'_>) {
111+
pub(crate) fn highlight_region_name<G: EmissionGuarantee>(&self, diag: &mut Diag<'_, G>) {
112112
match &self.source {
113113
RegionNameSource::NamedLateParamRegion(span)
114114
| RegionNameSource::NamedEarlyParamRegion(span) => {

compiler/rustc_borrowck/src/lib.rs

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
#![warn(unreachable_pub)]
1616
// tidy-alphabetical-end
1717

18+
use std::borrow::Cow;
1819
use std::cell::RefCell;
1920
use std::marker::PhantomData;
2021
use std::ops::Deref;
2122

2223
use rustc_abi::FieldIdx;
2324
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2425
use rustc_data_structures::graph::dominators::Dominators;
26+
use rustc_errors::LintDiagnostic;
2527
use rustc_hir as hir;
28+
use rustc_hir::CRATE_HIR_ID;
2629
use rustc_hir::def_id::LocalDefId;
2730
use rustc_index::bit_set::{BitSet, MixedBitSet};
2831
use rustc_index::{IndexSlice, IndexVec};
@@ -42,7 +45,7 @@ use rustc_mir_dataflow::move_paths::{
4245
InitIndex, InitLocation, LookupResult, MoveData, MovePathIndex,
4346
};
4447
use rustc_mir_dataflow::{Analysis, EntryStates, Results, ResultsVisitor, visit_results};
45-
use rustc_session::lint::builtin::UNUSED_MUT;
48+
use rustc_session::lint::builtin::{TAIL_EXPR_DROP_ORDER, UNUSED_MUT};
4649
use rustc_span::{Span, Symbol};
4750
use smallvec::SmallVec;
4851
use tracing::{debug, instrument};
@@ -636,9 +639,11 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
636639
| StatementKind::Coverage(..)
637640
// These do not actually affect borrowck
638641
| StatementKind::ConstEvalCounter
639-
// This do not affect borrowck
640-
| StatementKind::BackwardIncompatibleDropHint { .. }
641642
| StatementKind::StorageLive(..) => {}
643+
// This does not affect borrowck
644+
StatementKind::BackwardIncompatibleDropHint { place, reason: BackwardIncompatibleDropReason::Edition2024 } => {
645+
self.check_backward_incompatible_drop(location, (**place, span), state);
646+
}
642647
StatementKind::StorageDead(local) => {
643648
self.access_place(
644649
location,
@@ -1007,6 +1012,24 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
10071012
}
10081013
}
10091014

1015+
fn borrows_in_scope<'s>(
1016+
&self,
1017+
location: Location,
1018+
state: &'s BorrowckDomain,
1019+
) -> Cow<'s, BitSet<BorrowIndex>> {
1020+
if let Some(polonius) = &self.polonius_output {
1021+
// Use polonius output if it has been enabled.
1022+
let location = self.location_table.start_index(location);
1023+
let mut polonius_output = BitSet::new_empty(self.borrow_set.len());
1024+
for &idx in polonius.errors_at(location) {
1025+
polonius_output.insert(idx);
1026+
}
1027+
Cow::Owned(polonius_output)
1028+
} else {
1029+
Cow::Borrowed(&state.borrows)
1030+
}
1031+
}
1032+
10101033
#[instrument(level = "debug", skip(self, state))]
10111034
fn check_access_for_conflict(
10121035
&mut self,
@@ -1018,18 +1041,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
10181041
) -> bool {
10191042
let mut error_reported = false;
10201043

1021-
// Use polonius output if it has been enabled.
1022-
let mut polonius_output;
1023-
let borrows_in_scope = if let Some(polonius) = &self.polonius_output {
1024-
let location = self.location_table.start_index(location);
1025-
polonius_output = BitSet::new_empty(self.borrow_set.len());
1026-
for &idx in polonius.errors_at(location) {
1027-
polonius_output.insert(idx);
1028-
}
1029-
&polonius_output
1030-
} else {
1031-
&state.borrows
1032-
};
1044+
let borrows_in_scope = self.borrows_in_scope(location, state);
10331045

10341046
each_borrow_involving_path(
10351047
self,
@@ -1149,6 +1161,69 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
11491161
error_reported
11501162
}
11511163

1164+
/// Through #123739, backward incompatible drops (BIDs) are introduced.
1165+
/// We would like to emit lints whether borrow checking fails at these future drop locations.
1166+
#[instrument(level = "debug", skip(self, state))]
1167+
fn check_backward_incompatible_drop(
1168+
&mut self,
1169+
location: Location,
1170+
(place, place_span): (Place<'tcx>, Span),
1171+
state: &BorrowckDomain,
1172+
) {
1173+
let tcx = self.infcx.tcx;
1174+
// If this type does not need `Drop`, then treat it like a `StorageDead`.
1175+
// This is needed because we track the borrows of refs to thread locals,
1176+
// and we'll ICE because we don't track borrows behind shared references.
1177+
let sd = if place.ty(self.body, tcx).ty.needs_drop(tcx, self.body.typing_env(tcx)) {
1178+
AccessDepth::Drop
1179+
} else {
1180+
AccessDepth::Shallow(None)
1181+
};
1182+
1183+
let borrows_in_scope = self.borrows_in_scope(location, state);
1184+
1185+
// This is a very simplified version of `Self::check_access_for_conflict`.
1186+
// We are here checking on BIDs and specifically still-live borrows of data involving the BIDs.
1187+
each_borrow_involving_path(
1188+
self,
1189+
self.infcx.tcx,
1190+
self.body,
1191+
(sd, place),
1192+
self.borrow_set,
1193+
|borrow_index| borrows_in_scope.contains(borrow_index),
1194+
|this, _borrow_index, borrow| {
1195+
if matches!(borrow.kind, BorrowKind::Fake(_)) {
1196+
return Control::Continue;
1197+
}
1198+
let borrowed = this.retrieve_borrow_spans(borrow).var_or_use_path_span();
1199+
let explain = this.explain_why_borrow_contains_point(
1200+
location,
1201+
borrow,
1202+
Some((WriteKind::StorageDeadOrDrop, place)),
1203+
);
1204+
this.infcx.tcx.node_span_lint(
1205+
TAIL_EXPR_DROP_ORDER,
1206+
CRATE_HIR_ID,
1207+
borrowed,
1208+
|diag| {
1209+
session_diagnostics::TailExprDropOrder { borrowed }.decorate_lint(diag);
1210+
explain.add_explanation_to_diagnostic(
1211+
tcx,
1212+
this.body,
1213+
&this.local_names,
1214+
diag,
1215+
"",
1216+
None,
1217+
None,
1218+
);
1219+
},
1220+
);
1221+
// We may stop at the first case
1222+
Control::Break
1223+
},
1224+
);
1225+
}
1226+
11521227
fn mutate_place(
11531228
&mut self,
11541229
location: Location,

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 7 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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();

compiler/rustc_mir_transform/src/lint_tail_expr_drop_order.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,19 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
351351
{
352352
return;
353353
}
354+
355+
// FIXME(typing_env): This should be able to reveal the opaques local to the
356+
// body using the typeck results.
357+
let typing_env = ty::TypingEnv::non_body_analysis(tcx, def_id);
358+
354359
// ## About BIDs in blocks ##
355360
// Track the set of blocks that contain a backwards-incompatible drop (BID)
356361
// and, for each block, the vector of locations.
357362
//
358363
// We group them per-block because they tend to scheduled in the same drop ladder block.
359364
let mut bid_per_block = IndexMap::default();
360365
let mut bid_places = UnordSet::new();
361-
let typing_env = ty::TypingEnv::post_analysis(tcx, def_id);
366+
362367
let mut ty_dropped_components = UnordMap::default();
363368
for (block, data) in body.basic_blocks.iter_enumerated() {
364369
for (statement_index, stmt) in data.statements.iter().enumerate() {

tests/mir-opt/tail_expr_drop_order_unwind.method_1.ElaborateDrops.after.panic-abort.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn method_1(_1: Guard) -> () {
7474

7575
bb7: {
7676
backward incompatible drop(_2);
77+
backward incompatible drop(_4);
7778
backward incompatible drop(_5);
7879
goto -> bb21;
7980
}

tests/mir-opt/tail_expr_drop_order_unwind.method_1.ElaborateDrops.after.panic-unwind.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn method_1(_1: Guard) -> () {
7474

7575
bb7: {
7676
backward incompatible drop(_2);
77+
backward incompatible drop(_4);
7778
backward incompatible drop(_5);
7879
goto -> bb21;
7980
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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: relative drop order changing
11+
//~| WARN: this changes meaning in Rust 2024
12+
//~| NOTE: this temporary value will be dropped at the end of the block
13+
//~| borrow later used by call
14+
//~| NOTE: for more information, see
15+
}
16+
17+
fn should_lint_with_unsafe_block() {
18+
fn f(_: usize) {}
19+
f(unsafe { String::new().as_str() }.len());
20+
//~^ ERROR: relative drop order changing
21+
//~| WARN: this changes meaning in Rust 2024
22+
//~| NOTE: this temporary value will be dropped at the end of the block
23+
//~| borrow later used by call
24+
//~| NOTE: for more information, see
25+
}
26+
27+
#[rustfmt::skip]
28+
fn should_lint_with_big_block() {
29+
fn f<T>(_: T) {}
30+
f({
31+
&mut || 0
32+
//~^ ERROR: relative drop order changing
33+
//~| WARN: this changes meaning in Rust 2024
34+
//~| NOTE: this temporary value will be dropped at the end of the block
35+
//~| borrow later used here
36+
//~| NOTE: for more information, see
37+
})
38+
}
39+
40+
fn another_temp_that_is_copy_in_arg() {
41+
fn f() {}
42+
fn g(_: &()) {}
43+
g({ &f() });
44+
//~^ ERROR: relative drop order changing
45+
//~| WARN: this changes meaning in Rust 2024
46+
//~| NOTE: this temporary value will be dropped at the end of the block
47+
//~| borrow later used by call
48+
//~| NOTE: for more information, see
49+
}
50+
51+
fn main() {}

0 commit comments

Comments
 (0)