/
probe.rs
1292 lines (1124 loc) · 49.5 KB
/
probe.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::{MethodError};
use super::MethodIndex;
use super::{CandidateSource,ImplSource,TraitSource};
use super::suggest;
use check;
use check::{FnCtxt, NoPreference, UnresolvedTypeAction};
use middle::fast_reject;
use middle::subst;
use middle::subst::Subst;
use middle::traits;
use middle::ty::{self, RegionEscape, Ty, ToPolyTraitRef};
use middle::ty_fold::TypeFoldable;
use middle::infer;
use middle::infer::InferCtxt;
use syntax::ast;
use syntax::codemap::{Span, DUMMY_SP};
use std::collections::HashSet;
use std::mem;
use std::rc::Rc;
use util::ppaux::Repr;
use self::CandidateKind::*;
pub use self::PickAdjustment::*;
pub use self::PickKind::*;
struct ProbeContext<'a, 'tcx:'a> {
fcx: &'a FnCtxt<'a, 'tcx>,
span: Span,
method_name: ast::Name,
steps: Rc<Vec<CandidateStep<'tcx>>>,
opt_simplified_steps: Option<Vec<fast_reject::SimplifiedType>>,
inherent_candidates: Vec<Candidate<'tcx>>,
extension_candidates: Vec<Candidate<'tcx>>,
impl_dups: HashSet<ast::DefId>,
static_candidates: Vec<CandidateSource>,
}
struct CandidateStep<'tcx> {
self_ty: Ty<'tcx>,
adjustment: PickAdjustment,
}
struct Candidate<'tcx> {
xform_self_ty: Ty<'tcx>,
method_ty: Rc<ty::Method<'tcx>>,
kind: CandidateKind<'tcx>,
}
enum CandidateKind<'tcx> {
InherentImplCandidate(/* Impl */ ast::DefId, subst::Substs<'tcx>),
ObjectCandidate(/* Trait */ ast::DefId, /* method_num */ uint, /* vtable index */ uint),
ExtensionImplCandidate(/* Impl */ ast::DefId, Rc<ty::TraitRef<'tcx>>,
subst::Substs<'tcx>, MethodIndex),
ClosureCandidate(/* Trait */ ast::DefId, MethodIndex),
WhereClauseCandidate(ty::PolyTraitRef<'tcx>, MethodIndex),
ProjectionCandidate(ast::DefId, MethodIndex),
}
pub struct Pick<'tcx> {
pub method_ty: Rc<ty::Method<'tcx>>,
pub adjustment: PickAdjustment,
pub kind: PickKind<'tcx>,
}
#[derive(Clone,Debug)]
pub enum PickKind<'tcx> {
InherentImplPick(/* Impl */ ast::DefId),
ObjectPick(/* Trait */ ast::DefId, /* method_num */ uint, /* real_index */ uint),
ExtensionImplPick(/* Impl */ ast::DefId, MethodIndex),
TraitPick(/* Trait */ ast::DefId, MethodIndex),
WhereClausePick(/* Trait */ ty::PolyTraitRef<'tcx>, MethodIndex),
}
pub type PickResult<'tcx> = Result<Pick<'tcx>, MethodError>;
// This is a kind of "abstracted" version of ty::AutoAdjustment. The
// difference is that it doesn't embed any regions or other
// specifics. The "confirmation" step recreates those details as
// needed.
#[derive(Clone,Debug)]
pub enum PickAdjustment {
// Indicates that the source expression should be autoderef'd N times
//
// A = expr | *expr | **expr
AutoDeref(uint),
// Indicates that the source expression should be autoderef'd N
// times and then "unsized". This should probably eventually go
// away in favor of just coercing method receivers.
//
// A = unsize(expr | *expr | **expr)
AutoUnsizeLength(/* number of autoderefs */ uint, /* length*/ uint),
// Indicates that an autoref is applied after some number of other adjustments
//
// A = &A | &mut A
AutoRef(ast::Mutability, Box<PickAdjustment>),
}
pub fn probe<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span: Span,
method_name: ast::Name,
self_ty: Ty<'tcx>,
call_expr_id: ast::NodeId)
-> PickResult<'tcx>
{
debug!("probe(self_ty={}, method_name={}, call_expr_id={})",
self_ty.repr(fcx.tcx()),
method_name,
call_expr_id);
// FIXME(#18741) -- right now, creating the steps involves evaluating the
// `*` operator, which registers obligations that then escape into
// the global fulfillment context and thus has global
// side-effects. This is a bit of a pain to refactor. So just let
// it ride, although it's really not great, and in fact could I
// think cause spurious errors. Really though this part should
// take place in the `fcx.infcx().probe` below.
let steps = match create_steps(fcx, span, self_ty) {
Some(steps) => steps,
None => return Err(MethodError::NoMatch(Vec::new(), Vec::new())),
};
// Create a list of simplified self types, if we can.
let mut simplified_steps = Vec::new();
for step in &steps {
match fast_reject::simplify_type(fcx.tcx(), step.self_ty, true) {
None => { break; }
Some(simplified_type) => { simplified_steps.push(simplified_type); }
}
}
let opt_simplified_steps =
if simplified_steps.len() < steps.len() {
None // failed to convert at least one of the steps
} else {
Some(simplified_steps)
};
debug!("ProbeContext: steps for self_ty={} are {}",
self_ty.repr(fcx.tcx()),
steps.repr(fcx.tcx()));
// this creates one big transaction so that all type variables etc
// that we create during the probe process are removed later
let mut dummy = Some((steps, opt_simplified_steps)); // FIXME(#18101) need once closures
fcx.infcx().probe(|_| {
let (steps, opt_simplified_steps) = dummy.take().unwrap();
let mut probe_cx = ProbeContext::new(fcx, span, method_name, steps, opt_simplified_steps);
probe_cx.assemble_inherent_candidates();
try!(probe_cx.assemble_extension_candidates_for_traits_in_scope(call_expr_id));
probe_cx.pick()
})
}
fn create_steps<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span: Span,
self_ty: Ty<'tcx>)
-> Option<Vec<CandidateStep<'tcx>>> {
let mut steps = Vec::new();
let (final_ty, dereferences, _) = check::autoderef(fcx,
span,
self_ty,
None,
UnresolvedTypeAction::Error,
NoPreference,
|t, d| {
let adjustment = AutoDeref(d);
steps.push(CandidateStep { self_ty: t, adjustment: adjustment });
None::<()> // keep iterating until we can't anymore
});
match final_ty.sty {
ty::ty_vec(elem_ty, Some(len)) => {
steps.push(CandidateStep {
self_ty: ty::mk_vec(fcx.tcx(), elem_ty, None),
adjustment: AutoUnsizeLength(dereferences, len),
});
}
ty::ty_err => return None,
_ => (),
}
Some(steps)
}
impl<'a,'tcx> ProbeContext<'a,'tcx> {
fn new(fcx: &'a FnCtxt<'a,'tcx>,
span: Span,
method_name: ast::Name,
steps: Vec<CandidateStep<'tcx>>,
opt_simplified_steps: Option<Vec<fast_reject::SimplifiedType>>)
-> ProbeContext<'a,'tcx>
{
ProbeContext {
fcx: fcx,
span: span,
method_name: method_name,
inherent_candidates: Vec::new(),
extension_candidates: Vec::new(),
impl_dups: HashSet::new(),
steps: Rc::new(steps),
opt_simplified_steps: opt_simplified_steps,
static_candidates: Vec::new(),
}
}
fn reset(&mut self) {
self.inherent_candidates.clear();
self.extension_candidates.clear();
self.impl_dups.clear();
self.static_candidates.clear();
}
fn tcx(&self) -> &'a ty::ctxt<'tcx> {
self.fcx.tcx()
}
fn infcx(&self) -> &'a InferCtxt<'a, 'tcx> {
self.fcx.infcx()
}
///////////////////////////////////////////////////////////////////////////
// CANDIDATE ASSEMBLY
fn assemble_inherent_candidates(&mut self) {
let steps = self.steps.clone();
for step in &*steps {
self.assemble_probe(step.self_ty);
}
}
fn assemble_probe(&mut self, self_ty: Ty<'tcx>) {
debug!("assemble_probe: self_ty={}",
self_ty.repr(self.tcx()));
match self_ty.sty {
ty::ty_trait(box ref data) => {
self.assemble_inherent_candidates_from_object(self_ty, data);
self.assemble_inherent_impl_candidates_for_type(data.principal_def_id());
}
ty::ty_enum(did, _) |
ty::ty_struct(did, _) |
ty::ty_closure(did, _, _) => {
self.assemble_inherent_impl_candidates_for_type(did);
}
ty::ty_param(p) => {
self.assemble_inherent_candidates_from_param(self_ty, p);
}
_ => {
}
}
}
fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: ast::DefId) {
// Read the inherent implementation candidates for this type from the
// metadata if necessary.
ty::populate_implementations_for_type_if_necessary(self.tcx(), def_id);
if let Some(impl_infos) = self.tcx().inherent_impls.borrow().get(&def_id) {
for &impl_def_id in &***impl_infos {
self.assemble_inherent_impl_probe(impl_def_id);
}
}
}
fn assemble_inherent_impl_probe(&mut self, impl_def_id: ast::DefId) {
if !self.impl_dups.insert(impl_def_id) {
return; // already visited
}
debug!("assemble_inherent_impl_probe {:?}", impl_def_id);
let method = match impl_method(self.tcx(), impl_def_id, self.method_name) {
Some(m) => m,
None => { return; } // No method with correct name on this impl
};
if !self.has_applicable_self(&*method) {
// No receiver declared. Not a candidate.
return self.record_static_candidate(ImplSource(impl_def_id));
}
let impl_substs = self.impl_substs(impl_def_id);
// Determine the receiver type that the method itself expects.
let xform_self_ty =
self.xform_self_ty(&method, &impl_substs);
self.inherent_candidates.push(Candidate {
xform_self_ty: xform_self_ty,
method_ty: method,
kind: InherentImplCandidate(impl_def_id, impl_substs)
});
}
fn assemble_inherent_candidates_from_object(&mut self,
self_ty: Ty<'tcx>,
data: &ty::TyTrait<'tcx>) {
debug!("assemble_inherent_candidates_from_object(self_ty={})",
self_ty.repr(self.tcx()));
let tcx = self.tcx();
// It is illegal to invoke a method on a trait instance that
// refers to the `Self` type. An error will be reported by
// `enforce_object_limitations()` if the method refers to the
// `Self` type anywhere other than the receiver. Here, we use
// a substitution that replaces `Self` with the object type
// itself. Hence, a `&self` method will wind up with an
// argument type like `&Trait`.
let trait_ref = data.principal_trait_ref_with_self_ty(self.tcx(), self_ty);
self.elaborate_bounds(&[trait_ref.clone()], |this, new_trait_ref, m, method_num| {
let new_trait_ref = this.erase_late_bound_regions(&new_trait_ref);
let vtable_index =
traits::get_vtable_index_of_object_method(tcx,
trait_ref.clone(),
new_trait_ref.def_id,
method_num);
let xform_self_ty = this.xform_self_ty(&m, new_trait_ref.substs);
this.inherent_candidates.push(Candidate {
xform_self_ty: xform_self_ty,
method_ty: m,
kind: ObjectCandidate(new_trait_ref.def_id, method_num, vtable_index)
});
});
}
fn assemble_inherent_candidates_from_param(&mut self,
_rcvr_ty: Ty<'tcx>,
param_ty: ty::ParamTy) {
// FIXME -- Do we want to commit to this behavior for param bounds?
let bounds: Vec<_> =
self.fcx.inh.param_env.caller_bounds
.iter()
.filter_map(|predicate| {
match *predicate {
ty::Predicate::Trait(ref trait_predicate) => {
match trait_predicate.0.trait_ref.self_ty().sty {
ty::ty_param(ref p) if *p == param_ty => {
Some(trait_predicate.to_poly_trait_ref())
}
_ => None
}
}
ty::Predicate::Equate(..) |
ty::Predicate::Projection(..) |
ty::Predicate::RegionOutlives(..) |
ty::Predicate::TypeOutlives(..) => {
None
}
}
})
.collect();
self.elaborate_bounds(&bounds, |this, poly_trait_ref, m, method_num| {
let trait_ref =
this.erase_late_bound_regions(&poly_trait_ref);
let xform_self_ty =
this.xform_self_ty(&m, trait_ref.substs);
debug!("found match: trait_ref={} substs={} m={}",
trait_ref.repr(this.tcx()),
trait_ref.substs.repr(this.tcx()),
m.repr(this.tcx()));
assert_eq!(m.generics.types.get_slice(subst::TypeSpace).len(),
trait_ref.substs.types.get_slice(subst::TypeSpace).len());
assert_eq!(m.generics.regions.get_slice(subst::TypeSpace).len(),
trait_ref.substs.regions().get_slice(subst::TypeSpace).len());
assert_eq!(m.generics.types.get_slice(subst::SelfSpace).len(),
trait_ref.substs.types.get_slice(subst::SelfSpace).len());
assert_eq!(m.generics.regions.get_slice(subst::SelfSpace).len(),
trait_ref.substs.regions().get_slice(subst::SelfSpace).len());
// Because this trait derives from a where-clause, it
// should not contain any inference variables or other
// artifacts. This means it is safe to put into the
// `WhereClauseCandidate` and (eventually) into the
// `WhereClausePick`.
assert!(trait_ref.substs.types.iter().all(|&t| !ty::type_needs_infer(t)));
this.inherent_candidates.push(Candidate {
xform_self_ty: xform_self_ty,
method_ty: m,
kind: WhereClauseCandidate(poly_trait_ref, method_num)
});
});
}
// Do a search through a list of bounds, using a callback to actually
// create the candidates.
fn elaborate_bounds<F>(
&mut self,
bounds: &[ty::PolyTraitRef<'tcx>],
mut mk_cand: F,
) where
F: for<'b> FnMut(
&mut ProbeContext<'b, 'tcx>,
ty::PolyTraitRef<'tcx>,
Rc<ty::Method<'tcx>>,
uint,
),
{
debug!("elaborate_bounds(bounds={})", bounds.repr(self.tcx()));
let tcx = self.tcx();
let mut cache = HashSet::new();
for bound_trait_ref in traits::transitive_bounds(tcx, bounds) {
// Already visited this trait, skip it.
if !cache.insert(bound_trait_ref.def_id()) {
continue;
}
let (pos, method) = match trait_method(tcx,
bound_trait_ref.def_id(),
self.method_name) {
Some(v) => v,
None => { continue; }
};
if !self.has_applicable_self(&*method) {
self.record_static_candidate(TraitSource(bound_trait_ref.def_id()));
} else {
mk_cand(self, bound_trait_ref, method, pos);
}
}
}
fn assemble_extension_candidates_for_traits_in_scope(&mut self,
expr_id: ast::NodeId)
-> Result<(),MethodError>
{
let mut duplicates = HashSet::new();
let opt_applicable_traits = self.fcx.ccx.trait_map.get(&expr_id);
if let Some(applicable_traits) = opt_applicable_traits {
for &trait_did in applicable_traits {
if duplicates.insert(trait_did) {
try!(self.assemble_extension_candidates_for_trait(trait_did));
}
}
}
Ok(())
}
fn assemble_extension_candidates_for_all_traits(&mut self) -> Result<(),MethodError> {
let mut duplicates = HashSet::new();
for trait_info in suggest::all_traits(self.fcx.ccx) {
if duplicates.insert(trait_info.def_id) {
try!(self.assemble_extension_candidates_for_trait(trait_info.def_id));
}
}
Ok(())
}
fn assemble_extension_candidates_for_trait(&mut self,
trait_def_id: ast::DefId)
-> Result<(),MethodError>
{
debug!("assemble_extension_candidates_for_trait(trait_def_id={})",
trait_def_id.repr(self.tcx()));
// Check whether `trait_def_id` defines a method with suitable name:
let trait_items =
ty::trait_items(self.tcx(), trait_def_id);
let matching_index =
trait_items.iter()
.position(|item| item.name() == self.method_name);
let matching_index = match matching_index {
Some(i) => i,
None => { return Ok(()); }
};
let method = match (&*trait_items)[matching_index].as_opt_method() {
Some(m) => m,
None => { return Ok(()); }
};
// Check whether `trait_def_id` defines a method with suitable name:
if !self.has_applicable_self(&*method) {
debug!("method has inapplicable self");
self.record_static_candidate(TraitSource(trait_def_id));
return Ok(());
}
self.assemble_extension_candidates_for_trait_impls(trait_def_id,
method.clone(),
matching_index);
try!(self.assemble_closure_candidates(trait_def_id,
method.clone(),
matching_index));
self.assemble_projection_candidates(trait_def_id,
method.clone(),
matching_index);
self.assemble_where_clause_candidates(trait_def_id,
method,
matching_index);
Ok(())
}
fn assemble_extension_candidates_for_trait_impls(&mut self,
trait_def_id: ast::DefId,
method: Rc<ty::Method<'tcx>>,
method_index: uint)
{
ty::populate_implementations_for_trait_if_necessary(self.tcx(),
trait_def_id);
let trait_impls = self.tcx().trait_impls.borrow();
let impl_def_ids = trait_impls.get(&trait_def_id);
let impl_def_ids = match impl_def_ids {
None => { return; }
Some(impls) => impls,
};
for &impl_def_id in &*impl_def_ids.borrow() {
debug!("assemble_extension_candidates_for_trait_impl: trait_def_id={} impl_def_id={}",
trait_def_id.repr(self.tcx()),
impl_def_id.repr(self.tcx()));
if !self.impl_can_possibly_match(impl_def_id) {
continue;
}
let impl_substs = self.impl_substs(impl_def_id);
debug!("impl_substs={}", impl_substs.repr(self.tcx()));
let impl_trait_ref =
ty::impl_trait_ref(self.tcx(), impl_def_id)
.unwrap() // we know this is a trait impl
.subst(self.tcx(), &impl_substs);
debug!("impl_trait_ref={}", impl_trait_ref.repr(self.tcx()));
// Determine the receiver type that the method itself expects.
let xform_self_ty =
self.xform_self_ty(&method, impl_trait_ref.substs);
debug!("xform_self_ty={}", xform_self_ty.repr(self.tcx()));
self.extension_candidates.push(Candidate {
xform_self_ty: xform_self_ty,
method_ty: method.clone(),
kind: ExtensionImplCandidate(impl_def_id, impl_trait_ref, impl_substs, method_index)
});
}
}
fn impl_can_possibly_match(&self, impl_def_id: ast::DefId) -> bool {
let simplified_steps = match self.opt_simplified_steps {
Some(ref simplified_steps) => simplified_steps,
None => { return true; }
};
let impl_type = ty::lookup_item_type(self.tcx(), impl_def_id);
let impl_simplified_type =
match fast_reject::simplify_type(self.tcx(), impl_type.ty, false) {
Some(simplified_type) => simplified_type,
None => { return true; }
};
simplified_steps.contains(&impl_simplified_type)
}
fn assemble_closure_candidates(&mut self,
trait_def_id: ast::DefId,
method_ty: Rc<ty::Method<'tcx>>,
method_index: uint)
-> Result<(),MethodError>
{
// Check if this is one of the Fn,FnMut,FnOnce traits.
let tcx = self.tcx();
let kind = if Some(trait_def_id) == tcx.lang_items.fn_trait() {
ty::FnClosureKind
} else if Some(trait_def_id) == tcx.lang_items.fn_mut_trait() {
ty::FnMutClosureKind
} else if Some(trait_def_id) == tcx.lang_items.fn_once_trait() {
ty::FnOnceClosureKind
} else {
return Ok(());
};
// Check if there is an unboxed-closure self-type in the list of receivers.
// If so, add "synthetic impls".
let steps = self.steps.clone();
for step in &*steps {
let (closure_def_id, _, _) = match step.self_ty.sty {
ty::ty_closure(a, b, ref c) => (a, b, c),
_ => continue,
};
let closure_kinds = self.fcx.inh.closure_kinds.borrow();
let closure_kind = match closure_kinds.get(&closure_def_id) {
Some(&k) => k,
None => {
return Err(MethodError::ClosureAmbiguity(trait_def_id));
}
};
// this closure doesn't implement the right kind of `Fn` trait
if closure_kind != kind {
continue;
}
// create some substitutions for the argument/return type;
// for the purposes of our method lookup, we only take
// receiver type into account, so we can just substitute
// fresh types here to use during substitution and subtyping.
let trait_def = ty::lookup_trait_def(self.tcx(), trait_def_id);
let substs = self.infcx().fresh_substs_for_trait(self.span,
&trait_def.generics,
step.self_ty);
let xform_self_ty = self.xform_self_ty(&method_ty, &substs);
self.inherent_candidates.push(Candidate {
xform_self_ty: xform_self_ty,
method_ty: method_ty.clone(),
kind: ClosureCandidate(trait_def_id, method_index)
});
}
Ok(())
}
fn assemble_projection_candidates(&mut self,
trait_def_id: ast::DefId,
method: Rc<ty::Method<'tcx>>,
method_index: uint)
{
debug!("assemble_projection_candidates(\
trait_def_id={}, \
method={}, \
method_index={})",
trait_def_id.repr(self.tcx()),
method.repr(self.tcx()),
method_index);
for step in &*self.steps {
debug!("assemble_projection_candidates: step={}",
step.repr(self.tcx()));
let projection_trait_ref = match step.self_ty.sty {
ty::ty_projection(ref data) => &data.trait_ref,
_ => continue,
};
debug!("assemble_projection_candidates: projection_trait_ref={}",
projection_trait_ref.repr(self.tcx()));
let trait_predicates = ty::lookup_predicates(self.tcx(),
projection_trait_ref.def_id);
let bounds = trait_predicates.instantiate(self.tcx(), projection_trait_ref.substs);
let predicates = bounds.predicates.into_vec();
debug!("assemble_projection_candidates: predicates={}",
predicates.repr(self.tcx()));
for poly_bound in
traits::elaborate_predicates(self.tcx(), predicates)
.filter_map(|p| p.to_opt_poly_trait_ref())
.filter(|b| b.def_id() == trait_def_id)
{
let bound = self.erase_late_bound_regions(&poly_bound);
debug!("assemble_projection_candidates: projection_trait_ref={} bound={}",
projection_trait_ref.repr(self.tcx()),
bound.repr(self.tcx()));
if self.infcx().can_equate(&step.self_ty, &bound.self_ty()).is_ok() {
let xform_self_ty = self.xform_self_ty(&method, bound.substs);
debug!("assemble_projection_candidates: bound={} xform_self_ty={}",
bound.repr(self.tcx()),
xform_self_ty.repr(self.tcx()));
self.extension_candidates.push(Candidate {
xform_self_ty: xform_self_ty,
method_ty: method.clone(),
kind: ProjectionCandidate(trait_def_id, method_index)
});
}
}
}
}
fn assemble_where_clause_candidates(&mut self,
trait_def_id: ast::DefId,
method_ty: Rc<ty::Method<'tcx>>,
method_index: uint)
{
debug!("assemble_where_clause_candidates(trait_def_id={})",
trait_def_id.repr(self.tcx()));
let caller_predicates = self.fcx.inh.param_env.caller_bounds.clone();
for poly_bound in traits::elaborate_predicates(self.tcx(), caller_predicates)
.filter_map(|p| p.to_opt_poly_trait_ref())
.filter(|b| b.def_id() == trait_def_id)
{
let bound = self.erase_late_bound_regions(&poly_bound);
let xform_self_ty = self.xform_self_ty(&method_ty, bound.substs);
debug!("assemble_where_clause_candidates: bound={} xform_self_ty={}",
bound.repr(self.tcx()),
xform_self_ty.repr(self.tcx()));
self.extension_candidates.push(Candidate {
xform_self_ty: xform_self_ty,
method_ty: method_ty.clone(),
kind: WhereClauseCandidate(poly_bound, method_index)
});
}
}
///////////////////////////////////////////////////////////////////////////
// THE ACTUAL SEARCH
fn pick(mut self) -> PickResult<'tcx> {
match self.pick_core() {
Some(r) => return r,
None => {}
}
let static_candidates = mem::replace(&mut self.static_candidates, vec![]);
// things failed, so lets look at all traits, for diagnostic purposes now:
self.reset();
let span = self.span;
let tcx = self.tcx();
try!(self.assemble_extension_candidates_for_all_traits());
let out_of_scope_traits = match self.pick_core() {
Some(Ok(p)) => vec![p.method_ty.container.id()],
Some(Err(MethodError::Ambiguity(v))) => v.into_iter().map(|source| {
match source {
TraitSource(id) => id,
ImplSource(impl_id) => {
match ty::trait_id_of_impl(tcx, impl_id) {
Some(id) => id,
None =>
tcx.sess.span_bug(span,
"found inherent method when looking at traits")
}
}
}
}).collect(),
Some(Err(MethodError::NoMatch(_, others))) => {
assert!(others.is_empty());
vec![]
}
Some(Err(MethodError::ClosureAmbiguity(..))) => {
// this error only occurs when assembling candidates
tcx.sess.span_bug(span, "encountered ClosureAmbiguity from pick_core");
}
None => vec![],
};
Err(MethodError::NoMatch(static_candidates, out_of_scope_traits))
}
fn pick_core(&mut self) -> Option<PickResult<'tcx>> {
let steps = self.steps.clone();
// find the first step that works
steps.iter().filter_map(|step| self.pick_step(step)).next()
}
fn pick_step(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
debug!("pick_step: step={}", step.repr(self.tcx()));
if ty::type_is_error(step.self_ty) {
return None;
}
match self.pick_by_value_method(step) {
Some(result) => return Some(result),
None => {}
}
self.pick_autorefd_method(step)
}
fn pick_by_value_method(&mut self,
step: &CandidateStep<'tcx>)
-> Option<PickResult<'tcx>>
{
/*!
* For each type `T` in the step list, this attempts to find a
* method where the (transformed) self type is exactly `T`. We
* do however do one transformation on the adjustment: if we
* are passing a region pointer in, we will potentially
* *reborrow* it to a shorter lifetime. This allows us to
* transparently pass `&mut` pointers, in particular, without
* consuming them for their entire lifetime.
*/
let adjustment = match step.adjustment {
AutoDeref(d) => consider_reborrow(step.self_ty, d),
AutoUnsizeLength(..) | AutoRef(..) => step.adjustment.clone(),
};
return self.pick_method(step.self_ty).map(|r| self.adjust(r, adjustment.clone()));
fn consider_reborrow(ty: Ty, d: uint) -> PickAdjustment {
// Insert a `&*` or `&mut *` if this is a reference type:
match ty.sty {
ty::ty_rptr(_, ref mt) => AutoRef(mt.mutbl, box AutoDeref(d+1)),
_ => AutoDeref(d),
}
}
}
fn pick_autorefd_method(&mut self,
step: &CandidateStep<'tcx>)
-> Option<PickResult<'tcx>>
{
let tcx = self.tcx();
self.search_mutabilities(
|m| AutoRef(m, box step.adjustment.clone()),
|m,r| ty::mk_rptr(tcx, tcx.mk_region(r), ty::mt {ty:step.self_ty, mutbl:m}))
}
fn search_mutabilities<F, G>(&mut self,
mut mk_adjustment: F,
mut mk_autoref_ty: G)
-> Option<PickResult<'tcx>> where
F: FnMut(ast::Mutability) -> PickAdjustment,
G: FnMut(ast::Mutability, ty::Region) -> Ty<'tcx>,
{
// In general, during probing we erase regions. See
// `impl_self_ty()` for an explanation.
let region = ty::ReStatic;
// Search through mutabilities in order to find one where pick works:
[ast::MutImmutable, ast::MutMutable]
.iter()
.flat_map(|&m| {
let autoref_ty = mk_autoref_ty(m, region);
self.pick_method(autoref_ty)
.map(|r| self.adjust(r, mk_adjustment(m)))
.into_iter()
})
.nth(0)
}
fn adjust(&mut self,
result: PickResult<'tcx>,
adjustment: PickAdjustment)
-> PickResult<'tcx> {
match result {
Err(e) => Err(e),
Ok(mut pick) => {
pick.adjustment = adjustment;
Ok(pick)
}
}
}
fn pick_method(&mut self, self_ty: Ty<'tcx>) -> Option<PickResult<'tcx>> {
debug!("pick_method(self_ty={})", self.infcx().ty_to_string(self_ty));
debug!("searching inherent candidates");
match self.consider_candidates(self_ty, &self.inherent_candidates[]) {
None => {}
Some(pick) => {
return Some(pick);
}
}
debug!("searching extension candidates");
self.consider_candidates(self_ty, &self.extension_candidates[])
}
fn consider_candidates(&self,
self_ty: Ty<'tcx>,
probes: &[Candidate<'tcx>])
-> Option<PickResult<'tcx>> {
let mut applicable_candidates: Vec<_> =
probes.iter()
.filter(|&probe| self.consider_probe(self_ty, probe))
.collect();
debug!("applicable_candidates: {}", applicable_candidates.repr(self.tcx()));
if applicable_candidates.len() > 1 {
match self.collapse_candidates_to_trait_pick(&applicable_candidates[]) {
Some(pick) => { return Some(Ok(pick)); }
None => { }
}
}
if applicable_candidates.len() > 1 {
let sources = probes.iter().map(|p| p.to_source()).collect();
return Some(Err(MethodError::Ambiguity(sources)));
}
applicable_candidates.pop().map(|probe| {
let pick = probe.to_unadjusted_pick();
Ok(pick)
})
}
fn consider_probe(&self, self_ty: Ty<'tcx>, probe: &Candidate<'tcx>) -> bool {
debug!("consider_probe: self_ty={} probe={}",
self_ty.repr(self.tcx()),
probe.repr(self.tcx()));
self.infcx().probe(|_| {
// First check that the self type can be related.
match self.make_sub_ty(self_ty, probe.xform_self_ty) {
Ok(()) => { }
Err(_) => {
debug!("--> cannot relate self-types");
return false;
}
}
// If so, impls may carry other conditions (e.g., where
// clauses) that must be considered. Make sure that those
// match as well (or at least may match, sometimes we
// don't have enough information to fully evaluate).
match probe.kind {
InherentImplCandidate(impl_def_id, ref substs) |
ExtensionImplCandidate(impl_def_id, _, ref substs, _) => {
let selcx = &mut traits::SelectionContext::new(self.infcx(), self.fcx);
let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id);
// Check whether the impl imposes obligations we have to worry about.
let impl_bounds = ty::lookup_predicates(self.tcx(), impl_def_id);
let impl_bounds = impl_bounds.instantiate(self.tcx(), substs);
let traits::Normalized { value: impl_bounds,
obligations: norm_obligations } =
traits::normalize(selcx, cause.clone(), &impl_bounds);
// Convert the bounds into obligations.
let obligations =
traits::predicates_for_generics(self.tcx(),
cause.clone(),
&impl_bounds);
debug!("impl_obligations={}", obligations.repr(self.tcx()));
// Evaluate those obligations to see if they might possibly hold.
obligations.all(|o| selcx.evaluate_obligation(o)) &&
norm_obligations.iter().all(|o| selcx.evaluate_obligation(o))
}
ProjectionCandidate(..) |
ObjectCandidate(..) |
ClosureCandidate(..) |
WhereClauseCandidate(..) => {
// These have no additional conditions to check.
true
}
}
})
}
/// Sometimes we get in a situation where we have multiple probes that are all impls of the
/// same trait, but we don't know which impl to use. In this case, since in all cases the
/// external interface of the method can be determined from the trait, it's ok not to decide.
/// We can basically just collapse all of the probes for various impls into one where-clause
/// probe. This will result in a pending obligation so when more type-info is available we can
/// make the final decision.
///
/// Example (`src/test/run-pass/method-two-trait-defer-resolution-1.rs`):
///
/// ```
/// trait Foo { ... }
/// impl Foo for Vec<int> { ... }
/// impl Foo for Vec<uint> { ... }
/// ```
///
/// Now imagine the receiver is `Vec<_>`. It doesn't really matter at this time which impl we
/// use, so it's ok to just commit to "using the method from the trait Foo".
fn collapse_candidates_to_trait_pick(&self,
probes: &[&Candidate<'tcx>])
-> Option<Pick<'tcx>> {
// Do all probes correspond to the same trait?
let trait_data = match probes[0].to_trait_data() {
Some(data) => data,
None => return None,
};