@@ -96,6 +96,27 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
96
96
97
97
scope_expr_id : HirId ,
98
98
99
+ /// Delegation item can be expanded into method calls or fully qualified calls
100
+ /// depending on the callee's signature. Method calls are used to allow
101
+ /// autoref/autoderef for target expression. For example in:
102
+ ///
103
+ /// ```ignore (illustrative)
104
+ /// trait Trait : Sized {
105
+ /// fn by_value(self) -> i32 { 1 }
106
+ /// fn by_mut_ref(&mut self) -> i32 { 2 }
107
+ /// fn by_ref(&self) -> i32 { 3 }
108
+ /// }
109
+ ///
110
+ /// struct NewType(SomeType);
111
+ /// impl Trait for NewType {
112
+ /// reuse Trait::* { self.0 }
113
+ /// }
114
+ /// ```
115
+ ///
116
+ /// `self.0` will automatically coerce. The difference with existing method lookup
117
+ /// is that methods in delegation items are pre-resolved by callee path (`Trait::*`).
118
+ expected_def_id : Option < DefId > ,
119
+
99
120
/// Is this probe being done for a diagnostic? This will skip some error reporting
100
121
/// machinery, since we don't particularly care about, for example, similarly named
101
122
/// candidates if we're *reporting* similarly named candidates.
@@ -248,6 +269,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
248
269
IsSuggestion ( true ) ,
249
270
self_ty,
250
271
scope_expr_id,
272
+ None ,
251
273
ProbeScope :: AllTraits ,
252
274
|probe_cx| Ok ( probe_cx. candidate_method_names ( candidate_filter) ) ,
253
275
)
@@ -263,6 +285,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
263
285
IsSuggestion ( true ) ,
264
286
self_ty,
265
287
scope_expr_id,
288
+ None ,
266
289
ProbeScope :: AllTraits ,
267
290
|probe_cx| probe_cx. pick ( ) ,
268
291
)
@@ -281,6 +304,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
281
304
is_suggestion : IsSuggestion ,
282
305
self_ty : Ty < ' tcx > ,
283
306
scope_expr_id : HirId ,
307
+ expected_def_id : Option < DefId > ,
284
308
scope : ProbeScope ,
285
309
) -> PickResult < ' tcx > {
286
310
self . probe_op (
@@ -291,6 +315,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
291
315
is_suggestion,
292
316
self_ty,
293
317
scope_expr_id,
318
+ expected_def_id,
294
319
scope,
295
320
|probe_cx| probe_cx. pick ( ) ,
296
321
)
@@ -315,6 +340,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
315
340
is_suggestion,
316
341
self_ty,
317
342
scope_expr_id,
343
+ None ,
318
344
scope,
319
345
|probe_cx| {
320
346
Ok ( probe_cx
@@ -335,6 +361,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
335
361
is_suggestion : IsSuggestion ,
336
362
self_ty : Ty < ' tcx > ,
337
363
scope_expr_id : HirId ,
364
+ expected_def_id : Option < DefId > ,
338
365
scope : ProbeScope ,
339
366
op : OP ,
340
367
) -> Result < R , MethodError < ' tcx > >
@@ -476,6 +503,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
476
503
& orig_values,
477
504
steps. steps ,
478
505
scope_expr_id,
506
+ expected_def_id,
479
507
is_suggestion,
480
508
) ;
481
509
@@ -571,6 +599,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
571
599
orig_steps_var_values : & ' a OriginalQueryValues < ' tcx > ,
572
600
steps : & ' tcx [ CandidateStep < ' tcx > ] ,
573
601
scope_expr_id : HirId ,
602
+ expected_def_id : Option < DefId > ,
574
603
is_suggestion : IsSuggestion ,
575
604
) -> ProbeContext < ' a , ' tcx > {
576
605
ProbeContext {
@@ -590,6 +619,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
590
619
static_candidates : RefCell :: new ( Vec :: new ( ) ) ,
591
620
unsatisfied_predicates : RefCell :: new ( Vec :: new ( ) ) ,
592
621
scope_expr_id,
622
+ expected_def_id,
593
623
is_suggestion,
594
624
}
595
625
}
@@ -1220,6 +1250,9 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1220
1250
) -> Option < PickResult < ' tcx > > {
1221
1251
let mut applicable_candidates: Vec < _ > = candidates
1222
1252
. iter ( )
1253
+ . filter ( |candidate| {
1254
+ !matches ! ( self . expected_def_id, Some ( def_id) if def_id != candidate. item. def_id)
1255
+ } )
1223
1256
. map ( |probe| {
1224
1257
( probe, self . consider_probe ( self_ty, probe, possibly_unsatisfied_predicates) )
1225
1258
} )
@@ -1677,6 +1710,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1677
1710
self . orig_steps_var_values ,
1678
1711
self . steps ,
1679
1712
self . scope_expr_id ,
1713
+ None ,
1680
1714
IsSuggestion ( true ) ,
1681
1715
) ;
1682
1716
pcx. allow_similar_names = true ;
0 commit comments