]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/resolve_lifetime.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / librustc / middle / resolve_lifetime.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Name resolution for lifetimes.
12 //!
13 //! Name resolution for lifetimes follows MUCH simpler rules than the
14 //! full resolve. For example, lifetime names are never exported or
15 //! used between functions, and they operate in a purely top-down
16 //! way. Therefore we break lifetime name resolution into a separate pass.
17
18 use hir::map::Map;
19 use hir::def::Def;
20 use hir::def_id::DefId;
21 use middle::cstore::CrateStore;
22 use session::Session;
23 use ty;
24
25 use std::cell::Cell;
26 use std::mem::replace;
27 use syntax::ast;
28 use syntax::attr;
29 use syntax::ptr::P;
30 use syntax_pos::Span;
31 use errors::DiagnosticBuilder;
32 use util::common::ErrorReported;
33 use util::nodemap::{NodeMap, NodeSet, FxHashSet, FxHashMap, DefIdMap};
34 use rustc_back::slice;
35
36 use hir;
37 use hir::intravisit::{self, Visitor, NestedVisitorMap};
38
39 #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
40 pub enum Region {
41 Static,
42 EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
43 LateBound(ty::DebruijnIndex, /* lifetime decl */ DefId),
44 LateBoundAnon(ty::DebruijnIndex, /* anon index */ u32),
45 Free(DefId, /* lifetime decl */ DefId),
46 }
47
48 impl Region {
49 fn early(hir_map: &Map, index: &mut u32, def: &hir::LifetimeDef)
50 -> (hir::LifetimeName, Region)
51 {
52 let i = *index;
53 *index += 1;
54 let def_id = hir_map.local_def_id(def.lifetime.id);
55 (def.lifetime.name, Region::EarlyBound(i, def_id))
56 }
57
58 fn late(hir_map: &Map, def: &hir::LifetimeDef) -> (hir::LifetimeName, Region) {
59 let depth = ty::DebruijnIndex::new(1);
60 let def_id = hir_map.local_def_id(def.lifetime.id);
61 (def.lifetime.name, Region::LateBound(depth, def_id))
62 }
63
64 fn late_anon(index: &Cell<u32>) -> Region {
65 let i = index.get();
66 index.set(i + 1);
67 let depth = ty::DebruijnIndex::new(1);
68 Region::LateBoundAnon(depth, i)
69 }
70
71 fn id(&self) -> Option<DefId> {
72 match *self {
73 Region::Static |
74 Region::LateBoundAnon(..) => None,
75
76 Region::EarlyBound(_, id) |
77 Region::LateBound(_, id) |
78 Region::Free(_, id) => Some(id)
79 }
80 }
81
82 fn shifted(self, amount: u32) -> Region {
83 match self {
84 Region::LateBound(depth, id) => {
85 Region::LateBound(depth.shifted(amount), id)
86 }
87 Region::LateBoundAnon(depth, index) => {
88 Region::LateBoundAnon(depth.shifted(amount), index)
89 }
90 _ => self
91 }
92 }
93
94 fn from_depth(self, depth: u32) -> Region {
95 match self {
96 Region::LateBound(debruijn, id) => {
97 Region::LateBound(ty::DebruijnIndex {
98 depth: debruijn.depth - (depth - 1)
99 }, id)
100 }
101 Region::LateBoundAnon(debruijn, index) => {
102 Region::LateBoundAnon(ty::DebruijnIndex {
103 depth: debruijn.depth - (depth - 1)
104 }, index)
105 }
106 _ => self
107 }
108 }
109
110 fn subst(self, params: &[hir::Lifetime], map: &NamedRegionMap)
111 -> Option<Region> {
112 if let Region::EarlyBound(index, _) = self {
113 params.get(index as usize).and_then(|lifetime| {
114 map.defs.get(&lifetime.id).cloned()
115 })
116 } else {
117 Some(self)
118 }
119 }
120 }
121
122 /// A set containing, at most, one known element.
123 /// If two distinct values are inserted into a set, then it
124 /// becomes `Many`, which can be used to detect ambiguities.
125 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
126 pub enum Set1<T> {
127 Empty,
128 One(T),
129 Many
130 }
131
132 impl<T: PartialEq> Set1<T> {
133 pub fn insert(&mut self, value: T) {
134 if let Set1::Empty = *self {
135 *self = Set1::One(value);
136 return;
137 }
138 if let Set1::One(ref old) = *self {
139 if *old == value {
140 return;
141 }
142 }
143 *self = Set1::Many;
144 }
145 }
146
147 pub type ObjectLifetimeDefault = Set1<Region>;
148
149 // Maps the id of each lifetime reference to the lifetime decl
150 // that it corresponds to.
151 pub struct NamedRegionMap {
152 // maps from every use of a named (not anonymous) lifetime to a
153 // `Region` describing how that region is bound
154 pub defs: NodeMap<Region>,
155
156 // the set of lifetime def ids that are late-bound; a region can
157 // be late-bound if (a) it does NOT appear in a where-clause and
158 // (b) it DOES appear in the arguments.
159 pub late_bound: NodeSet,
160
161 // For each type and trait definition, maps type parameters
162 // to the trait object lifetime defaults computed from them.
163 pub object_lifetime_defaults: NodeMap<Vec<ObjectLifetimeDefault>>,
164 }
165
166 struct LifetimeContext<'a, 'tcx: 'a> {
167 sess: &'a Session,
168 cstore: &'a CrateStore,
169 hir_map: &'a Map<'tcx>,
170 map: &'a mut NamedRegionMap,
171 scope: ScopeRef<'a>,
172 // Deep breath. Our representation for poly trait refs contains a single
173 // binder and thus we only allow a single level of quantification. However,
174 // the syntax of Rust permits quantification in two places, e.g., `T: for <'a> Foo<'a>`
175 // and `for <'a, 'b> &'b T: Foo<'a>`. In order to get the de Bruijn indices
176 // correct when representing these constraints, we should only introduce one
177 // scope. However, we want to support both locations for the quantifier and
178 // during lifetime resolution we want precise information (so we can't
179 // desugar in an earlier phase).
180
181 // SO, if we encounter a quantifier at the outer scope, we set
182 // trait_ref_hack to true (and introduce a scope), and then if we encounter
183 // a quantifier at the inner scope, we error. If trait_ref_hack is false,
184 // then we introduce the scope at the inner quantifier.
185
186 // I'm sorry.
187 trait_ref_hack: bool,
188
189 // List of labels in the function/method currently under analysis.
190 labels_in_fn: Vec<(ast::Name, Span)>,
191
192 // Cache for cross-crate per-definition object lifetime defaults.
193 xcrate_object_lifetime_defaults: DefIdMap<Vec<ObjectLifetimeDefault>>,
194 }
195
196 #[derive(Debug)]
197 enum Scope<'a> {
198 /// Declares lifetimes, and each can be early-bound or late-bound.
199 /// The `DebruijnIndex` of late-bound lifetimes starts at `1` and
200 /// it should be shifted by the number of `Binder`s in between the
201 /// declaration `Binder` and the location it's referenced from.
202 Binder {
203 lifetimes: FxHashMap<hir::LifetimeName, Region>,
204 s: ScopeRef<'a>
205 },
206
207 /// Lifetimes introduced by a fn are scoped to the call-site for that fn,
208 /// if this is a fn body, otherwise the original definitions are used.
209 /// Unspecified lifetimes are inferred, unless an elision scope is nested,
210 /// e.g. `(&T, fn(&T) -> &T);` becomes `(&'_ T, for<'a> fn(&'a T) -> &'a T)`.
211 Body {
212 id: hir::BodyId,
213 s: ScopeRef<'a>
214 },
215
216 /// A scope which either determines unspecified lifetimes or errors
217 /// on them (e.g. due to ambiguity). For more details, see `Elide`.
218 Elision {
219 elide: Elide,
220 s: ScopeRef<'a>
221 },
222
223 /// Use a specific lifetime (if `Some`) or leave it unset (to be
224 /// inferred in a function body or potentially error outside one),
225 /// for the default choice of lifetime in a trait object type.
226 ObjectLifetimeDefault {
227 lifetime: Option<Region>,
228 s: ScopeRef<'a>
229 },
230
231 Root
232 }
233
234 #[derive(Clone, Debug)]
235 enum Elide {
236 /// Use a fresh anonymous late-bound lifetime each time, by
237 /// incrementing the counter to generate sequential indices.
238 FreshLateAnon(Cell<u32>),
239 /// Always use this one lifetime.
240 Exact(Region),
241 /// Less or more than one lifetime were found, error on unspecified.
242 Error(Vec<ElisionFailureInfo>)
243 }
244
245 #[derive(Clone, Debug)]
246 struct ElisionFailureInfo {
247 /// Where we can find the argument pattern.
248 parent: Option<hir::BodyId>,
249 /// The index of the argument in the original definition.
250 index: usize,
251 lifetime_count: usize,
252 have_bound_regions: bool
253 }
254
255 type ScopeRef<'a> = &'a Scope<'a>;
256
257 const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;
258
259 pub fn krate(sess: &Session,
260 cstore: &CrateStore,
261 hir_map: &Map)
262 -> Result<NamedRegionMap, ErrorReported> {
263 let krate = hir_map.krate();
264 let mut map = NamedRegionMap {
265 defs: NodeMap(),
266 late_bound: NodeSet(),
267 object_lifetime_defaults: compute_object_lifetime_defaults(sess, hir_map),
268 };
269 sess.track_errors(|| {
270 let mut visitor = LifetimeContext {
271 sess,
272 cstore,
273 hir_map,
274 map: &mut map,
275 scope: ROOT_SCOPE,
276 trait_ref_hack: false,
277 labels_in_fn: vec![],
278 xcrate_object_lifetime_defaults: DefIdMap(),
279 };
280 for (_, item) in &krate.items {
281 visitor.visit_item(item);
282 }
283 })?;
284 Ok(map)
285 }
286
287 impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
288 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
289 NestedVisitorMap::All(self.hir_map)
290 }
291
292 // We want to nest trait/impl items in their parent, but nothing else.
293 fn visit_nested_item(&mut self, _: hir::ItemId) {}
294
295 fn visit_nested_body(&mut self, body: hir::BodyId) {
296 // Each body has their own set of labels, save labels.
297 let saved = replace(&mut self.labels_in_fn, vec![]);
298 let body = self.hir_map.body(body);
299 extract_labels(self, body);
300 self.with(Scope::Body { id: body.id(), s: self.scope }, |_, this| {
301 this.visit_body(body);
302 });
303 replace(&mut self.labels_in_fn, saved);
304 }
305
306 fn visit_item(&mut self, item: &'tcx hir::Item) {
307 match item.node {
308 hir::ItemFn(ref decl, _, _, _, ref generics, _) => {
309 self.visit_early_late(None, decl, generics, |this| {
310 intravisit::walk_item(this, item);
311 });
312 }
313 hir::ItemExternCrate(_) |
314 hir::ItemUse(..) |
315 hir::ItemMod(..) |
316 hir::ItemAutoImpl(..) |
317 hir::ItemForeignMod(..) |
318 hir::ItemGlobalAsm(..) => {
319 // These sorts of items have no lifetime parameters at all.
320 intravisit::walk_item(self, item);
321 }
322 hir::ItemStatic(..) |
323 hir::ItemConst(..) => {
324 // No lifetime parameters, but implied 'static.
325 let scope = Scope::Elision {
326 elide: Elide::Exact(Region::Static),
327 s: ROOT_SCOPE
328 };
329 self.with(scope, |_, this| intravisit::walk_item(this, item));
330 }
331 hir::ItemTy(_, ref generics) |
332 hir::ItemEnum(_, ref generics) |
333 hir::ItemStruct(_, ref generics) |
334 hir::ItemUnion(_, ref generics) |
335 hir::ItemTrait(_, _, ref generics, ..) |
336 hir::ItemImpl(_, _, _, ref generics, ..) => {
337 // These kinds of items have only early bound lifetime parameters.
338 let mut index = if let hir::ItemTrait(..) = item.node {
339 1 // Self comes before lifetimes
340 } else {
341 0
342 };
343 let lifetimes = generics.lifetimes.iter().map(|def| {
344 Region::early(self.hir_map, &mut index, def)
345 }).collect();
346 let scope = Scope::Binder {
347 lifetimes,
348 s: ROOT_SCOPE
349 };
350 self.with(scope, |old_scope, this| {
351 this.check_lifetime_defs(old_scope, &generics.lifetimes);
352 intravisit::walk_item(this, item);
353 });
354 }
355 }
356 }
357
358 fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
359 match item.node {
360 hir::ForeignItemFn(ref decl, _, ref generics) => {
361 self.visit_early_late(None, decl, generics, |this| {
362 intravisit::walk_foreign_item(this, item);
363 })
364 }
365 hir::ForeignItemStatic(..) => {
366 intravisit::walk_foreign_item(self, item);
367 }
368 hir::ForeignItemType => {
369 intravisit::walk_foreign_item(self, item);
370 }
371 }
372 }
373
374 fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
375 match ty.node {
376 hir::TyBareFn(ref c) => {
377 let scope = Scope::Binder {
378 lifetimes: c.lifetimes.iter().map(|def| {
379 Region::late(self.hir_map, def)
380 }).collect(),
381 s: self.scope
382 };
383 self.with(scope, |old_scope, this| {
384 // a bare fn has no bounds, so everything
385 // contained within is scoped within its binder.
386 this.check_lifetime_defs(old_scope, &c.lifetimes);
387 intravisit::walk_ty(this, ty);
388 });
389 }
390 hir::TyTraitObject(ref bounds, ref lifetime) => {
391 for bound in bounds {
392 self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
393 }
394 if lifetime.is_elided() {
395 self.resolve_object_lifetime_default(lifetime)
396 } else {
397 self.visit_lifetime(lifetime);
398 }
399 }
400 hir::TyRptr(ref lifetime_ref, ref mt) => {
401 self.visit_lifetime(lifetime_ref);
402 let scope = Scope::ObjectLifetimeDefault {
403 lifetime: self.map.defs.get(&lifetime_ref.id).cloned(),
404 s: self.scope
405 };
406 self.with(scope, |_, this| this.visit_ty(&mt.ty));
407 }
408 _ => {
409 intravisit::walk_ty(self, ty)
410 }
411 }
412 }
413
414 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
415 if let hir::TraitItemKind::Method(ref sig, _) = trait_item.node {
416 self.visit_early_late(
417 Some(self.hir_map.get_parent(trait_item.id)),
418 &sig.decl, &trait_item.generics,
419 |this| intravisit::walk_trait_item(this, trait_item))
420 } else {
421 intravisit::walk_trait_item(self, trait_item);
422 }
423 }
424
425 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
426 if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {
427 self.visit_early_late(
428 Some(self.hir_map.get_parent(impl_item.id)),
429 &sig.decl, &impl_item.generics,
430 |this| intravisit::walk_impl_item(this, impl_item))
431 } else {
432 intravisit::walk_impl_item(self, impl_item);
433 }
434 }
435
436 fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
437 if lifetime_ref.is_elided() {
438 self.resolve_elided_lifetimes(slice::ref_slice(lifetime_ref));
439 return;
440 }
441 if lifetime_ref.is_static() {
442 self.insert_lifetime(lifetime_ref, Region::Static);
443 return;
444 }
445 self.resolve_lifetime_ref(lifetime_ref);
446 }
447
448 fn visit_path(&mut self, path: &'tcx hir::Path, _: ast::NodeId) {
449 for (i, segment) in path.segments.iter().enumerate() {
450 let depth = path.segments.len() - i - 1;
451 if let Some(ref parameters) = segment.parameters {
452 self.visit_segment_parameters(path.def, depth, parameters);
453 }
454 }
455 }
456
457 fn visit_fn_decl(&mut self, fd: &'tcx hir::FnDecl) {
458 let output = match fd.output {
459 hir::DefaultReturn(_) => None,
460 hir::Return(ref ty) => Some(ty)
461 };
462 self.visit_fn_like_elision(&fd.inputs, output);
463 }
464
465 fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
466 for ty_param in generics.ty_params.iter() {
467 walk_list!(self, visit_ty_param_bound, &ty_param.bounds);
468 if let Some(ref ty) = ty_param.default {
469 self.visit_ty(&ty);
470 }
471 }
472 for predicate in &generics.where_clause.predicates {
473 match predicate {
474 &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{ ref bounded_ty,
475 ref bounds,
476 ref bound_lifetimes,
477 .. }) => {
478 if !bound_lifetimes.is_empty() {
479 self.trait_ref_hack = true;
480 let scope = Scope::Binder {
481 lifetimes: bound_lifetimes.iter().map(|def| {
482 Region::late(self.hir_map, def)
483 }).collect(),
484 s: self.scope
485 };
486 let result = self.with(scope, |old_scope, this| {
487 this.check_lifetime_defs(old_scope, bound_lifetimes);
488 this.visit_ty(&bounded_ty);
489 walk_list!(this, visit_ty_param_bound, bounds);
490 });
491 self.trait_ref_hack = false;
492 result
493 } else {
494 self.visit_ty(&bounded_ty);
495 walk_list!(self, visit_ty_param_bound, bounds);
496 }
497 }
498 &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{ref lifetime,
499 ref bounds,
500 .. }) => {
501
502 self.visit_lifetime(lifetime);
503 for bound in bounds {
504 self.visit_lifetime(bound);
505 }
506 }
507 &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ref lhs_ty,
508 ref rhs_ty,
509 .. }) => {
510 self.visit_ty(lhs_ty);
511 self.visit_ty(rhs_ty);
512 }
513 }
514 }
515 }
516
517 fn visit_poly_trait_ref(&mut self,
518 trait_ref: &'tcx hir::PolyTraitRef,
519 _modifier: hir::TraitBoundModifier) {
520 debug!("visit_poly_trait_ref trait_ref={:?}", trait_ref);
521
522 if !self.trait_ref_hack || !trait_ref.bound_lifetimes.is_empty() {
523 if self.trait_ref_hack {
524 span_err!(self.sess, trait_ref.span, E0316,
525 "nested quantification of lifetimes");
526 }
527 let scope = Scope::Binder {
528 lifetimes: trait_ref.bound_lifetimes.iter().map(|def| {
529 Region::late(self.hir_map, def)
530 }).collect(),
531 s: self.scope
532 };
533 self.with(scope, |old_scope, this| {
534 this.check_lifetime_defs(old_scope, &trait_ref.bound_lifetimes);
535 for lifetime in &trait_ref.bound_lifetimes {
536 this.visit_lifetime_def(lifetime);
537 }
538 this.visit_trait_ref(&trait_ref.trait_ref)
539 })
540 } else {
541 self.visit_trait_ref(&trait_ref.trait_ref)
542 }
543 }
544 }
545
546 #[derive(Copy, Clone, PartialEq)]
547 enum ShadowKind { Label, Lifetime }
548 struct Original { kind: ShadowKind, span: Span }
549 struct Shadower { kind: ShadowKind, span: Span }
550
551 fn original_label(span: Span) -> Original {
552 Original { kind: ShadowKind::Label, span: span }
553 }
554 fn shadower_label(span: Span) -> Shadower {
555 Shadower { kind: ShadowKind::Label, span: span }
556 }
557 fn original_lifetime(span: Span) -> Original {
558 Original { kind: ShadowKind::Lifetime, span: span }
559 }
560 fn shadower_lifetime(l: &hir::Lifetime) -> Shadower {
561 Shadower { kind: ShadowKind::Lifetime, span: l.span }
562 }
563
564 impl ShadowKind {
565 fn desc(&self) -> &'static str {
566 match *self {
567 ShadowKind::Label => "label",
568 ShadowKind::Lifetime => "lifetime",
569 }
570 }
571 }
572
573 fn signal_shadowing_problem(sess: &Session, name: ast::Name, orig: Original, shadower: Shadower) {
574 let mut err = if let (ShadowKind::Lifetime, ShadowKind::Lifetime) = (orig.kind, shadower.kind) {
575 // lifetime/lifetime shadowing is an error
576 struct_span_err!(sess, shadower.span, E0496,
577 "{} name `{}` shadows a \
578 {} name that is already in scope",
579 shadower.kind.desc(), name, orig.kind.desc())
580 } else {
581 // shadowing involving a label is only a warning, due to issues with
582 // labels and lifetimes not being macro-hygienic.
583 sess.struct_span_warn(shadower.span,
584 &format!("{} name `{}` shadows a \
585 {} name that is already in scope",
586 shadower.kind.desc(), name, orig.kind.desc()))
587 };
588 err.span_label(orig.span, "first declared here");
589 err.span_label(shadower.span,
590 format!("lifetime {} already in scope", name));
591 err.emit();
592 }
593
594 // Adds all labels in `b` to `ctxt.labels_in_fn`, signalling a warning
595 // if one of the label shadows a lifetime or another label.
596 fn extract_labels(ctxt: &mut LifetimeContext, body: &hir::Body) {
597 struct GatherLabels<'a, 'tcx: 'a> {
598 sess: &'a Session,
599 hir_map: &'a Map<'tcx>,
600 scope: ScopeRef<'a>,
601 labels_in_fn: &'a mut Vec<(ast::Name, Span)>,
602 }
603
604 let mut gather = GatherLabels {
605 sess: ctxt.sess,
606 hir_map: ctxt.hir_map,
607 scope: ctxt.scope,
608 labels_in_fn: &mut ctxt.labels_in_fn,
609 };
610 gather.visit_body(body);
611
612 impl<'v, 'a, 'tcx> Visitor<'v> for GatherLabels<'a, 'tcx> {
613 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
614 NestedVisitorMap::None
615 }
616
617 fn visit_expr(&mut self, ex: &hir::Expr) {
618 if let Some((label, label_span)) = expression_label(ex) {
619 for &(prior, prior_span) in &self.labels_in_fn[..] {
620 // FIXME (#24278): non-hygienic comparison
621 if label == prior {
622 signal_shadowing_problem(self.sess,
623 label,
624 original_label(prior_span),
625 shadower_label(label_span));
626 }
627 }
628
629 check_if_label_shadows_lifetime(self.sess,
630 self.hir_map,
631 self.scope,
632 label,
633 label_span);
634
635 self.labels_in_fn.push((label, label_span));
636 }
637 intravisit::walk_expr(self, ex)
638 }
639 }
640
641 fn expression_label(ex: &hir::Expr) -> Option<(ast::Name, Span)> {
642 match ex.node {
643 hir::ExprWhile(.., Some(label)) |
644 hir::ExprLoop(_, Some(label), _) => Some((label.node, label.span)),
645 _ => None,
646 }
647 }
648
649 fn check_if_label_shadows_lifetime<'a>(sess: &'a Session,
650 hir_map: &Map,
651 mut scope: ScopeRef<'a>,
652 label: ast::Name,
653 label_span: Span) {
654 loop {
655 match *scope {
656 Scope::Body { s, .. } |
657 Scope::Elision { s, .. } |
658 Scope::ObjectLifetimeDefault { s, .. } => { scope = s; }
659
660 Scope::Root => { return; }
661
662 Scope::Binder { ref lifetimes, s } => {
663 // FIXME (#24278): non-hygienic comparison
664 if let Some(def) = lifetimes.get(&hir::LifetimeName::Name(label)) {
665 let node_id = hir_map.as_local_node_id(def.id().unwrap())
666 .unwrap();
667
668 signal_shadowing_problem(
669 sess,
670 label,
671 original_lifetime(hir_map.span(node_id)),
672 shadower_label(label_span));
673 return;
674 }
675 scope = s;
676 }
677 }
678 }
679 }
680 }
681
682 fn compute_object_lifetime_defaults(sess: &Session, hir_map: &Map)
683 -> NodeMap<Vec<ObjectLifetimeDefault>> {
684 let mut map = NodeMap();
685 for item in hir_map.krate().items.values() {
686 match item.node {
687 hir::ItemStruct(_, ref generics) |
688 hir::ItemUnion(_, ref generics) |
689 hir::ItemEnum(_, ref generics) |
690 hir::ItemTy(_, ref generics) |
691 hir::ItemTrait(_, _, ref generics, ..) => {
692 let result = object_lifetime_defaults_for_item(hir_map, generics);
693
694 // Debugging aid.
695 if attr::contains_name(&item.attrs, "rustc_object_lifetime_default") {
696 let object_lifetime_default_reprs: String =
697 result.iter().map(|set| {
698 match *set {
699 Set1::Empty => "BaseDefault".to_string(),
700 Set1::One(Region::Static) => "'static".to_string(),
701 Set1::One(Region::EarlyBound(i, _)) => {
702 generics.lifetimes[i as usize].lifetime.name.name().to_string()
703 }
704 Set1::One(_) => bug!(),
705 Set1::Many => "Ambiguous".to_string(),
706 }
707 }).collect::<Vec<String>>().join(",");
708 sess.span_err(item.span, &object_lifetime_default_reprs);
709 }
710
711 map.insert(item.id, result);
712 }
713 _ => {}
714 }
715 }
716 map
717 }
718
719 /// Scan the bounds and where-clauses on parameters to extract bounds
720 /// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
721 /// for each type parameter.
722 fn object_lifetime_defaults_for_item(hir_map: &Map, generics: &hir::Generics)
723 -> Vec<ObjectLifetimeDefault> {
724 fn add_bounds(set: &mut Set1<hir::LifetimeName>, bounds: &[hir::TyParamBound]) {
725 for bound in bounds {
726 if let hir::RegionTyParamBound(ref lifetime) = *bound {
727 set.insert(lifetime.name);
728 }
729 }
730 }
731
732 generics.ty_params.iter().map(|param| {
733 let mut set = Set1::Empty;
734
735 add_bounds(&mut set, &param.bounds);
736
737 let param_def_id = hir_map.local_def_id(param.id);
738 for predicate in &generics.where_clause.predicates {
739 // Look for `type: ...` where clauses.
740 let data = match *predicate {
741 hir::WherePredicate::BoundPredicate(ref data) => data,
742 _ => continue
743 };
744
745 // Ignore `for<'a> type: ...` as they can change what
746 // lifetimes mean (although we could "just" handle it).
747 if !data.bound_lifetimes.is_empty() {
748 continue;
749 }
750
751 let def = match data.bounded_ty.node {
752 hir::TyPath(hir::QPath::Resolved(None, ref path)) => path.def,
753 _ => continue
754 };
755
756 if def == Def::TyParam(param_def_id) {
757 add_bounds(&mut set, &data.bounds);
758 }
759 }
760
761 match set {
762 Set1::Empty => Set1::Empty,
763 Set1::One(name) => {
764 if name == hir::LifetimeName::Static {
765 Set1::One(Region::Static)
766 } else {
767 generics.lifetimes.iter().enumerate().find(|&(_, def)| {
768 def.lifetime.name == name
769 }).map_or(Set1::Many, |(i, def)| {
770 let def_id = hir_map.local_def_id(def.lifetime.id);
771 Set1::One(Region::EarlyBound(i as u32, def_id))
772 })
773 }
774 }
775 Set1::Many => Set1::Many
776 }
777 }).collect()
778 }
779
780 impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
781 // FIXME(#37666) this works around a limitation in the region inferencer
782 fn hack<F>(&mut self, f: F) where
783 F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
784 {
785 f(self)
786 }
787
788 fn with<F>(&mut self, wrap_scope: Scope, f: F) where
789 F: for<'b> FnOnce(ScopeRef, &mut LifetimeContext<'b, 'tcx>),
790 {
791 let LifetimeContext {sess, cstore, hir_map, ref mut map, ..} = *self;
792 let labels_in_fn = replace(&mut self.labels_in_fn, vec![]);
793 let xcrate_object_lifetime_defaults =
794 replace(&mut self.xcrate_object_lifetime_defaults, DefIdMap());
795 let mut this = LifetimeContext {
796 sess,
797 cstore,
798 hir_map,
799 map: *map,
800 scope: &wrap_scope,
801 trait_ref_hack: self.trait_ref_hack,
802 labels_in_fn,
803 xcrate_object_lifetime_defaults,
804 };
805 debug!("entering scope {:?}", this.scope);
806 f(self.scope, &mut this);
807 debug!("exiting scope {:?}", this.scope);
808 self.labels_in_fn = this.labels_in_fn;
809 self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
810 }
811
812 /// Visits self by adding a scope and handling recursive walk over the contents with `walk`.
813 ///
814 /// Handles visiting fns and methods. These are a bit complicated because we must distinguish
815 /// early- vs late-bound lifetime parameters. We do this by checking which lifetimes appear
816 /// within type bounds; those are early bound lifetimes, and the rest are late bound.
817 ///
818 /// For example:
819 ///
820 /// fn foo<'a,'b,'c,T:Trait<'b>>(...)
821 ///
822 /// Here `'a` and `'c` are late bound but `'b` is early bound. Note that early- and late-bound
823 /// lifetimes may be interspersed together.
824 ///
825 /// If early bound lifetimes are present, we separate them into their own list (and likewise
826 /// for late bound). They will be numbered sequentially, starting from the lowest index that is
827 /// already in scope (for a fn item, that will be 0, but for a method it might not be). Late
828 /// bound lifetimes are resolved by name and associated with a binder id (`binder_id`), so the
829 /// ordering is not important there.
830 fn visit_early_late<F>(&mut self,
831 parent_id: Option<ast::NodeId>,
832 decl: &'tcx hir::FnDecl,
833 generics: &'tcx hir::Generics,
834 walk: F) where
835 F: for<'b, 'c> FnOnce(&'b mut LifetimeContext<'c, 'tcx>),
836 {
837 insert_late_bound_lifetimes(self.map, decl, generics);
838
839 // Find the start of nested early scopes, e.g. in methods.
840 let mut index = 0;
841 if let Some(parent_id) = parent_id {
842 let parent = self.hir_map.expect_item(parent_id);
843 if let hir::ItemTrait(..) = parent.node {
844 index += 1; // Self comes first.
845 }
846 match parent.node {
847 hir::ItemTrait(_, _, ref generics, ..) |
848 hir::ItemImpl(_, _, _, ref generics, ..) => {
849 index += (generics.lifetimes.len() + generics.ty_params.len()) as u32;
850 }
851 _ => {}
852 }
853 }
854
855 let lifetimes = generics.lifetimes.iter().map(|def| {
856 if self.map.late_bound.contains(&def.lifetime.id) {
857 Region::late(self.hir_map, def)
858 } else {
859 Region::early(self.hir_map, &mut index, def)
860 }
861 }).collect();
862
863 let scope = Scope::Binder {
864 lifetimes,
865 s: self.scope
866 };
867 self.with(scope, move |old_scope, this| {
868 this.check_lifetime_defs(old_scope, &generics.lifetimes);
869 this.hack(walk); // FIXME(#37666) workaround in place of `walk(this)`
870 });
871 }
872
873 fn resolve_lifetime_ref(&mut self, lifetime_ref: &hir::Lifetime) {
874 // Walk up the scope chain, tracking the number of fn scopes
875 // that we pass through, until we find a lifetime with the
876 // given name or we run out of scopes.
877 // search.
878 let mut late_depth = 0;
879 let mut scope = self.scope;
880 let mut outermost_body = None;
881 let result = loop {
882 match *scope {
883 Scope::Body { id, s } => {
884 outermost_body = Some(id);
885 scope = s;
886 }
887
888 Scope::Root => {
889 break None;
890 }
891
892 Scope::Binder { ref lifetimes, s } => {
893 if let Some(&def) = lifetimes.get(&lifetime_ref.name) {
894 break Some(def.shifted(late_depth));
895 } else {
896 late_depth += 1;
897 scope = s;
898 }
899 }
900
901 Scope::Elision { s, .. } |
902 Scope::ObjectLifetimeDefault { s, .. } => {
903 scope = s;
904 }
905 }
906 };
907
908 if let Some(mut def) = result {
909 if let Region::EarlyBound(..) = def {
910 // Do not free early-bound regions, only late-bound ones.
911 } else if let Some(body_id) = outermost_body {
912 let fn_id = self.hir_map.body_owner(body_id);
913 match self.hir_map.get(fn_id) {
914 hir::map::NodeItem(&hir::Item {
915 node: hir::ItemFn(..), ..
916 }) |
917 hir::map::NodeTraitItem(&hir::TraitItem {
918 node: hir::TraitItemKind::Method(..), ..
919 }) |
920 hir::map::NodeImplItem(&hir::ImplItem {
921 node: hir::ImplItemKind::Method(..), ..
922 }) => {
923 let scope = self.hir_map.local_def_id(fn_id);
924 def = Region::Free(scope, def.id().unwrap());
925 }
926 _ => {}
927 }
928 }
929 self.insert_lifetime(lifetime_ref, def);
930 } else {
931 struct_span_err!(self.sess, lifetime_ref.span, E0261,
932 "use of undeclared lifetime name `{}`", lifetime_ref.name.name())
933 .span_label(lifetime_ref.span, "undeclared lifetime")
934 .emit();
935 }
936 }
937
938 fn visit_segment_parameters(&mut self,
939 def: Def,
940 depth: usize,
941 params: &'tcx hir::PathParameters) {
942 if params.parenthesized {
943 self.visit_fn_like_elision(params.inputs(), Some(&params.bindings[0].ty));
944 return;
945 }
946
947 if params.lifetimes.iter().all(|l| l.is_elided()) {
948 self.resolve_elided_lifetimes(&params.lifetimes);
949 } else {
950 for l in &params.lifetimes { self.visit_lifetime(l); }
951 }
952
953 // Figure out if this is a type/trait segment,
954 // which requires object lifetime defaults.
955 let parent_def_id = |this: &mut Self, def_id: DefId| {
956 let def_key = if def_id.is_local() {
957 this.hir_map.def_key(def_id)
958 } else {
959 this.cstore.def_key(def_id)
960 };
961 DefId {
962 krate: def_id.krate,
963 index: def_key.parent.expect("missing parent")
964 }
965 };
966 let type_def_id = match def {
967 Def::AssociatedTy(def_id) if depth == 1 => {
968 Some(parent_def_id(self, def_id))
969 }
970 Def::Variant(def_id) if depth == 0 => {
971 Some(parent_def_id(self, def_id))
972 }
973 Def::Struct(def_id) |
974 Def::Union(def_id) |
975 Def::Enum(def_id) |
976 Def::TyAlias(def_id) |
977 Def::Trait(def_id) if depth == 0 => Some(def_id),
978 _ => None
979 };
980
981 let object_lifetime_defaults = type_def_id.map_or(vec![], |def_id| {
982 let in_body = {
983 let mut scope = self.scope;
984 loop {
985 match *scope {
986 Scope::Root => break false,
987
988 Scope::Body { .. } => break true,
989
990 Scope::Binder { s, .. } |
991 Scope::Elision { s, .. } |
992 Scope::ObjectLifetimeDefault { s, .. } => {
993 scope = s;
994 }
995 }
996 }
997 };
998
999 let map = &self.map;
1000 let unsubst = if let Some(id) = self.hir_map.as_local_node_id(def_id) {
1001 &map.object_lifetime_defaults[&id]
1002 } else {
1003 let cstore = self.cstore;
1004 let sess = self.sess;
1005 self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| {
1006 cstore.item_generics_cloned_untracked(def_id, sess)
1007 .types
1008 .into_iter()
1009 .map(|def| {
1010 def.object_lifetime_default
1011 }).collect()
1012 })
1013 };
1014 unsubst.iter().map(|set| {
1015 match *set {
1016 Set1::Empty => {
1017 if in_body {
1018 None
1019 } else {
1020 Some(Region::Static)
1021 }
1022 }
1023 Set1::One(r) => r.subst(&params.lifetimes, map),
1024 Set1::Many => None
1025 }
1026 }).collect()
1027 });
1028
1029 for (i, ty) in params.types.iter().enumerate() {
1030 if let Some(&lt) = object_lifetime_defaults.get(i) {
1031 let scope = Scope::ObjectLifetimeDefault {
1032 lifetime: lt,
1033 s: self.scope
1034 };
1035 self.with(scope, |_, this| this.visit_ty(ty));
1036 } else {
1037 self.visit_ty(ty);
1038 }
1039 }
1040
1041 for b in &params.bindings { self.visit_assoc_type_binding(b); }
1042 }
1043
1044 fn visit_fn_like_elision(&mut self, inputs: &'tcx [P<hir::Ty>],
1045 output: Option<&'tcx P<hir::Ty>>) {
1046 let mut arg_elide = Elide::FreshLateAnon(Cell::new(0));
1047 let arg_scope = Scope::Elision {
1048 elide: arg_elide.clone(),
1049 s: self.scope
1050 };
1051 self.with(arg_scope, |_, this| {
1052 for input in inputs {
1053 this.visit_ty(input);
1054 }
1055 match *this.scope {
1056 Scope::Elision { ref elide, .. } => {
1057 arg_elide = elide.clone();
1058 }
1059 _ => bug!()
1060 }
1061 });
1062
1063 let output = match output {
1064 Some(ty) => ty,
1065 None => return
1066 };
1067
1068 // Figure out if there's a body we can get argument names from,
1069 // and whether there's a `self` argument (treated specially).
1070 let mut assoc_item_kind = None;
1071 let mut impl_self = None;
1072 let parent = self.hir_map.get_parent_node(output.id);
1073 let body = match self.hir_map.get(parent) {
1074 // `fn` definitions and methods.
1075 hir::map::NodeItem(&hir::Item {
1076 node: hir::ItemFn(.., body), ..
1077 }) => Some(body),
1078
1079 hir::map::NodeTraitItem(&hir::TraitItem {
1080 node: hir::TraitItemKind::Method(_, ref m), ..
1081 }) => {
1082 match self.hir_map.expect_item(self.hir_map.get_parent(parent)).node {
1083 hir::ItemTrait(.., ref trait_items) => {
1084 assoc_item_kind = trait_items.iter().find(|ti| ti.id.node_id == parent)
1085 .map(|ti| ti.kind);
1086 }
1087 _ => {}
1088 }
1089 match *m {
1090 hir::TraitMethod::Required(_) => None,
1091 hir::TraitMethod::Provided(body) => Some(body),
1092 }
1093 }
1094
1095 hir::map::NodeImplItem(&hir::ImplItem {
1096 node: hir::ImplItemKind::Method(_, body), ..
1097 }) => {
1098 match self.hir_map.expect_item(self.hir_map.get_parent(parent)).node {
1099 hir::ItemImpl(.., ref self_ty, ref impl_items) => {
1100 impl_self = Some(self_ty);
1101 assoc_item_kind = impl_items.iter().find(|ii| ii.id.node_id == parent)
1102 .map(|ii| ii.kind);
1103 }
1104 _ => {}
1105 }
1106 Some(body)
1107 }
1108
1109 // Foreign functions, `fn(...) -> R` and `Trait(...) -> R` (both types and bounds).
1110 hir::map::NodeForeignItem(_) | hir::map::NodeTy(_) | hir::map::NodeTraitRef(_) => None,
1111
1112 // Everything else (only closures?) doesn't
1113 // actually enjoy elision in return types.
1114 _ => {
1115 self.visit_ty(output);
1116 return;
1117 }
1118 };
1119
1120 let has_self = match assoc_item_kind {
1121 Some(hir::AssociatedItemKind::Method { has_self }) => has_self,
1122 _ => false
1123 };
1124
1125 // In accordance with the rules for lifetime elision, we can determine
1126 // what region to use for elision in the output type in two ways.
1127 // First (determined here), if `self` is by-reference, then the
1128 // implied output region is the region of the self parameter.
1129 if has_self {
1130 // Look for `self: &'a Self` - also desugared from `&'a self`,
1131 // and if that matches, use it for elision and return early.
1132 let is_self_ty = |def: Def| {
1133 if let Def::SelfTy(..) = def {
1134 return true;
1135 }
1136
1137 // Can't always rely on literal (or implied) `Self` due
1138 // to the way elision rules were originally specified.
1139 let impl_self = impl_self.map(|ty| &ty.node);
1140 if let Some(&hir::TyPath(hir::QPath::Resolved(None, ref path))) = impl_self {
1141 match path.def {
1142 // Whitelist the types that unambiguously always
1143 // result in the same type constructor being used
1144 // (it can't differ between `Self` and `self`).
1145 Def::Struct(_) |
1146 Def::Union(_) |
1147 Def::Enum(_) |
1148 Def::PrimTy(_) => return def == path.def,
1149 _ => {}
1150 }
1151 }
1152
1153 false
1154 };
1155
1156 if let hir::TyRptr(lifetime_ref, ref mt) = inputs[0].node {
1157 if let hir::TyPath(hir::QPath::Resolved(None, ref path)) = mt.ty.node {
1158 if is_self_ty(path.def) {
1159 if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.id) {
1160 let scope = Scope::Elision {
1161 elide: Elide::Exact(lifetime),
1162 s: self.scope
1163 };
1164 self.with(scope, |_, this| this.visit_ty(output));
1165 return;
1166 }
1167 }
1168 }
1169 }
1170 }
1171
1172 // Second, if there was exactly one lifetime (either a substitution or a
1173 // reference) in the arguments, then any anonymous regions in the output
1174 // have that lifetime.
1175 let mut possible_implied_output_region = None;
1176 let mut lifetime_count = 0;
1177 let arg_lifetimes = inputs.iter().enumerate().skip(has_self as usize).map(|(i, input)| {
1178 let mut gather = GatherLifetimes {
1179 map: self.map,
1180 binder_depth: 1,
1181 have_bound_regions: false,
1182 lifetimes: FxHashSet()
1183 };
1184 gather.visit_ty(input);
1185
1186 lifetime_count += gather.lifetimes.len();
1187
1188 if lifetime_count == 1 && gather.lifetimes.len() == 1 {
1189 // there's a chance that the unique lifetime of this
1190 // iteration will be the appropriate lifetime for output
1191 // parameters, so lets store it.
1192 possible_implied_output_region = gather.lifetimes.iter().cloned().next();
1193 }
1194
1195 ElisionFailureInfo {
1196 parent: body,
1197 index: i,
1198 lifetime_count: gather.lifetimes.len(),
1199 have_bound_regions: gather.have_bound_regions
1200 }
1201 }).collect();
1202
1203 let elide = if lifetime_count == 1 {
1204 Elide::Exact(possible_implied_output_region.unwrap())
1205 } else {
1206 Elide::Error(arg_lifetimes)
1207 };
1208
1209 let scope = Scope::Elision {
1210 elide,
1211 s: self.scope
1212 };
1213 self.with(scope, |_, this| this.visit_ty(output));
1214
1215 struct GatherLifetimes<'a> {
1216 map: &'a NamedRegionMap,
1217 binder_depth: u32,
1218 have_bound_regions: bool,
1219 lifetimes: FxHashSet<Region>,
1220 }
1221
1222 impl<'v, 'a> Visitor<'v> for GatherLifetimes<'a> {
1223 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
1224 NestedVisitorMap::None
1225 }
1226
1227 fn visit_ty(&mut self, ty: &hir::Ty) {
1228 if let hir::TyBareFn(_) = ty.node {
1229 self.binder_depth += 1;
1230 }
1231 if let hir::TyTraitObject(ref bounds, ref lifetime) = ty.node {
1232 for bound in bounds {
1233 self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
1234 }
1235
1236 // Stay on the safe side and don't include the object
1237 // lifetime default (which may not end up being used).
1238 if !lifetime.is_elided() {
1239 self.visit_lifetime(lifetime);
1240 }
1241 } else {
1242 intravisit::walk_ty(self, ty);
1243 }
1244 if let hir::TyBareFn(_) = ty.node {
1245 self.binder_depth -= 1;
1246 }
1247 }
1248
1249 fn visit_poly_trait_ref(&mut self,
1250 trait_ref: &hir::PolyTraitRef,
1251 modifier: hir::TraitBoundModifier) {
1252 self.binder_depth += 1;
1253 intravisit::walk_poly_trait_ref(self, trait_ref, modifier);
1254 self.binder_depth -= 1;
1255 }
1256
1257 fn visit_lifetime_def(&mut self, lifetime_def: &hir::LifetimeDef) {
1258 for l in &lifetime_def.bounds { self.visit_lifetime(l); }
1259 }
1260
1261 fn visit_lifetime(&mut self, lifetime_ref: &hir::Lifetime) {
1262 if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.id) {
1263 match lifetime {
1264 Region::LateBound(debruijn, _) |
1265 Region::LateBoundAnon(debruijn, _)
1266 if debruijn.depth < self.binder_depth => {
1267 self.have_bound_regions = true;
1268 }
1269 _ => {
1270 self.lifetimes.insert(lifetime.from_depth(self.binder_depth));
1271 }
1272 }
1273 }
1274 }
1275 }
1276
1277 }
1278
1279 fn resolve_elided_lifetimes(&mut self, lifetime_refs: &[hir::Lifetime]) {
1280 if lifetime_refs.is_empty() {
1281 return;
1282 }
1283
1284 let span = lifetime_refs[0].span;
1285 let mut late_depth = 0;
1286 let mut scope = self.scope;
1287 let error = loop {
1288 match *scope {
1289 // Do not assign any resolution, it will be inferred.
1290 Scope::Body { .. } => return,
1291
1292 Scope::Root => break None,
1293
1294 Scope::Binder { s, .. } => {
1295 late_depth += 1;
1296 scope = s;
1297 }
1298
1299 Scope::Elision { ref elide, .. } => {
1300 let lifetime = match *elide {
1301 Elide::FreshLateAnon(ref counter) => {
1302 for lifetime_ref in lifetime_refs {
1303 let lifetime = Region::late_anon(counter).shifted(late_depth);
1304 self.insert_lifetime(lifetime_ref, lifetime);
1305 }
1306 return;
1307 }
1308 Elide::Exact(l) => l.shifted(late_depth),
1309 Elide::Error(ref e) => break Some(e)
1310 };
1311 for lifetime_ref in lifetime_refs {
1312 self.insert_lifetime(lifetime_ref, lifetime);
1313 }
1314 return;
1315 }
1316
1317 Scope::ObjectLifetimeDefault { s, .. } => {
1318 scope = s;
1319 }
1320 }
1321 };
1322
1323 let mut err = struct_span_err!(self.sess, span, E0106,
1324 "missing lifetime specifier{}",
1325 if lifetime_refs.len() > 1 { "s" } else { "" });
1326 let msg = if lifetime_refs.len() > 1 {
1327 format!("expected {} lifetime parameters", lifetime_refs.len())
1328 } else {
1329 format!("expected lifetime parameter")
1330 };
1331 err.span_label(span, msg);
1332
1333 if let Some(params) = error {
1334 if lifetime_refs.len() == 1 {
1335 self.report_elision_failure(&mut err, params);
1336 }
1337 }
1338 err.emit();
1339 }
1340
1341 fn report_elision_failure(&mut self,
1342 db: &mut DiagnosticBuilder,
1343 params: &[ElisionFailureInfo]) {
1344 let mut m = String::new();
1345 let len = params.len();
1346
1347 let elided_params: Vec<_> = params.iter().cloned()
1348 .filter(|info| info.lifetime_count > 0)
1349 .collect();
1350
1351 let elided_len = elided_params.len();
1352
1353 for (i, info) in elided_params.into_iter().enumerate() {
1354 let ElisionFailureInfo {
1355 parent, index, lifetime_count: n, have_bound_regions
1356 } = info;
1357
1358 let help_name = if let Some(body) = parent {
1359 let arg = &self.hir_map.body(body).arguments[index];
1360 format!("`{}`", self.hir_map.node_to_pretty_string(arg.pat.id))
1361 } else {
1362 format!("argument {}", index + 1)
1363 };
1364
1365 m.push_str(&(if n == 1 {
1366 help_name
1367 } else {
1368 format!("one of {}'s {} {}lifetimes", help_name, n,
1369 if have_bound_regions { "free " } else { "" } )
1370 })[..]);
1371
1372 if elided_len == 2 && i == 0 {
1373 m.push_str(" or ");
1374 } else if i + 2 == elided_len {
1375 m.push_str(", or ");
1376 } else if i != elided_len - 1 {
1377 m.push_str(", ");
1378 }
1379
1380 }
1381
1382 if len == 0 {
1383 help!(db,
1384 "this function's return type contains a borrowed value, but \
1385 there is no value for it to be borrowed from");
1386 help!(db,
1387 "consider giving it a 'static lifetime");
1388 } else if elided_len == 0 {
1389 help!(db,
1390 "this function's return type contains a borrowed value with \
1391 an elided lifetime, but the lifetime cannot be derived from \
1392 the arguments");
1393 help!(db,
1394 "consider giving it an explicit bounded or 'static \
1395 lifetime");
1396 } else if elided_len == 1 {
1397 help!(db,
1398 "this function's return type contains a borrowed value, but \
1399 the signature does not say which {} it is borrowed from",
1400 m);
1401 } else {
1402 help!(db,
1403 "this function's return type contains a borrowed value, but \
1404 the signature does not say whether it is borrowed from {}",
1405 m);
1406 }
1407 }
1408
1409 fn resolve_object_lifetime_default(&mut self, lifetime_ref: &hir::Lifetime) {
1410 let mut late_depth = 0;
1411 let mut scope = self.scope;
1412 let lifetime = loop {
1413 match *scope {
1414 Scope::Binder { s, .. } => {
1415 late_depth += 1;
1416 scope = s;
1417 }
1418
1419 Scope::Root |
1420 Scope::Elision { .. } => break Region::Static,
1421
1422 Scope::Body { .. } |
1423 Scope::ObjectLifetimeDefault { lifetime: None, .. } => return,
1424
1425 Scope::ObjectLifetimeDefault { lifetime: Some(l), .. } => break l
1426 }
1427 };
1428 self.insert_lifetime(lifetime_ref, lifetime.shifted(late_depth));
1429 }
1430
1431 fn check_lifetime_defs(&mut self, old_scope: ScopeRef, lifetimes: &[hir::LifetimeDef]) {
1432 for i in 0..lifetimes.len() {
1433 let lifetime_i = &lifetimes[i];
1434
1435 for lifetime in lifetimes {
1436 match lifetime.lifetime.name {
1437 hir::LifetimeName::Static | hir::LifetimeName::Underscore => {
1438 let lifetime = lifetime.lifetime;
1439 let name = lifetime.name.name();
1440 let mut err = struct_span_err!(self.sess, lifetime.span, E0262,
1441 "invalid lifetime parameter name: `{}`", name);
1442 err.span_label(lifetime.span,
1443 format!("{} is a reserved lifetime name", name));
1444 err.emit();
1445 }
1446 hir::LifetimeName::Implicit | hir::LifetimeName::Name(_) => {}
1447 }
1448 }
1449
1450 // It is a hard error to shadow a lifetime within the same scope.
1451 for j in i + 1..lifetimes.len() {
1452 let lifetime_j = &lifetimes[j];
1453
1454 if lifetime_i.lifetime.name == lifetime_j.lifetime.name {
1455 struct_span_err!(self.sess, lifetime_j.lifetime.span, E0263,
1456 "lifetime name `{}` declared twice in the same scope",
1457 lifetime_j.lifetime.name.name())
1458 .span_label(lifetime_j.lifetime.span,
1459 "declared twice")
1460 .span_label(lifetime_i.lifetime.span,
1461 "previous declaration here")
1462 .emit();
1463 }
1464 }
1465
1466 // It is a soft error to shadow a lifetime within a parent scope.
1467 self.check_lifetime_def_for_shadowing(old_scope, &lifetime_i.lifetime);
1468
1469 for bound in &lifetime_i.bounds {
1470 match bound.name {
1471 hir::LifetimeName::Underscore => {
1472 let mut err = struct_span_err!(self.sess, bound.span, E0637,
1473 "invalid lifetime bound name: `'_`");
1474 err.span_label(bound.span, "`'_` is a reserved lifetime name");
1475 err.emit();
1476 }
1477 hir::LifetimeName::Static => {
1478 self.insert_lifetime(bound, Region::Static);
1479 self.sess.struct_span_warn(lifetime_i.lifetime.span.to(bound.span),
1480 &format!("unnecessary lifetime parameter `{}`",
1481 lifetime_i.lifetime.name.name()))
1482 .help(&format!(
1483 "you can use the `'static` lifetime directly, in place \
1484 of `{}`", lifetime_i.lifetime.name.name()))
1485 .emit();
1486 }
1487 hir::LifetimeName::Implicit |
1488 hir::LifetimeName::Name(_) => {
1489 self.resolve_lifetime_ref(bound);
1490 }
1491 }
1492 }
1493 }
1494 }
1495
1496 fn check_lifetime_def_for_shadowing(&self,
1497 mut old_scope: ScopeRef,
1498 lifetime: &hir::Lifetime)
1499 {
1500 for &(label, label_span) in &self.labels_in_fn {
1501 // FIXME (#24278): non-hygienic comparison
1502 if lifetime.name.name() == label {
1503 signal_shadowing_problem(self.sess,
1504 label,
1505 original_label(label_span),
1506 shadower_lifetime(&lifetime));
1507 return;
1508 }
1509 }
1510
1511 loop {
1512 match *old_scope {
1513 Scope::Body { s, .. } |
1514 Scope::Elision { s, .. } |
1515 Scope::ObjectLifetimeDefault { s, .. } => {
1516 old_scope = s;
1517 }
1518
1519 Scope::Root => {
1520 return;
1521 }
1522
1523 Scope::Binder { ref lifetimes, s } => {
1524 if let Some(&def) = lifetimes.get(&lifetime.name) {
1525 let node_id = self.hir_map
1526 .as_local_node_id(def.id().unwrap())
1527 .unwrap();
1528
1529 signal_shadowing_problem(
1530 self.sess,
1531 lifetime.name.name(),
1532 original_lifetime(self.hir_map.span(node_id)),
1533 shadower_lifetime(&lifetime));
1534 return;
1535 }
1536
1537 old_scope = s;
1538 }
1539 }
1540 }
1541 }
1542
1543 fn insert_lifetime(&mut self,
1544 lifetime_ref: &hir::Lifetime,
1545 def: Region) {
1546 if lifetime_ref.id == ast::DUMMY_NODE_ID {
1547 span_bug!(lifetime_ref.span,
1548 "lifetime reference not renumbered, \
1549 probably a bug in syntax::fold");
1550 }
1551
1552 debug!("{} resolved to {:?} span={:?}",
1553 self.hir_map.node_to_string(lifetime_ref.id),
1554 def,
1555 self.sess.codemap().span_to_string(lifetime_ref.span));
1556 self.map.defs.insert(lifetime_ref.id, def);
1557 }
1558 }
1559
1560 ///////////////////////////////////////////////////////////////////////////
1561
1562 /// Detects late-bound lifetimes and inserts them into
1563 /// `map.late_bound`.
1564 ///
1565 /// A region declared on a fn is **late-bound** if:
1566 /// - it is constrained by an argument type;
1567 /// - it does not appear in a where-clause.
1568 ///
1569 /// "Constrained" basically means that it appears in any type but
1570 /// not amongst the inputs to a projection. In other words, `<&'a
1571 /// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`.
1572 fn insert_late_bound_lifetimes(map: &mut NamedRegionMap,
1573 decl: &hir::FnDecl,
1574 generics: &hir::Generics) {
1575 debug!("insert_late_bound_lifetimes(decl={:?}, generics={:?})", decl, generics);
1576
1577 let mut constrained_by_input = ConstrainedCollector { regions: FxHashSet() };
1578 for arg_ty in &decl.inputs {
1579 constrained_by_input.visit_ty(arg_ty);
1580 }
1581
1582 let mut appears_in_output = AllCollector {
1583 regions: FxHashSet(),
1584 impl_trait: false
1585 };
1586 intravisit::walk_fn_ret_ty(&mut appears_in_output, &decl.output);
1587
1588 debug!("insert_late_bound_lifetimes: constrained_by_input={:?}",
1589 constrained_by_input.regions);
1590
1591 // Walk the lifetimes that appear in where clauses.
1592 //
1593 // Subtle point: because we disallow nested bindings, we can just
1594 // ignore binders here and scrape up all names we see.
1595 let mut appears_in_where_clause = AllCollector {
1596 regions: FxHashSet(),
1597 impl_trait: false
1598 };
1599 for ty_param in generics.ty_params.iter() {
1600 walk_list!(&mut appears_in_where_clause,
1601 visit_ty_param_bound,
1602 &ty_param.bounds);
1603 }
1604 walk_list!(&mut appears_in_where_clause,
1605 visit_where_predicate,
1606 &generics.where_clause.predicates);
1607 // We need to collect argument impl Trait lifetimes as well,
1608 // we do so here.
1609 walk_list!(&mut appears_in_where_clause,
1610 visit_ty,
1611 decl.inputs.iter().filter(|ty| {
1612 if let hir::TyImplTraitUniversal(..) = ty.node {
1613 true
1614 } else {
1615 false
1616 }
1617 }));
1618 for lifetime_def in &generics.lifetimes {
1619 if !lifetime_def.bounds.is_empty() {
1620 // `'a: 'b` means both `'a` and `'b` are referenced
1621 appears_in_where_clause.visit_lifetime_def(lifetime_def);
1622 }
1623 }
1624
1625 debug!("insert_late_bound_lifetimes: appears_in_where_clause={:?}",
1626 appears_in_where_clause.regions);
1627
1628 // Late bound regions are those that:
1629 // - appear in the inputs
1630 // - do not appear in the where-clauses
1631 // - are not implicitly captured by `impl Trait`
1632 for lifetime in &generics.lifetimes {
1633 let name = lifetime.lifetime.name;
1634
1635 // appears in the where clauses? early-bound.
1636 if appears_in_where_clause.regions.contains(&name) { continue; }
1637
1638 // any `impl Trait` in the return type? early-bound.
1639 if appears_in_output.impl_trait { continue; }
1640
1641 // does not appear in the inputs, but appears in the return type? early-bound.
1642 if !constrained_by_input.regions.contains(&name) &&
1643 appears_in_output.regions.contains(&name) {
1644 continue;
1645 }
1646
1647 debug!("insert_late_bound_lifetimes: \
1648 lifetime {:?} with id {:?} is late-bound",
1649 lifetime.lifetime.name, lifetime.lifetime.id);
1650
1651 let inserted = map.late_bound.insert(lifetime.lifetime.id);
1652 assert!(inserted, "visited lifetime {:?} twice", lifetime.lifetime.id);
1653 }
1654
1655 return;
1656
1657 struct ConstrainedCollector {
1658 regions: FxHashSet<hir::LifetimeName>,
1659 }
1660
1661 impl<'v> Visitor<'v> for ConstrainedCollector {
1662 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
1663 NestedVisitorMap::None
1664 }
1665
1666 fn visit_ty(&mut self, ty: &'v hir::Ty) {
1667 match ty.node {
1668 hir::TyPath(hir::QPath::Resolved(Some(_), _)) |
1669 hir::TyPath(hir::QPath::TypeRelative(..)) => {
1670 // ignore lifetimes appearing in associated type
1671 // projections, as they are not *constrained*
1672 // (defined above)
1673 }
1674
1675 hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
1676 // consider only the lifetimes on the final
1677 // segment; I am not sure it's even currently
1678 // valid to have them elsewhere, but even if it
1679 // is, those would be potentially inputs to
1680 // projections
1681 if let Some(last_segment) = path.segments.last() {
1682 self.visit_path_segment(path.span, last_segment);
1683 }
1684 }
1685
1686 _ => {
1687 intravisit::walk_ty(self, ty);
1688 }
1689 }
1690 }
1691
1692 fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
1693 self.regions.insert(lifetime_ref.name);
1694 }
1695 }
1696
1697 struct AllCollector {
1698 regions: FxHashSet<hir::LifetimeName>,
1699 impl_trait: bool
1700 }
1701
1702 impl<'v> Visitor<'v> for AllCollector {
1703 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
1704 NestedVisitorMap::None
1705 }
1706
1707 fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
1708 self.regions.insert(lifetime_ref.name);
1709 }
1710
1711 fn visit_ty(&mut self, ty: &hir::Ty) {
1712 if let hir::TyImplTraitExistential(_) = ty.node {
1713 self.impl_trait = true;
1714 }
1715 intravisit::walk_ty(self, ty);
1716 }
1717 }
1718 }