@@ -57,9 +57,7 @@ impl<'tcx> InferCtxt<'tcx> {
5757 }
5858 let mut obligations = vec ! [ ] ;
5959 let replace_opaque_type = |def_id : DefId | {
60- def_id
61- . as_local ( )
62- . map_or ( false , |def_id| self . opaque_type_origin ( def_id, span) . is_some ( ) )
60+ def_id. as_local ( ) . map_or ( false , |def_id| self . opaque_type_origin ( def_id) . is_some ( ) )
6361 } ;
6462 let value = value. fold_with ( & mut BottomUpFolder {
6563 tcx : self . tcx ,
@@ -144,9 +142,9 @@ impl<'tcx> InferCtxt<'tcx> {
144142 // let x = || foo(); // returns the Opaque assoc with `foo`
145143 // }
146144 // ```
147- self . opaque_type_origin ( def_id, cause . span ) ?
145+ self . opaque_type_origin ( def_id) ?
148146 }
149- DefiningAnchor :: Bubble => self . opaque_ty_origin_unchecked ( def_id, cause . span ) ,
147+ DefiningAnchor :: Bubble => self . opaque_type_origin_unchecked ( def_id) ,
150148 DefiningAnchor :: Error => return None ,
151149 } ;
152150 if let ty:: Alias ( ty:: Opaque , ty:: AliasTy { def_id : b_def_id, .. } ) = * b. kind ( ) {
@@ -155,9 +153,8 @@ impl<'tcx> InferCtxt<'tcx> {
155153 // no one encounters it in practice.
156154 // It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
157155 // where it is of no concern, so we only check for TAITs.
158- if let Some ( OpaqueTyOrigin :: TyAlias ) = b_def_id
159- . as_local ( )
160- . and_then ( |b_def_id| self . opaque_type_origin ( b_def_id, cause. span ) )
156+ if let Some ( OpaqueTyOrigin :: TyAlias ) =
157+ b_def_id. as_local ( ) . and_then ( |b_def_id| self . opaque_type_origin ( b_def_id) )
161158 {
162159 self . tcx . sess . emit_err ( OpaqueHiddenTypeDiag {
163160 span : cause. span ,
@@ -371,24 +368,18 @@ impl<'tcx> InferCtxt<'tcx> {
371368 } ) ;
372369 }
373370
371+ /// Returns the origin of the opaque type `def_id` if we're currently
372+ /// in its defining scope.
374373 #[ instrument( skip( self ) , level = "trace" , ret) ]
375- pub fn opaque_type_origin ( & self , def_id : LocalDefId , span : Span ) -> Option < OpaqueTyOrigin > {
374+ pub fn opaque_type_origin ( & self , def_id : LocalDefId ) -> Option < OpaqueTyOrigin > {
376375 let opaque_hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( def_id) ;
377376 let parent_def_id = match self . defining_use_anchor {
378377 DefiningAnchor :: Bubble | DefiningAnchor :: Error => return None ,
379378 DefiningAnchor :: Bind ( bind) => bind,
380379 } ;
381- let item_kind = & self . tcx . hir ( ) . expect_item ( def_id) . kind ;
382-
383- let hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy { origin, .. } ) = item_kind else {
384- span_bug ! (
385- span,
386- "weird opaque type: {:#?}, {:#?}" ,
387- def_id,
388- item_kind
389- )
390- } ;
391- let in_definition_scope = match * origin {
380+
381+ let origin = self . opaque_type_origin_unchecked ( def_id) ;
382+ let in_definition_scope = match origin {
392383 // Async `impl Trait`
393384 hir:: OpaqueTyOrigin :: AsyncFn ( parent) => parent == parent_def_id,
394385 // Anonymous `impl Trait`
@@ -398,16 +389,17 @@ impl<'tcx> InferCtxt<'tcx> {
398389 may_define_opaque_type ( self . tcx , parent_def_id, opaque_hir_id)
399390 }
400391 } ;
401- trace ! ( ?origin) ;
402- in_definition_scope. then_some ( * origin)
392+ in_definition_scope. then_some ( origin)
403393 }
404394
395+ /// Returns the origin of the opaque type `def_id` even if we are not in its
396+ /// defining scope.
405397 #[ instrument( skip( self ) , level = "trace" , ret) ]
406- fn opaque_ty_origin_unchecked ( & self , def_id : LocalDefId , span : Span ) -> OpaqueTyOrigin {
398+ fn opaque_type_origin_unchecked ( & self , def_id : LocalDefId ) -> OpaqueTyOrigin {
407399 match self . tcx . hir ( ) . expect_item ( def_id) . kind {
408400 hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy { origin, .. } ) => origin,
409401 ref itemkind => {
410- span_bug ! ( span , "weird opaque type: {:?}, {:#?}" , def_id, itemkind)
402+ bug ! ( "weird opaque type: {:?}, {:#?}" , def_id, itemkind)
411403 }
412404 }
413405 }
0 commit comments