@@ -4,7 +4,7 @@ use crate::errors::{
4
4
} ;
5
5
use crate :: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
6
6
use crate :: infer:: InferCtxt ;
7
- use rustc_errors:: { DiagnosticBuilder , ErrorGuaranteed } ;
7
+ use rustc_errors:: { DiagnosticBuilder , ErrorGuaranteed , IntoDiagnosticArg } ;
8
8
use rustc_hir as hir;
9
9
use rustc_hir:: def:: Res ;
10
10
use rustc_hir:: def:: { CtorOf , DefKind , Namespace } ;
@@ -65,6 +65,7 @@ pub struct InferenceDiagnosticsParentData {
65
65
name : String ,
66
66
}
67
67
68
+ #[ derive( Clone ) ]
68
69
pub enum UnderspecifiedArgKind {
69
70
Type { prefix : Cow < ' static , str > } ,
70
71
Const { is_parameter : bool } ,
@@ -101,7 +102,7 @@ impl InferenceDiagnosticsData {
101
102
InferenceBadError {
102
103
span,
103
104
bad_kind,
104
- prefix_kind : self . kind . prefix_kind ( ) ,
105
+ prefix_kind : self . kind . clone ( ) ,
105
106
prefix : self . kind . try_get_prefix ( ) . unwrap_or_default ( ) ,
106
107
name : self . name . clone ( ) ,
107
108
has_parent,
@@ -130,14 +131,18 @@ impl InferenceDiagnosticsParentData {
130
131
}
131
132
}
132
133
133
- impl UnderspecifiedArgKind {
134
- fn prefix_kind ( & self ) -> & ' static str {
135
- match self {
134
+ impl IntoDiagnosticArg for UnderspecifiedArgKind {
135
+ fn into_diagnostic_arg ( self ) -> rustc_errors :: DiagnosticArgValue < ' static > {
136
+ let kind = match self {
136
137
Self :: Type { .. } => "type" ,
137
138
Self :: Const { is_parameter : true } => "const_with_param" ,
138
139
Self :: Const { is_parameter : false } => "const" ,
139
- }
140
+ } ;
141
+ rustc_errors:: DiagnosticArgValue :: Str ( kind. into ( ) )
140
142
}
143
+ }
144
+
145
+ impl UnderspecifiedArgKind {
141
146
fn try_get_prefix ( & self ) -> Option < & str > {
142
147
match self {
143
148
Self :: Type { prefix } => Some ( prefix. as_ref ( ) ) ,
@@ -405,7 +410,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
405
410
span : insert_span,
406
411
name : pattern_name. map ( |name| name. to_string ( ) ) . unwrap_or_else ( String :: new) ,
407
412
x_kind : arg_data. where_x_is_kind ( ty) ,
408
- prefix_kind : arg_data. kind . prefix_kind ( ) ,
413
+ prefix_kind : arg_data. kind . clone ( ) ,
409
414
prefix : arg_data. kind . try_get_prefix ( ) . unwrap_or_default ( ) ,
410
415
arg_name : arg_data. name ,
411
416
kind : if pattern_name. is_some ( ) { "with_pattern" } else { "other" } ,
@@ -417,7 +422,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
417
422
span : insert_span,
418
423
name : String :: new ( ) ,
419
424
x_kind : arg_data. where_x_is_kind ( ty) ,
420
- prefix_kind : arg_data. kind . prefix_kind ( ) ,
425
+ prefix_kind : arg_data. kind . clone ( ) ,
421
426
prefix : arg_data. kind . try_get_prefix ( ) . unwrap_or_default ( ) ,
422
427
arg_name : arg_data. name ,
423
428
kind : "closure" ,
@@ -568,12 +573,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
568
573
NeedTypeInfoInGenerator {
569
574
bad_label : data. make_bad_error ( span) ,
570
575
span,
571
- generator_kind : kind . descr ( ) ,
576
+ generator_kind : GeneratorKindAsDiagArg ( kind ) ,
572
577
}
573
578
. into_diagnostic ( & self . tcx . sess . parse_sess )
574
579
}
575
580
}
576
581
582
+ pub struct GeneratorKindAsDiagArg ( pub hir:: GeneratorKind ) ;
583
+
584
+ impl IntoDiagnosticArg for GeneratorKindAsDiagArg {
585
+ fn into_diagnostic_arg ( self ) -> rustc_errors:: DiagnosticArgValue < ' static > {
586
+ let kind = match self . 0 {
587
+ hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Block ) => "async_block" ,
588
+ hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Closure ) => "async_closure" ,
589
+ hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Fn ) => "async_fn" ,
590
+ hir:: GeneratorKind :: Gen => "generator" ,
591
+ } ;
592
+ rustc_errors:: DiagnosticArgValue :: Str ( kind. into ( ) )
593
+ }
594
+ }
595
+
577
596
#[ derive( Debug ) ]
578
597
struct InferSource < ' tcx > {
579
598
span : Span ,
0 commit comments