Skip to content

Commit f18236d

Browse files
committed
Auto merge of rust-lang#110273 - flip1995:beta-backport, r=Manishearth
[beta] Clippy: backport optimization and ICE fixes The first commit optimizes a lint, that runs noticeably slow and is a perf issue. See rust-lang/rust-clippy#10533 The second commit fixes an ICE that occurred when running Clippy on `gitoxide` besides others. Since this is a rather popular crate, we want to fix this rather sooner than later. See rust-lang/rust-clippy#10403 --- Both commits are already on `master` and deployed on `nightly`.
2 parents 831d7c9 + a615440 commit f18236d

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

Diff for: src/tools/clippy/clippy_lints/src/dereference.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1357,10 +1357,10 @@ fn replace_types<'tcx>(
13571357
&& let Some(term_ty) = projection_predicate.term.ty()
13581358
&& let ty::Param(term_param_ty) = term_ty.kind()
13591359
{
1360-
let item_def_id = projection_predicate.projection_ty.def_id;
1361-
let assoc_item = cx.tcx.associated_item(item_def_id);
1362-
let projection = cx.tcx
1363-
.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(new_ty, []));
1360+
let projection = cx.tcx.mk_ty_from_kind(ty::Alias(
1361+
ty::Projection,
1362+
projection_predicate.projection_ty.with_self_ty(cx.tcx, new_ty),
1363+
));
13641364

13651365
if let Ok(projected_ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, projection)
13661366
&& substs[term_param_ty.index as usize] != ty::GenericArg::from(projected_ty)

Diff for: src/tools/clippy/clippy_lints/src/significant_drop_tightening.rs

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::FxHashSet;
21
use clippy_utils::{
32
diagnostics::span_lint_and_then,
43
get_attr,
54
source::{indent_of, snippet},
65
};
6+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
use rustc_errors::{Applicability, Diagnostic};
88
use rustc_hir::{
99
self as hir,
@@ -58,6 +58,7 @@ impl_lint_pass!(SignificantDropTightening<'_> => [SIGNIFICANT_DROP_TIGHTENING]);
5858
pub struct SignificantDropTightening<'tcx> {
5959
/// Auxiliary structure used to avoid having to verify the same type multiple times.
6060
seen_types: FxHashSet<Ty<'tcx>>,
61+
type_cache: FxHashMap<Ty<'tcx>, bool>,
6162
}
6263

6364
impl<'tcx> SignificantDropTightening<'tcx> {
@@ -118,7 +119,7 @@ impl<'tcx> SignificantDropTightening<'tcx> {
118119
stmt: &hir::Stmt<'_>,
119120
cb: impl Fn(&mut SigDropAuxParams),
120121
) {
121-
let mut sig_drop_finder = SigDropFinder::new(cx, &mut self.seen_types);
122+
let mut sig_drop_finder = SigDropFinder::new(cx, &mut self.seen_types, &mut self.type_cache);
122123
sig_drop_finder.visit_expr(expr);
123124
if sig_drop_finder.has_sig_drop {
124125
cb(sdap);
@@ -296,15 +297,24 @@ impl Default for SigDropAuxParams {
296297
struct SigDropChecker<'cx, 'sdt, 'tcx> {
297298
cx: &'cx LateContext<'tcx>,
298299
seen_types: &'sdt mut FxHashSet<Ty<'tcx>>,
300+
type_cache: &'sdt mut FxHashMap<Ty<'tcx>, bool>,
299301
}
300302

301303
impl<'cx, 'sdt, 'tcx> SigDropChecker<'cx, 'sdt, 'tcx> {
302-
pub(crate) fn new(cx: &'cx LateContext<'tcx>, seen_types: &'sdt mut FxHashSet<Ty<'tcx>>) -> Self {
304+
pub(crate) fn new(
305+
cx: &'cx LateContext<'tcx>,
306+
seen_types: &'sdt mut FxHashSet<Ty<'tcx>>,
307+
type_cache: &'sdt mut FxHashMap<Ty<'tcx>, bool>,
308+
) -> Self {
303309
seen_types.clear();
304-
Self { cx, seen_types }
310+
Self {
311+
cx,
312+
seen_types,
313+
type_cache,
314+
}
305315
}
306316

307-
pub(crate) fn has_sig_drop_attr(&mut self, ty: Ty<'tcx>) -> bool {
317+
pub(crate) fn has_sig_drop_attr_uncached(&mut self, ty: Ty<'tcx>) -> bool {
308318
if let Some(adt) = ty.ty_adt_def() {
309319
let mut iter = get_attr(
310320
self.cx.sess(),
@@ -340,6 +350,16 @@ impl<'cx, 'sdt, 'tcx> SigDropChecker<'cx, 'sdt, 'tcx> {
340350
}
341351
}
342352

353+
pub(crate) fn has_sig_drop_attr(&mut self, ty: Ty<'tcx>) -> bool {
354+
// The borrow checker prevents us from using something fancier like or_insert_with.
355+
if let Some(ty) = self.type_cache.get(&ty) {
356+
return *ty;
357+
}
358+
let value = self.has_sig_drop_attr_uncached(ty);
359+
self.type_cache.insert(ty, value);
360+
value
361+
}
362+
343363
fn has_seen_ty(&mut self, ty: Ty<'tcx>) -> bool {
344364
!self.seen_types.insert(ty)
345365
}
@@ -353,11 +373,15 @@ struct SigDropFinder<'cx, 'sdt, 'tcx> {
353373
}
354374

355375
impl<'cx, 'sdt, 'tcx> SigDropFinder<'cx, 'sdt, 'tcx> {
356-
fn new(cx: &'cx LateContext<'tcx>, seen_types: &'sdt mut FxHashSet<Ty<'tcx>>) -> Self {
376+
fn new(
377+
cx: &'cx LateContext<'tcx>,
378+
seen_types: &'sdt mut FxHashSet<Ty<'tcx>>,
379+
type_cache: &'sdt mut FxHashMap<Ty<'tcx>, bool>,
380+
) -> Self {
357381
Self {
358382
cx,
359383
has_sig_drop: false,
360-
sig_drop_checker: SigDropChecker::new(cx, seen_types),
384+
sig_drop_checker: SigDropChecker::new(cx, seen_types, type_cache),
361385
}
362386
}
363387
}

Diff for: src/tools/clippy/tests/ui/crashes/ice-rust-107877.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![allow(dead_code)]
2+
3+
struct Foo;
4+
5+
impl<'a> std::convert::TryFrom<&'a String> for Foo {
6+
type Error = std::convert::Infallible;
7+
8+
fn try_from(_: &'a String) -> Result<Self, Self::Error> {
9+
Ok(Foo)
10+
}
11+
}
12+
13+
fn find<E>(_: impl std::convert::TryInto<Foo, Error = E>) {}
14+
15+
fn main() {
16+
find(&String::new());
17+
}

0 commit comments

Comments
 (0)