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
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
// Copyright 2012-2013 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.

//! Name resolution for lifetimes.
//!
//! Name resolution for lifetimes follows MUCH simpler rules than the
//! full resolve. For example, lifetime names are never exported or
//! used between functions, and they operate in a purely top-down
//! way. Therefore we break lifetime name resolution into a separate pass.

use dep_graph::DepNode;
use hir::map::Map;
use session::Session;
use hir::def::Def;
use hir::def_id::DefId;
use middle::region;
use ty;

use std::cell::Cell;
use std::mem::replace;
use syntax::ast;
use syntax::attr;
use syntax::ptr::P;
use syntax::symbol::keywords;
use syntax_pos::Span;
use errors::DiagnosticBuilder;
use util::nodemap::{NodeMap, NodeSet, FxHashSet, FxHashMap, DefIdMap};
use rustc_back::slice;

use hir;
use hir::intravisit::{self, Visitor, NestedVisitorMap};

#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
pub enum Region {
    Static,
    EarlyBound(/* index */ u32, /* lifetime decl */ ast::NodeId),
    LateBound(ty::DebruijnIndex, /* lifetime decl */ ast::NodeId),
    LateBoundAnon(ty::DebruijnIndex, /* anon index */ u32),
    Free(region::CallSiteScopeData, /* lifetime decl */ ast::NodeId),
}

impl Region {
    fn early(index: &mut u32, def: &hir::LifetimeDef) -> (ast::Name, Region) {
        let i = *index;
        *index += 1;
        (def.lifetime.name, Region::EarlyBound(i, def.lifetime.id))
    }

    fn late(def: &hir::LifetimeDef) -> (ast::Name, Region) {
        let depth = ty::DebruijnIndex::new(1);
        (def.lifetime.name, Region::LateBound(depth, def.lifetime.id))
    }

    fn late_anon(index: &Cell<u32>) -> Region {
        let i = index.get();
        index.set(i + 1);
        let depth = ty::DebruijnIndex::new(1);
        Region::LateBoundAnon(depth, i)
    }

    fn id(&self) -> Option<ast::NodeId> {
        match *self {
            Region::Static |
            Region::LateBoundAnon(..) => None,

            Region::EarlyBound(_, id) |
            Region::LateBound(_, id) |
            Region::Free(_, id) => Some(id)
        }
    }

    fn shifted(self, amount: u32) -> Region {
        match self {
            Region::LateBound(depth, id) => {
                Region::LateBound(depth.shifted(amount), id)
            }
            Region::LateBoundAnon(depth, index) => {
                Region::LateBoundAnon(depth.shifted(amount), index)
            }
            _ => self
        }
    }

    fn from_depth(self, depth: u32) -> Region {
        match self {
            Region::LateBound(debruijn, id) => {
                Region::LateBound(ty::DebruijnIndex {
                    depth: debruijn.depth - (depth - 1)
                }, id)
            }
            Region::LateBoundAnon(debruijn, index) => {
                Region::LateBoundAnon(ty::DebruijnIndex {
                    depth: debruijn.depth - (depth - 1)
                }, index)
            }
            _ => self
        }
    }

    fn subst(self, params: &[hir::Lifetime], map: &NamedRegionMap)
             -> Option<Region> {
        if let Region::EarlyBound(index, _) = self {
            params.get(index as usize).and_then(|lifetime| {
                map.defs.get(&lifetime.id).cloned()
            })
        } else {
            Some(self)
        }
    }
}

/// A set containing, at most, one known element.
/// If two distinct values are inserted into a set, then it
/// becomes `Many`, which can be used to detect ambiguities.
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
pub enum Set1<T> {
    Empty,
    One(T),
    Many
}

impl<T: PartialEq> Set1<T> {
    pub fn insert(&mut self, value: T) {
        if let Set1::Empty = *self {
            *self = Set1::One(value);
            return;
        }
        if let Set1::One(ref old) = *self {
            if *old == value {
                return;
            }
        }
        *self = Set1::Many;
    }
}

pub type ObjectLifetimeDefault = Set1<Region>;

// Maps the id of each lifetime reference to the lifetime decl
// that it corresponds to.
pub struct NamedRegionMap {
    // maps from every use of a named (not anonymous) lifetime to a
    // `Region` describing how that region is bound
    pub defs: NodeMap<Region>,

    // the set of lifetime def ids that are late-bound; a region can
    // be late-bound if (a) it does NOT appear in a where-clause and
    // (b) it DOES appear in the arguments.
    pub late_bound: NodeSet,

    // Contains the node-ids for lifetimes that were (incorrectly) categorized
    // as late-bound, until #32330 was fixed.
    pub issue_32330: NodeMap<ty::Issue32330>,

    // For each type and trait definition, maps type parameters
    // to the trait object lifetime defaults computed from them.
    pub object_lifetime_defaults: NodeMap<Vec<ObjectLifetimeDefault>>,
}

struct LifetimeContext<'a, 'tcx: 'a> {
    sess: &'a Session,
    hir_map: &'a Map<'tcx>,
    map: &'a mut NamedRegionMap,
    scope: ScopeRef<'a>,
    // Deep breath. Our representation for poly trait refs contains a single
    // binder and thus we only allow a single level of quantification. However,
    // the syntax of Rust permits quantification in two places, e.g., `T: for <'a> Foo<'a>`
    // and `for <'a, 'b> &'b T: Foo<'a>`. In order to get the de Bruijn indices
    // correct when representing these constraints, we should only introduce one
    // scope. However, we want to support both locations for the quantifier and
    // during lifetime resolution we want precise information (so we can't
    // desugar in an earlier phase).

    // SO, if we encounter a quantifier at the outer scope, we set
    // trait_ref_hack to true (and introduce a scope), and then if we encounter
    // a quantifier at the inner scope, we error. If trait_ref_hack is false,
    // then we introduce the scope at the inner quantifier.

    // I'm sorry.
    trait_ref_hack: bool,

    // List of labels in the function/method currently under analysis.
    labels_in_fn: Vec<(ast::Name, Span)>,

    // Cache for cross-crate per-definition object lifetime defaults.
    xcrate_object_lifetime_defaults: DefIdMap<Vec<ObjectLifetimeDefault>>,
}

#[derive(Debug)]
enum Scope<'a> {
    /// Declares lifetimes, and each can be early-bound or late-bound.
    /// The `DebruijnIndex` of late-bound lifetimes starts at `1` and
    /// it should be shifted by the number of `Binder`s in between the
    /// declaration `Binder` and the location it's referenced from.
    Binder {
        lifetimes: FxHashMap<ast::Name, Region>,
        s: ScopeRef<'a>
    },

    /// Lifetimes introduced by a fn are scoped to the call-site for that fn,
    /// if this is a fn body, otherwise the original definitions are used.
    /// Unspecified lifetimes are inferred, unless an elision scope is nested,
    /// e.g. `(&T, fn(&T) -> &T);` becomes `(&'_ T, for<'a> fn(&'a T) -> &'a T)`.
    Body {
        id: hir::BodyId,
        s: ScopeRef<'a>
    },

    /// A scope which either determines unspecified lifetimes or errors
    /// on them (e.g. due to ambiguity). For more details, see `Elide`.
    Elision {
        elide: Elide,
        s: ScopeRef<'a>
    },

    /// Use a specific lifetime (if `Some`) or leave it unset (to be
    /// inferred in a function body or potentially error outside one),
    /// for the default choice of lifetime in a trait object type.
    ObjectLifetimeDefault {
        lifetime: Option<Region>,
        s: ScopeRef<'a>
    },

    Root
}

