@@ -2378,6 +2378,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23782378 return Ok ( None ) ;
23792379 }
23802380
2381+ //
2382+ // Select applicable inherent associated type candidates modulo regions.
2383+ //
2384+
23812385 // In contexts that have no inference context, just make a new one.
23822386 // We do need a local variable to store it, though.
23832387 let infcx_;
@@ -2390,14 +2394,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23902394 }
23912395 } ;
23922396
2393- let param_env = tcx. param_env ( block. owner . to_def_id ( ) ) ;
2397+ // FIXME(inherent_associated_types): Acquiring the ParamEnv this early leads to cycle errors
2398+ // when inside of an ADT (#108491) or where clause.
2399+ let param_env = tcx. param_env ( block. owner ) ;
23942400 let cause = ObligationCause :: misc ( span, block. owner . def_id ) ;
23952401
23962402 let mut fulfillment_errors = Vec :: new ( ) ;
23972403 let mut applicable_candidates: Vec < _ > = infcx. probe ( |_| {
23982404 let universe = infcx. create_next_universe ( ) ;
23992405
24002406 // Regions are not considered during selection.
2407+ // FIXME(non_lifetime_binders): Here we are "truncating" or "flattening" the universes
2408+ // of type and const binders. Is that correct in the selection phase? See also #109505.
24012409 let self_ty = tcx. replace_escaping_bound_vars_uncached (
24022410 self_ty,
24032411 FnMutDelegate {
@@ -2413,41 +2421,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24132421
24142422 candidates
24152423 . iter ( )
2416- . filter_map ( |& ( impl_, ( assoc_item, def_scope) ) | {
2424+ . copied ( )
2425+ . filter ( |& ( impl_, _) | {
24172426 infcx. probe ( |_| {
24182427 let ocx = ObligationCtxt :: new_in_snapshot ( & infcx) ;
24192428
2420- let impl_ty = tcx. type_of ( impl_) ;
24212429 let impl_substs = infcx. fresh_item_substs ( impl_) ;
2422- let impl_ty = impl_ty . subst ( tcx, impl_substs) ;
2430+ let impl_ty = tcx . type_of ( impl_ ) . subst ( tcx, impl_substs) ;
24232431 let impl_ty = ocx. normalize ( & cause, param_env, impl_ty) ;
24242432
2425- // Check that the Self-types can be related.
2426- // FIXME(fmease): Should we use `eq` here?
2427- ocx. sup ( & ObligationCause :: dummy ( ) , param_env, impl_ty, self_ty) . ok ( ) ?;
2433+ // Check that the self types can be related.
2434+ // FIXME(inherent_associated_types): Should we use `eq` here? Method probing uses
2435+ // `sup` for this situtation, too. What for? To constrain inference variables?
2436+ if ocx. sup ( & ObligationCause :: dummy ( ) , param_env, impl_ty, self_ty) . is_err ( )
2437+ {
2438+ return false ;
2439+ }
24282440
24292441 // Check whether the impl imposes obligations we have to worry about.
2430- let impl_bounds = tcx. predicates_of ( impl_) ;
2431- let impl_bounds = impl_bounds. instantiate ( tcx, impl_substs) ;
2432-
2442+ let impl_bounds = tcx. predicates_of ( impl_) . instantiate ( tcx, impl_substs) ;
24332443 let impl_bounds = ocx. normalize ( & cause, param_env, impl_bounds) ;
2434-
24352444 let impl_obligations = traits:: predicates_for_generics (
24362445 |_, _| cause. clone ( ) ,
24372446 param_env,
24382447 impl_bounds,
24392448 ) ;
2440-
24412449 ocx. register_obligations ( impl_obligations) ;
24422450
24432451 let mut errors = ocx. select_where_possible ( ) ;
24442452 if !errors. is_empty ( ) {
24452453 fulfillment_errors. append ( & mut errors) ;
2446- return None ;
2454+ return false ;
24472455 }
24482456
2449- // FIXME(fmease): Unsolved vars can escape this InferCtxt snapshot.
2450- Some ( ( assoc_item, def_scope, infcx. resolve_vars_if_possible ( impl_substs) ) )
2457+ true
24512458 } )
24522459 } )
24532460 . collect ( )
@@ -2456,24 +2463,26 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24562463 if applicable_candidates. len ( ) > 1 {
24572464 return Err ( self . complain_about_ambiguous_inherent_assoc_type (
24582465 name,
2459- applicable_candidates. into_iter ( ) . map ( |( candidate, .. ) | candidate) . collect ( ) ,
2466+ applicable_candidates. into_iter ( ) . map ( |( _ , ( candidate, _ ) ) | candidate) . collect ( ) ,
24602467 span,
24612468 ) ) ;
24622469 }
24632470
2464- if let Some ( ( assoc_item , def_scope , impl_substs ) ) = applicable_candidates. pop ( ) {
2471+ if let Some ( ( impl_ , ( assoc_item , def_scope ) ) ) = applicable_candidates. pop ( ) {
24652472 self . check_assoc_ty ( assoc_item, name, def_scope, block, span) ;
24662473
2467- // FIXME(inherent_associated_types): To fully *confirm* the *probed* candidate, we still
2468- // need to relate the Self-type with fresh item substs & register region obligations for
2469- // regionck to prove/disprove.
2470-
2471- let item_substs =
2472- self . create_substs_for_associated_item ( span, assoc_item, segment, impl_substs) ;
2474+ // FIXME(fmease): Currently creating throwaway `parent_substs` to please
2475+ // `create_substs_for_associated_item`. Modify the latter instead (or sth. similar) to
2476+ // not require the parent substs logic.
2477+ let parent_substs = InternalSubsts :: identity_for_item ( tcx, impl_) ;
2478+ let substs =
2479+ self . create_substs_for_associated_item ( span, assoc_item, segment, parent_substs) ;
2480+ let substs = tcx. mk_substs_from_iter (
2481+ std:: iter:: once ( ty:: GenericArg :: from ( self_ty) )
2482+ . chain ( substs. into_iter ( ) . skip ( parent_substs. len ( ) ) ) ,
2483+ ) ;
24732484
2474- // FIXME(fmease, #106722): Check if the bounds on the parameters of the
2475- // associated type hold, if any.
2476- let ty = tcx. type_of ( assoc_item) . subst ( tcx, item_substs) ;
2485+ let ty = tcx. mk_alias ( ty:: Inherent , tcx. mk_alias_ty ( assoc_item, substs) ) ;
24772486
24782487 return Ok ( Some ( ( ty, assoc_item) ) ) ;
24792488 }
0 commit comments