@@ -11,7 +11,7 @@ use rustc_macros::extension;
1111use rustc_middle:: mir:: { Body , ConstraintCategory } ;
1212use rustc_middle:: ty:: {
1313 self , DefiningScopeKind , FallibleTypeFolder , GenericArg , GenericArgsRef , OpaqueHiddenType ,
14- OpaqueTypeKey , Region , RegionVid , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable ,
14+ OpaqueTypeKey , Region , RegionVid , Ty , TyCtxt , TypeFoldable , TypeSuperFoldable ,
1515 TypeVisitableExt , fold_regions,
1616} ;
1717use rustc_mir_dataflow:: points:: DenseLocationMap ;
@@ -49,6 +49,10 @@ pub(crate) enum DeferredOpaqueTypeError<'tcx> {
4949 /// The unexpected region.
5050 member_region : Region < ' tcx > ,
5151 } ,
52+ NonDefiningUseInDefiningScope {
53+ span : Span ,
54+ opaque_type_key : OpaqueTypeKey < ' tcx > ,
55+ } ,
5256}
5357
5458pub ( crate ) fn handle_opaque_type_uses < ' tcx > (
@@ -99,7 +103,7 @@ pub(crate) fn handle_opaque_type_uses<'tcx>(
99103 return errors;
100104 }
101105
102- apply_defining_uses (
106+ let errors = apply_defining_uses (
103107 root_cx,
104108 infcx,
105109 body,
@@ -123,7 +127,7 @@ pub(crate) fn handle_opaque_type_uses<'tcx>(
123127
124128 let _ = infcx. take_opaque_types ( ) ;
125129
126- Vec :: new ( )
130+ errors
127131}
128132
129133#[ derive( Debug ) ]
@@ -149,21 +153,26 @@ fn collect_defining_uses<'tcx>(
149153 let mut errors = vec ! [ ] ;
150154 for & ( opaque_type_key, hidden_type) in opaque_types {
151155 let non_nll_opaque_type_key = opaque_type_key. fold_captured_lifetime_args ( infcx. tcx , |r| {
152- nll_var_to_universal_region ( & rcx, r. as_var ( ) ) . unwrap_or_else ( || {
153- ty:: Region :: new_error_with_message (
154- infcx. tcx ,
155- hidden_type. span ,
156- "opaque type with non-universal region args" ,
157- )
158- } )
156+ nll_var_to_universal_region ( & rcx, r. as_var ( ) ) . unwrap_or ( r)
159157 } ) ;
160158 if let Err ( err) = check_opaque_type_parameter_valid (
161159 infcx,
162160 non_nll_opaque_type_key,
163161 hidden_type. span ,
164162 DefiningScopeKind :: MirBorrowck ,
165163 ) {
166- errors. push ( DeferredOpaqueTypeError :: InvalidOpaqueTypeArgs ( err) ) ;
164+ if tcx. use_typing_mode_borrowck ( ) {
165+ match err {
166+ InvalidOpaqueTypeArgs :: AlreadyReported ( guar) => root_cx
167+ . add_concrete_opaque_type (
168+ opaque_type_key. def_id ,
169+ OpaqueHiddenType :: new_error ( tcx, guar) ,
170+ ) ,
171+ _ => debug ! ( ?non_nll_opaque_type_key, ?err, "ignoring non-defining use" ) ,
172+ }
173+ } else {
174+ errors. push ( DeferredOpaqueTypeError :: InvalidOpaqueTypeArgs ( err) ) ;
175+ }
167176 continue ;
168177 }
169178
@@ -439,16 +448,25 @@ fn apply_defining_uses<'tcx>(
439448 known_type_outlives_obligations : & [ ty:: PolyTypeOutlivesPredicate < ' tcx > ] ,
440449 constraints : & mut MirTypeckRegionConstraints < ' tcx > ,
441450 opaque_types : & [ ( OpaqueTypeKey < ' tcx > , OpaqueHiddenType < ' tcx > ) ] ,
442- ) {
451+ ) -> Vec < DeferredOpaqueTypeError < ' tcx > > {
443452 let tcx = infcx. tcx ;
453+ let mut errors = Vec :: new ( ) ;
444454 for & ( key, hidden_type) in opaque_types {
445455 let Some ( expected) = root_cx. get_concrete_opaque_type ( key. def_id ) else {
446- let guar = tcx
447- . dcx ( )
448- . span_delayed_bug ( hidden_type. span , "non-defining use in the defining scope" ) ;
449- root_cx. add_concrete_opaque_type ( key. def_id , OpaqueHiddenType :: new_error ( tcx, guar) ) ;
450- infcx. set_tainted_by_errors ( guar) ;
451- continue ;
456+ if tcx. use_typing_mode_borrowck ( ) {
457+ errors. push ( DeferredOpaqueTypeError :: NonDefiningUseInDefiningScope {
458+ span : hidden_type. span ,
459+ opaque_type_key : key,
460+ } ) ;
461+ let guar = tcx
462+ . dcx ( )
463+ . span_delayed_bug ( hidden_type. span , "non-defining use in the defining scope" ) ;
464+ root_cx
465+ . add_concrete_opaque_type ( key. def_id , OpaqueHiddenType :: new_error ( tcx, guar) ) ;
466+ continue ;
467+ } else {
468+ unreachable ! ( )
469+ }
452470 } ;
453471
454472 let expected = ty:: fold_regions ( tcx, expected. instantiate ( tcx, key. args ) , |re, _dbi| {
@@ -487,6 +505,7 @@ fn apply_defining_uses<'tcx>(
487505 root_cx. add_concrete_opaque_type ( key. def_id , OpaqueHiddenType :: new_error ( tcx, guar) ) ;
488506 }
489507 }
508+ errors
490509}
491510
492511impl < ' tcx > RegionInferenceContext < ' tcx > {
0 commit comments