@@ -15,7 +15,7 @@ use rustc::traits::Obligation;
15
15
use rustc:: ty:: { self , Ty , TyCtxt , ToPolyTraitRef , ToPredicate , TypeFoldable } ;
16
16
use rustc:: ty:: print:: with_crate_prefix;
17
17
use syntax_pos:: { Span , FileName } ;
18
- use syntax:: ast;
18
+ use syntax:: { ast, source_map } ;
19
19
use syntax:: util:: lev_distance;
20
20
21
21
use rustc_error_codes:: * ;
@@ -79,61 +79,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
79
79
return None ;
80
80
}
81
81
82
- let print_disambiguation_help = |
83
- err : & mut DiagnosticBuilder < ' _ > ,
84
- trait_name : String ,
85
- rcvr_ty : Ty < ' _ > ,
86
- kind : ty:: AssocKind ,
87
- span : Span ,
88
- candidate : Option < usize > ,
89
- | {
90
- let mut applicability = Applicability :: MachineApplicable ;
91
- let sugg_args = if let ty:: AssocKind :: Method = kind {
92
- format!(
93
- "({}{})" ,
94
- if rcvr_ty. is_region_ptr ( ) && args. is_some ( ) {
95
- if rcvr_ty. is_mutable_ptr ( ) {
96
- "&mut "
97
- } else {
98
- "&"
99
- }
100
- } else {
101
- ""
102
- } ,
103
- args. map ( |arg| arg
104
- . iter ( )
105
- . map ( |arg| self . tcx . sess . source_map ( ) . span_to_snippet ( arg. span )
106
- . unwrap_or_else ( |_| {
107
- applicability = Applicability :: HasPlaceholders ;
108
- "..." . to_owned ( )
109
- } ) )
110
- . collect :: < Vec < _ > > ( )
111
- . join ( ", " )
112
- ) . unwrap_or_else ( || {
113
- applicability = Applicability :: HasPlaceholders ;
114
- "..." . to_owned ( )
115
- } ) ,
116
- )
117
- } else {
118
- String :: new ( )
119
- } ;
120
- let sugg = format ! ( "{}::{}{}" , trait_name, item_name, sugg_args) ;
121
- err. span_suggestion (
122
- span,
123
- & format ! (
124
- "disambiguate the {} for {}" ,
125
- kind. suggestion_descr( ) ,
126
- if let Some ( candidate) = candidate {
127
- format!( "candidate #{}" , candidate)
128
- } else {
129
- "the candidate" . to_string( )
130
- } ,
131
- ) ,
132
- sugg,
133
- applicability,
134
- ) ;
135
- } ;
136
-
137
82
let report_candidates = |
138
83
span : Span ,
139
84
err : & mut DiagnosticBuilder < ' _ > ,
@@ -215,7 +160,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
215
160
. map ( |ty| * ty)
216
161
. unwrap_or ( rcvr_ty) ,
217
162
} ;
218
- print_disambiguation_help ( err, path, ty, item. kind , sugg_span, idx) ;
163
+ print_disambiguation_help (
164
+ item_name,
165
+ args,
166
+ err,
167
+ path,
168
+ ty,
169
+ item. kind ,
170
+ sugg_span,
171
+ idx,
172
+ self . tcx . sess . source_map ( ) ,
173
+ ) ;
219
174
}
220
175
}
221
176
CandidateSource :: TraitSource ( trait_did) => {
@@ -244,7 +199,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
244
199
None
245
200
} ;
246
201
let path = self . tcx . def_path_str ( trait_did) ;
247
- print_disambiguation_help ( err, path, rcvr_ty, item. kind , sugg_span, idx) ;
202
+ print_disambiguation_help (
203
+ item_name,
204
+ args,
205
+ err,
206
+ path,
207
+ rcvr_ty,
208
+ item. kind ,
209
+ sugg_span,
210
+ idx,
211
+ self . tcx . sess . source_map ( ) ,
212
+ ) ;
248
213
}
249
214
}
250
215
}
@@ -1180,3 +1145,56 @@ impl hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> {
1180
1145
hir:: intravisit:: NestedVisitorMap :: None
1181
1146
}
1182
1147
}
1148
+
1149
+ fn print_disambiguation_help (
1150
+ item_name : ast:: Ident ,
1151
+ args : Option < & ' tcx [ hir:: Expr ] > ,
1152
+ err : & mut DiagnosticBuilder < ' _ > ,
1153
+ trait_name : String ,
1154
+ rcvr_ty : Ty < ' _ > ,
1155
+ kind : ty:: AssocKind ,
1156
+ span : Span ,
1157
+ candidate : Option < usize > ,
1158
+ source_map : & source_map:: SourceMap ,
1159
+ ) {
1160
+ let mut applicability = Applicability :: MachineApplicable ;
1161
+ let sugg_args = if let ( ty:: AssocKind :: Method , Some ( args) ) = ( kind, args) {
1162
+ format ! (
1163
+ "({}{})" ,
1164
+ if rcvr_ty. is_region_ptr( ) {
1165
+ if rcvr_ty. is_mutable_ptr( ) {
1166
+ "&mut "
1167
+ } else {
1168
+ "&"
1169
+ }
1170
+ } else {
1171
+ ""
1172
+ } ,
1173
+ args. iter( )
1174
+ . map( |arg| source_map. span_to_snippet( arg. span)
1175
+ . unwrap_or_else( |_| {
1176
+ applicability = Applicability :: HasPlaceholders ;
1177
+ "_" . to_owned( )
1178
+ } ) )
1179
+ . collect:: <Vec <_>>( )
1180
+ . join( ", " ) ,
1181
+ )
1182
+ } else {
1183
+ String :: new ( )
1184
+ } ;
1185
+ let sugg = format ! ( "{}::{}{}" , trait_name, item_name, sugg_args) ;
1186
+ err. span_suggestion (
1187
+ span,
1188
+ & format ! (
1189
+ "disambiguate the {} for {}" ,
1190
+ kind. suggestion_descr( ) ,
1191
+ if let Some ( candidate) = candidate {
1192
+ format!( "candidate #{}" , candidate)
1193
+ } else {
1194
+ "the candidate" . to_string( )
1195
+ } ,
1196
+ ) ,
1197
+ sugg,
1198
+ applicability,
1199
+ ) ;
1200
+ }
0 commit comments