@@ -100,26 +100,17 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
100
100
let substs = InternalSubsts :: identity_for_item ( tcx, def_id) ;
101
101
tcx. mk_adt ( def, substs)
102
102
}
103
+ ItemKind :: OpaqueTy ( OpaqueTy { origin : hir:: OpaqueTyOrigin :: Binding , .. } ) => {
104
+ let_position_impl_trait_type ( tcx, def_id. expect_local ( ) )
105
+ }
103
106
ItemKind :: OpaqueTy ( OpaqueTy { impl_trait_fn : None , .. } ) => {
104
107
find_opaque_ty_constraints ( tcx, def_id. expect_local ( ) )
105
108
}
106
109
// Opaque types desugared from `impl Trait`.
107
- ItemKind :: OpaqueTy ( OpaqueTy { impl_trait_fn : Some ( owner) , origin, .. } ) => {
108
- let concrete_types = match origin {
109
- OpaqueTyOrigin :: FnReturn | OpaqueTyOrigin :: AsyncFn => {
110
- & tcx. mir_borrowck ( owner. expect_local ( ) ) . concrete_opaque_types
111
- }
112
- OpaqueTyOrigin :: Misc => {
113
- // We shouldn't leak borrowck results through impl trait in bindings.
114
- // For example, we shouldn't be able to tell if `x` in
115
- // `let x: impl Sized + 'a = &()` has type `&'static ()` or `&'a ()`.
116
- & tcx. typeck_tables_of ( owner. expect_local ( ) ) . concrete_opaque_types
117
- }
118
- OpaqueTyOrigin :: TypeAlias => {
119
- span_bug ! ( item. span, "Type alias impl trait shouldn't have an owner" )
120
- }
121
- } ;
122
- let concrete_ty = concrete_types
110
+ ItemKind :: OpaqueTy ( OpaqueTy { impl_trait_fn : Some ( owner) , .. } ) => {
111
+ let concrete_ty = tcx
112
+ . mir_borrowck ( owner. expect_local ( ) )
113
+ . concrete_opaque_types
123
114
. get ( & def_id)
124
115
. map ( |opaque| opaque. concrete_type )
125
116
. unwrap_or_else ( || {
@@ -148,13 +139,6 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
148
139
}
149
140
} ) ;
150
141
debug ! ( "concrete_ty = {:?}" , concrete_ty) ;
151
- if concrete_ty. has_erased_regions ( ) {
152
- // FIXME(impl_trait_in_bindings) Handle this case.
153
- tcx. sess . span_fatal (
154
- item. span ,
155
- "lifetimes in impl Trait types in bindings are not currently supported" ,
156
- ) ;
157
- }
158
142
concrete_ty
159
143
}
160
144
ItemKind :: Trait ( ..)
@@ -589,6 +573,50 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
589
573
}
590
574
}
591
575
576
+ fn let_position_impl_trait_type ( tcx : TyCtxt < ' _ > , opaque_ty_id : LocalDefId ) -> Ty < ' _ > {
577
+ let scope = tcx. hir ( ) . get_defining_scope ( tcx. hir ( ) . as_local_hir_id ( opaque_ty_id) ) ;
578
+ let scope_def_id = tcx. hir ( ) . local_def_id ( scope) ;
579
+
580
+ let opaque_ty_def_id = opaque_ty_id. to_def_id ( ) ;
581
+
582
+ let owner_tables = tcx. typeck_tables_of ( scope_def_id) ;
583
+ let concrete_ty = owner_tables
584
+ . concrete_opaque_types
585
+ . get ( & opaque_ty_def_id)
586
+ . map ( |opaque| opaque. concrete_type )
587
+ . unwrap_or_else ( || {
588
+ tcx. sess . delay_span_bug (
589
+ DUMMY_SP ,
590
+ & format ! (
591
+ "owner {:?} has no opaque type for {:?} in its tables" ,
592
+ scope_def_id, opaque_ty_id
593
+ ) ,
594
+ ) ;
595
+ if let Some ( ErrorReported ) = owner_tables. tainted_by_errors {
596
+ // Some error in the owner fn prevented us from populating the
597
+ // `concrete_opaque_types` table.
598
+ tcx. types . err
599
+ } else {
600
+ // We failed to resolve the opaque type or it resolves to
601
+ // itself. Return the non-revealed type, which should result in
602
+ // E0720.
603
+ tcx. mk_opaque (
604
+ opaque_ty_def_id,
605
+ InternalSubsts :: identity_for_item ( tcx, opaque_ty_def_id) ,
606
+ )
607
+ }
608
+ } ) ;
609
+ debug ! ( "concrete_ty = {:?}" , concrete_ty) ;
610
+ if concrete_ty. has_erased_regions ( ) {
611
+ // FIXME(impl_trait_in_bindings) Handle this case.
612
+ tcx. sess . span_fatal (
613
+ tcx. hir ( ) . span ( tcx. hir ( ) . as_local_hir_id ( opaque_ty_id) ) ,
614
+ "lifetimes in impl Trait types in bindings are not currently supported" ,
615
+ ) ;
616
+ }
617
+ concrete_ty
618
+ }
619
+
592
620
fn infer_placeholder_type (
593
621
tcx : TyCtxt < ' _ > ,
594
622
def_id : LocalDefId ,
0 commit comments