@@ -1388,6 +1388,7 @@ fn opaque_type_cycle_error(
13881388 struct OpaqueTypeCollector {
13891389 opaques : Vec < DefId > ,
13901390 closures : Vec < DefId > ,
1391+ coroutine : Vec < DefId > ,
13911392 }
13921393 impl < ' tcx > ty:: visit:: TypeVisitor < TyCtxt < ' tcx > > for OpaqueTypeCollector {
13931394 fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
@@ -1396,10 +1397,14 @@ fn opaque_type_cycle_error(
13961397 self . opaques . push ( def) ;
13971398 ControlFlow :: Continue ( ( ) )
13981399 }
1399- ty:: Closure ( def_id, ..) | ty :: Coroutine ( def_id , .. ) => {
1400+ ty:: Closure ( def_id, ..) => {
14001401 self . closures . push ( def_id) ;
14011402 t. super_visit_with ( self )
14021403 }
1404+ ty:: Coroutine ( def_id, ..) => {
1405+ self . coroutine . push ( def_id) ;
1406+ t. super_visit_with ( self )
1407+ }
14031408 _ => t. super_visit_with ( self ) ,
14041409 }
14051410 }
@@ -1417,43 +1422,48 @@ fn opaque_type_cycle_error(
14171422 err. span_label ( sp, format ! ( "returning here with type `{ty}`" ) ) ;
14181423 }
14191424
1420- for closure_def_id in visitor. closures {
1421- let Some ( closure_local_did) = closure_def_id. as_local ( ) else {
1422- continue ;
1423- } ;
1424- let typeck_results = tcx. typeck ( closure_local_did) ;
1425-
1426- let mut label_match = |ty : Ty < ' _ > , span| {
1427- for arg in ty. walk ( ) {
1428- if let ty:: GenericArgKind :: Type ( ty) = arg. unpack ( )
1429- && let ty:: Alias (
1430- ty:: Opaque ,
1431- ty:: AliasTy { def_id : captured_def_id, .. } ,
1432- ) = * ty. kind ( )
1433- && captured_def_id == opaque_def_id. to_def_id ( )
1434- {
1435- err. span_label (
1436- span,
1437- format ! (
1438- "{} captures itself here" ,
1439- tcx. def_descr( closure_def_id)
1440- ) ,
1441- ) ;
1442- }
1425+ let label_match = |err : & mut DiagnosticBuilder < ' _ , _ > , ty : Ty < ' _ > , span, def_id| {
1426+ for arg in ty. walk ( ) {
1427+ if let ty:: GenericArgKind :: Type ( ty) = arg. unpack ( )
1428+ && let ty:: Alias (
1429+ ty:: Opaque ,
1430+ ty:: AliasTy { def_id : captured_def_id, .. } ,
1431+ ) = * ty. kind ( )
1432+ && captured_def_id == opaque_def_id. to_def_id ( )
1433+ {
1434+ err. span_label (
1435+ span,
1436+ format ! ( "{} captures itself here" , tcx. def_descr( def_id) ) ,
1437+ ) ;
14431438 }
1444- } ;
1439+ }
1440+ } ;
14451441
1442+ let capture = |err : & mut DiagnosticBuilder < ' _ , _ > , def_id : DefId | {
1443+ let Some ( local_id) = def_id. as_local ( ) else {
1444+ return ;
1445+ } ;
1446+ let typeck_results = tcx. typeck ( local_id) ;
14461447 // Label any closure upvars that capture the opaque
1447- for capture in typeck_results. closure_min_captures_flattened ( closure_local_did)
1448- {
1449- label_match ( capture. place . ty ( ) , capture. get_path_span ( tcx) ) ;
1448+ for capture in typeck_results. closure_min_captures_flattened ( local_id) {
1449+ label_match ( err, capture. place . ty ( ) , capture. get_path_span ( tcx) , def_id) ;
14501450 }
1451- // Label any coroutine locals that capture the opaque
1452- if tcx. is_coroutine ( closure_def_id)
1453- && let Some ( coroutine_layout) = tcx. mir_coroutine_witnesses ( closure_def_id)
1454- {
1451+ } ;
1452+
1453+ for closure_def_id in visitor. closures {
1454+ capture ( & mut err, closure_def_id) ;
1455+ }
1456+
1457+ for coroutine_def_id in visitor. coroutine {
1458+ capture ( & mut err, coroutine_def_id) ;
1459+ if let Some ( coroutine_layout) = tcx. mir_coroutine_witnesses ( coroutine_def_id) {
14551460 for interior_ty in & coroutine_layout. field_tys {
1456- label_match ( interior_ty. ty , interior_ty. source_info . span ) ;
1461+ label_match (
1462+ & mut err,
1463+ interior_ty. ty ,
1464+ interior_ty. source_info . span ,
1465+ coroutine_def_id,
1466+ ) ;
14571467 }
14581468 }
14591469 }
0 commit comments