1
Fork 0

Rollup merge of #103415 - compiler-errors:tiny-perf-increase-on-diagnostic, r=TaKO8Ki

filter candidates in pick probe for diagnostics

Fixes #103411, though also fine with closing this PR if my opinion (https://github.com/rust-lang/rust/issues/103411#issuecomment-1287900069) is shared that this doesn't need to  be fixed.

```
~/rust3$ time rustc +nightly ~/test.rs 2>/dev/null

real    0m4.853s
user    0m4.837s
sys     0m0.016s

~/rust3$ time rustc +rust3 ~/test.rs 2>/dev/null

real    0m0.193s
user    0m0.169s
sys     0m0.024s
```

Also fixes #103427.
This commit is contained in:
Guillaume Gomez 2022-10-29 14:18:02 +02:00 committed by GitHub
commit 07b5c6bdaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 22 deletions

View file

@ -530,9 +530,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
checked_ty: Ty<'tcx>, checked_ty: Ty<'tcx>,
hir_id: hir::HirId, hir_id: hir::HirId,
) -> Vec<AssocItem> { ) -> Vec<AssocItem> {
let mut methods = let methods = self.probe_for_return_type(
self.probe_for_return_type(span, probe::Mode::MethodCall, expected, checked_ty, hir_id); span,
methods.retain(|m| { probe::Mode::MethodCall,
expected,
checked_ty,
hir_id,
|m| {
self.has_only_self_parameter(m) self.has_only_self_parameter(m)
&& self && self
.tcx .tcx
@ -547,7 +551,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// FIXME? Other potential candidate methods: `as_ref` and // FIXME? Other potential candidate methods: `as_ref` and
// `as_mut`? // `as_mut`?
.has_attr(m.def_id, sym::rustc_conversion_suggestion) .has_attr(m.def_id, sym::rustc_conversion_suggestion)
}); },
);
methods methods
} }

View file

@ -252,7 +252,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// would result in an error (basically, the same criteria we /// would result in an error (basically, the same criteria we
/// would use to decide if a method is a plausible fit for /// would use to decide if a method is a plausible fit for
/// ambiguity purposes). /// ambiguity purposes).
#[instrument(level = "debug", skip(self))] #[instrument(level = "debug", skip(self, candidate_filter))]
pub fn probe_for_return_type( pub fn probe_for_return_type(
&self, &self,
span: Span, span: Span,
@ -260,6 +260,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return_type: Ty<'tcx>, return_type: Ty<'tcx>,
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
scope_expr_id: hir::HirId, scope_expr_id: hir::HirId,
candidate_filter: impl Fn(&ty::AssocItem) -> bool,
) -> Vec<ty::AssocItem> { ) -> Vec<ty::AssocItem> {
let method_names = self let method_names = self
.probe_op( .probe_op(
@ -271,7 +272,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self_ty, self_ty,
scope_expr_id, scope_expr_id,
ProbeScope::AllTraits, ProbeScope::AllTraits,
|probe_cx| Ok(probe_cx.candidate_method_names()), |probe_cx| Ok(probe_cx.candidate_method_names(candidate_filter)),
) )
.unwrap_or_default(); .unwrap_or_default();
method_names method_names
@ -966,12 +967,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
} }
} }
fn candidate_method_names(&self) -> Vec<Ident> { fn candidate_method_names(
&self,
candidate_filter: impl Fn(&ty::AssocItem) -> bool,
) -> Vec<Ident> {
let mut set = FxHashSet::default(); let mut set = FxHashSet::default();
let mut names: Vec<_> = self let mut names: Vec<_> = self
.inherent_candidates .inherent_candidates
.iter() .iter()
.chain(&self.extension_candidates) .chain(&self.extension_candidates)
.filter(|candidate| candidate_filter(&candidate.item))
.filter(|candidate| { .filter(|candidate| {
if let Some(return_ty) = self.return_type { if let Some(return_ty) = self.return_type {
self.matches_return_type(&candidate.item, None, return_ty) self.matches_return_type(&candidate.item, None, return_ty)
@ -1689,7 +1694,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
pcx.allow_similar_names = true; pcx.allow_similar_names = true;
pcx.assemble_inherent_candidates(); pcx.assemble_inherent_candidates();
let method_names = pcx.candidate_method_names(); let method_names = pcx.candidate_method_names(|_| true);
pcx.allow_similar_names = false; pcx.allow_similar_names = false;
let applicable_close_candidates: Vec<ty::AssocItem> = method_names let applicable_close_candidates: Vec<ty::AssocItem> = method_names
.iter() .iter()