@@ -149,9 +149,9 @@ impl SimplifiedType {
149149 }
150150}
151151
152- /// Given generic arguments from an obligation and an impl ,
153- /// could these two be unified after replacing parameters in the
154- /// the impl with inference variables.
152+ /// Given generic arguments from an obligation and a candidate ,
153+ /// could these two be unified after replacing parameters and bound
154+ /// variables in the candidate with inference variables.
155155///
156156/// For obligations, parameters won't be replaced by inference
157157/// variables and only unify with themselves. We treat them
@@ -170,28 +170,30 @@ impl DeepRejectCtxt {
170170 pub fn args_may_unify < ' tcx > (
171171 self ,
172172 obligation_args : GenericArgsRef < ' tcx > ,
173- impl_args : GenericArgsRef < ' tcx > ,
173+ candidate_args : GenericArgsRef < ' tcx > ,
174174 ) -> bool {
175- iter:: zip ( obligation_args, impl_args ) . all ( |( obl, imp) | {
175+ iter:: zip ( obligation_args, candidate_args ) . all ( |( obl, imp) | {
176176 match ( obl. unpack ( ) , imp. unpack ( ) ) {
177177 // We don't fast reject based on regions.
178178 ( GenericArgKind :: Lifetime ( _) , GenericArgKind :: Lifetime ( _) ) => true ,
179- ( GenericArgKind :: Type ( obl) , GenericArgKind :: Type ( imp ) ) => {
180- self . types_may_unify ( obl, imp )
179+ ( GenericArgKind :: Type ( obl) , GenericArgKind :: Type ( candidate ) ) => {
180+ self . types_may_unify ( obl, candidate )
181181 }
182- ( GenericArgKind :: Const ( obl) , GenericArgKind :: Const ( imp ) ) => {
183- self . consts_may_unify ( obl, imp )
182+ ( GenericArgKind :: Const ( obl) , GenericArgKind :: Const ( candidate ) ) => {
183+ self . consts_may_unify ( obl, candidate )
184184 }
185185 _ => bug ! ( "kind mismatch: {obl} {imp}" ) ,
186186 }
187187 } )
188188 }
189189
190- pub fn types_may_unify < ' tcx > ( self , obligation_ty : Ty < ' tcx > , impl_ty : Ty < ' tcx > ) -> bool {
191- match impl_ty . kind ( ) {
192- // Start by checking whether the type in the impl may unify with
190+ pub fn types_may_unify < ' tcx > ( self , obligation_ty : Ty < ' tcx > , candidate_ty : Ty < ' tcx > ) -> bool {
191+ match candidate_ty . kind ( ) {
192+ // Start by checking whether the type in the candidate may unify with
193193 // pretty much everything. Just return `true` in that case.
194- ty:: Param ( _) | ty:: Error ( _) | ty:: Alias ( ..) => return true ,
194+ ty:: Param ( _) | ty:: Error ( _) | ty:: Alias ( ..) | ty:: Infer ( _) | ty:: Placeholder ( ..) => {
195+ return true ;
196+ }
195197 // These types only unify with inference variables or their own
196198 // variant.
197199 ty:: Bool
@@ -210,18 +212,15 @@ impl DeepRejectCtxt {
210212 | ty:: Never
211213 | ty:: Tuple ( ..)
212214 | ty:: FnPtr ( ..)
213- | ty:: Foreign ( ..) => debug_assert ! ( impl_ty . is_known_rigid( ) ) ,
215+ | ty:: Foreign ( ..) => debug_assert ! ( candidate_ty . is_known_rigid( ) ) ,
214216 ty:: FnDef ( ..)
215217 | ty:: Closure ( ..)
216218 | ty:: CoroutineClosure ( ..)
217219 | ty:: Coroutine ( ..)
218220 | ty:: CoroutineWitness ( ..)
219- | ty:: Placeholder ( ..)
220- | ty:: Bound ( ..)
221- | ty:: Infer ( _) => bug ! ( "unexpected impl_ty: {impl_ty}" ) ,
221+ | ty:: Bound ( ..) => bug ! ( "unexpected candidate_ty: {candidate_ty}" ) ,
222222 }
223223
224- let k = impl_ty. kind ( ) ;
225224 match * obligation_ty. kind ( ) {
226225 // Purely rigid types, use structural equivalence.
227226 ty:: Bool
@@ -231,65 +230,68 @@ impl DeepRejectCtxt {
231230 | ty:: Float ( _)
232231 | ty:: Str
233232 | ty:: Never
234- | ty:: Foreign ( _) => obligation_ty == impl_ty ,
235- ty:: Ref ( _, obl_ty, obl_mutbl) => match k {
236- & ty:: Ref ( _, impl_ty , impl_mutbl ) => {
237- obl_mutbl == impl_mutbl && self . types_may_unify ( obl_ty, impl_ty )
233+ | ty:: Foreign ( _) => obligation_ty == candidate_ty ,
234+ ty:: Ref ( _, obl_ty, obl_mutbl) => match candidate_ty . kind ( ) {
235+ & ty:: Ref ( _, cand_ty , cand_mutbl ) => {
236+ obl_mutbl == cand_mutbl && self . types_may_unify ( obl_ty, cand_ty )
238237 }
239238 _ => false ,
240239 } ,
241- ty:: Adt ( obl_def, obl_args) => match k {
242- & ty:: Adt ( impl_def , impl_args ) => {
243- obl_def == impl_def && self . args_may_unify ( obl_args, impl_args )
240+ ty:: Adt ( obl_def, obl_args) => match candidate_ty . kind ( ) {
241+ & ty:: Adt ( cand_def , cand_args ) => {
242+ obl_def == cand_def && self . args_may_unify ( obl_args, cand_args )
244243 }
245244 _ => false ,
246245 } ,
247- ty:: Pat ( obl_ty, _) => {
246+ ty:: Pat ( obl_ty, _) => match candidate_ty . kind ( ) {
248247 // FIXME(pattern_types): take pattern into account
249- matches ! ( k, & ty:: Pat ( impl_ty, _) if self . types_may_unify( obl_ty, impl_ty) )
250- }
251- ty:: Slice ( obl_ty) => {
252- matches ! ( k, & ty:: Slice ( impl_ty) if self . types_may_unify( obl_ty, impl_ty) )
253- }
254- ty:: Array ( obl_ty, obl_len) => match k {
255- & ty:: Array ( impl_ty, impl_len) => {
256- self . types_may_unify ( obl_ty, impl_ty)
257- && self . consts_may_unify ( obl_len, impl_len)
248+ & ty:: Pat ( cand_ty, _) => self . types_may_unify ( obl_ty, cand_ty) ,
249+ _ => false ,
250+ } ,
251+ ty:: Slice ( obl_ty) => match candidate_ty. kind ( ) {
252+ & ty:: Slice ( cand_ty) => self . types_may_unify ( obl_ty, cand_ty) ,
253+ _ => false ,
254+ } ,
255+ ty:: Array ( obl_ty, obl_len) => match candidate_ty. kind ( ) {
256+ & ty:: Array ( cand_ty, cand_len) => {
257+ self . types_may_unify ( obl_ty, cand_ty)
258+ && self . consts_may_unify ( obl_len, cand_len)
258259 }
259260 _ => false ,
260261 } ,
261- ty:: Tuple ( obl) => match k {
262- & ty:: Tuple ( imp ) => {
263- obl. len ( ) == imp . len ( )
264- && iter:: zip ( obl, imp ) . all ( |( obl, imp ) | self . types_may_unify ( obl, imp ) )
262+ ty:: Tuple ( obl) => match candidate_ty . kind ( ) {
263+ & ty:: Tuple ( cand ) => {
264+ obl. len ( ) == cand . len ( )
265+ && iter:: zip ( obl, cand ) . all ( |( obl, cand ) | self . types_may_unify ( obl, cand ) )
265266 }
266267 _ => false ,
267268 } ,
268- ty:: RawPtr ( obl_ty, obl_mutbl) => match * k {
269- ty:: RawPtr ( imp_ty , imp_mutbl ) => {
270- obl_mutbl == imp_mutbl && self . types_may_unify ( obl_ty, imp_ty )
269+ ty:: RawPtr ( obl_ty, obl_mutbl) => match * candidate_ty . kind ( ) {
270+ ty:: RawPtr ( cand_ty , cand_mutbl ) => {
271+ obl_mutbl == cand_mutbl && self . types_may_unify ( obl_ty, cand_ty )
271272 }
272273 _ => false ,
273274 } ,
274- ty:: Dynamic ( obl_preds, ..) => {
275+ ty:: Dynamic ( obl_preds, ..) => match candidate_ty . kind ( ) {
275276 // Ideally we would walk the existential predicates here or at least
276277 // compare their length. But considering that the relevant `Relate` impl
277278 // actually sorts and deduplicates these, that doesn't work.
278- matches ! ( k, ty:: Dynamic ( impl_preds, ..) if
279- obl_preds. principal_def_id( ) == impl_preds. principal_def_id( )
280- )
281- }
282- ty:: FnPtr ( obl_sig) => match k {
283- ty:: FnPtr ( impl_sig) => {
279+ ty:: Dynamic ( cand_preds, ..) => {
280+ obl_preds. principal_def_id ( ) == cand_preds. principal_def_id ( )
281+ }
282+ _ => false ,
283+ } ,
284+ ty:: FnPtr ( obl_sig) => match candidate_ty. kind ( ) {
285+ ty:: FnPtr ( cand_sig) => {
284286 let ty:: FnSig { inputs_and_output, c_variadic, safety, abi } =
285287 obl_sig. skip_binder ( ) ;
286- let impl_sig = impl_sig . skip_binder ( ) ;
288+ let cand_sig = cand_sig . skip_binder ( ) ;
287289
288- abi == impl_sig . abi
289- && c_variadic == impl_sig . c_variadic
290- && safety == impl_sig . safety
291- && inputs_and_output. len ( ) == impl_sig . inputs_and_output . len ( )
292- && iter:: zip ( inputs_and_output, impl_sig . inputs_and_output )
290+ abi == cand_sig . abi
291+ && c_variadic == cand_sig . c_variadic
292+ && safety == cand_sig . safety
293+ && inputs_and_output. len ( ) == cand_sig . inputs_and_output . len ( )
294+ && iter:: zip ( inputs_and_output, cand_sig . inputs_and_output )
293295 . all ( |( obl, imp) | self . types_may_unify ( obl, imp) )
294296 }
295297 _ => false ,
@@ -308,9 +310,9 @@ impl DeepRejectCtxt {
308310 TreatParams :: AsCandidateKey => true ,
309311 } ,
310312
311- ty:: Infer ( ty:: IntVar ( _) ) => impl_ty . is_integral ( ) ,
313+ ty:: Infer ( ty:: IntVar ( _) ) => candidate_ty . is_integral ( ) ,
312314
313- ty:: Infer ( ty:: FloatVar ( _) ) => impl_ty . is_floating_point ( ) ,
315+ ty:: Infer ( ty:: FloatVar ( _) ) => candidate_ty . is_floating_point ( ) ,
314316
315317 ty:: Infer ( _) => true ,
316318
@@ -329,17 +331,22 @@ impl DeepRejectCtxt {
329331 }
330332 }
331333
332- pub fn consts_may_unify ( self , obligation_ct : ty:: Const < ' _ > , impl_ct : ty:: Const < ' _ > ) -> bool {
333- let impl_val = match impl_ct. kind ( ) {
334+ pub fn consts_may_unify (
335+ self ,
336+ obligation_ct : ty:: Const < ' _ > ,
337+ candidate_ct : ty:: Const < ' _ > ,
338+ ) -> bool {
339+ let candidate_val = match candidate_ct. kind ( ) {
334340 ty:: ConstKind :: Expr ( _)
335341 | ty:: ConstKind :: Param ( _)
336342 | ty:: ConstKind :: Unevaluated ( _)
343+ | ty:: ConstKind :: Placeholder ( _)
337344 | ty:: ConstKind :: Error ( _) => {
338345 return true ;
339346 }
340- ty:: ConstKind :: Value ( impl_val ) => impl_val ,
341- ty:: ConstKind :: Infer ( _) | ty:: ConstKind :: Bound ( ..) | ty :: ConstKind :: Placeholder ( _ ) => {
342- bug ! ( "unexpected impl arg: {:?}" , impl_ct )
347+ ty:: ConstKind :: Value ( candidate_val ) => candidate_val ,
348+ ty:: ConstKind :: Infer ( _) | ty:: ConstKind :: Bound ( ..) => {
349+ bug ! ( "unexpected candidate arg: {:?}" , candidate_ct )
343350 }
344351 } ;
345352
@@ -357,7 +364,7 @@ impl DeepRejectCtxt {
357364 ty:: ConstKind :: Expr ( _) | ty:: ConstKind :: Unevaluated ( _) | ty:: ConstKind :: Error ( _) => {
358365 true
359366 }
360- ty:: ConstKind :: Value ( obl_val) => obl_val == impl_val ,
367+ ty:: ConstKind :: Value ( obl_val) => obl_val == candidate_val ,
361368
362369 ty:: ConstKind :: Infer ( _) => true ,
363370
0 commit comments