@@ -418,92 +418,94 @@ struct Instantiator<'a, 'tcx> {
418418impl < ' a , ' tcx > Instantiator < ' a , ' tcx > {
419419 fn instantiate_opaque_types_in_map < T : TypeFoldable < ' tcx > > ( & mut self , value : T ) -> T {
420420 let tcx = self . infcx . tcx ;
421- value. fold_with ( & mut BottomUpFolder {
422- tcx,
423- ty_op : |ty| {
424- if ty. references_error ( ) {
425- return tcx. ty_error ( ) ;
426- } else if let ty:: Opaque ( def_id, substs) = ty. kind ( ) {
427- // Check that this is `impl Trait` type is
428- // declared by `parent_def_id` -- i.e., one whose
429- // value we are inferring. At present, this is
430- // always true during the first phase of
431- // type-check, but not always true later on during
432- // NLL. Once we support named opaque types more fully,
433- // this same scenario will be able to arise during all phases.
434- //
435- // Here is an example using type alias `impl Trait`
436- // that indicates the distinction we are checking for:
437- //
438- // ```rust
439- // mod a {
440- // pub type Foo = impl Iterator;
441- // pub fn make_foo() -> Foo { .. }
442- // }
443- //
444- // mod b {
445- // fn foo() -> a::Foo { a::make_foo() }
446- // }
447- // ```
448- //
449- // Here, the return type of `foo` references an
450- // `Opaque` indeed, but not one whose value is
451- // presently being inferred. You can get into a
452- // similar situation with closure return types
453- // today:
454- //
455- // ```rust
456- // fn foo() -> impl Iterator { .. }
457- // fn bar() {
458- // let x = || foo(); // returns the Opaque assoc with `foo`
459- // }
460- // ```
461- if let Some ( def_id) = def_id. as_local ( ) {
462- let opaque_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
463- let parent_def_id = self . infcx . defining_use_anchor ;
464- let def_scope_default = || {
465- let opaque_parent_hir_id = tcx. hir ( ) . get_parent_item ( opaque_hir_id) ;
466- parent_def_id == tcx. hir ( ) . local_def_id ( opaque_parent_hir_id)
467- } ;
468- let ( in_definition_scope, origin) =
469- match tcx. hir ( ) . expect_item ( opaque_hir_id) . kind {
470- // Anonymous `impl Trait`
471- hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy {
472- impl_trait_fn : Some ( parent) ,
473- origin,
474- ..
475- } ) => ( parent == parent_def_id. to_def_id ( ) , origin) ,
476- // Named `type Foo = impl Bar;`
477- hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy {
478- impl_trait_fn : None ,
479- origin,
480- ..
481- } ) => (
482- may_define_opaque_type ( tcx, parent_def_id, opaque_hir_id) ,
483- origin,
484- ) ,
485- _ => ( def_scope_default ( ) , hir:: OpaqueTyOrigin :: TyAlias ) ,
421+ value
422+ . fold_with ( & mut BottomUpFolder {
423+ tcx,
424+ ty_op : |ty| {
425+ if ty. references_error ( ) {
426+ return tcx. ty_error ( ) ;
427+ } else if let ty:: Opaque ( def_id, substs) = ty. kind ( ) {
428+ // Check that this is `impl Trait` type is
429+ // declared by `parent_def_id` -- i.e., one whose
430+ // value we are inferring. At present, this is
431+ // always true during the first phase of
432+ // type-check, but not always true later on during
433+ // NLL. Once we support named opaque types more fully,
434+ // this same scenario will be able to arise during all phases.
435+ //
436+ // Here is an example using type alias `impl Trait`
437+ // that indicates the distinction we are checking for:
438+ //
439+ // ```rust
440+ // mod a {
441+ // pub type Foo = impl Iterator;
442+ // pub fn make_foo() -> Foo { .. }
443+ // }
444+ //
445+ // mod b {
446+ // fn foo() -> a::Foo { a::make_foo() }
447+ // }
448+ // ```
449+ //
450+ // Here, the return type of `foo` references an
451+ // `Opaque` indeed, but not one whose value is
452+ // presently being inferred. You can get into a
453+ // similar situation with closure return types
454+ // today:
455+ //
456+ // ```rust
457+ // fn foo() -> impl Iterator { .. }
458+ // fn bar() {
459+ // let x = || foo(); // returns the Opaque assoc with `foo`
460+ // }
461+ // ```
462+ if let Some ( def_id) = def_id. as_local ( ) {
463+ let opaque_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
464+ let parent_def_id = self . infcx . defining_use_anchor ;
465+ let def_scope_default = || {
466+ let opaque_parent_hir_id = tcx. hir ( ) . get_parent_item ( opaque_hir_id) ;
467+ parent_def_id == tcx. hir ( ) . local_def_id ( opaque_parent_hir_id)
486468 } ;
487- if in_definition_scope {
488- let opaque_type_key =
489- OpaqueTypeKey { def_id : def_id. to_def_id ( ) , substs } ;
490- return self . fold_opaque_ty ( ty, opaque_type_key, origin) ;
491- }
492-
493- debug ! (
494- "instantiate_opaque_types_in_map: \
469+ let ( in_definition_scope, origin) =
470+ match tcx. hir ( ) . expect_item ( opaque_hir_id) . kind {
471+ // Anonymous `impl Trait`
472+ hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy {
473+ impl_trait_fn : Some ( parent) ,
474+ origin,
475+ ..
476+ } ) => ( parent == parent_def_id. to_def_id ( ) , origin) ,
477+ // Named `type Foo = impl Bar;`
478+ hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy {
479+ impl_trait_fn : None ,
480+ origin,
481+ ..
482+ } ) => (
483+ may_define_opaque_type ( tcx, parent_def_id, opaque_hir_id) ,
484+ origin,
485+ ) ,
486+ _ => ( def_scope_default ( ) , hir:: OpaqueTyOrigin :: TyAlias ) ,
487+ } ;
488+ if in_definition_scope {
489+ let opaque_type_key =
490+ OpaqueTypeKey { def_id : def_id. to_def_id ( ) , substs } ;
491+ return self . fold_opaque_ty ( ty, opaque_type_key, origin) ;
492+ }
493+
494+ debug ! (
495+ "instantiate_opaque_types_in_map: \
495496 encountered opaque outside its definition scope \
496497 def_id={:?}",
497- def_id,
498- ) ;
498+ def_id,
499+ ) ;
500+ }
499501 }
500- }
501502
502- ty
503- } ,
504- lt_op : |lt| lt,
505- ct_op : |ct| ct,
506- } )
503+ ty
504+ } ,
505+ lt_op : |lt| lt,
506+ ct_op : |ct| ct,
507+ } )
508+ . into_ok ( )
507509 }
508510
509511 #[ instrument( skip( self ) , level = "debug" ) ]
@@ -556,21 +558,23 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
556558 debug ! ( ?predicate) ;
557559
558560 // We can't normalize associated types from `rustc_infer`, but we can eagerly register inference variables for them.
559- let predicate = predicate. fold_with ( & mut BottomUpFolder {
560- tcx,
561- ty_op : |ty| match ty. kind ( ) {
562- ty:: Projection ( projection_ty) => infcx. infer_projection (
563- self . param_env ,
564- * projection_ty,
565- traits:: ObligationCause :: misc ( self . value_span , self . body_id ) ,
566- 0 ,
567- & mut self . obligations ,
568- ) ,
569- _ => ty,
570- } ,
571- lt_op : |lt| lt,
572- ct_op : |ct| ct,
573- } ) ;
561+ let predicate = predicate
562+ . fold_with ( & mut BottomUpFolder {
563+ tcx,
564+ ty_op : |ty| match ty. kind ( ) {
565+ ty:: Projection ( projection_ty) => infcx. infer_projection (
566+ self . param_env ,
567+ * projection_ty,
568+ traits:: ObligationCause :: misc ( self . value_span , self . body_id ) ,
569+ 0 ,
570+ & mut self . obligations ,
571+ ) ,
572+ _ => ty,
573+ } ,
574+ lt_op : |lt| lt,
575+ ct_op : |ct| ct,
576+ } )
577+ . into_ok ( ) ;
574578 debug ! ( ?predicate) ;
575579
576580 if let ty:: PredicateKind :: Projection ( projection) = predicate. kind ( ) . skip_binder ( ) {
0 commit comments