@@ -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 } ;
@@ -3142,55 +3143,38 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
3142
3143
kind : ty:: ClosureKind ,
3143
3144
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
3144
3145
let closure_span = self . tcx . def_span ( closure_def_id) ;
3145
- let mut err = struct_span_err ! (
3146
- self . tcx. sess,
3147
- closure_span,
3148
- E0525 ,
3149
- "expected a closure that implements the `{}` trait, \
3150
- but this closure only implements `{}`",
3151
- kind,
3152
- found_kind
3153
- ) ;
3154
3146
3155
- err. span_label (
3147
+ let mut err = ClosureKindMismatch {
3156
3148
closure_span,
3157
- format ! ( "this closure implements `{found_kind}`, not `{ kind}`" ) ,
3158
- ) ;
3159
- err . span_label (
3160
- obligation . cause . span ,
3161
- format ! ( "the requirement to implement `{kind}` derives from here" ) ,
3162
- ) ;
3149
+ expected : kind,
3150
+ found : found_kind ,
3151
+ cause_span : obligation . cause . span ,
3152
+ fn_once_label : None ,
3153
+ fn_mut_label : None ,
3154
+ } ;
3163
3155
3164
3156
// Additional context information explaining why the closure only implements
3165
3157
// a particular trait.
3166
3158
if let Some ( typeck_results) = & self . typeck_results {
3167
3159
let hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( closure_def_id. expect_local ( ) ) ;
3168
3160
match ( found_kind, typeck_results. closure_kind_origins ( ) . get ( hir_id) ) {
3169
3161
( ty:: ClosureKind :: FnOnce , Some ( ( span, place) ) ) => {
3170
- err. span_label (
3171
- * span,
3172
- format ! (
3173
- "closure is `FnOnce` because it moves the \
3174
- variable `{}` out of its environment",
3175
- ty:: place_to_string_for_capture( self . tcx, place)
3176
- ) ,
3177
- ) ;
3162
+ err. fn_once_label = Some ( ClosureFnOnceLabel {
3163
+ span : * span,
3164
+ place : ty:: place_to_string_for_capture ( self . tcx , & place) ,
3165
+ } )
3178
3166
}
3179
3167
( ty:: ClosureKind :: FnMut , Some ( ( span, place) ) ) => {
3180
- err. span_label (
3181
- * span,
3182
- format ! (
3183
- "closure is `FnMut` because it mutates the \
3184
- variable `{}` here",
3185
- ty:: place_to_string_for_capture( self . tcx, place)
3186
- ) ,
3187
- ) ;
3168
+ err. fn_mut_label = Some ( ClosureFnMutLabel {
3169
+ span : * span,
3170
+ place : ty:: place_to_string_for_capture ( self . tcx , & place) ,
3171
+ } )
3188
3172
}
3189
3173
_ => { }
3190
3174
}
3191
3175
}
3192
3176
3193
- err
3177
+ self . tcx . sess . create_err ( err)
3194
3178
}
3195
3179
3196
3180
fn report_type_parameter_mismatch_cyclic_type_error (
0 commit comments