@@ -7,6 +7,7 @@ use super::{
7
7
ObligationCauseCode , ObligationCtxt , OutputTypeParameterMismatch , Overflow ,
8
8
PredicateObligation , SelectionError , TraitNotObjectSafe ,
9
9
} ;
10
+ use crate :: errors:: { ClosureFnMutLabel , ClosureFnOnceLabel , ClosureKindMismatch } ;
10
11
use crate :: infer:: error_reporting:: { TyCategory , TypeAnnotationNeeded as ErrorCode } ;
11
12
use crate :: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
12
13
use crate :: infer:: { self , InferCtxt } ;
@@ -3121,55 +3122,38 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
3121
3122
kind : ty:: ClosureKind ,
3122
3123
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
3123
3124
let closure_span = self . tcx . def_span ( closure_def_id) ;
3124
- let mut err = struct_span_err ! (
3125
- self . tcx. sess,
3126
- closure_span,
3127
- E0525 ,
3128
- "expected a closure that implements the `{}` trait, \
3129
- but this closure only implements `{}`",
3130
- kind,
3131
- found_kind
3132
- ) ;
3133
3125
3134
- err. span_label (
3126
+ let mut err = ClosureKindMismatch {
3135
3127
closure_span,
3136
- format ! ( "this closure implements `{found_kind}`, not `{ kind}`" ) ,
3137
- ) ;
3138
- err . span_label (
3139
- obligation . cause . span ,
3140
- format ! ( "the requirement to implement `{kind}` derives from here" ) ,
3141
- ) ;
3128
+ expected : kind,
3129
+ found : found_kind ,
3130
+ cause_span : obligation . cause . span ,
3131
+ fn_once_label : None ,
3132
+ fn_mut_label : None ,
3133
+ } ;
3142
3134
3143
3135
// Additional context information explaining why the closure only implements
3144
3136
// a particular trait.
3145
3137
if let Some ( typeck_results) = & self . typeck_results {
3146
3138
let hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( closure_def_id. expect_local ( ) ) ;
3147
3139
match ( found_kind, typeck_results. closure_kind_origins ( ) . get ( hir_id) ) {
3148
3140
( ty:: ClosureKind :: FnOnce , Some ( ( span, place) ) ) => {
3149
- err. span_label (
3150
- * span,
3151
- format ! (
3152
- "closure is `FnOnce` because it moves the \
3153
- variable `{}` out of its environment",
3154
- ty:: place_to_string_for_capture( self . tcx, place)
3155
- ) ,
3156
- ) ;
3141
+ err. fn_once_label = Some ( ClosureFnOnceLabel {
3142
+ span : * span,
3143
+ place : ty:: place_to_string_for_capture ( self . tcx , & place) ,
3144
+ } )
3157
3145
}
3158
3146
( ty:: ClosureKind :: FnMut , Some ( ( span, place) ) ) => {
3159
- err. span_label (
3160
- * span,
3161
- format ! (
3162
- "closure is `FnMut` because it mutates the \
3163
- variable `{}` here",
3164
- ty:: place_to_string_for_capture( self . tcx, place)
3165
- ) ,
3166
- ) ;
3147
+ err. fn_mut_label = Some ( ClosureFnMutLabel {
3148
+ span : * span,
3149
+ place : ty:: place_to_string_for_capture ( self . tcx , & place) ,
3150
+ } )
3167
3151
}
3168
3152
_ => { }
3169
3153
}
3170
3154
}
3171
3155
3172
- err
3156
+ self . tcx . sess . create_err ( err)
3173
3157
}
3174
3158
3175
3159
fn report_type_parameter_mismatch_cyclic_type_error (
0 commit comments