Skip to content

Commit

Permalink
reduce false positives of tail-expr-drop-order from consumed values
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Sep 22, 2024
1 parent 8ed95d1 commit 08ec439
Show file tree
Hide file tree
Showing 37 changed files with 801 additions and 373 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4090,6 +4090,7 @@ dependencies = [
"rustc_hir",
"rustc_index",
"rustc_infer",
"rustc_lint",
"rustc_macros",
"rustc_middle",
"rustc_mir_build",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
TerminatorKind::SwitchInt { discr, targets: _ } => {
self.consume_operand(loc, (discr, span), state);
}
TerminatorKind::Drop { place, target: _, unwind: _, replace } => {
TerminatorKind::Drop { place, target: _, scope: _, unwind: _, replace } => {
debug!(
"visit_terminator_drop \
loc: {:?} term: {:?} place: {:?} span: {:?}",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/polonius/loan_invalidations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
TerminatorKind::SwitchInt { discr, targets: _ } => {
self.consume_operand(location, discr);
}
TerminatorKind::Drop { place: drop_place, target: _, unwind: _, replace } => {
TerminatorKind::Drop { place: drop_place, target: _, scope: _, unwind: _, replace } => {
let write_kind =
if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
self.access_place(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
| TerminatorKind::CoroutineDrop => {
bug!("shouldn't exist at codegen {:?}", bb_data.terminator());
}
TerminatorKind::Drop { place, target, unwind: _, replace: _ } => {
TerminatorKind::Drop { place, target, unwind: _, scope: _, replace: _ } => {
let drop_place = codegen_place(fx, *place);
crate::abi::codegen_drop(fx, source_info, drop_place, *target);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
MergingSucc::False
}

mir::TerminatorKind::Drop { place, target, unwind, replace: _ } => self
mir::TerminatorKind::Drop { place, target, unwind, scope: _, replace: _ } => self
.codegen_drop_terminator(
helper,
bx,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
}
}

Drop { place, target, unwind, replace: _ } => {
Drop { place, target, unwind, scope: _, replace: _ } => {
let place = self.eval_place(place)?;
let instance = Instance::resolve_drop_in_place(*self.tcx, place.layout.ty);
if let ty::InstanceKind::DropGlue(_, None) = instance.def {
Expand Down
44 changes: 44 additions & 0 deletions compiler/rustc_hir_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,50 @@ pub trait TypeInformationCtxt<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx>;
}

impl<'tcx> TypeInformationCtxt<'tcx> for (TyCtxt<'tcx>, LocalDefId) {
type TypeckResults<'a> = &'tcx ty::TypeckResults<'tcx>
where
Self: 'a;

type Error = !;

fn typeck_results(&self) -> Self::TypeckResults<'_> {
self.0.typeck(self.1)
}

fn resolve_vars_if_possible<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
t
}

fn try_structurally_resolve_type(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
ty
}

fn report_error(&self, span: Span, msg: impl ToString) -> Self::Error {
span_bug!(span, "{}", msg.to_string())
}

fn error_reported_in_ty(&self, _ty: Ty<'tcx>) -> Result<(), Self::Error> {
Ok(())
}

fn tainted_by_errors(&self) -> Result<(), Self::Error> {
Ok(())
}

fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>) -> bool {
ty.is_copy_modulo_regions(self.0, self.0.param_env(self.1))
}

fn body_owner_def_id(&self) -> LocalDefId {
self.1
}

fn tcx(&self) -> TyCtxt<'tcx> {
self.0
}
}

impl<'tcx> TypeInformationCtxt<'tcx> for &FnCtxt<'_, 'tcx> {
type TypeckResults<'a> = Ref<'a, ty::TypeckResults<'tcx>>
where
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod fallback;
mod fn_ctxt;
mod gather_locals;
mod intrinsicck;
mod lint;
mod method;
mod op;
mod pat;
Expand Down Expand Up @@ -506,5 +507,6 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {

pub fn provide(providers: &mut Providers) {
method::provide(providers);
lint::provide(providers);
*providers = Providers { typeck, diagnostic_only_typeck, used_trait_imports, ..*providers };
}
67 changes: 67 additions & 0 deletions compiler/rustc_hir_typeck/src/lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use rustc_hir::def_id::DefId;
use rustc_hir::{HirId, HirIdSet};
use rustc_middle::hir::place::{PlaceBase, PlaceWithHirId};
use rustc_middle::mir::FakeReadCause;
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
use tracing::instrument;

use crate::expr_use_visitor::{Delegate, ExprUseVisitor};

#[derive(Default)]
struct ExtractConsumingNodeDelegate {
nodes: HirIdSet,
}

impl<'tcx> Delegate<'tcx> for ExtractConsumingNodeDelegate {
#[instrument(level = "debug", skip(self))]
fn consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>, _: HirId) {
match place_with_id.place.base {
PlaceBase::Rvalue => {
self.nodes.insert(place_with_id.hir_id);
}
PlaceBase::Local(id) => {
self.nodes.insert(id);
}
PlaceBase::Upvar(upvar) => {
self.nodes.insert(upvar.var_path.hir_id);
}
PlaceBase::StaticItem => {}
}
}

fn borrow(
&mut self,
_place_with_id: &PlaceWithHirId<'tcx>,
_diag_expr_id: HirId,
_bk: ty::BorrowKind,
) {
// do nothing
}

fn mutate(&mut self, _assignee_place: &PlaceWithHirId<'tcx>, _diag_expr_id: HirId) {
// do nothing
}

fn fake_read(
&mut self,
_place_with_id: &PlaceWithHirId<'tcx>,
_cause: FakeReadCause,
_diag_expr_id: HirId,
) {
// do nothing
}
}

fn extract_tail_expr_consuming_nodes<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx HirIdSet {
let hir = tcx.hir();
let body = hir.body_owned_by(def_id.expect_local());
let mut delegate = ExtractConsumingNodeDelegate::default();
let euv = ExprUseVisitor::new((tcx, def_id.expect_local()), &mut delegate);
let _ = euv.walk_expr(body.value);
tcx.arena.alloc(delegate.nodes)
}

pub(crate) fn provide(providers: &mut Providers) {
providers.extract_tail_expr_consuming_nodes = extract_tail_expr_consuming_nodes;
}
3 changes: 0 additions & 3 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -789,9 +789,6 @@ lint_suspicious_double_ref_clone =
lint_suspicious_double_ref_deref =
using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type
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
.label = these values have significant drop implementation and will observe changes in drop order under Edition 2024
lint_trailing_semi_macro = trailing semicolon in macro used in expression position
.note1 = macro invocations at the end of a block are treated as expressions
.note2 = to ignore the value produced by the macro, add a semicolon after the invocation of `{$name}`
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ mod redundant_semicolon;
mod reference_casting;
mod shadowed_into_iter;
mod static_mut_refs;
mod tail_expr_drop_order;
mod traits;
mod types;
mod unit_bindings;
Expand Down Expand Up @@ -122,7 +121,6 @@ use rustc_middle::ty::TyCtxt;
use shadowed_into_iter::ShadowedIntoIter;
pub use shadowed_into_iter::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER};
use static_mut_refs::*;
use tail_expr_drop_order::TailExprDropOrder;
use traits::*;
use types::*;
use unit_bindings::*;
Expand Down Expand Up @@ -246,7 +244,6 @@ late_lint_methods!(
AsyncFnInTrait: AsyncFnInTrait,
NonLocalDefinitions: NonLocalDefinitions::default(),
ImplTraitOvercaptures: ImplTraitOvercaptures,
TailExprDropOrder: TailExprDropOrder,
IfLetRescope: IfLetRescope::default(),
StaticMutRefs: StaticMutRefs,
]
Expand Down
Loading

0 comments on commit 08ec439

Please sign in to comment.