Skip to content

Commit

Permalink
filter out ecosystem types earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Oct 31, 2024
1 parent 1bfa639 commit 509f9d0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_mir_transform/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ mir_transform_tail_expr_drop_order = relative drop order changing in Rust 2024
*[false] `{$name}` will be dropped later since Edition 2024
}
mir_transform_tail_expr_dtor = `{$name}` invokes this custom destructor
mir_transform_tail_expr_dtor = {$dtor_kind ->
[dyn] `{$name}` may invoke a custom destructor because contains a trait object
*[concrete] `{$name}` invokes this custom destructor
}
mir_transform_tail_expr_local = {$is_generated_name ->
[true] this value will be stored in a temporary; let us call it `{$name}`
Expand Down
71 changes: 58 additions & 13 deletions compiler/rustc_mir_transform/src/lint_tail_expr_drop_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::hash_map;
use std::rc::Rc;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::unord::UnordSet;
use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::Subdiagnostic;
use rustc_hir::CRATE_HIR_ID;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand All @@ -20,7 +20,7 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
use rustc_mir_dataflow::{Analysis, MaybeReachable, ResultsCursor};
use rustc_session::lint;
use rustc_span::{Span, Symbol};
use rustc_span::{DUMMY_SP, Span, Symbol};
use rustc_type_ir::data_structures::IndexMap;
use smallvec::{SmallVec, smallvec};
use tracing::{debug, instrument};
Expand Down Expand Up @@ -170,7 +170,9 @@ fn true_significant_drop_ty<'tcx>(
debug!(?name_str);
match name_str[..] {
// These are the types from Rust core ecosystem
["sym" | "proc_macro2", ..] | ["core" | "std", "task", "RawWaker"] => Some(smallvec![]),
["sym" | "proc_macro2", ..]
| ["core" | "std", "task", "LocalWaker" | "Waker"]
| ["core" | "std", "task", "wake", "LocalWaker" | "Waker"] => Some(smallvec![]),
// These are important types from Rust ecosystem
["tracing", "instrument", "Instrumented"] | ["bytes", "Bytes"] => Some(smallvec![]),
["hashbrown", "raw", "RawTable" | "RawIntoIter"] => {
Expand Down Expand Up @@ -328,9 +330,19 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
// because they tend to be scheduled in the same drop ladder block.
let mut bid_per_block = IndexMap::default();
let mut bid_places = UnordSet::new();
let param_env = tcx.param_env(def_id).with_reveal_all_normalized(tcx);
let mut ty_dropped_components = UnordMap::default();
for (block, data) in body.basic_blocks.iter_enumerated() {
for (statement_index, stmt) in data.statements.iter().enumerate() {
if let StatementKind::BackwardIncompatibleDropHint { place, reason: _ } = &stmt.kind {
let ty = place.ty(body, tcx).ty;
if ty_dropped_components
.entry(ty)
.or_insert_with(|| extract_component_with_significant_dtor(tcx, param_env, ty))
.is_empty()
{
continue;
}
bid_per_block
.entry(block)
.or_insert(vec![])
Expand All @@ -345,7 +357,6 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<

dump_mir(tcx, false, "lint_tail_expr_drop_order", &0 as _, body, |_, _| Ok(()));
let locals_with_user_names = collect_user_names(body);
let param_env = tcx.param_env(def_id).with_reveal_all_normalized(tcx);
let is_closure_like = tcx.is_closure_like(def_id.to_def_id());
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
let maybe_init = MaybeInitializedPlaces::new(tcx, body, &move_data);
Expand Down Expand Up @@ -402,7 +413,13 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
}
let observer_ty = move_path.place.ty(body, tcx).ty;
// d) The collect local has no custom destructor.
if !observer_ty.has_significant_drop(tcx, param_env) {
if ty_dropped_components
.entry(observer_ty)
.or_insert_with(|| {
extract_component_with_significant_dtor(tcx, param_env, observer_ty)
})
.is_empty()
{
debug!(?dropped_local, "skip non-droppy types");
to_exclude.insert(path_idx);
continue;
Expand Down Expand Up @@ -450,12 +467,26 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
}

// Collect spans of the custom destructors.
let destructors =
extract_component_with_significant_dtor(tcx, param_env, linted_local_decl.ty)
.into_iter()
.filter_map(|ty| ty_dtor_span(tcx, ty))
.map(|span| DestructorLabel { span, name })
.collect();
let mut seen_dyn = false;
let destructors = ty_dropped_components
.get(&linted_local_decl.ty)
.unwrap()
.iter()
.filter_map(|&ty| {
if let Some(span) = ty_dtor_span(tcx, ty) {
Some(DestructorLabel { span, name, dtor_kind: "concrete" })
} else if matches!(ty.kind(), ty::Dynamic(..)) {
if seen_dyn {
None
} else {
seen_dyn = true;
Some(DestructorLabel { span: DUMMY_SP, name, dtor_kind: "dyn" })
}
} else {
None
}
})
.collect();
local_labels.push(LocalLabel {
span: linted_local_decl.source_info.span,
destructors,
Expand All @@ -477,10 +508,23 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
};
let name = name.as_str();

let mut seen_dyn = false;
let destructors = extract_component_with_significant_dtor(tcx, param_env, observer_ty)
.into_iter()
.filter_map(|ty| ty_dtor_span(tcx, ty))
.map(|span| DestructorLabel { span, name })
.filter_map(|ty| {
if let Some(span) = ty_dtor_span(tcx, ty) {
Some(DestructorLabel { span, name, dtor_kind: "concrete" })
} else if matches!(ty.kind(), ty::Dynamic(..)) {
if seen_dyn {
None
} else {
seen_dyn = true;
Some(DestructorLabel { span: DUMMY_SP, name, dtor_kind: "dyn" })
}
} else {
None
}
})
.collect();
local_labels.push(LocalLabel {
span: observer_local_decl.source_info.span,
Expand Down Expand Up @@ -583,5 +627,6 @@ impl Subdiagnostic for LocalLabel<'_> {
struct DestructorLabel<'a> {
#[primary_span]
span: Span,
dtor_kind: &'static str,
name: &'a str,
}

0 comments on commit 509f9d0

Please sign in to comment.