@@ -10,7 +10,6 @@ use rustc::hir::{self, ExprKind, Node, QPath};
10
10
use rustc:: hir:: def:: { Res , DefKind } ;
11
11
use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , LOCAL_CRATE , DefId } ;
12
12
use rustc:: hir:: map as hir_map;
13
- use rustc:: hir:: print;
14
13
use rustc:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
15
14
use rustc:: traits:: Obligation ;
16
15
use rustc:: ty:: { self , Ty , TyCtxt , ToPolyTraitRef , ToPredicate , TypeFoldable } ;
@@ -78,6 +77,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
78
77
return ;
79
78
}
80
79
80
+ let print_disambiguation_help = |
81
+ err : & mut DiagnosticBuilder < ' _ > ,
82
+ trait_name : String ,
83
+ | {
84
+ err. help( & format ! (
85
+ "to disambiguate the method call, write `{}::{}({}{})` instead" ,
86
+ trait_name,
87
+ item_name,
88
+ if rcvr_ty. is_region_ptr( ) && args. is_some( ) {
89
+ if rcvr_ty. is_mutable_pointer( ) {
90
+ "&mut "
91
+ } else {
92
+ "&"
93
+ }
94
+ } else {
95
+ ""
96
+ } ,
97
+ args. map( |arg| arg
98
+ . iter( )
99
+ . map( |arg| self . tcx. sess. source_map( ) . span_to_snippet( arg. span)
100
+ . unwrap_or_else( |_| "..." . to_owned( ) ) )
101
+ . collect:: <Vec <_>>( )
102
+ . join( ", " )
103
+ ) . unwrap_or_else( || "..." . to_owned( ) )
104
+ ) ) ;
105
+ } ;
106
+
81
107
let report_candidates = |
82
108
span: Span ,
83
109
err: & mut DiagnosticBuilder <' _>,
@@ -139,6 +165,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
139
165
} else {
140
166
err. note ( & note_str) ;
141
167
}
168
+ if let Some ( trait_ref) = self . tcx . impl_trait_ref ( impl_did) {
169
+ print_disambiguation_help ( err, self . tcx . def_path_str ( trait_ref. def_id ) ) ;
170
+ }
142
171
}
143
172
CandidateSource :: TraitSource ( trait_did) => {
144
173
let item = match self . associated_item (
@@ -163,24 +192,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
163
192
"the candidate is defined in the trait `{}`" ,
164
193
self . tcx. def_path_str( trait_did) ) ;
165
194
}
166
- err. help ( & format ! ( "to disambiguate the method call, write `{}::{}({}{})` \
167
- instead",
168
- self . tcx. def_path_str( trait_did) ,
169
- item_name,
170
- if rcvr_ty. is_region_ptr( ) && args. is_some( ) {
171
- if rcvr_ty. is_mutable_pointer( ) {
172
- "&mut "
173
- } else {
174
- "&"
175
- }
176
- } else {
177
- ""
178
- } ,
179
- args. map( |arg| arg. iter( )
180
- . map( |arg| print:: to_string( print:: NO_ANN ,
181
- |s| s. print_expr( arg) ) )
182
- . collect:: <Vec <_>>( )
183
- . join( ", " ) ) . unwrap_or_else( || "..." . to_owned( ) ) ) ) ;
195
+ print_disambiguation_help ( err, self . tcx . def_path_str ( trait_did) ) ;
184
196
}
185
197
}
186
198
}
0 commit comments