@@ -15,6 +15,7 @@ use super::{
15
15
Obligation ,
16
16
ObligationCause ,
17
17
ObligationCauseCode ,
18
+ OnUnimplementedInfo ,
18
19
OutputTypeParameterMismatch ,
19
20
TraitNotObjectSafe ,
20
21
PredicateObligation ,
@@ -25,7 +26,6 @@ use super::{
25
26
} ;
26
27
27
28
use errors:: DiagnosticBuilder ;
28
- use fmt_macros:: { Parser , Piece , Position } ;
29
29
use hir;
30
30
use hir:: def_id:: DefId ;
31
31
use infer:: { self , InferCtxt } ;
@@ -39,148 +39,9 @@ use ty::fast_reject;
39
39
use ty:: fold:: TypeFolder ;
40
40
use ty:: subst:: Subst ;
41
41
use ty:: SubtypePredicate ;
42
- use util:: common:: ErrorReported ;
43
42
use util:: nodemap:: { FxHashMap , FxHashSet } ;
44
43
45
44
use syntax_pos:: { DUMMY_SP , Span } ;
46
- use syntax_pos:: symbol:: InternedString ;
47
-
48
- pub struct OnUnimplementedFormatString ( InternedString ) ;
49
- pub struct OnUnimplementedInfo {
50
- note : OnUnimplementedFormatString
51
- }
52
-
53
- impl < ' a , ' gcx , ' tcx > OnUnimplementedInfo {
54
- pub fn of_item ( tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
55
- trait_def_id : DefId ,
56
- impl_def_id : DefId ,
57
- span : Span )
58
- -> Result < Option < Self > , ErrorReported >
59
- {
60
- let attrs = tcx. get_attrs ( impl_def_id) ;
61
-
62
- let attr = if let Some ( item) =
63
- attrs. into_iter ( ) . find ( |a| a. check_name ( "rustc_on_unimplemented" ) )
64
- {
65
- item
66
- } else {
67
- return Ok ( None ) ;
68
- } ;
69
-
70
- let span = attr. span . substitute_dummy ( span) ;
71
- if let Some ( label) = attr. value_str ( ) {
72
- Ok ( Some ( OnUnimplementedInfo {
73
- note : OnUnimplementedFormatString :: try_parse (
74
- tcx, trait_def_id, label. as_str ( ) , span) ?
75
- } ) )
76
- } else {
77
- struct_span_err ! (
78
- tcx. sess, span, E0232 ,
79
- "this attribute must have a value" )
80
- . span_label ( attr. span , "attribute requires a value" )
81
- . note ( & format ! ( "eg `#[rustc_on_unimplemented = \" foo\" ]`" ) )
82
- . emit ( ) ;
83
- Err ( ErrorReported )
84
- }
85
- }
86
- }
87
-
88
- impl < ' a , ' gcx , ' tcx > OnUnimplementedFormatString {
89
- pub fn try_parse ( tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
90
- trait_def_id : DefId ,
91
- from : InternedString ,
92
- err_sp : Span )
93
- -> Result < Self , ErrorReported >
94
- {
95
- let result = OnUnimplementedFormatString ( from) ;
96
- result. verify ( tcx, trait_def_id, err_sp) ?;
97
- Ok ( result)
98
- }
99
-
100
- fn verify ( & self ,
101
- tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
102
- trait_def_id : DefId ,
103
- span : Span )
104
- -> Result < ( ) , ErrorReported >
105
- {
106
- let name = tcx. item_name ( trait_def_id) . as_str ( ) ;
107
- let generics = tcx. generics_of ( trait_def_id) ;
108
- let parser = Parser :: new ( & self . 0 ) ;
109
- let types = & generics. types ;
110
- let mut result = Ok ( ( ) ) ;
111
- for token in parser {
112
- match token {
113
- Piece :: String ( _) => ( ) , // Normal string, no need to check it
114
- Piece :: NextArgument ( a) => match a. position {
115
- // `{Self}` is allowed
116
- Position :: ArgumentNamed ( s) if s == "Self" => ( ) ,
117
- // `{ThisTraitsName}` is allowed
118
- Position :: ArgumentNamed ( s) if s == name => ( ) ,
119
- // So is `{A}` if A is a type parameter
120
- Position :: ArgumentNamed ( s) => match types. iter ( ) . find ( |t| {
121
- t. name == s
122
- } ) {
123
- Some ( _) => ( ) ,
124
- None => {
125
- span_err ! ( tcx. sess, span, E0230 ,
126
- "there is no type parameter \
127
- {} on trait {}",
128
- s, name) ;
129
- result = Err ( ErrorReported ) ;
130
- }
131
- } ,
132
- // `{:1}` and `{}` are not to be used
133
- Position :: ArgumentIs ( _) => {
134
- span_err ! ( tcx. sess, span, E0231 ,
135
- "only named substitution \
136
- parameters are allowed") ;
137
- result = Err ( ErrorReported ) ;
138
- }
139
- }
140
- }
141
- }
142
-
143
- result
144
- }
145
-
146
- fn format ( & self ,
147
- tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
148
- trait_ref : ty:: TraitRef < ' tcx > )
149
- -> String
150
- {
151
- let name = tcx. item_name ( trait_ref. def_id ) . as_str ( ) ;
152
- let trait_str = tcx. item_path_str ( trait_ref. def_id ) ;
153
- let generics = tcx. generics_of ( trait_ref. def_id ) ;
154
- let generic_map = generics. types . iter ( ) . map ( |param| {
155
- ( param. name . as_str ( ) . to_string ( ) ,
156
- trait_ref. substs . type_for_def ( param) . to_string ( ) )
157
- } ) . collect :: < FxHashMap < String , String > > ( ) ;
158
-
159
- let parser = Parser :: new ( & self . 0 ) ;
160
- parser. map ( |p| {
161
- match p {
162
- Piece :: String ( s) => s,
163
- Piece :: NextArgument ( a) => match a. position {
164
- Position :: ArgumentNamed ( s) => match generic_map. get ( s) {
165
- Some ( val) => val,
166
- None if s == name => {
167
- & trait_str
168
- }
169
- None => {
170
- bug ! ( "broken on_unimplemented {:?} for {:?}: \
171
- no argument matching {:?}",
172
- self . 0 , trait_ref, s)
173
- }
174
- } ,
175
- _ => {
176
- bug ! ( "broken on_unimplemented {:?} - bad \
177
- format arg", self . 0 )
178
- }
179
- }
180
- }
181
- } ) . collect ( )
182
- }
183
- }
184
45
185
46
impl < ' a , ' gcx , ' tcx > InferCtxt < ' a , ' gcx , ' tcx > {
186
47
pub fn report_fulfillment_errors ( & self ,
@@ -465,7 +326,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
465
326
match OnUnimplementedInfo :: of_item (
466
327
self . tcx , trait_ref. def_id , def_id, obligation. cause . span
467
328
) {
468
- Ok ( Some ( info) ) => Some ( info. note . format ( self . tcx , * trait_ref) ) ,
329
+ Ok ( Some ( info) ) => Some ( info. label . format ( self . tcx , * trait_ref) ) ,
469
330
_ => None
470
331
}
471
332
}
0 commit comments