summary refs log tree commit diff
path: root/src/librustc/middle/traits/select.rs
blob: b86fabccf93de222f7ab68c0bfb97283fccea77d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
// 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.

/*! See `doc.rs` for high-level documentation */

use super::{Obligation, ObligationCause};
use super::{EvaluationResult, EvaluatedToMatch,
            EvaluatedToAmbiguity, EvaluatedToUnmatch};
use super::{SelectionError, Unimplemented, Overflow,
            OutputTypeParameterMismatch};
use super::{Selection};
use super::{SelectionResult};
use super::{VtableBuiltin, VtableImpl, VtableParam, VtableUnboxedClosure};
use super::{VtableImplData, VtableParamData};
use super::{util};

use middle::mem_categorization::Typer;
use middle::subst::{Subst, Substs, VecPerParamSpace};
use middle::ty;
use middle::ty_fold::TypeFoldable;
use middle::typeck::check::regionmanip;
use middle::typeck::infer;
use middle::typeck::infer::{InferCtxt, TypeSkolemizer};
use std::cell::RefCell;
use std::collections::hashmap::HashMap;
use std::rc::Rc;
use syntax::ast;
use util::ppaux::Repr;

pub struct SelectionContext<'cx, 'tcx:'cx> {
    infcx: &'cx InferCtxt<'cx, 'tcx>,
    param_env: &'cx ty::ParameterEnvironment,
    typer: &'cx Typer<'tcx>+'cx,
    skolemizer: TypeSkolemizer<'cx, 'tcx>,
}

// A stack that walks back up the stack frame.
struct ObligationStack<'prev> {
    obligation: &'prev Obligation,
    skol_obligation_self_ty: ty::t,
    previous: Option<&'prev ObligationStack<'prev>>
}

pub struct SelectionCache {
    hashmap: RefCell<HashMap<CacheKey, SelectionResult<Candidate>>>,
}

#[deriving(Hash,Eq,PartialEq)]
struct CacheKey {
    trait_def_id: ast::DefId,
    skol_obligation_self_ty: ty::t,
}

#[deriving(PartialEq,Eq)]
enum MatchResult<T> {
    Matched(T),
    AmbiguousMatch,
    NoMatch
}

/**
 * The selection process begins by considering all impls, where
 * clauses, and so forth that might resolve an obligation.  Sometimes
 * we'll be able to say definitively that (e.g.) an impl does not
 * apply to the obligation: perhaps it is defined for `uint` but the
 * obligation is for `int`. In that case, we drop the impl out of the
 * list.  But the other cases are considered *candidates*.
 *
 * Candidates can either be definitive or ambiguous. An ambiguous
 * candidate is one that might match or might not, depending on how
 * type variables wind up being resolved. This only occurs during inference.
 *
 * For selection to suceed, there must be exactly one non-ambiguous
 * candidate.  Usually, it is not possible to have more than one
 * definitive candidate, due to the coherence rules. However, there is
 * one case where it could occur: if there is a blanket impl for a
 * trait (that is, an impl applied to all T), and a type parameter
 * with a where clause. In that case, we can have a candidate from the
 * where clause and a second candidate from the impl. This is not a
 * problem because coherence guarantees us that the impl which would
 * be used to satisfy the where clause is the same one that we see
 * now. To resolve this issue, therefore, we ignore impls if we find a
 * matching where clause. Part of the reason for this is that where
 * clauses can give additional information (like, the types of output
 * parameters) that would have to be inferred from the impl.
 */
#[deriving(Clone)]
enum Candidate {
    MatchedBuiltinCandidate,
    AmbiguousBuiltinCandidate,
    MatchedParamCandidate(VtableParamData),
    AmbiguousParamCandidate,
    Impl(ImplCandidate),
    MatchedUnboxedClosureCandidate(/* closure */ ast::DefId),
    ErrorCandidate,
}

#[deriving(Clone)]
enum ImplCandidate {
    MatchedImplCandidate(ast::DefId),
    AmbiguousImplCandidate(ast::DefId),
}

impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
    pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>,
               param_env: &'cx ty::ParameterEnvironment,
               typer: &'cx Typer<'tcx>)
               -> SelectionContext<'cx, 'tcx> {
        SelectionContext {
            infcx: infcx,
            param_env: param_env,
            typer: typer,
            skolemizer: infcx.skolemizer(),
        }
    }

    pub fn tcx(&self) -> &'cx ty::ctxt<'tcx> {
        self.infcx.tcx
    }

    ///////////////////////////////////////////////////////////////////////////
    // Selection
    //
    // The selection phase tries to identify *how* an obligation will
    // be resolved. For example, it will identify which impl or
    // parameter bound is to be used. The process can be inconclusive
    // if the self type in the obligation is not fully inferred. Selection
    // can result in an error in one of two ways:
    //
    // 1. If no applicable impl or parameter bound can be found.
    // 2. If the output type parameters in the obligation do not match
    //    those specified by the impl/bound. For example, if the obligation
    //    is `Vec<Foo>:Iterable<Bar>`, but the impl specifies
    //    `impl<T> Iterable<T> for Vec<T>`, than an error would result.

    pub fn select(&mut self, obligation: &Obligation) -> SelectionResult<Selection> {
        /*!
         * Evaluates whether the obligation can be satisfied. Returns
         * an indication of whether the obligation can be satisfied
         * and, if so, by what means. Never affects surrounding typing
         * environment.
         */

        debug!("select({})", obligation.repr(self.tcx()));

        let stack = self.new_stack(obligation);
        match try!(self.candidate_from_obligation(&stack)) {
            None => Ok(None),
            Some(candidate) => self.confirm_candidate(obligation, candidate),
        }
    }

    pub fn select_inherent_impl(&mut self,
                                impl_def_id: ast::DefId,
                                obligation_cause: ObligationCause,
                                obligation_self_ty: ty::t)
                                -> SelectionResult<VtableImplData<Obligation>>
    {
        debug!("select_inherent_impl(impl_def_id={}, obligation_self_ty={})",
               impl_def_id.repr(self.tcx()),
               obligation_self_ty.repr(self.tcx()));

        match self.candidate_from_impl(impl_def_id,
                                       obligation_cause,
                                       obligation_self_ty) {
            Some(MatchedImplCandidate(impl_def_id)) => {
                let vtable_impl =
                    try!(self.confirm_inherent_impl_candidate(
                        impl_def_id,
                        obligation_cause,
                        obligation_self_ty,
                        0));
                Ok(Some(vtable_impl))
            }
            Some(AmbiguousImplCandidate(_)) => {
                Ok(None)
            }
            None => {
                Err(Unimplemented)
            }
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // EVALUATION
    //
    // Tests whether an obligation can be selected or whether an impl can be
    // applied to particular types. It skips the "confirmation" step and
    // hence completely ignores output type parameters.

    pub fn evaluate_obligation(&mut self,
                               obligation: &Obligation)
                               -> EvaluationResult
    {
        /*!
         * Evaluates whether the obligation `obligation` can be
         * satisfied (by any means).
         */

        debug!("evaluate_obligation({})",
               obligation.repr(self.tcx()));

        let stack = self.new_stack(obligation);
        match self.candidate_from_obligation(&stack) {
            Ok(Some(c)) => c.to_evaluation_result(),
            Ok(None) => EvaluatedToAmbiguity,
            Err(_) => EvaluatedToUnmatch,
        }
    }

    fn evaluate_builtin_bound_recursively(&mut self,
                                          bound: ty::BuiltinBound,
                                          previous_stack: &ObligationStack,
                                          ty: ty::t)
                                          -> EvaluationResult
    {
        let obligation =
            util::obligation_for_builtin_bound(
                self.tcx(),
                previous_stack.obligation.cause,
                bound,
                previous_stack.obligation.recursion_depth + 1,
                ty);
        let obligation = match obligation {
            Ok(ob) => ob,
            _ => return EvaluatedToMatch
        };

        self.evaluate_obligation_recursively(previous_stack, &obligation)
    }

    fn evaluate_obligation_recursively(&mut self,
                                       previous_stack: &ObligationStack,
                                       obligation: &Obligation)
                                       -> EvaluationResult
    {
        debug!("evaluate_obligation_recursively({})",
               obligation.repr(self.tcx()));

        // If there is any previous entry on the stack that precisely
        // matches this obligation, then we can assume that the
        // obligation is satisfied for now (still all other conditions
        // must be met of course). One obvious case this comes up is
        // marker traits like `Send`. Think of a a linked list:
        //
        //    struct List<T> { data: T, next: Option<Box<List<T>>> {
        //
        // `Box<List<T>>` will be `Send` if `T` is `Send` and
        // `Option<Box<List<T>>>` is `Send`, and in turn
        // `Option<Box<List<T>>>` is `Send` if `Box<List<T>>` is
        // `Send`.
        if
            previous_stack.iter()
            .filter(|e| e.obligation.trait_ref.def_id == obligation.trait_ref.def_id)
            .find(|e| self.match_self_types(obligation.cause,
                                            e.skol_obligation_self_ty,
                                            obligation.self_ty()) == Matched(()))
            .is_some()
        {
            return EvaluatedToMatch;
        }

        let stack = self.push_stack(previous_stack, obligation);
        match self.candidate_from_obligation(&stack) {
            Ok(Some(c)) => c.to_evaluation_result(),
            Ok(None) => EvaluatedToAmbiguity,
            Err(_) => EvaluatedToUnmatch,
        }
    }

    pub fn evaluate_impl(&mut self,
                         impl_def_id: ast::DefId,
                         obligation_cause: ObligationCause,
                         obligation_self_ty: ty::t)
                         -> EvaluationResult
    {
        /*!
         * Evaluates whether the impl with id `impl_def_id` could be
         * applied to the self type `obligation_self_ty`. This can be
         * used either for trait or inherent impls.
         */

        debug!("evaluate_impl(impl_def_id={}, obligation_self_ty={})",
               impl_def_id.repr(self.tcx()),
               obligation_self_ty.repr(self.tcx()));

        match self.candidate_from_impl(impl_def_id,
                                       obligation_cause,
                                       obligation_self_ty) {
            Some(c) => c.to_evaluation_result(),
            None => EvaluatedToUnmatch,
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // CANDIDATE ASSEMBLY
    //
    // The selection process begins by examining all in-scope impls,
    // caller obligations, and so forth and assembling a list of
    // candidates. See `doc.rs` and the `Candidate` type for more details.

    fn candidate_from_obligation(&mut self,
                                 stack: &ObligationStack)
                                 -> SelectionResult<Candidate>
    {
        debug!("candidate_from_obligation({})",
               stack.repr(self.tcx()));

        // First, check the cache.
        match self.check_candidate_cache(stack.obligation, stack.skol_obligation_self_ty) {
            Some(c) => {
                debug!("check_candidate_cache(obligation={}, skol_obligation_self_ty={}, \
                       candidate={})",
                       stack.obligation.trait_ref.def_id,
                       stack.skol_obligation_self_ty.repr(self.tcx()),
                       c.repr(self.tcx()));
                return c;
            }
            None => { }
        }

        // If no match, compute result and insert into cache.
        let result = self.pick_candidate(stack);
        self.insert_candidate_cache(stack.obligation,
                                    stack.skol_obligation_self_ty,
                                    result.clone());
        result
    }

    fn pick_candidate(&mut self,
                      stack: &ObligationStack)
                      -> SelectionResult<Candidate>
    {
        if ty::type_is_error(stack.skol_obligation_self_ty) {
            return Ok(Some(ErrorCandidate));
        }

        let mut candidates = try!(self.assemble_candidates(stack));

        debug!("assembled {} candidates for {}",
               candidates.len(), stack.repr(self.tcx()));

        // Examine candidates to determine outcome. Ideally we will
        // have exactly one candidate that is definitively applicable.

        if candidates.len() == 0 {
            // Annoying edge case: if there are no impls, then there
            // is no way that this trait reference is implemented,
            // *unless* it contains unbound variables. In that case,
            // it is possible that one of those unbound variables will
            // be bound to a new type from some other crate which will
            // also contain impls.
            return if !self.contains_skolemized_types(stack.skol_obligation_self_ty) {
                debug!("0 matches, unimpl");
                Err(Unimplemented)
            } else {
                debug!("0 matches, ambig");
                Ok(None)
            }
        } else if candidates.len() > 1 {
            // Ambiguity. Possibly we should report back more
            // information on the potential candidates so we can give
            // a better error message.
            debug!("multiple matches, ambig");
            Ok(None)
        } else {
            let candidate = candidates.pop().unwrap();
            Ok(Some(candidate))
        }
    }

    fn pick_candidate_cache(&self,
                            _obligation: &Obligation,
                            skol_obligation_self_ty: ty::t)
                            -> &SelectionCache
    {
        if
            ty::type_has_self(skol_obligation_self_ty) ||
            ty::type_has_params(skol_obligation_self_ty)
        {
            &self.param_env.selection_cache
        } else {
            &self.tcx().selection_cache
        }
    }

    fn check_candidate_cache(&mut self,
                             obligation: &Obligation,
                             skol_obligation_self_ty: ty::t)
                             -> Option<SelectionResult<Candidate>>
    {
        let cache = self.pick_candidate_cache(obligation, skol_obligation_self_ty);
        let cache_key = CacheKey::new(obligation.trait_ref.def_id,
                                      skol_obligation_self_ty);
        let hashmap = cache.hashmap.borrow();
        hashmap.find(&cache_key).map(|c| (*c).clone())
    }

    fn insert_candidate_cache(&mut self,
                              obligation: &Obligation,
                              skol_obligation_self_ty: ty::t,
                              candidate: SelectionResult<Candidate>)
    {
        debug!("insert_candidate_cache(obligation={}, skol_obligation_self_ty={}, candidate={})",
               obligation.trait_ref.def_id,
               skol_obligation_self_ty.repr(self.tcx()),
               candidate.repr(self.tcx()));

        let cache = self.pick_candidate_cache(obligation, skol_obligation_self_ty);
        let cache_key = CacheKey::new(obligation.trait_ref.def_id,
                                      skol_obligation_self_ty);
        let mut hashmap = cache.hashmap.borrow_mut();
        hashmap.insert(cache_key, candidate);
    }

    fn assemble_candidates(&mut self,
                           stack: &ObligationStack)
                           -> Result<Vec<Candidate>, SelectionError>
    {
        // Check for overflow.

        let ObligationStack { obligation, skol_obligation_self_ty, .. } = *stack;

        let recursion_limit = self.infcx.tcx.sess.recursion_limit.get();
        if obligation.recursion_depth >= recursion_limit {
            debug!("{} --> overflow", stack.obligation.repr(self.tcx()));
            return Err(Overflow);
        }

        let mut candidates = Vec::new();

        // Other bounds. Consider both in-scope bounds from fn decl
        // and applicable impls. There is a certain set of precedence rules here.

        // Where clauses have highest precedence.
        try!(self.assemble_candidates_from_caller_bounds(
            obligation,
            skol_obligation_self_ty,
            &mut candidates));

        // In the special case of builtin bounds, consider the "compiler-supplied" impls.
        if candidates.len() == 0 {
            match self.tcx().lang_items.to_builtin_kind(obligation.trait_ref.def_id) {
                Some(bound) => {
                    try!(self.assemble_builtin_bound_candidates(bound, stack, &mut candidates));
                }

                None => { }
            }
        }

        // In the special case of fn traits and synthesized unboxed
        // closure types, consider the compiler-supplied impls. Note
        // that this is exclusive with the builtin bound case above.
        if candidates.len() == 0 {
            try!(self.assemble_unboxed_candidates(
                obligation,
                skol_obligation_self_ty,
                &mut candidates));
        }

        // Finally, consider the actual impls found in the program.
        if candidates.len() == 0 {
            try!(self.assemble_candidates_from_impls(
                obligation,
                skol_obligation_self_ty,
                &mut candidates));
        }

        Ok(candidates)
    }

    fn assemble_candidates_from_caller_bounds(&mut self,
                                              obligation: &Obligation,
                                              skol_obligation_self_ty: ty::t,
                                              candidates: &mut Vec<Candidate>)
                                              -> Result<(),SelectionError>
    {
        /*!
         * Given an obligation like `<SomeTrait for T>`, search the obligations
         * that the caller supplied to find out whether it is listed among
         * them.
         *
         * Never affects inference environment.
         */

        debug!("assemble_candidates_from_caller_bounds({}, {})",
               obligation.repr(self.tcx()),
               skol_obligation_self_ty.repr(self.tcx()));

        for caller_obligation in self.param_env.caller_obligations.iter() {
            // Skip over obligations that don't apply to
            // `self_ty`.
            let caller_bound = &caller_obligation.trait_ref;
            let caller_self_ty = caller_bound.substs.self_ty().unwrap();
            debug!("caller_obligation={}, caller_self_ty={}",
                   caller_obligation.repr(self.tcx()),
                   self.infcx.ty_to_string(caller_self_ty));
            match self.match_self_types(obligation.cause,
                                        caller_self_ty,
                                        skol_obligation_self_ty) {
                AmbiguousMatch => {
                    debug!("-> AmbiguousMatch");
                    candidates.push(AmbiguousParamCandidate);
                    return Ok(());
                }
                NoMatch => {
                    debug!("-> NoMatch");
                    continue;
                }
                Matched(()) => { }
            }

            // Search through the trait (and its supertraits) to
            // see if it matches the def-id we are looking for.
            let caller_bound = (*caller_bound).clone();
            for bound in util::transitive_bounds(self.tcx(), &[caller_bound]) {
                debug!("-> check bound={}", bound.repr(self.tcx()));
                if bound.def_id == obligation.trait_ref.def_id {
                    // If so, we're done!
                    debug!("-> MatchedParamCandidate({})", bound.repr(self.tcx()));
                    let vtable_param = VtableParamData { bound: bound };
                    candidates.push(MatchedParamCandidate(vtable_param));
                    return Ok(());
                }
            }
        }

        Ok(())
    }

    fn assemble_unboxed_candidates(&mut self,
                                   obligation: &Obligation,
                                   skol_obligation_self_ty: ty::t,
                                   candidates: &mut Vec<Candidate>)
                                   -> Result<(),SelectionError>
    {
        /*!
         * Check for the artificial impl that the compiler will create
         * for an obligation like `X : FnMut<..>` where `X` is an
         * unboxed closure type.
         */

        let closure_def_id = match ty::get(skol_obligation_self_ty).sty {
            ty::ty_unboxed_closure(id, _) => id,
            _ => { return Ok(()); }
        };

        let tcx = self.tcx();
        let fn_traits = [
            (ty::FnUnboxedClosureKind, tcx.lang_items.fn_trait()),
            (ty::FnMutUnboxedClosureKind, tcx.lang_items.fn_mut_trait()),
            (ty::FnOnceUnboxedClosureKind, tcx.lang_items.fn_once_trait()),
            ];
        for tuple in fn_traits.iter() {
            let kind = match tuple {
                &(kind, Some(ref fn_trait))
                    if *fn_trait == obligation.trait_ref.def_id =>
                {
                    kind
                }
                _ => continue,
            };

            // Check to see whether the argument and return types match.
            let closure_kind = match self.typer.unboxed_closures().borrow().find(&closure_def_id) {
                Some(closure) => closure.kind,
                None => {
                    self.tcx().sess.span_bug(
                        obligation.cause.span,
                        format!("No entry for unboxed closure: {}",
                                closure_def_id.repr(self.tcx())).as_slice());
                }
            };

            if closure_kind != kind {
                continue;
            }

            candidates.push(MatchedUnboxedClosureCandidate(closure_def_id));
        }

        Ok(())
    }

    fn assemble_candidates_from_impls(&mut self,
                                      obligation: &Obligation,
                                      skol_obligation_self_ty: ty::t,
                                      candidates: &mut Vec<Candidate>)
                                      -> Result<(), SelectionError>
    {
        /*!
         * Search for impls that might apply to `obligation`.
         */

        let all_impls = self.all_impls(obligation.trait_ref.def_id);
        for &impl_def_id in all_impls.iter() {
            self.infcx.probe(|| {
                match self.candidate_from_impl(impl_def_id,
                                               obligation.cause,
                                               skol_obligation_self_ty) {
                    Some(c) => {
                        candidates.push(Impl(c));
                    }

                    None => { }
                }
            });
        }
        Ok(())
    }

    fn candidate_from_impl(&mut self,
                           impl_def_id: ast::DefId,
                           obligation_cause: ObligationCause,
                           skol_obligation_self_ty: ty::t)
                           -> Option<ImplCandidate>
    {
        match self.match_impl_self_types(impl_def_id,
                                         obligation_cause,
                                         skol_obligation_self_ty) {
            Matched(_) => {
                Some(MatchedImplCandidate(impl_def_id))
            }

            AmbiguousMatch => {
                Some(AmbiguousImplCandidate(impl_def_id))
            }

            NoMatch => {
                None
            }
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // BUILTIN BOUNDS
    //
    // These cover the traits that are built-in to the language
    // itself.  This includes `Copy` and `Sized` for sure. For the
    // moment, it also includes `Send` / `Sync` and a few others, but
    // those will hopefully change to library-defined traits in the
    // future.

    fn assemble_builtin_bound_candidates(&mut self,
                                         bound: ty::BuiltinBound,
                                         stack: &ObligationStack,
                                         candidates: &mut Vec<Candidate>)
                                         -> Result<(),SelectionError>
    {
        // Copy -- owned, dtor, managed, marker, &mut -- only INTERIOR?
        // Sized -- str, [T], Trait -- but only INTERIOR
        // Send -- managed data, nonsend annot, borrowed data -- REACHABILITY
        // Sync -- non-sync marker trait -- REACHABILITY

        // Ideally, we'd only have to examine the immediate fields.
        // But think this through carefully I guess.

        enum WhenOk<'a> {
            Always,
            Unknown,
            Never,
            If(ty::t),
            IfAll(&'a [ty::t]),
            IfTrue(bool)
        }

        let ok = |this: &mut SelectionContext, w: WhenOk| {
            let r = match w {
                Always => EvaluatedToMatch,
                Unknown => EvaluatedToAmbiguity,
                Never => EvaluatedToUnmatch,
                IfTrue(true) => EvaluatedToMatch,
                IfTrue(false) => EvaluatedToUnmatch,
                If(ty) => this.evaluate_builtin_bound_recursively(bound, stack, ty),
                IfAll(tys) => {
                    let mut result = EvaluatedToMatch;
                    for &ty in tys.iter() {
                        match this.evaluate_builtin_bound_recursively(bound, stack, ty) {
                            EvaluatedToMatch => { }
                            EvaluatedToAmbiguity => {
                                result = EvaluatedToAmbiguity;
                            }
                            EvaluatedToUnmatch => {
                                result = EvaluatedToUnmatch;
                                break;
                            }
                        }
                    }
                    result
                }
            };

            match r {
                EvaluatedToMatch => Ok(candidates.push(MatchedBuiltinCandidate)),
                EvaluatedToAmbiguity => Ok(candidates.push(AmbiguousBuiltinCandidate)),
                EvaluatedToUnmatch => Err(Unimplemented)
            }
        };

        return match ty::get(stack.skol_obligation_self_ty).sty {
            ty::ty_uint(_) | ty::ty_int(_) | ty::ty_infer(ty::SkolemizedIntTy(_)) |
            ty::ty_nil | ty::ty_bot | ty::ty_bool | ty::ty_float(_) |
            ty::ty_bare_fn(_) | ty::ty_char => {
                // safe for everything
                ok(self, Always)
            }

            ty::ty_uniq(referent_ty) => {  // Box<T>
                match bound {
                    ty::BoundCopy => {
                        ok(self, Never)
                    }

                    ty::BoundSized => {
                        ok(self, Always)
                    }

                    ty::BoundSync |
                    ty::BoundSend => {
                        ok(self, If(referent_ty))
                    }
                }
            }

            ty::ty_ptr(ty::mt { ty: referent_ty, .. }) => {     // *const T, *mut T
                match bound {
                    ty::BoundCopy |
                    ty::BoundSized => {
                        ok(self, Always)
                    }

                    ty::BoundSync |
                    ty::BoundSend => {
                        ok(self, If(referent_ty))
                    }
                }
            }

            ty::ty_closure(ref c) => {
                match c.store {
                    ty::UniqTraitStore => {
                        // proc: Equivalent to `Box<FnOnce>`
                        match bound {
                            ty::BoundCopy => {
                                ok(self, Never)
                            }

                            ty::BoundSized => {
                                ok(self, Always)
                            }

                            ty::BoundSync |
                            ty::BoundSend => {
                                ok(self, IfTrue(c.bounds.builtin_bounds.contains_elem(bound)))
                            }
                        }
                    }
                    ty::RegionTraitStore(_, mutbl) => {
                        // ||: Equivalent to `&FnMut` or `&mut FnMut` or something like that.
                        match bound {
                            ty::BoundCopy => {
                                ok(self, match mutbl {
                                    ast::MutMutable => Never,  // &mut T is affine
                                    ast::MutImmutable => Always,  // &T is copyable
                                })
                            }

                            ty::BoundSized => {
                                ok(self, Always)
                            }

                            ty::BoundSync |
                            ty::BoundSend => {
                                ok(self, IfTrue(c.bounds.builtin_bounds.contains_elem(bound)))
                            }
                        }
                    }
                }
            }

            ty::ty_trait(box ty::TyTrait { bounds, .. }) => {
                match bound {
                    ty::BoundSized => {
                        ok(self, Never)
                    }
                    ty::BoundCopy | ty::BoundSync | ty::BoundSend => {
                        ok(self, IfTrue(bounds.builtin_bounds.contains_elem(bound)))
                    }
                }
            }

            ty::ty_rptr(_, ty::mt { ty: referent_ty, mutbl: mutbl }) => {
                // &mut T or &T
                match bound {
                    ty::BoundCopy => {
                        ok(self, match mutbl {
                            ast::MutMutable => Never,  // &mut T is affine and hence never `Copy`
                            ast::MutImmutable => Always,  // &T is copyable
                        })
                    }

                    ty::BoundSized => {
                        ok(self, Always)
                    }

                    ty::BoundSync |
                    ty::BoundSend => {
                        // Note: technically, a region pointer is only
                        // sendable if it has lifetime
                        // `'static`. However, we don't take regions
                        // into account when doing trait matching:
                        // instead, when we decide that `T : Send`, we
                        // will register a separate constraint with
                        // the region inferencer that `T : 'static`
                        // holds as well (because the trait `Send`
                        // requires it). This will ensure that there
                        // is no borrowed data in `T` (or else report
                        // an inference error). The reason we do it
                        // this way is that we do not yet *know* what
                        // lifetime the borrowed reference has, since
                        // we haven't finished running inference -- in
                        // other words, there's a kind of
                        // chicken-and-egg problem.
                        ok(self, If(referent_ty))
                    }
                }
            }

            ty::ty_vec(element_ty, ref len) => {
                // [T, ..n] and [T]
                match bound {
                    ty::BoundCopy => {
                        match *len {
                            Some(_) => ok(self, If(element_ty)), // [T, ..n] is copy iff T is copy
                            None => ok(self, Never), // [T] is unsized and hence affine
                        }
                    }

                    ty::BoundSized => {
                        ok(self, IfTrue(len.is_some()))
                    }

                    ty::BoundSync |
                    ty::BoundSend => {
                        ok(self, If(element_ty))
                    }
                }
            }

            ty::ty_str => {
                // Equivalent to [u8]
                match bound {
                    ty::BoundSync |
                    ty::BoundSend => {
                        ok(self, Always)
                    }

                    ty::BoundCopy |
                    ty::BoundSized => {
                        ok(self, Never)
                    }
                }
            }

            ty::ty_tup(ref tys) => {
                // (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
                ok(self, IfAll(tys.as_slice()))
            }

            ty::ty_unboxed_closure(def_id, _) => {
                // FIXME -- This case is tricky. In the case of by-ref
                // closures particularly, we need the results of
                // inference to decide how to reflect the type of each
                // upvar (the upvar may have type `T`, but the runtime
                // type could be `&mut`, `&`, or just `T`). For now,
                // though, we'll do this unsoundly and assume that all
                // captures are by value. Really what we ought to do
                // is reserve judgement and then intertwine this
                // analysis with closure inference.
                //
                // FIXME -- this is wrong with respect to
                // skolemization. We want to skolemize the types of
                // the variables, but to do THAT we need the ability
                // to "start" the skolemization numbering from a
                // higher point. Perhaps this just means creating a
                // single skolemizer and then using it again here?
                assert_eq!(def_id.krate, ast::LOCAL_CRATE);
                match self.tcx().freevars.borrow().find(&def_id.node) {
                    None => {
                        // No upvars.
                        ok(self, Always)
                    }

                    Some(freevars) => {
                        let tys: Vec<ty::t> =
                            freevars
                            .iter()
                            .map(|freevar| {
                                let freevar_def_id = freevar.def.def_id();
                                let freevar_ty = self.typer.node_ty(freevar_def_id.node)
                                    .unwrap_or(ty::mk_err());
                                freevar_ty.fold_with(&mut self.skolemizer)
                            })
                            .collect();
                        ok(self, IfAll(tys.as_slice()))
                    }
                }
            }

            ty::ty_struct(def_id, ref substs) => {
                let types: Vec<ty::t> =
                    ty::struct_fields(self.tcx(), def_id, substs)
                    .iter()
                    .map(|f| f.mt.ty)
                    .collect();
                nominal(self, bound, def_id, types, ok)
            }

            ty::ty_enum(def_id, ref substs) => {
                let types: Vec<ty::t> =
                    ty::substd_enum_variants(self.tcx(), def_id, substs)
                    .iter()
                    .flat_map(|variant| variant.args.iter())
                    .map(|&ty| ty)
                    .collect();
                nominal(self, bound, def_id, types, ok)
            }

            ty::ty_param(_) => {
                // Note: A type parameter is only considered to meet a
                // particular bound if there is a where clause telling
                // us that it does, and that case is handled by
                // `assemble_candidates_from_caller_bounds()`.
                ok(self, Never)
            }

            ty::ty_infer(ty::SkolemizedTy(_)) => {
                // Skolemized types represent unbound type
                // variables. They might or might not have applicable
                // impls and so forth, depending on what those type
                // variables wind up being bound to.
                ok(self, Unknown)
            }

            ty::ty_open(_) |
            ty::ty_infer(ty::TyVar(_)) |
            ty::ty_infer(ty::IntVar(_)) |
            ty::ty_infer(ty::FloatVar(_)) |
            ty::ty_err => {
                self.tcx().sess.span_bug(
                    stack.obligation.cause.span,
                    format!(
                        "asked to compute contents of unexpected type: {}",
                        stack.skol_obligation_self_ty.repr(self.tcx())).as_slice());
            }
        };

        fn nominal(this: &mut SelectionContext,
                   bound: ty::BuiltinBound,
                   def_id: ast::DefId,
                   types: Vec<ty::t>,
                   ok: |&mut SelectionContext, WhenOk| -> Result<(),SelectionError>)
                   -> Result<(),SelectionError>
        {
            // First check for markers and other nonsense.
            let tcx = this.tcx();
            match bound {
                ty::BoundSend => {
                    if
                        Some(def_id) == tcx.lang_items.no_send_bound() ||
                        Some(def_id) == tcx.lang_items.managed_bound()
                    {
                        return ok(this, Never);
                    }
                }

                ty::BoundCopy => {
                    if
                        Some(def_id) == tcx.lang_items.no_copy_bound() ||
                        Some(def_id) == tcx.lang_items.managed_bound() ||
                        ty::has_dtor(tcx, def_id)
                    {
                        return ok(this, Never);
                    }
                }

                ty::BoundSync => {
                    if
                        Some(def_id) == tcx.lang_items.no_sync_bound() ||
                        Some(def_id) == tcx.lang_items.managed_bound()
                    {
                        return ok(this, Never);
                    } else if
                        Some(def_id) == tcx.lang_items.unsafe_type()
                    {
                        // FIXME(#13231) -- we currently consider `UnsafeCell<T>`
                        // to always be sync. This is allow for types like `Queue`
                        // and `Mutex`, where `Queue<T> : Sync` is `T : Send`.
                        return ok(this, Always);
                    }
                }

                ty::BoundSized => { }
            }

            ok(this, IfAll(types.as_slice()))
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // CONFIRMATION
    //
    // Confirmation unifies the output type parameters of the trait
    // with the values found in the obligation, possibly yielding a
    // type error.  See `doc.rs` for more details.

    fn confirm_candidate(&mut self,
                         obligation: &Obligation,
                         candidate: Candidate)
                         -> SelectionResult<Selection>
    {
        debug!("confirm_candidate({}, {})",
               obligation.repr(self.tcx()),
               candidate.repr(self.tcx()));

        match candidate {
            AmbiguousBuiltinCandidate |
            AmbiguousParamCandidate |
            Impl(AmbiguousImplCandidate(_)) => {
                Ok(None)
            }

            ErrorCandidate |
            MatchedBuiltinCandidate => {
                Ok(Some(VtableBuiltin))
            }

            MatchedParamCandidate(param) => {
                Ok(Some(VtableParam(
                    try!(self.confirm_param_candidate(obligation, param)))))
            }

            Impl(MatchedImplCandidate(impl_def_id)) => {
                let vtable_impl = try!(self.confirm_impl_candidate(obligation,
                                                                   impl_def_id));
                Ok(Some(VtableImpl(vtable_impl)))
            }

            MatchedUnboxedClosureCandidate(closure_def_id) => {
                try!(self.confirm_unboxed_closure_candidate(obligation, closure_def_id));
                Ok(Some(VtableUnboxedClosure(closure_def_id)))
            }
        }
    }

    fn confirm_param_candidate(&mut self,
                               obligation: &Obligation,
                               param: VtableParamData)
                               -> Result<VtableParamData,SelectionError>
    {
        debug!("confirm_param_candidate({},{})",
               obligation.repr(self.tcx()),
               param.repr(self.tcx()));

        let () = try!(self.confirm(obligation.cause,
                                   obligation.trait_ref.clone(),
                                   param.bound.clone()));
        Ok(param)
    }

    fn confirm_impl_candidate(&mut self,
                              obligation: &Obligation,
                              impl_def_id: ast::DefId)
                              -> Result<VtableImplData<Obligation>,SelectionError>
    {
        debug!("confirm_impl_candidate({},{})",
               obligation.repr(self.tcx()),
               impl_def_id.repr(self.tcx()));

        // For a non-inhernet impl, we begin the same way as an
        // inherent impl, by matching the self-type and assembling
        // list of nested obligations.
        let vtable_impl =
            try!(self.confirm_inherent_impl_candidate(
                impl_def_id,
                obligation.cause,
                obligation.trait_ref.self_ty(),
                obligation.recursion_depth));

        // But then we must also match the output types.
        let () = try!(self.confirm_impl_vtable(impl_def_id,
                                               obligation.cause,
                                               obligation.trait_ref.clone(),
                                               &vtable_impl.substs));
        Ok(vtable_impl)
    }

    fn confirm_inherent_impl_candidate(&mut self,
                                       impl_def_id: ast::DefId,
                                       obligation_cause: ObligationCause,
                                       obligation_self_ty: ty::t,
                                       obligation_recursion_depth: uint)
                                       -> Result<VtableImplData<Obligation>,
                                                 SelectionError>
    {
        let substs = match self.match_impl_self_types(impl_def_id,
                                                      obligation_cause,
                                                      obligation_self_ty) {
            Matched(substs) => substs,
            AmbiguousMatch | NoMatch => {
                self.tcx().sess.bug(
                    format!("Impl {} was matchable against {} but now is not",
                            impl_def_id.repr(self.tcx()),
                            obligation_self_ty.repr(self.tcx()))
                        .as_slice());
            }
        };

        let impl_obligations =
            self.impl_obligations(obligation_cause,
                                  obligation_recursion_depth,
                                  impl_def_id,
                                  &substs);
        let vtable_impl = VtableImplData { impl_def_id: impl_def_id,
                                       substs: substs,
                                       nested: impl_obligations };

        Ok(vtable_impl)
    }

    fn confirm_unboxed_closure_candidate(&mut self,
                                         obligation: &Obligation,
                                         closure_def_id: ast::DefId)
                                         -> Result<(),SelectionError>
    {
        debug!("confirm_unboxed_closure_candidate({},{})",
               obligation.repr(self.tcx()),
               closure_def_id.repr(self.tcx()));

        let closure_type = match self.typer.unboxed_closures().borrow().find(&closure_def_id) {
            Some(closure) => closure.closure_type.clone(),
            None => {
                self.tcx().sess.span_bug(
                    obligation.cause.span,
                    format!("No entry for unboxed closure: {}",
                            closure_def_id.repr(self.tcx())).as_slice());
            }
        };

        // FIXME(pcwalton): This is a bogus thing to do, but
        // it'll do for now until we get the new trait-bound
        // region skolemization working.
        let (_, new_signature) =
            regionmanip::replace_late_bound_regions_in_fn_sig(
                self.tcx(),
                &closure_type.sig,
                |br| self.infcx.next_region_var(
                         infer::LateBoundRegion(obligation.cause.span, br)));

        let arguments_tuple = *new_signature.inputs.get(0);
        let trait_ref = Rc::new(ty::TraitRef {
            def_id: obligation.trait_ref.def_id,
            substs: Substs::new_trait(
                vec![arguments_tuple, new_signature.output],
                vec![],
                obligation.self_ty())
        });

        self.confirm(obligation.cause,
                     obligation.trait_ref.clone(),
                     trait_ref)
    }

    ///////////////////////////////////////////////////////////////////////////
    // Matching
    //
    // Matching is a common path used for both evaluation and
    // confirmation.  It basically unifies types that appear in impls
    // and traits. This does affect the surrounding environment;
    // therefore, when used during evaluation, match routines must be
    // run inside of a `probe()` so that their side-effects are
    // contained.

    fn match_impl_self_types(&mut self,
                             impl_def_id: ast::DefId,
                             obligation_cause: ObligationCause,
                             obligation_self_ty: ty::t)
                             -> MatchResult<Substs>
    {
        /*!
         * Determines whether the self type declared against
         * `impl_def_id` matches `obligation_self_ty`. If successful,
         * returns the substitutions used to make them match. See
         * `match_impl()`.  For example, if `impl_def_id` is declared
         * as:
         *
         *    impl<T:Copy> Foo for ~T { ... }
         *
         * and `obligation_self_ty` is `int`, we'd back an `Err(_)`
         * result. But if `obligation_self_ty` were `~int`, we'd get
         * back `Ok(T=int)`.
         */

        // Create fresh type variables for each type parameter declared
        // on the impl etc.
        let impl_substs = util::fresh_substs_for_impl(self.infcx,
                                                      obligation_cause.span,
                                                      impl_def_id);

        // Find the self type for the impl.
        let impl_self_ty = ty::lookup_item_type(self.tcx(), impl_def_id).ty;
        let impl_self_ty = impl_self_ty.subst(self.tcx(), &impl_substs);

        debug!("match_impl_self_types(obligation_self_ty={}, impl_self_ty={})",
               obligation_self_ty.repr(self.tcx()),
               impl_self_ty.repr(self.tcx()));

        match self.match_self_types(obligation_cause,
                                    impl_self_ty,
                                    obligation_self_ty) {
            Matched(()) => {
                debug!("Matched impl_substs={}", impl_substs.repr(self.tcx()));
                Matched(impl_substs)
            }
            AmbiguousMatch => {
                debug!("AmbiguousMatch");
                AmbiguousMatch
            }
            NoMatch => {
                debug!("NoMatch");
                NoMatch
            }
        }
    }

    fn match_self_types(&mut self,
                        cause: ObligationCause,

                        // The self type provided by the impl/caller-obligation:
                        provided_self_ty: ty::t,

                        // The self type the obligation is for:
                        required_self_ty: ty::t)
                        -> MatchResult<()>
    {
        // FIXME(#5781) -- equating the types is stronger than
        // necessary. Should consider variance of trait w/r/t Self.

        let origin = infer::RelateSelfType(cause.span);
        match self.infcx.eq_types(false,
                                  origin,
                                  provided_self_ty,
                                  required_self_ty) {
            Ok(()) => Matched(()),
            Err(ty::terr_sorts(ty::expected_found{expected: t1, found: t2})) => {
                // This error occurs when there is an unresolved type
                // variable in the `required_self_ty` that was forced
                // to unify with a non-type-variable. That basically
                // means we don't know enough to say with certainty
                // whether there is a match or not -- it depends on
                // how that type variable is ultimately resolved.
                if ty::type_is_skolemized(t1) || ty::type_is_skolemized(t2) {
                    AmbiguousMatch
                } else {
                    NoMatch
                }
            }
            Err(_) => NoMatch,
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // Confirmation
    //
    // The final step of selection: once we know how an obligation is
    // is resolved, we confirm that selection in order to have
    // side-effects on the typing environment. This step also unifies
    // the output type parameters from the obligation with those found
    // on the impl/bound, which may yield type errors.

    fn confirm_impl_vtable(&mut self,
                           impl_def_id: ast::DefId,
                           obligation_cause: ObligationCause,
                           obligation_trait_ref: Rc<ty::TraitRef>,
                           substs: &Substs)
                           -> Result<(), SelectionError>
    {
        /*!
         * Relates the output type parameters from an impl to the
         * trait.  This may lead to type errors. The confirmation step
         * is separated from the main match procedure because these
         * type errors do not cause us to select another impl.
         *
         * As an example, consider matching the obligation
         * `Iterator<char> for Elems<int>` using the following impl:
         *
         *    impl<T> Iterator<T> for Elems<T> { ... }
         *
         * The match phase will succeed with substitution `T=int`.
         * The confirm step will then try to unify `int` and `char`
         * and yield an error.
         */

        let impl_trait_ref = ty::impl_trait_ref(self.tcx(),
                                                impl_def_id).unwrap();
        let impl_trait_ref = impl_trait_ref.subst(self.tcx(),
                                                  substs);
        self.confirm(obligation_cause, obligation_trait_ref, impl_trait_ref)
    }

    fn confirm(&mut self,
               obligation_cause: ObligationCause,
               obligation_trait_ref: Rc<ty::TraitRef>,
               expected_trait_ref: Rc<ty::TraitRef>)
               -> Result<(), SelectionError>
    {
        /*!
         * After we have determined which impl applies, and with what
         * substitutions, there is one last step. We have to go back
         * and relate the "output" type parameters from the obligation
         * to the types that are specified in the impl.
         *
         * For example, imagine we have:
         *
         *     impl<T> Iterator<T> for Vec<T> { ... }
         *
         * and our obligation is `Iterator<Foo> for Vec<int>` (note
         * the mismatch in the obligation types). Up until this step,
         * no error would be reported: the self type is `Vec<int>`,
         * and that matches `Vec<T>` with the substitution `T=int`.
         * At this stage, we could then go and check that the type
         * parameters to the `Iterator` trait match.
         * (In terms of the parameters, the `expected_trait_ref`
         * here would be `Iterator<int> for Vec<int>`, and the
         * `obligation_trait_ref` would be `Iterator<Foo> for Vec<int>`.
         *
         * Note that this checking occurs *after* the impl has
         * selected, because these output type parameters should not
         * affect the selection of the impl. Therefore, if there is a
         * mismatch, we report an error to the user.
         */

        let origin = infer::RelateOutputImplTypes(obligation_cause.span);

        let obligation_trait_ref = obligation_trait_ref.clone();
        match self.infcx.sub_trait_refs(false,
                                        origin,
                                        expected_trait_ref.clone(),
                                        obligation_trait_ref) {
            Ok(()) => Ok(()),
            Err(e) => Err(OutputTypeParameterMismatch(expected_trait_ref, e))
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // Miscellany

    fn new_stack<'o>(&mut self, obligation: &'o Obligation) -> ObligationStack<'o> {
        let skol_obligation_self_ty =
            obligation.self_ty().fold_with(&mut self.skolemizer);

        ObligationStack {
            obligation: obligation,
            skol_obligation_self_ty: skol_obligation_self_ty,
            previous: None
        }
    }

    fn push_stack<'o>(&self,
                      previous_stack: &'o ObligationStack<'o>,
                      obligation: &'o Obligation)
                      -> ObligationStack<'o>
    {
        // No need to skolemize obligation.self_ty, because we
        // guarantee the self-type for all recursive obligations are
        // already skolemized.
        ObligationStack {
            obligation: obligation,
            skol_obligation_self_ty: obligation.self_ty(),
            previous: Some(previous_stack)
        }
    }

    fn all_impls(&self, trait_def_id: ast::DefId) -> Vec<ast::DefId> {
        /*!
         * Returns se tof all impls for a given trait.
         */

        ty::populate_implementations_for_trait_if_necessary(self.tcx(),
                                                            trait_def_id);
        match self.tcx().trait_impls.borrow().find(&trait_def_id) {
            None => Vec::new(),
            Some(impls) => impls.borrow().clone()
        }
    }

    fn impl_obligations(&self,
                        cause: ObligationCause,
                        recursion_depth: uint,
                        impl_def_id: ast::DefId,
                        impl_substs: &Substs)
                        -> VecPerParamSpace<Obligation>
    {
        let impl_generics = ty::lookup_item_type(self.tcx(),
                                                 impl_def_id).generics;
        util::obligations_for_generics(self.tcx(), cause, recursion_depth,
                                       &impl_generics, impl_substs)
    }

    fn contains_skolemized_types(&self,
                                 ty: ty::t)
                                 -> bool
    {
        /*!
         * True if the type contains skolemized variables.
         */

        let mut found_skol = false;

        ty::walk_ty(ty, |t| {
            match ty::get(t).sty {
                ty::ty_infer(ty::SkolemizedTy(_)) => { found_skol = true; }
                _ => { }
            }
        });

        found_skol
    }
}

impl Candidate {
    fn to_evaluation_result(&self) -> EvaluationResult {
        match *self {
            Impl(ref i) => i.to_evaluation_result(),

            ErrorCandidate |
            MatchedUnboxedClosureCandidate(..) |
            MatchedBuiltinCandidate |
            MatchedParamCandidate(..) => {
                EvaluatedToMatch
            }

            AmbiguousBuiltinCandidate |
            AmbiguousParamCandidate => {
                EvaluatedToAmbiguity
            }
        }
    }
}

impl ImplCandidate {
    fn to_evaluation_result(&self) -> EvaluationResult {
        match *self {
            MatchedImplCandidate(..) => EvaluatedToMatch,
            AmbiguousImplCandidate(..) => EvaluatedToAmbiguity
        }
    }
}

impl Repr for Candidate {
    fn repr(&self, tcx: &ty::ctxt) -> String {
        match *self {
            ErrorCandidate => format!("ErrorCandidate"),
            MatchedBuiltinCandidate => format!("MatchedBuiltinCandidate"),
            AmbiguousBuiltinCandidate => format!("AmbiguousBuiltinCandidate"),
            MatchedUnboxedClosureCandidate(c) => format!("MatchedUnboxedClosureCandidate({})", c),
            MatchedParamCandidate(ref r) => format!("MatchedParamCandidate({})",
                                                    r.repr(tcx)),
            AmbiguousParamCandidate => format!("AmbiguousParamCandidate"),
            Impl(ref i) => i.repr(tcx)
        }
    }
}

impl Repr for ImplCandidate {
    fn repr(&self, tcx: &ty::ctxt) -> String {
        match *self {
            MatchedImplCandidate(ref d) => format!("MatchedImplCandidate({})",
                                                   d.repr(tcx)),
            AmbiguousImplCandidate(ref d) => format!("AmbiguousImplCandidate({})",
                                                     d.repr(tcx)),
        }
    }
}

impl SelectionCache {
    pub fn new() -> SelectionCache {
        SelectionCache {
            hashmap: RefCell::new(HashMap::new())
        }
    }
}

impl<'o> ObligationStack<'o> {
    fn iter(&self) -> Option<&ObligationStack> {
        Some(self)
    }
}

impl<'o> Iterator<&'o ObligationStack<'o>> for Option<&'o ObligationStack<'o>> {
    fn next(&mut self) -> Option<&'o ObligationStack<'o>> {
        match *self {
            Some(o) => {
                *self = o.previous;
                Some(o)
            }
            None => {
                None
            }
        }
    }
}

impl<'o> Repr for ObligationStack<'o> {
    fn repr(&self, tcx: &ty::ctxt) -> String {
        format!("ObligationStack({}, {})",
                self.obligation.repr(tcx),
                self.skol_obligation_self_ty.repr(tcx))
    }
}

impl CacheKey {
    pub fn new(trait_def_id: ast::DefId,
               skol_obligation_self_ty: ty::t)
               -> CacheKey
    {
        CacheKey {
            trait_def_id: trait_def_id,
            skol_obligation_self_ty: skol_obligation_self_ty
        }
    }
}