@@ -5,9 +5,9 @@ use rustc_infer::infer::{
5
5
error_reporting:: nice_region_error:: NiceRegionError ,
6
6
error_reporting:: unexpected_hidden_region_diagnostic, NLLRegionVariableOrigin ,
7
7
} ;
8
- use rustc_middle:: mir:: { ConstraintCategory , ReturnConstraint } ;
8
+ use rustc_middle:: mir:: ConstraintCategory ;
9
9
use rustc_middle:: ty:: { self , RegionVid , Ty } ;
10
- use rustc_span:: symbol:: { kw , sym } ;
10
+ use rustc_span:: symbol:: kw ;
11
11
use rustc_span:: Span ;
12
12
13
13
use crate :: util:: borrowck_errors;
@@ -26,7 +26,7 @@ impl ConstraintDescription for ConstraintCategory {
26
26
// Must end with a space. Allows for empty names to be provided.
27
27
match self {
28
28
ConstraintCategory :: Assignment => "assignment " ,
29
- ConstraintCategory :: Return ( _ ) => "returning this value " ,
29
+ ConstraintCategory :: Return => "returning this value " ,
30
30
ConstraintCategory :: Yield => "yielding this value " ,
31
31
ConstraintCategory :: UseAsConst => "using this value as a constant " ,
32
32
ConstraintCategory :: UseAsStatic => "using this value as a static " ,
@@ -37,7 +37,6 @@ impl ConstraintDescription for ConstraintCategory {
37
37
ConstraintCategory :: SizedBound => "proving this value is `Sized` " ,
38
38
ConstraintCategory :: CopyBound => "copying this value " ,
39
39
ConstraintCategory :: OpaqueType => "opaque type " ,
40
- ConstraintCategory :: ClosureUpvar ( _) => "closure capture " ,
41
40
ConstraintCategory :: Boring
42
41
| ConstraintCategory :: BoringNoLocation
43
42
| ConstraintCategory :: Internal => "" ,
@@ -307,8 +306,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
307
306
} ;
308
307
309
308
let diag = match ( category, fr_is_local, outlived_fr_is_local) {
310
- ( ConstraintCategory :: Return ( kind ) , true , false ) if self . is_closure_fn_mut ( fr) => {
311
- self . report_fnmut_error ( & errci, kind )
309
+ ( ConstraintCategory :: Return , true , false ) if self . is_closure_fn_mut ( fr) => {
310
+ self . report_fnmut_error ( & errci)
312
311
}
313
312
( ConstraintCategory :: Assignment , true , false )
314
313
| ( ConstraintCategory :: CallArgument , true , false ) => {
@@ -348,11 +347,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
348
347
/// executing...
349
348
/// = note: ...therefore, returned references to captured variables will escape the closure
350
349
/// ```
351
- fn report_fnmut_error (
352
- & self ,
353
- errci : & ErrorConstraintInfo ,
354
- kind : ReturnConstraint ,
355
- ) -> DiagnosticBuilder < ' tcx > {
350
+ fn report_fnmut_error ( & self , errci : & ErrorConstraintInfo ) -> DiagnosticBuilder < ' tcx > {
356
351
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
357
352
358
353
let mut diag = self
@@ -361,39 +356,19 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
361
356
. sess
362
357
. struct_span_err ( * span, "captured variable cannot escape `FnMut` closure body" ) ;
363
358
364
- let mut output_ty = self . regioncx . universal_regions ( ) . unnormalized_output_ty ;
365
- if let ty:: Opaque ( def_id, _) = output_ty. kind {
366
- output_ty = self . infcx . tcx . type_of ( def_id)
367
- } ;
368
-
369
- debug ! ( "report_fnmut_error: output_ty={:?}" , output_ty) ;
370
-
371
- let message = match output_ty. kind {
372
- ty:: Closure ( _, _) => {
373
- "returns a closure that contains a reference to a captured variable, which then \
374
- escapes the closure body"
375
- }
376
- ty:: Adt ( def, _) if self . infcx . tcx . is_diagnostic_item ( sym:: gen_future, def. did ) => {
377
- "returns an `async` block that contains a reference to a captured variable, which then \
378
- escapes the closure body"
379
- }
380
- _ => "returns a reference to a captured variable which escapes the closure body" ,
359
+ // We should check if the return type of this closure is in fact a closure - in that
360
+ // case, we can special case the error further.
361
+ let return_type_is_closure =
362
+ self . regioncx . universal_regions ( ) . unnormalized_output_ty . is_closure ( ) ;
363
+ let message = if return_type_is_closure {
364
+ "returns a closure that contains a reference to a captured variable, which then \
365
+ escapes the closure body"
366
+ } else {
367
+ "returns a reference to a captured variable which escapes the closure body"
381
368
} ;
382
369
383
370
diag. span_label ( * span, message) ;
384
371
385
- if let ReturnConstraint :: ClosureUpvar ( upvar) = kind {
386
- let def_id = match self . regioncx . universal_regions ( ) . defining_ty {
387
- DefiningTy :: Closure ( def_id, _) => def_id,
388
- ty @ _ => bug ! ( "unexpected DefiningTy {:?}" , ty) ,
389
- } ;
390
-
391
- let upvar_def_span = self . infcx . tcx . hir ( ) . span ( upvar) ;
392
- let upvar_span = self . infcx . tcx . upvars_mentioned ( def_id) . unwrap ( ) [ & upvar] . span ;
393
- diag. span_label ( upvar_def_span, "variable defined here" ) ;
394
- diag. span_label ( upvar_span, "variable captured here" ) ;
395
- }
396
-
397
372
match self . give_region_a_name ( * outlived_fr) . unwrap ( ) . source {
398
373
RegionNameSource :: NamedEarlyBoundRegion ( fr_span)
399
374
| RegionNameSource :: NamedFreeRegion ( fr_span)
@@ -531,7 +506,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
531
506
outlived_fr_name. highlight_region_name ( & mut diag) ;
532
507
533
508
match ( category, outlived_fr_is_local, fr_is_local) {
534
- ( ConstraintCategory :: Return ( _ ) , true , _) => {
509
+ ( ConstraintCategory :: Return , true , _) => {
535
510
diag. span_label (
536
511
* span,
537
512
format ! (
0 commit comments