#[derive(Clone, Debug)]
enum Elide {
    /// Use a fresh anonymous late-bound lifetime each time, by
    /// incrementing the counter to generate sequential indices.
    FreshLateAnon(Cell<u32>),
    /// Always use this one lifetime.
    Exact(Region),
    /// Less or more than one lifetime were found, error on unspecified.
    Error(Vec<ElisionFailureInfo>)
}

#[derive(Clone, Debug)]
struct ElisionFailureInfo {
    /// Where we can find the argument pattern.
    parent: Option<hir::BodyId>,
    /// The index of the argument in the original definition.
    index: usize,
    lifetime_count: usize,
    have_bound_regions: bool
}

type ScopeRef<'a> = &'a Scope<'a>;

const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;

pub fn krate(sess: &Session,
             hir_map: &Map)
             -> Result<NamedRegionMap, usize> {
    let _task = hir_map.dep_graph.in_task(DepNode::ResolveLifetimes);
    let krate = hir_map.krate();
    let mut map = NamedRegionMap {
        defs: NodeMap(),
        late_bound: NodeSet(),
        issue_32330: NodeMap(),
        object_lifetime_defaults: compute_object_lifetime_defaults(sess, hir_map),
    };
    sess.track_errors(|| {
        let mut visitor = LifetimeContext {
            sess: sess,
            hir_map: hir_map,
            map: &mut map,
            scope: ROOT_SCOPE,
            trait_ref_hack: false,
            labels_in_fn: vec![],
            xcrate_object_lifetime_defaults: DefIdMap(),
        };
        for (_, item) in &krate.items {
            visitor.visit_item(item);
        }
    })?;
    Ok(map)
}

impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
        NestedVisitorMap::All(self.hir_map)
    }

    // We want to nest trait/impl items in their parent, but nothing else.
    fn visit_nested_item(&mut self, _: hir::ItemId) {}

    fn visit_nested_body(&mut self, body: hir::BodyId) {
        // Each body has their own set of labels, save labels.
        let saved = replace(&mut self.labels_in_fn, vec![]);
        let body = self.hir_map.body(body);
        extract_labels(self, body);
        self.with(Scope::Body { id: body.id(), s: self.scope }, |_, this| {
            this.visit_body(body);
        });
        replace(&mut self.labels_in_fn, saved);
    }

    fn visit_item(&mut self, item: &'tcx hir::Item) {
        match item.node {
            hir::ItemFn(ref decl, _, _, _, ref generics, _) => {
                self.visit_early_late(item.id, None, decl, generics, |this| {
                    intravisit::walk_item(this, item);
                });
            }
            hir::ItemExternCrate(_) |
            hir::ItemUse(..) |
            hir::ItemMod(..) |
            hir::ItemDefaultImpl(..) |
            hir::ItemForeignMod(..) => {
                // These sorts of items have no lifetime parameters at all.
                intravisit::walk_item(self, item);
            }
            hir::ItemStatic(..) |
            hir::ItemConst(..) => {
                // No lifetime parameters, but implied 'static.
                let scope = Scope::Elision {
                    elide: Elide::Exact(Region::Static),
                    s: ROOT_SCOPE
                };
                self.with(scope, |_, this| intravisit::walk_item(this, item));
            }
            hir::ItemTy(_, ref generics) |
            hir::ItemEnum(_, ref generics) |
            hir::ItemStruct(_, ref generics) |
            hir::ItemUnion(_, ref generics) |
            hir::ItemTrait(_, ref generics, ..) |
            hir::ItemImpl(_, _, ref generics, ..) => {
                // These kinds of items have only early bound lifetime parameters.
                let mut index = if let hir::ItemTrait(..) = item.node {
                    1 // Self comes before lifetimes
                } else {
                    0
                };
                let lifetimes = generics.lifetimes.iter().map(|def| {
                    Region::early(&mut index, def)
                }).collect();
                let scope = Scope::Binder {
                    lifetimes: lifetimes,
                    s: ROOT_SCOPE
                };
                self.with(scope, |old_scope, this| {
                    this.check_lifetime_defs(old_scope, &generics.lifetimes);
                    intravisit::walk_item(this, item);
                });
            }
        }
    }

    fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
        match item.node {
            hir::ForeignItemFn(ref decl, _, ref generics) => {
                self.visit_early_late(item.id, None, decl, generics, |this| {
                    intravisit::walk_foreign_item(this, item);
                })
            }
            hir::ForeignItemStatic(..) => {
                intravisit::walk_foreign_item(self, item);
            }
        }
    }

    fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
        match ty.node {
            hir::TyBareFn(ref c) => {
                let scope = Scope::Binder {
                    lifetimes: c.lifetimes.iter().map(Region::late).collect(),
                    s: self.scope
                };
                self.with(scope, |old_scope, this| {
                    // a bare fn has no bounds, so everything
                    // contained within is scoped within its binder.
                    this.check_lifetime_defs(old_scope, &c.lifetimes);
                    intravisit::walk_ty(this, ty);
                });
            }
            hir::TyTraitObject(ref bounds, ref lifetime) => {
                for bound in bounds {
                    self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
                }
                if lifetime.is_elided() {
                    self.resolve_object_lifetime_default(lifetime)
                } else {
                    self.visit_lifetime(lifetime);
                }
            }
            hir::TyRptr(ref lifetime_ref, ref mt) => {
                self.visit_lifetime(lifetime_ref);
                let scope = Scope::ObjectLifetimeDefault {
                    lifetime: self.map.defs.get(&lifetime_ref.id).cloned(),
                    s: self.scope
                };
                self.with(scope, |_, this| this.visit_ty(&mt.ty));
            }
            _ => {
                intravisit::walk_ty(self, ty)
            }
        }
    }

    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
        if let hir::TraitItemKind::Method(ref sig, _) = trait_item.node {
            self.visit_early_late(
                trait_item.id,
                Some(self.hir_map.get_parent(trait_item.id)),
                &sig.decl, &sig.generics,
                |this| intravisit::walk_trait_item(this, trait_item))
        } else {
            intravisit::walk_trait_item(self, trait_item);
        }
    }

    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
        if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {
            self.visit_early_late(
                impl_item.id,
                Some(self.hir_map.get_parent(impl_item.id)),
                &sig.decl, &sig.generics,
                |this| intravisit::walk_impl_item(this, impl_item))
        } else {
            intravisit::walk_impl_item(self, impl_item);
        }
    }

    fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
        if lifetime_ref.is_elided() {
            self.resolve_elided_lifetimes(slice::ref_slice(lifetime_ref));
            return;
        }
        if lifetime_ref.name == keywords::StaticLifetime.name() {
            self.insert_lifetime(lifetime_ref, Region::Static);
            return;
        }
        self.resolve_lifetime_ref(lifetime_ref);
    }

    fn visit_path(&mut self, path: &'tcx hir::Path, _: ast::NodeId) {
        for (i, segment) in path.segments.iter().enumerate() {
            let depth = path.segments.len() - i - 1;
            self.visit_segment_parameters(path.def, depth, &segment.parameters);
        }
    }

    fn visit_fn_decl(&mut self, fd: &'tcx hir::FnDecl) {
        let output = match fd.output {
            hir::DefaultReturn(_) => None,
            hir::Return(ref ty) => Some(ty)
        };
        self.visit_fn_like_elision(&fd.inputs, output);
    }

    fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
        for ty_param in generics.ty_params.iter() {
            walk_list!(self, visit_ty_param_bound, &ty_param.bounds);
            if let Some(ref ty) = ty_param.default {
                self.visit_ty(&ty);
            }
        }
        for predicate in &generics.where_clause.predicates {
            match predicate {
                &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ ref bounded_ty,
                                                                               ref bounds,
                                                                               ref bound_lifetimes,
                                                                               .. }) => {
                    if !bound_lifetimes.is_empty() {
                        self.trait_ref_hack = true;
                        let scope = Scope::Binder {
                            lifetimes: bound_lifetimes.iter().map(Region::late).collect(),
                            s: self.scope
                        };
                        let result = self.with(scope, |old_scope, this| {
                            this.check_lifetime_defs(old_scope, bound_lifetimes);
                            this.visit_ty(&bounded_ty);
                            walk_list!(this, visit_ty_param_bound, bounds);
                        });
                        self.trait_ref_hack = false;
                        result
                    } else {
                        self.visit_ty(&bounded_ty);
                        walk_list!(self, visit_ty_param_bound, bounds);
                    }
                }
                &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
                                                                                ref bounds,
                                                                                .. }) => {

                    self.visit_lifetime(lifetime);
                    for bound in bounds {
                        self.visit_lifetime(bound);
                    }
                }
                &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ref lhs_ty,
                                                                        ref rhs_ty,
                                                                        .. }) => {
                    self.visit_ty(lhs_ty);
                    self.visit_ty(rhs_ty);
                }
            }
        }
    }

    fn visit_poly_trait_ref(&mut self,
                            trait_ref: &'tcx hir::PolyTraitRef,
                            _modifier: hir::TraitBoundModifier) {
        debug!("visit_poly_trait_ref trait_ref={:?}", trait_ref);

        if !self.trait_ref_hack || !trait_ref.bound_lifetimes.is_empty() {
            if self.trait_ref_hack {
                span_err!(self.sess, trait_ref.span, E0316,
                          "nested quantification of lifetimes");
            }
            let scope = Scope::Binder {
                lifetimes: trait_ref.bound_lifetimes.iter().map(Region::late).collect(),
                s: self.scope
            };
            self.with(scope, |old_scope, this| {
                this.check_lifetime_defs(old_scope, &trait_ref.bound_lifetimes);
                for lifetime in &trait_ref.bound_lifetimes {
                    this.visit_lifetime_def(lifetime);
                }
                this.visit_trait_ref(&trait_ref.trait_ref)
            })
        } else {
            self.visit_trait_ref(&trait_ref.trait_ref)
        }
    }
}

