Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7014e13

Browse files
committedSep 25, 2024
Auto merge of #129864 - dingxiangfei2009:reduce-false-positives-from-consumed-droppers, r=<try>
Reduce false positives of tail-expr-drop-order from consumed values r? `@jieyouxu` To reduce false positives, the lint does not fire if the locals are consumed/moved, or the values with significant drop are consumed/moved at the tail expression location. For this, we rely on solving a small dataflow to determine whether a `Local` is either live, or moved/dropped. I am also printing the type involved for easier diagnosis.
2 parents 38352b0 + 2457860 commit 7014e13

File tree

35 files changed

+612
-384
lines changed

35 files changed

+612
-384
lines changed
 

‎Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4090,6 +4090,7 @@ dependencies = [
40904090
"rustc_hir",
40914091
"rustc_index",
40924092
"rustc_infer",
4093+
"rustc_lint",
40934094
"rustc_macros",
40944095
"rustc_middle",
40954096
"rustc_mir_build",

‎compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
691691
TerminatorKind::SwitchInt { discr, targets: _ } => {
692692
self.consume_operand(loc, (discr, span), state);
693693
}
694-
TerminatorKind::Drop { place, target: _, unwind: _, replace } => {
694+
TerminatorKind::Drop { place, target: _, scope: _, unwind: _, replace } => {
695695
debug!(
696696
"visit_terminator_drop \
697697
loc: {:?} term: {:?} place: {:?} span: {:?}",

‎compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
103103
TerminatorKind::SwitchInt { discr, targets: _ } => {
104104
self.consume_operand(location, discr);
105105
}
106-
TerminatorKind::Drop { place: drop_place, target: _, unwind: _, replace } => {
106+
TerminatorKind::Drop { place: drop_place, target: _, scope: _, unwind: _, replace } => {
107107
let write_kind =
108108
if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
109109
self.access_place(

‎compiler/rustc_codegen_cranelift/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
549549
| TerminatorKind::CoroutineDrop => {
550550
bug!("shouldn't exist at codegen {:?}", bb_data.terminator());
551551
}
552-
TerminatorKind::Drop { place, target, unwind: _, replace: _ } => {
552+
TerminatorKind::Drop { place, target, unwind: _, scope: _, replace: _ } => {
553553
let drop_place = codegen_place(fx, *place);
554554
crate::abi::codegen_drop(fx, source_info, drop_place, *target);
555555
}

‎compiler/rustc_codegen_ssa/src/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13221322
MergingSucc::False
13231323
}
13241324

1325-
mir::TerminatorKind::Drop { place, target, unwind, replace: _ } => self
1325+
mir::TerminatorKind::Drop { place, target, unwind, scope: _, replace: _ } => self
13261326
.codegen_drop_terminator(
13271327
helper,
13281328
bx,

‎compiler/rustc_const_eval/src/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
528528
}
529529
}
530530

531-
Drop { place, target, unwind, replace: _ } => {
531+
Drop { place, target, unwind, scope: _, replace: _ } => {
532532
let place = self.eval_place(place)?;
533533
let instance = Instance::resolve_drop_in_place(*self.tcx, place.layout.ty);
534534
if let ty::InstanceKind::DropGlue(_, None) = instance.def {

‎compiler/rustc_index/src/bit_set.rs

+58-2
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ impl<T: Idx> ChunkedBitSet<T> {
460460
self.chunks.iter().map(|chunk| chunk.count()).sum()
461461
}
462462

463+
pub fn is_empty(&self) -> bool {
464+
self.chunks.iter().all(|chunk| matches!(chunk, Chunk::Zeros(..) | Chunk::Ones(0)))
465+
}
466+
463467
/// Returns `true` if `self` contains `elem`.
464468
#[inline]
465469
pub fn contains(&self, elem: T) -> bool {
@@ -672,8 +676,60 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
672676
unimplemented!("implement if/when necessary");
673677
}
674678

675-
fn intersect(&mut self, _other: &ChunkedBitSet<T>) -> bool {
676-
unimplemented!("implement if/when necessary");
679+
fn intersect(&mut self, other: &ChunkedBitSet<T>) -> bool {
680+
assert_eq!(self.domain_size, other.domain_size);
681+
debug_assert_eq!(self.chunks.len(), other.chunks.len());
682+
683+
let mut changed = false;
684+
for (mut self_chunk, other_chunk) in self.chunks.iter_mut().zip(other.chunks.iter()) {
685+
match (&mut self_chunk, &other_chunk) {
686+
(Zeros(..), _) | (_, Ones(..)) => {}
687+
(
688+
Ones(self_chunk_domain_size),
689+
Zeros(other_chunk_domain_size) | Mixed(other_chunk_domain_size, ..),
690+
)
691+
| (Mixed(self_chunk_domain_size, ..), Zeros(other_chunk_domain_size)) => {
692+
debug_assert_eq!(self_chunk_domain_size, other_chunk_domain_size);
693+
changed = true;
694+
*self_chunk = other_chunk.clone();
695+
}
696+
(
697+
Mixed(
698+
self_chunk_domain_size,
699+
ref mut self_chunk_count,
700+
ref mut self_chunk_words,
701+
),
702+
Mixed(_other_chunk_domain_size, _other_chunk_count, other_chunk_words),
703+
) => {
704+
// See [`<Self as BitRelations<ChunkedBitSet<T>>>::union`] for the explanation
705+
let op = |a, b| a & b;
706+
let num_words = num_words(*self_chunk_domain_size as usize);
707+
if bitwise_changes(
708+
&self_chunk_words[0..num_words],
709+
&other_chunk_words[0..num_words],
710+
op,
711+
) {
712+
let self_chunk_words = Rc::make_mut(self_chunk_words);
713+
let has_changed = bitwise(
714+
&mut self_chunk_words[0..num_words],
715+
&other_chunk_words[0..num_words],
716+
op,
717+
);
718+
debug_assert!(has_changed);
719+
*self_chunk_count = self_chunk_words[0..num_words]
720+
.iter()
721+
.map(|w| w.count_ones() as ChunkSize)
722+
.sum();
723+
if *self_chunk_count == 0 {
724+
*self_chunk = Zeros(*self_chunk_domain_size);
725+
}
726+
changed = true;
727+
}
728+
}
729+
}
730+
}
731+
732+
changed
677733
}
678734
}
679735

‎compiler/rustc_lint/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,6 @@ lint_suspicious_double_ref_clone =
778778
lint_suspicious_double_ref_deref =
779779
using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type
780780
781-
lint_tail_expr_drop_order = these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
782-
.label = these values have significant drop implementation and will observe changes in drop order under Edition 2024
783-
784781
lint_trailing_semi_macro = trailing semicolon in macro used in expression position
785782
.note1 = macro invocations at the end of a block are treated as expressions
786783
.note2 = to ignore the value produced by the macro, add a semicolon after the invocation of `{$name}`

‎compiler/rustc_lint/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ mod redundant_semicolon;
8282
mod reference_casting;
8383
mod shadowed_into_iter;
8484
mod static_mut_refs;
85-
mod tail_expr_drop_order;
8685
mod traits;
8786
mod types;
8887
mod unit_bindings;
@@ -123,7 +122,6 @@ use rustc_middle::ty::TyCtxt;
123122
use shadowed_into_iter::ShadowedIntoIter;
124123
pub use shadowed_into_iter::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER};
125124
use static_mut_refs::*;
126-
use tail_expr_drop_order::TailExprDropOrder;
127125
use traits::*;
128126
use types::*;
129127
use unit_bindings::*;
@@ -248,7 +246,6 @@ late_lint_methods!(
248246
AsyncFnInTrait: AsyncFnInTrait,
249247
NonLocalDefinitions: NonLocalDefinitions::default(),
250248
ImplTraitOvercaptures: ImplTraitOvercaptures,
251-
TailExprDropOrder: TailExprDropOrder,
252249
IfLetRescope: IfLetRescope::default(),
253250
StaticMutRefs: StaticMutRefs,
254251
UnqualifiedLocalImports: UnqualifiedLocalImports,
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.