Skip to content

Commit 3fee0f1

Browse files
committed
Auto merge of rust-lang#131326 - dingxiangfei2009:issue-130836-attempt-2, r=nikomatsakis
Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) r? `@nikomatsakis` Tracked by rust-lang#123739. Related to rust-lang#129864 but not replacing, yet. Related to rust-lang#130836. This is an implementation of the approach suggested in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/temporary.20drop.20order.20changes). A new MIR statement `BackwardsIncompatibleDrop` is added to the MIR syntax. The lint now works by inspecting possibly live move paths before at the `BackwardsIncompatibleDrop` location and the actual drop under the current edition, which should be one before Edition 2024 in practice.
2 parents a1f2999 + 297b618 commit 3fee0f1

File tree

58 files changed

+2011
-534
lines changed

Some content is hidden

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

58 files changed

+2011
-534
lines changed

compiler/rustc_borrowck/src/dataflow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
641641
| mir::StatementKind::Coverage(..)
642642
| mir::StatementKind::Intrinsic(..)
643643
| mir::StatementKind::ConstEvalCounter
644+
| mir::StatementKind::BackwardIncompatibleDropHint { .. }
644645
| mir::StatementKind::Nop => {}
645646
}
646647
}

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
652652
| StatementKind::Coverage(..)
653653
// These do not actually affect borrowck
654654
| StatementKind::ConstEvalCounter
655+
// This do not affect borrowck
656+
| StatementKind::BackwardIncompatibleDropHint { .. }
655657
| StatementKind::StorageLive(..) => {}
656658
StatementKind::StorageDead(local) => {
657659
self.access_place(

compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
8888
| StatementKind::Nop
8989
| StatementKind::Retag { .. }
9090
| StatementKind::Deinit(..)
91+
| StatementKind::BackwardIncompatibleDropHint { .. }
9192
| StatementKind::SetDiscriminant { .. } => {
9293
bug!("Statement not allowed in this MIR phase")
9394
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12521252
| StatementKind::Coverage(..)
12531253
| StatementKind::ConstEvalCounter
12541254
| StatementKind::PlaceMention(..)
1255+
| StatementKind::BackwardIncompatibleDropHint { .. }
12551256
| StatementKind::Nop => {}
12561257
StatementKind::Deinit(..) | StatementKind::SetDiscriminant { .. } => {
12571258
bug!("Statement not allowed in this MIR phase")

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ fn codegen_stmt<'tcx>(
924924
| StatementKind::FakeRead(..)
925925
| StatementKind::Retag { .. }
926926
| StatementKind::PlaceMention(..)
927+
| StatementKind::BackwardIncompatibleDropHint { .. }
927928
| StatementKind::AscribeUserType(..) => {}
928929

929930
StatementKind::Coverage { .. } => unreachable!(),

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
583583
| StatementKind::PlaceMention(..)
584584
| StatementKind::Coverage(_)
585585
| StatementKind::ConstEvalCounter
586+
| StatementKind::BackwardIncompatibleDropHint { .. }
586587
| StatementKind::Nop => {}
587588
}
588589
}

compiler/rustc_codegen_ssa/src/mir/statement.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9292
| mir::StatementKind::AscribeUserType(..)
9393
| mir::StatementKind::ConstEvalCounter
9494
| mir::StatementKind::PlaceMention(..)
95+
| mir::StatementKind::BackwardIncompatibleDropHint { .. }
9596
| mir::StatementKind::Nop => {}
9697
}
9798
}

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
609609
| StatementKind::Coverage(..)
610610
| StatementKind::Intrinsic(..)
611611
| StatementKind::ConstEvalCounter
612+
| StatementKind::BackwardIncompatibleDropHint { .. }
612613
| StatementKind::Nop => {}
613614
}
614615
}

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
143143
// Defined to do nothing. These are added by optimization passes, to avoid changing the
144144
// size of MIR constantly.
145145
Nop => {}
146+
147+
// Only used for temporary lifetime lints
148+
BackwardIncompatibleDropHint { .. } => {}
146149
}
147150

148151
interp_ok(())

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_index::Idx;
1717
use rustc_middle::bug;
1818
use rustc_middle::middle::region::*;
1919
use rustc_middle::ty::TyCtxt;
20+
use rustc_session::lint;
2021
use rustc_span::source_map;
2122
use tracing::debug;
2223

@@ -167,8 +168,23 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
167168
}
168169
}
169170
if let Some(tail_expr) = blk.expr {
170-
if blk.span.edition().at_least_rust_2024() {
171-
visitor.terminating_scopes.insert(tail_expr.hir_id.local_id);
171+
let local_id = tail_expr.hir_id.local_id;
172+
let edition = blk.span.edition();
173+
if edition.at_least_rust_2024() {
174+
visitor.terminating_scopes.insert(local_id);
175+
} else if !visitor
176+
.tcx
177+
.lints_that_dont_need_to_run(())
178+
.contains(&lint::LintId::of(lint::builtin::TAIL_EXPR_DROP_ORDER))
179+
{
180+
// If this temporary scope will be changing once the codebase adopts Rust 2024,
181+
// and we are linting about possible semantic changes that would result,
182+
// then record this node-id in the field `backwards_incompatible_scope`
183+
// for future reference.
184+
visitor
185+
.scope_tree
186+
.backwards_incompatible_scope
187+
.insert(local_id, Scope { id: local_id, data: ScopeData::Node });
172188
}
173189
visitor.visit_expr(tail_expr);
174190
}

0 commit comments

Comments
 (0)