#[derive(Copy, Clone, PartialEq)]
enum ShadowKind { Label, Lifetime }
struct Original { kind: ShadowKind, span: Span }
struct Shadower { kind: ShadowKind, span: Span }

fn original_label(span: Span) -> Original {
    Original { kind: ShadowKind::Label, span: span }
}
fn shadower_label(span: Span) -> Shadower {
    Shadower { kind: ShadowKind::Label, span: span }
}
fn original_lifetime(span: Span) -> Original {
    Original { kind: ShadowKind::Lifetime, span: span }
}
fn shadower_lifetime(l: &hir::Lifetime) -> Shadower {
    Shadower { kind: ShadowKind::Lifetime, span: l.span }
}

impl ShadowKind {
    fn desc(&self) -> &'static str {
        match *self {
            ShadowKind::Label => "label",
            ShadowKind::Lifetime => "lifetime",
        }
    }
}

fn signal_shadowing_problem(sess: &Session, name: ast::Name, orig: Original, shadower: Shadower) {
    let mut err = if let (ShadowKind::Lifetime, ShadowKind::Lifetime) = (orig.kind, shadower.kind) {
        // lifetime/lifetime shadowing is an error
        struct_span_err!(sess, shadower.span, E0496,
                         "{} name `{}` shadows a \
                          {} name that is already in scope",
                         shadower.kind.desc(), name, orig.kind.desc())
    } else {
        // shadowing involving a label is only a warning, due to issues with
        // labels and lifetimes not being macro-hygienic.
        sess.struct_span_warn(shadower.span,
                              &format!("{} name `{}` shadows a \
                                        {} name that is already in scope",
                                       shadower.kind.desc(), name, orig.kind.desc()))
    };
    err.span_label(orig.span, &"first declared here");
    err.span_label(shadower.span,
                   &format!("lifetime {} already in scope", name));
    err.emit();
}

