1+ use crate :: mir:: Mutability ;
12use crate :: ty:: { self , Ty , TyCtxt } ;
23use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
34use rustc_hir:: def_id:: DefId ;
2728 UintSimplifiedType ( ty:: UintTy ) ,
2829 FloatSimplifiedType ( ty:: FloatTy ) ,
2930 AdtSimplifiedType ( D ) ,
31+ ForeignSimplifiedType ( DefId ) ,
3032 StrSimplifiedType ,
3133 ArraySimplifiedType ,
32- PtrSimplifiedType ,
34+ SliceSimplifiedType ,
35+ RefSimplifiedType ( Mutability ) ,
36+ PtrSimplifiedType ( Mutability ) ,
3337 NeverSimplifiedType ,
3438 TupleSimplifiedType ( usize ) ,
3539 /// A trait object, all of whose components are markers
4246 OpaqueSimplifiedType ( D ) ,
4347 FunctionSimplifiedType ( usize ) ,
4448 ParameterSimplifiedType ,
45- ForeignSimplifiedType ( DefId ) ,
49+ }
50+
51+ #[ derive( PartialEq , Eq , Debug , Clone , Copy ) ]
52+ pub enum SimplifyParams {
53+ Yes ,
54+ No ,
55+ }
56+
57+ #[ derive( PartialEq , Eq , Debug , Clone , Copy ) ]
58+ pub enum StripReferences {
59+ Yes ,
60+ No ,
4661}
4762
4863/// Tries to simplify a type by dropping type parameters, deref'ing away any reference types, etc.
5772pub fn simplify_type (
5873 tcx : TyCtxt < ' _ > ,
5974 ty : Ty < ' _ > ,
60- can_simplify_params : bool ,
75+ can_simplify_params : SimplifyParams ,
76+ strip_references : StripReferences ,
6177) -> Option < SimplifiedType > {
6278 match * ty. kind ( ) {
6379 ty:: Bool => Some ( BoolSimplifiedType ) ,
@@ -67,19 +83,24 @@ pub fn simplify_type(
6783 ty:: Float ( float_type) => Some ( FloatSimplifiedType ( float_type) ) ,
6884 ty:: Adt ( def, _) => Some ( AdtSimplifiedType ( def. did ) ) ,
6985 ty:: Str => Some ( StrSimplifiedType ) ,
70- ty:: Array ( ..) | ty:: Slice ( _) => Some ( ArraySimplifiedType ) ,
71- ty:: RawPtr ( _) => Some ( PtrSimplifiedType ) ,
86+ ty:: Array ( ..) => Some ( ArraySimplifiedType ) ,
87+ ty:: Slice ( ..) => Some ( SliceSimplifiedType ) ,
88+ ty:: RawPtr ( ptr) => Some ( PtrSimplifiedType ( ptr. mutbl ) ) ,
7289 ty:: Dynamic ( ref trait_info, ..) => match trait_info. principal_def_id ( ) {
7390 Some ( principal_def_id) if !tcx. trait_is_auto ( principal_def_id) => {
7491 Some ( TraitSimplifiedType ( principal_def_id) )
7592 }
7693 _ => Some ( MarkerTraitObjectSimplifiedType ) ,
7794 } ,
78- ty:: Ref ( _, ty, _) => {
79- // since we introduce auto-refs during method lookup, we
80- // just treat &T and T as equivalent from the point of
81- // view of possibly unifying
82- simplify_type ( tcx, ty, can_simplify_params)
95+ ty:: Ref ( _, ty, mutbl) => {
96+ if strip_references == StripReferences :: Yes {
97+ // For diagnostics, when recommending similar impls we want to
98+ // recommend impls even when there is a reference mismatch,
99+ // so we treat &T and T equivalently in that case.
100+ simplify_type ( tcx, ty, can_simplify_params, strip_references)
101+ } else {
102+ Some ( RefSimplifiedType ( mutbl) )
103+ }
83104 }
84105 ty:: FnDef ( def_id, _) | ty:: Closure ( def_id, _) => Some ( ClosureSimplifiedType ( def_id) ) ,
85106 ty:: Generator ( def_id, _, _) => Some ( GeneratorSimplifiedType ( def_id) ) ,
@@ -90,7 +111,7 @@ pub fn simplify_type(
90111 ty:: Tuple ( ref tys) => Some ( TupleSimplifiedType ( tys. len ( ) ) ) ,
91112 ty:: FnPtr ( ref f) => Some ( FunctionSimplifiedType ( f. skip_binder ( ) . inputs ( ) . len ( ) ) ) ,
92113 ty:: Projection ( _) | ty:: Param ( _) => {
93- if can_simplify_params {
114+ if can_simplify_params == SimplifyParams :: Yes {
94115 // In normalized types, projections don't unify with
95116 // anything. when lazy normalization happens, this
96117 // will change. It would still be nice to have a way
@@ -120,9 +141,12 @@ impl<D: Copy + Debug + Ord + Eq> SimplifiedTypeGen<D> {
120141 UintSimplifiedType ( t) => UintSimplifiedType ( t) ,
121142 FloatSimplifiedType ( t) => FloatSimplifiedType ( t) ,
122143 AdtSimplifiedType ( d) => AdtSimplifiedType ( map ( d) ) ,
144+ ForeignSimplifiedType ( d) => ForeignSimplifiedType ( d) ,
123145 StrSimplifiedType => StrSimplifiedType ,
124146 ArraySimplifiedType => ArraySimplifiedType ,
125- PtrSimplifiedType => PtrSimplifiedType ,
147+ SliceSimplifiedType => SliceSimplifiedType ,
148+ RefSimplifiedType ( m) => RefSimplifiedType ( m) ,
149+ PtrSimplifiedType ( m) => PtrSimplifiedType ( m) ,
126150 NeverSimplifiedType => NeverSimplifiedType ,
127151 MarkerTraitObjectSimplifiedType => MarkerTraitObjectSimplifiedType ,
128152 TupleSimplifiedType ( n) => TupleSimplifiedType ( n) ,
@@ -133,7 +157,6 @@ impl<D: Copy + Debug + Ord + Eq> SimplifiedTypeGen<D> {
133157 OpaqueSimplifiedType ( d) => OpaqueSimplifiedType ( map ( d) ) ,
134158 FunctionSimplifiedType ( n) => FunctionSimplifiedType ( n) ,
135159 ParameterSimplifiedType => ParameterSimplifiedType ,
136- ForeignSimplifiedType ( d) => ForeignSimplifiedType ( d) ,
137160 }
138161 }
139162}
@@ -149,12 +172,13 @@ where
149172 | CharSimplifiedType
150173 | StrSimplifiedType
151174 | ArraySimplifiedType
152- | PtrSimplifiedType
175+ | SliceSimplifiedType
153176 | NeverSimplifiedType
154177 | ParameterSimplifiedType
155178 | MarkerTraitObjectSimplifiedType => {
156179 // nothing to do
157180 }
181+ RefSimplifiedType ( m) | PtrSimplifiedType ( m) => m. hash_stable ( hcx, hasher) ,
158182 IntSimplifiedType ( t) => t. hash_stable ( hcx, hasher) ,
159183 UintSimplifiedType ( t) => t. hash_stable ( hcx, hasher) ,
160184 FloatSimplifiedType ( t) => t. hash_stable ( hcx, hasher) ,
0 commit comments