Skip to content

Commit 52dd59e

Browse files
committed
Auto merge of #93298 - lcnr:issue-92113, r=cjgillot
make `find_similar_impl_candidates` even fuzzier continues the good work of `@BGR360` in #92223. I might have overshot a bit and we're now slightly too fuzzy 😅 with this we can now also simplify `simplify_type`, which is nice :3
2 parents b321742 + f2aea1e commit 52dd59e

35 files changed

+212
-153
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::mir::interpret;
2626
use rustc_middle::thir;
2727
use rustc_middle::traits::specialization_graph;
2828
use rustc_middle::ty::codec::TyEncoder;
29-
use rustc_middle::ty::fast_reject::{self, SimplifiedType, SimplifyParams, StripReferences};
29+
use rustc_middle::ty::fast_reject::{self, SimplifiedType, SimplifyParams};
3030
use rustc_middle::ty::query::Providers;
3131
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
3232
use rustc_serialize::{opaque, Encodable, Encoder};
@@ -2066,7 +2066,6 @@ impl<'tcx, 'v> ItemLikeVisitor<'v> for ImplsVisitor<'tcx> {
20662066
self.tcx,
20672067
trait_ref.self_ty(),
20682068
SimplifyParams::No,
2069-
StripReferences::No,
20702069
);
20712070

20722071
self.impls

compiler/rustc_middle/src/ty/fast_reject.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ pub enum SimplifyParams {
5454
No,
5555
}
5656

57-
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
58-
pub enum StripReferences {
59-
Yes,
60-
No,
61-
}
62-
6357
/// Tries to simplify a type by only returning the outermost injective¹ layer, if one exists.
6458
///
6559
/// The idea is to get something simple that we can use to quickly decide if two types could unify,
@@ -73,8 +67,6 @@ pub enum StripReferences {
7367
/// When using `SimplifyParams::Yes`, we still return a simplified type for params and projections²,
7468
/// the reasoning for this can be seen at the places doing this.
7569
///
76-
/// For diagnostics we strip references with `StripReferences::Yes`. This is currently the best
77-
/// way to skip some unhelpful suggestions.
7870
///
7971
/// ¹ meaning that if two outermost layers are different, then the whole types are also different.
8072
/// ² FIXME(@lcnr): this seems like it can actually end up being unsound with the way it's used during
@@ -87,7 +79,6 @@ pub fn simplify_type(
8779
tcx: TyCtxt<'_>,
8880
ty: Ty<'_>,
8981
can_simplify_params: SimplifyParams,
90-
strip_references: StripReferences,
9182
) -> Option<SimplifiedType> {
9283
match *ty.kind() {
9384
ty::Bool => Some(BoolSimplifiedType),
@@ -106,16 +97,7 @@ pub fn simplify_type(
10697
}
10798
_ => Some(MarkerTraitObjectSimplifiedType),
10899
},
109-
ty::Ref(_, ty, mutbl) => {
110-
if strip_references == StripReferences::Yes {
111-
// For diagnostics, when recommending similar impls we want to
112-
// recommend impls even when there is a reference mismatch,
113-
// so we treat &T and T equivalently in that case.
114-
simplify_type(tcx, ty, can_simplify_params, strip_references)
115-
} else {
116-
Some(RefSimplifiedType(mutbl))
117-
}
118-
}
100+
ty::Ref(_, _, mutbl) => Some(RefSimplifiedType(mutbl)),
119101
ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
120102
ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
121103
ty::GeneratorWitness(ref tys) => {

compiler/rustc_middle/src/ty/trait_def.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::traits::specialization_graph;
2-
use crate::ty::fast_reject::{self, SimplifiedType, SimplifyParams, StripReferences};
2+
use crate::ty::fast_reject::{self, SimplifiedType, SimplifyParams};
33
use crate::ty::fold::TypeFoldable;
44
use crate::ty::{Ident, Ty, TyCtxt};
55
use rustc_hir as hir;
@@ -150,9 +150,7 @@ impl<'tcx> TyCtxt<'tcx> {
150150
self_ty: Ty<'tcx>,
151151
) -> impl Iterator<Item = DefId> + 'tcx {
152152
let impls = self.trait_impls_of(def_id);
153-
if let Some(simp) =
154-
fast_reject::simplify_type(self, self_ty, SimplifyParams::No, StripReferences::No)
155-
{
153+
if let Some(simp) = fast_reject::simplify_type(self, self_ty, SimplifyParams::No) {
156154
if let Some(impls) = impls.non_blanket_impls.get(&simp) {
157155
return impls.iter().copied();
158156
}
@@ -189,9 +187,7 @@ impl<'tcx> TyCtxt<'tcx> {
189187
// whose outer level is not a parameter or projection. Especially for things like
190188
// `T: Clone` this is incredibly useful as we would otherwise look at all the impls
191189
// of `Clone` for `Option<T>`, `Vec<T>`, `ConcreteType` and so on.
192-
if let Some(simp) =
193-
fast_reject::simplify_type(self, self_ty, SimplifyParams::Yes, StripReferences::No)
194-
{
190+
if let Some(simp) = fast_reject::simplify_type(self, self_ty, SimplifyParams::Yes) {
195191
if let Some(impls) = impls.non_blanket_impls.get(&simp) {
196192
for &impl_def_id in impls {
197193
if let result @ Some(_) = f(impl_def_id) {
@@ -251,7 +247,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
251247
}
252248

253249
if let Some(simplified_self_ty) =
254-
fast_reject::simplify_type(tcx, impl_self_ty, SimplifyParams::No, StripReferences::No)
250+
fast_reject::simplify_type(tcx, impl_self_ty, SimplifyParams::No)
255251
{
256252
impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id);
257253
} else {

compiler/rustc_trait_selection/src/traits/coherence.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::traits::{
1515
};
1616
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1717
use rustc_middle::traits::specialization_graph::OverlapMode;
18-
use rustc_middle::ty::fast_reject::{self, SimplifyParams, StripReferences};
18+
use rustc_middle::ty::fast_reject::{self, SimplifyParams};
1919
use rustc_middle::ty::fold::TypeFoldable;
2020
use rustc_middle::ty::subst::Subst;
2121
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -82,8 +82,8 @@ where
8282
impl2_ref.iter().flat_map(|tref| tref.substs.types()),
8383
)
8484
.any(|(ty1, ty2)| {
85-
let t1 = fast_reject::simplify_type(tcx, ty1, SimplifyParams::No, StripReferences::No);
86-
let t2 = fast_reject::simplify_type(tcx, ty2, SimplifyParams::No, StripReferences::No);
85+
let t1 = fast_reject::simplify_type(tcx, ty1, SimplifyParams::No);
86+
let t2 = fast_reject::simplify_type(tcx, ty2, SimplifyParams::No);
8787

8888
if let (Some(t1), Some(t2)) = (t1, t2) {
8989
// Simplified successfully

0 commit comments

Comments
 (0)