// Adds all labels in `b` to `ctxt.labels_in_fn`, signalling a warning
// if one of the label shadows a lifetime or another label.
fn extract_labels(ctxt: &mut LifetimeContext, body: &hir::Body) {
    struct GatherLabels<'a, 'tcx: 'a> {
        sess: &'a Session,
        hir_map: &'a Map<'tcx>,
        scope: ScopeRef<'a>,
        labels_in_fn: &'a mut Vec<(ast::Name, Span)>,
    }

    let mut gather = GatherLabels {
        sess: ctxt.sess,
        hir_map: ctxt.hir_map,
        scope: ctxt.scope,
        labels_in_fn: &mut ctxt.labels_in_fn,
    };
    gather.visit_body(body);

    impl<'v, 'a, 'tcx> Visitor<'v> for GatherLabels<'a, 'tcx> {
        fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
            NestedVisitorMap::None
        }

        fn visit_expr(&mut self, ex: &hir::Expr) {
            if let Some((label, label_span)) = expression_label(ex) {
                for &(prior, prior_span) in &self.labels_in_fn[..] {
                    // FIXME (#24278): non-hygienic comparison
                    if label == prior {
                        signal_shadowing_problem(self.sess,
                                                 label,
                                                 original_label(prior_span),
                                                 shadower_label(label_span));
                    }
                }

                check_if_label_shadows_lifetime(self.sess,
                                                self.hir_map,
                                                self.scope,
                                                label,
                                                label_span);

                self.labels_in_fn.push((label, label_span));
            }
            intravisit::walk_expr(self, ex)
        }
    }

    fn expression_label(ex: &hir::Expr) -> Option<(ast::Name, Span)> {
        match ex.node {
            hir::ExprWhile(.., Some(label)) |
            hir::ExprLoop(_, Some(label), _) => Some((label.node, label.span)),
            _ => None,
        }
    }

    fn check_if_label_shadows_lifetime<'a>(sess: &'a Session,
                                           hir_map: &Map,
                                           mut scope: ScopeRef<'a>,
                                           label: ast::Name,
                                           label_span: Span) {
        loop {
            match *scope {
                Scope::Body { s, .. } |
                Scope::Elision { s, .. } |
                Scope::ObjectLifetimeDefault { s, .. } => { scope = s; }

                Scope::Root => { return; }

                Scope::Binder { ref lifetimes, s } => {
                    // FIXME (#24278): non-hygienic comparison
                    if let Some(def) = lifetimes.get(&label) {
                        signal_shadowing_problem(
                            sess,
                            label,
                            original_lifetime(hir_map.span(def.id().unwrap())),
                            shadower_label(label_span));
                        return;
                    }
                    scope = s;
                }
            }
        }
    }
}

fn compute_object_lifetime_defaults(sess: &Session, hir_map: &Map)
                                    -> NodeMap<Vec<ObjectLifetimeDefault>> {
    let mut map = NodeMap();
    for item in hir_map.krate().items.values() {
        match item.node {
            hir::ItemStruct(_, ref generics) |
            hir::ItemUnion(_, ref generics) |
            hir::ItemEnum(_, ref generics) |
            hir::ItemTy(_, ref generics) |
            hir::ItemTrait(_, ref generics, ..) => {
                let result = object_lifetime_defaults_for_item(hir_map, generics);

                // Debugging aid.
                if attr::contains_name(&item.attrs, "rustc_object_lifetime_default") {
                    let object_lifetime_default_reprs: String =
                        result.iter().map(|set| {
                            match *set {
                                Set1::Empty => "BaseDefault".to_string(),
                                Set1::One(Region::Static) => "'static".to_string(),
                                Set1::One(Region::EarlyBound(i, _)) => {
                                    generics.lifetimes[i as usize].lifetime.name.to_string()
                                }
                                Set1::One(_) => bug!(),
                                Set1::Many => "Ambiguous".to_string(),
                            }
                        }).collect::<Vec<String>>().join(",");
                    sess.span_err(item.span, &object_lifetime_default_reprs);
                }

                map.insert(item.id, result);
            }
            _ => {}
        }
    }
    map
}

/// Scan the bounds and where-clauses on parameters to extract bounds
/// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
/// for each type parameter.
fn object_lifetime_defaults_for_item(hir_map: &Map, generics: &hir::Generics)
                                     -> Vec<ObjectLifetimeDefault> {
    fn add_bounds(set: &mut Set1<ast::Name>, bounds: &[hir::TyParamBound]) {
        for bound in bounds {
            if let hir::RegionTyParamBound(ref lifetime) = *bound {
                set.insert(lifetime.name);
            }
        }
    }

    generics.ty_params.iter().map(|param| {
        let mut set = Set1::Empty;

        add_bounds(&mut set, &param.bounds);

        let param_def_id = hir_map.local_def_id(param.id);
        for predicate in &generics.where_clause.predicates {
            // Look for `type: ...` where clauses.
            let data = match *predicate {
                hir::WherePredicate::BoundPredicate(ref data) => data,
                _ => continue
            };

            // Ignore `for<'a> type: ...` as they can change what
            // lifetimes mean (although we could "just" handle it).
            if !data.bound_lifetimes.is_empty() {
                continue;
            }

            let def = match data.bounded_ty.node {
                hir::TyPath(hir::QPath::Resolved(None, ref path)) => path.def,
                _ => continue
            };

            if def == Def::TyParam(param_def_id) {
                add_bounds(&mut set, &data.bounds);
            }
        }

        match set {
            Set1::Empty => Set1::Empty,
            Set1::One(name) => {
                if name == keywords::StaticLifetime.name() {
                    Set1::One(Region::Static)
                } else {
                    generics.lifetimes.iter().enumerate().find(|&(_, def)| {
                        def.lifetime.name == name
                    }).map_or(Set1::Many, |(i, def)| {
                        Set1::One(Region::EarlyBound(i as u32, def.lifetime.id))
                    })
                }
            }
            Set1::Many => Set1::Many
        }
    }).collect()
}

impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
    // FIXME(#37666) this works around a limitation in the region inferencer
    fn hack<F>(&mut self, f: F) where
        F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
    {
        f(self)
    }

    fn with<F>(&mut self, wrap_scope: Scope, f: F) where
        F: for<'b> FnOnce(ScopeRef, &mut LifetimeContext<'b, 'tcx>),
    {
        let LifetimeContext {sess, hir_map, ref mut map, ..} = *self;
        let labels_in_fn = replace(&mut self.labels_in_fn, vec![]);
        let xcrate_object_lifetime_defaults =
            replace(&mut self.xcrate_object_lifetime_defaults, DefIdMap());
        let mut this = LifetimeContext {
            sess: sess,
            hir_map: hir_map,
            map: *map,
            scope: &wrap_scope,
            trait_ref_hack: self.trait_ref_hack,
            labels_in_fn: labels_in_fn,
            xcrate_object_lifetime_defaults: xcrate_object_lifetime_defaults,
        };
        debug!("entering scope {:?}", this.scope);
        f(self.scope, &mut this);
        debug!("exiting scope {:?}", this.scope);
        self.labels_in_fn = this.labels_in_fn;
        self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
    }

    /// Visits self by adding a scope and handling recursive walk over the contents with `walk`.
    ///
    /// Handles visiting fns and methods. These are a bit complicated because we must distinguish
    /// early- vs late-bound lifetime parameters. We do this by checking which lifetimes appear
    /// within type bounds; those are early bound lifetimes, and the rest are late bound.
    ///
    /// For example:
    ///
    ///    fn foo<'a,'b,'c,T:Trait<'b>>(...)
    ///
    /// Here `'a` and `'c` are late bound but `'b` is early bound. Note that early- and late-bound
    /// lifetimes may be interspersed together.
    ///
    /// If early bound lifetimes are present, we separate them into their own list (and likewise
    /// for late bound). They will be numbered sequentially, starting from the lowest index that is
    /// already in scope (for a fn item, that will be 0, but for a method it might not be). Late
    /// bound lifetimes are resolved by name and associated with a binder id (`binder_id`), so the
    /// ordering is not important there.
    fn visit_early_late<F>(&mut self,
                           fn_id: ast::NodeId,
                           parent_id: Option<ast::NodeId>,
                           decl: &'tcx hir::FnDecl,
                           generics: &'tcx hir::Generics,
                           walk: F) where
        F: for<'b, 'c> FnOnce(&'b mut LifetimeContext<'c, 'tcx>),
    {
        let fn_def_id = self.hir_map.local_def_id(fn_id);
        insert_late_bound_lifetimes(self.map,
                                    fn_def_id,
                                    decl,
                                    generics);

        // Find the start of nested early scopes, e.g. in methods.
        let mut index = 0;
        if let Some(parent_id) = parent_id {
            let parent = self.hir_map.expect_item(parent_id);
            if let hir::ItemTrait(..) = parent.node {
                index += 1; // Self comes first.
            }
            match parent.node {
                hir::ItemTrait(_, ref generics, ..) |
                hir::ItemImpl(_, _, ref generics, ..) => {
                    index += (generics.lifetimes.len() + generics.ty_params.len()) as u32;
                }
                _ => {}
            }
        }

        let lifetimes = generics.lifetimes.iter().map(|def| {
            if self.map.late_bound.contains(&def.lifetime.id) {
                Region::late(def)
            } else {
                Region::early(&mut index, def)
            }
        }).collect();

        let scope = Scope::Binder {
            lifetimes: lifetimes,
            s: self.scope
        };
        self.with(scope, move |old_scope, this| {
            this.check_lifetime_defs(old_scope, &generics.lifetimes);
            this.hack(walk); // FIXME(#37666) workaround in place of `walk(this)`
        });
    }

    fn resolve_lifetime_ref(&mut self, lifetime_ref: &hir::Lifetime) {
        // Walk up the scope chain, tracking the number of fn scopes
        // that we pass through, until we find a lifetime with the
        // given name or we run out of scopes.
        // search.
        let mut late_depth = 0;
        let mut scope = self.scope;
        let mut outermost_body = None;
        let result = loop {
            match *scope {
                Scope::Body { id, s } => {
                    outermost_body = Some(id);
                    scope = s;
                }

                Scope::Root => {
                    break None;
                }

                Scope::Binder { ref lifetimes, s } => {
                    if let Some(&def) = lifetimes.get(&lifetime_ref.name) {
                        break Some(def.shifted(late_depth));
                    } else {
                        late_depth += 1;
                        scope = s;
                    }
                }

                Scope::Elision { s, .. } |
                Scope::ObjectLifetimeDefault { s, .. } => {
                    scope = s;
                }
            }
        };

        if let Some(mut def) = result {
            if let Some(body_id) = outermost_body {
                let fn_id = self.hir_map.body_owner(body_id);
                let scope_data = region::CallSiteScopeData {
                    fn_id: fn_id, body_id: body_id.node_id
                };
                match self.hir_map.get(fn_id) {
                    hir::map::NodeItem(&hir::Item {
                        node: hir::ItemFn(..), ..
                    }) |
                    hir::map::NodeTraitItem(&hir::TraitItem {
                        node: hir::TraitItemKind::Method(..), ..
                    }) |
                    hir::map::NodeImplItem(&hir::ImplItem {
                        node: hir::ImplItemKind::Method(..), ..
                    }) => {
                        def = Region::Free(scope_data, def.id().unwrap());
                    }
                    _ => {}
                }
            }
            self.insert_lifetime(lifetime_ref, def);
        } else {
            struct_span_err!(self.sess, lifetime_ref.span, E0261,
                "use of undeclared lifetime name `{}`", lifetime_ref.name)
                .span_label(lifetime_ref.span, &format!("undeclared lifetime"))
                .emit();
        }
    }

    fn visit_segment_parameters(&mut self,
                                def: Def,
                                depth: usize,
                                params: &'tcx hir::PathParameters) {
        let data = match *params {
            hir::ParenthesizedParameters(ref data) => {
                self.visit_fn_like_elision(&data.inputs, data.output.as_ref());
                return;
            }
            hir::AngleBracketedParameters(ref data) => data
        };

        if data.lifetimes.iter().all(|l| l.is_elided()) {
            self.resolve_elided_lifetimes(&data.lifetimes);
        } else {
            for l in &data.lifetimes { self.visit_lifetime(l); }
        }

        // Figure out if this is a type/trait segment,
        // which requires object lifetime defaults.
        let parent_def_id = |this: &mut Self, def_id: DefId| {
            let def_key = if def_id.is_local() {
                this.hir_map.def_key(def_id)
            } else {
                this.sess.cstore.def_key(def_id)
            };
            DefId {
                krate: def_id.krate,
                index: def_key.parent.expect("missing parent")
            }
        };
        let type_def_id = match def {
            Def::AssociatedTy(def_id) if depth == 1 => {
                Some(parent_def_id(self, def_id))
            }
            Def::Variant(def_id) if depth == 0 => {
                Some(parent_def_id(self, def_id))
            }
            Def::Struct(def_id) |
            Def::Union(def_id) |
            Def::Enum(def_id) |
            Def::TyAlias(def_id) |
            Def::Trait(def_id) if depth == 0 => Some(def_id),
            _ => None
        };

        let object_lifetime_defaults = type_def_id.map_or(vec![], |def_id| {
            let in_body = {
                let mut scope = self.scope;
                loop {
                    match *scope {
                        Scope::Root => break false,

                        Scope::Body { .. } => break true,

                        Scope::Binder { s, .. } |
                        Scope::Elision { s, .. } |
                        Scope::ObjectLifetimeDefault { s, .. } => {
                            scope = s;
                        }
                    }
                }
            };

            let map = &self.map;
            let unsubst = if let Some(id) = self.hir_map.as_local_node_id(def_id) {
                &map.object_lifetime_defaults[&id]
            } else {
                let cstore = &self.sess.cstore;
                self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| {
                    cstore.item_generics_cloned(def_id).types.into_iter().map(|def| {
                        def.object_lifetime_default
                    }).collect()
                })
            };
            unsubst.iter().map(|set| {
                match *set {
                    Set1::Empty => {
                        if in_body {
                            None
                        } else {
                            Some(Region::Static)
                        }
                    }
                    Set1::One(r) => r.subst(&data.lifetimes, map),
                    Set1::Many => None
                }
            }).collect()
        });

        for (i, ty) in data.types.iter().enumerate() {
            if let Some(&lt) = object_lifetime_defaults.get(i) {
                let scope = Scope::ObjectLifetimeDefault {
                    lifetime: lt,
                    s: self.scope
                };
                self.with(scope, |_, this| this.visit_ty(ty));
            } else {
                self.visit_ty(ty);
            }
        }

        for b in &data.bindings { self.visit_assoc_type_binding(b); }
    }

    fn visit_fn_like_elision(&mut self, inputs: &'tcx [P<hir::Ty>],
                             output: Option<&'tcx P<hir::Ty>>) {
        let mut arg_elide = Elide::FreshLateAnon(Cell::new(0));
        let arg_scope = Scope::Elision {
            elide: arg_elide.clone(),
            s: self.scope
        };
        self.with(arg_scope, |_, this| {
            for input in inputs {
                this.visit_ty(input);
            }
            match *this.scope {
                Scope::Elision { ref elide, .. } => {
                    arg_elide = elide.clone();
                }
                _ => bug!()
            }
        });

        let output = match output {
            Some(ty) => ty,
            None => return
        };

        // Figure out if there's a body we can get argument names from,
        // and whether there's a `self` argument (treated specially).
        let mut assoc_item_kind = None;
        let mut impl_self = None;
        let parent = self.hir_map.get_parent_node(output.id);
        let body = match self.hir_map.get(parent) {
            // `fn` definitions and methods.
            hir::map::NodeItem(&hir::Item {
                node: hir::ItemFn(.., body), ..
            })  => Some(body),

            hir::map::NodeTraitItem(&hir::TraitItem {
                node: hir::TraitItemKind::Method(_, ref m), ..
            }) => {
                match self.hir_map.expect_item(self.hir_map.get_parent(parent)).node {
                    hir::ItemTrait(.., ref trait_items) => {
                        assoc_item_kind = trait_items.iter().find(|ti| ti.id.node_id == parent)
                                                            .map(|ti| ti.kind);
                    }
                    _ => {}
                }
                match *m {
                    hir::TraitMethod::Required(_) => None,
                    hir::TraitMethod::Provided(body) => Some(body),
                }
            }

            hir::map::NodeImplItem(&hir::ImplItem {
                node: hir::ImplItemKind::Method(_, body), ..
            }) => {
                match self.hir_map.expect_item(self.hir_map.get_parent(parent)).node {
                    hir::ItemImpl(.., ref self_ty, ref impl_items) => {
                        impl_self = Some(self_ty);
                        assoc_item_kind = impl_items.iter().find(|ii| ii.id.node_id == parent)
                                                           .map(|ii| ii.kind);
                    }
                    _ => {}
                }
                Some(body)
            }

            // `fn(...) -> R` and `Trait(...) -> R` (both types and bounds).
            hir::map::NodeTy(_) | hir::map::NodeTraitRef(_) => None,

            // Foreign `fn` decls are terrible because we messed up,
            // and their return types get argument type elision.
            // And now too much code out there is abusing this rule.
            hir::map::NodeForeignItem(_) => {
                let arg_scope = Scope::Elision {
                    elide: arg_elide,
                    s: self.scope
                };
                self.with(arg_scope, |_, this| this.visit_ty(output));
                return;
            }

            // Everything else (only closures?) doesn't
            // actually enjoy elision in return types.
            _ => {
                self.visit_ty(output);
                return;
            }
        };

        let has_self = match assoc_item_kind {
            Some(hir::AssociatedItemKind::Method { has_self }) => has_self,
            _ => false
        };

        // In accordance with the rules for lifetime elision, we can determine
        // what region to use for elision in the output type in two ways.
        // First (determined here), if `self` is by-reference, then the
        // implied output region is the region of the self parameter.
        if has_self {
            // Look for `self: &'a Self` - also desugared from `&'a self`,
            // and if that matches, use it for elision and return early.
            let is_self_ty = |def: Def| {
                if let Def::SelfTy(..) = def {
                    return true;
                }

                // Can't always rely on literal (or implied) `Self` due
                // to the way elision rules were originally specified.
                let impl_self = impl_self.map(|ty| &ty.node);
                if let Some(&hir::TyPath(hir::QPath::Resolved(None, ref path))) = impl_self {
                    match path.def {
                        // Whitelist the types that unambiguously always
                        // result in the same type constructor being used
                        // (it can't differ between `Self` and `self`).
                        Def::Struct(_) |
                        Def::Union(_) |
                        Def::Enum(_) |
                        Def::PrimTy(_) => return def == path.def,
                        _ => {}
                    }
                }

                false
            };

            if let hir::TyRptr(lifetime_ref, ref mt) = inputs[0].node {
                if let hir::TyPath(hir::QPath::Resolved(None, ref path)) = mt.ty.node {
                    if is_self_ty(path.def) {
                        if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.id) {
                            let scope = Scope::Elision {
                                elide: Elide::Exact(lifetime),
                                s: self.scope
                            };
                            self.with(scope, |_, this| this.visit_ty(output));
                            return;
                        }
                    }
                }
            }
        }

        // Second, if there was exactly one lifetime (either a substitution or a
        // reference) in the arguments, then any anonymous regions in the output
        // have that lifetime.
        let mut possible_implied_output_region = None;
        let mut lifetime_count = 0;
        let arg_lifetimes = inputs.iter().enumerate().skip(has_self as usize).map(|(i, input)| {
            let mut gather = GatherLifetimes {
                map: self.map,
                binder_depth: 1,
                have_bound_regions: false,
                lifetimes: FxHashSet()
            };
            gather.visit_ty(input);

            lifetime_count += gather.lifetimes.len();

            if lifetime_count == 1 && gather.lifetimes.len() == 1 {
                // there's a chance that the unique lifetime of this
                // iteration will be the appropriate lifetime for output
                // parameters, so lets store it.
                possible_implied_output_region = gather.lifetimes.iter().cloned().next();
            }

            ElisionFailureInfo {
                parent: body,
                index: i,
                lifetime_count: gather.lifetimes.len(),
                have_bound_regions: gather.have_bound_regions
            }
        }).collect();

        let elide = if lifetime_count == 1 {
            Elide::Exact(possible_implied_output_region.unwrap())
        } else {
            Elide::Error(arg_lifetimes)
        };

        let scope = Scope::Elision {
            elide: elide,
            s: self.scope
        };
        self.with(scope, |_, this| this.visit_ty(output));

        struct GatherLifetimes<'a> {
            map: &'a NamedRegionMap,
            binder_depth: u32,
            have_bound_regions: bool,
            lifetimes: FxHashSet<Region>,
        }

        impl<'v, 'a> Visitor<'v> for GatherLifetimes<'a> {
            fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
                NestedVisitorMap::None
            }

            fn visit_ty(&mut self, ty: &hir::Ty) {
                if let hir::TyBareFn(_) = ty.node {
                    self.binder_depth += 1;
                }
                if let hir::TyTraitObject(ref bounds, ref lifetime) = ty.node {
                    for bound in bounds {
                        self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
                    }

                    // Stay on the safe side and don't include the object
                    // lifetime default (which may not end up being used).
                    if !lifetime.is_elided() {
                        self.visit_lifetime(lifetime);
                    }
                } else {
                    intravisit::walk_ty(self, ty);
                }
                if let hir::TyBareFn(_) = ty.node {
                    self.binder_depth -= 1;
                }
            }

            fn visit_poly_trait_ref(&mut self,
                                    trait_ref: &hir::PolyTraitRef,
                                    modifier: hir::TraitBoundModifier) {
                self.binder_depth += 1;
                intravisit::walk_poly_trait_ref(self, trait_ref, modifier);
                self.binder_depth -= 1;
            }

            fn visit_lifetime_def(&mut self, lifetime_def: &hir::LifetimeDef) {
                for l in &lifetime_def.bounds { self.visit_lifetime(l); }
            }

            fn visit_lifetime(&mut self, lifetime_ref: &hir::Lifetime) {
                if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.id) {
                    match lifetime {
                        Region::LateBound(debruijn, _) |
                        Region::LateBoundAnon(debruijn, _)
                                if debruijn.depth < self.binder_depth => {
                            self.have_bound_regions = true;
                        }
                        _ => {
                            self.lifetimes.insert(lifetime.from_depth(self.binder_depth));
                        }
                    }
                }
            }
        }

    }

    fn resolve_elided_lifetimes(&mut self, lifetime_refs: &[hir::Lifetime]) {
        if lifetime_refs.is_empty() {
            return;
        }

        let span = lifetime_refs[0].span;
        let mut late_depth = 0;
        let mut scope = self.scope;
        let error = loop {
            match *scope {
                // Do not assign any resolution, it will be inferred.
                Scope::Body { .. } => return,

                Scope::Root => break None,

                Scope::Binder { s, .. } => {
                    late_depth += 1;
                    scope = s;
                }

                Scope::Elision { ref elide, .. } => {
                    let lifetime = match *elide {
                        Elide::FreshLateAnon(ref counter) => {
                            for lifetime_ref in lifetime_refs {
                                let lifetime = Region::late_anon(counter).shifted(late_depth);
                                self.insert_lifetime(lifetime_ref, lifetime);
                            }
                            return;
                        }
                        Elide::Exact(l) => l.shifted(late_depth),
                        Elide::Error(ref e) => break Some(e)
                    };
                    for lifetime_ref in lifetime_refs {
                        self.insert_lifetime(lifetime_ref, lifetime);
                    }
                    return;
                }

                Scope::ObjectLifetimeDefault { s, .. } => {
                    scope = s;
                }
            }
        };

        let mut err = struct_span_err!(self.sess, span, E0106,
            "missing lifetime specifier{}",
            if lifetime_refs.len() > 1 { "s" } else { "" });
        let msg = if lifetime_refs.len() > 1 {
            format!("expected {} lifetime parameters", lifetime_refs.len())
        } else {
            format!("expected lifetime parameter")
        };
        err.span_label(span, &msg);

        if let Some(params) = error {
            if lifetime_refs.len() == 1 {
                self.report_elision_failure(&mut err, params);
            }
        }
        err.emit();
    }

    fn report_elision_failure(&mut self,
                              db: &mut DiagnosticBuilder,
                              params: &[ElisionFailureInfo]) {
        let mut m = String::new();
        let len = params.len();

        let elided_params: Vec<_> = params.iter().cloned()
                                          .filter(|info| info.lifetime_count > 0)
                                          .collect();

        let elided_len = elided_params.len();

        for (i, info) in elided_params.into_iter().enumerate() {
            let ElisionFailureInfo {
                parent, index, lifetime_count: n, have_bound_regions
            } = info;

            let help_name = if let Some(body) = parent {
                let arg = &self.hir_map.body(body).arguments[index];
                format!("`{}`", self.hir_map.node_to_pretty_string(arg.pat.id))
            } else {
                format!("argument {}", index + 1)
            };

            m.push_str(&(if n == 1 {
                help_name
            } else {
                format!("one of {}'s {} elided {}lifetimes", help_name, n,
                        if have_bound_regions { "free " } else { "" } )
            })[..]);

            if elided_len == 2 && i == 0 {
                m.push_str(" or ");
            } else if i + 2 == elided_len {
                m.push_str(", or ");
            } else if i != elided_len - 1 {
                m.push_str(", ");
            }

        }

        if len == 0 {
            help!(db,
                  "this function's return type contains a borrowed value, but \
                   there is no value for it to be borrowed from");
            help!(db,
                  "consider giving it a 'static lifetime");
        } else if elided_len == 0 {
            help!(db,
                  "this function's return type contains a borrowed value with \
                   an elided lifetime, but the lifetime cannot be derived from \
                   the arguments");
            help!(db,
                  "consider giving it an explicit bounded or 'static \
                   lifetime");
        } else if elided_len == 1 {
            help!(db,
                  "this function's return type contains a borrowed value, but \
                   the signature does not say which {} it is borrowed from",
                  m);
        } else {
            help!(db,
                  "this function's return type contains a borrowed value, but \
                   the signature does not say whether it is borrowed from {}",
                  m);
        }
    }

    fn resolve_object_lifetime_default(&mut self, lifetime_ref: &hir::Lifetime) {
        let mut late_depth = 0;
        let mut scope = self.scope;
        let lifetime = loop {
            match *scope {
                Scope::Binder { s, .. } => {
                    late_depth += 1;
                    scope = s;
                }

                Scope::Root |
                Scope::Elision { .. } => break Region::Static,

                Scope::Body { .. } |
                Scope::ObjectLifetimeDefault { lifetime: None, .. } => return,

                Scope::ObjectLifetimeDefault { lifetime: Some(l), .. } => break l
            }
        };
        self.insert_lifetime(lifetime_ref, lifetime.shifted(late_depth));
    }

    fn check_lifetime_defs(&mut self, old_scope: ScopeRef, lifetimes: &[hir::LifetimeDef]) {
        for i in 0..lifetimes.len() {
            let lifetime_i = &lifetimes[i];

            for lifetime in lifetimes {
                if lifetime.lifetime.name == keywords::StaticLifetime.name() {
                    let lifetime = lifetime.lifetime;
                    let mut err = struct_span_err!(self.sess, lifetime.span, E0262,
                                  "invalid lifetime parameter name: `{}`", lifetime.name);
                    err.span_label(lifetime.span,
                                   &format!("{} is a reserved lifetime name", lifetime.name));
                    err.emit();
                }
            }

            // It is a hard error to shadow a lifetime within the same scope.
            for j in i + 1..lifetimes.len() {
                let lifetime_j = &lifetimes[j];

                if lifetime_i.lifetime.name == lifetime_j.lifetime.name {
                    struct_span_err!(self.sess, lifetime_j.lifetime.span, E0263,
                                     "lifetime name `{}` declared twice in the same scope",
                                     lifetime_j.lifetime.name)
                        .span_label(lifetime_j.lifetime.span,
                                    &format!("declared twice"))
                        .span_label(lifetime_i.lifetime.span,
                                   &format!("previous declaration here"))
                        .emit();
                }
            }

            // It is a soft error to shadow a lifetime within a parent scope.
            self.check_lifetime_def_for_shadowing(old_scope, &lifetime_i.lifetime);

            for bound in &lifetime_i.bounds {
                self.resolve_lifetime_ref(bound);
            }
        }
    }

    fn check_lifetime_def_for_shadowing(&self,
                                        mut old_scope: ScopeRef,
                                        lifetime: &hir::Lifetime)
    {
        for &(label, label_span) in &self.labels_in_fn {
            // FIXME (#24278): non-hygienic comparison
            if lifetime.name == label {
                signal_shadowing_problem(self.sess,
                                         lifetime.name,
                                         original_label(label_span),
                                         shadower_lifetime(&lifetime));
                return;
            }
        }

        loop {
            match *old_scope {
                Scope::Body { s, .. } |
                Scope::Elision { s, .. } |
                Scope::ObjectLifetimeDefault { s, .. } => {
                    old_scope = s;
                }

                Scope::Root => {
                    return;
                }

                Scope::Binder { ref lifetimes, s } => {
                    if let Some(&def) = lifetimes.get(&lifetime.name) {
                        signal_shadowing_problem(
                            self.sess,
                            lifetime.name,
                            original_lifetime(self.hir_map.span(def.id().unwrap())),
                            shadower_lifetime(&lifetime));
                        return;
                    }

                    old_scope = s;
                }
            }
        }
    }

    fn insert_lifetime(&mut self,
                       lifetime_ref: &hir::Lifetime,
                       def: Region) {
        if lifetime_ref.id == ast::DUMMY_NODE_ID {
            span_bug!(lifetime_ref.span,
                      "lifetime reference not renumbered, \
                       probably a bug in syntax::fold");
        }

        debug!("{} resolved to {:?} span={:?}",
               self.hir_map.node_to_string(lifetime_ref.id),
               def,
               self.sess.codemap().span_to_string(lifetime_ref.span));
        self.map.defs.insert(lifetime_ref.id, def);
    }
}

///////////////////////////////////////////////////////////////////////////

/// Detects late-bound lifetimes and inserts them into
/// `map.late_bound`.
///
/// A region declared on a fn is **late-bound** if:
/// - it is constrained by an argument type;
/// - it does not appear in a where-clause.
///
/// "Constrained" basically means that it appears in any type but
/// not amongst the inputs to a projection.  In other words, `<&'a
/// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`.
fn insert_late_bound_lifetimes(map: &mut NamedRegionMap,
                               fn_def_id: DefId,
                               decl: &hir::FnDecl,
                               generics: &hir::Generics) {
    debug!("insert_late_bound_lifetimes(decl={:?}, generics={:?})", decl, generics);

    let mut constrained_by_input = ConstrainedCollector { regions: FxHashSet() };
    for arg_ty in &decl.inputs {
        constrained_by_input.visit_ty(arg_ty);
    }

    let mut appears_in_output = AllCollector {
        regions: FxHashSet(),
        impl_trait: false
    };
    intravisit::walk_fn_ret_ty(&mut appears_in_output, &decl.output);

    debug!("insert_late_bound_lifetimes: constrained_by_input={:?}",
           constrained_by_input.regions);

    // Walk the lifetimes that appear in where clauses.
    //
    // Subtle point: because we disallow nested bindings, we can just
    // ignore binders here and scrape up all names we see.
    let mut appears_in_where_clause = AllCollector {
        regions: FxHashSet(),
        impl_trait: false
    };
    for ty_param in generics.ty_params.iter() {
        walk_list!(&mut appears_in_where_clause,
                   visit_ty_param_bound,
                   &ty_param.bounds);
    }
    walk_list!(&mut appears_in_where_clause,
               visit_where_predicate,
               &generics.where_clause.predicates);
    for lifetime_def in &generics.lifetimes {
        if !lifetime_def.bounds.is_empty() {
            // `'a: 'b` means both `'a` and `'b` are referenced
            appears_in_where_clause.visit_lifetime_def(lifetime_def);
        }
    }

    debug!("insert_late_bound_lifetimes: appears_in_where_clause={:?}",
           appears_in_where_clause.regions);

    // Late bound regions are those that:
    // - appear in the inputs
    // - do not appear in the where-clauses
    // - are not implicitly captured by `impl Trait`
    for lifetime in &generics.lifetimes {
        let name = lifetime.lifetime.name;

        // appears in the where clauses? early-bound.
        if appears_in_where_clause.regions.contains(&name) { continue; }

        // any `impl Trait` in the return type? early-bound.
        if appears_in_output.impl_trait { continue; }

        // does not appear in the inputs, but appears in the return
        // type? eventually this will be early-bound, but for now we
        // just mark it so we can issue warnings.
        let constrained_by_input = constrained_by_input.regions.contains(&name);
        let appears_in_output = appears_in_output.regions.contains(&name);
        if !constrained_by_input && appears_in_output {
            debug!("inserting issue_32330 entry for {:?}, {:?} on {:?}",
                   lifetime.lifetime.id,
                   name,
                   fn_def_id);
            map.issue_32330.insert(
                lifetime.lifetime.id,
                ty::Issue32330 {
                    fn_def_id: fn_def_id,
                    region_name: name,
                });
            continue;
        }

        debug!("insert_late_bound_lifetimes: \
                lifetime {:?} with id {:?} is late-bound",
               lifetime.lifetime.name, lifetime.lifetime.id);

        let inserted = map.late_bound.insert(lifetime.lifetime.id);
        assert!(inserted, "visited lifetime {:?} twice", lifetime.lifetime.id);
    }

    return;

    struct ConstrainedCollector {
        regions: FxHashSet<ast::Name>,
    }

    impl<'v> Visitor<'v> for ConstrainedCollector {
        fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
            NestedVisitorMap::None
        }

        fn visit_ty(&mut self, ty: &'v hir::Ty) {
            match ty.node {
                hir::TyPath(hir::QPath::Resolved(Some(_), _)) |
                hir::TyPath(hir::QPath::TypeRelative(..)) => {
                    // ignore lifetimes appearing in associated type
                    // projections, as they are not *constrained*
                    // (defined above)
                }

                hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
                    // consider only the lifetimes on the final
                    // segment; I am not sure it's even currently
                    // valid to have them elsewhere, but even if it
                    // is, those would be potentially inputs to
                    // projections
                    if let Some(last_segment) = path.segments.last() {
                        self.visit_path_segment(path.span, last_segment);
                    }
                }

                _ => {
                    intravisit::walk_ty(self, ty);
                }
            }
        }

        fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
            self.regions.insert(lifetime_ref.name);
        }
    }

    struct AllCollector {
        regions: FxHashSet<ast::Name>,
        impl_trait: bool
    }

    impl<'v> Visitor<'v> for AllCollector {
        fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
            NestedVisitorMap::None
        }

        fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
            self.regions.insert(lifetime_ref.name);
        }

        fn visit_ty(&mut self, ty: &hir::Ty) {
            if let hir::TyImplTrait(_) = ty.node {
                self.impl_trait = true;
            }
            intravisit::walk_ty(self, ty);
        }
    }
}