diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 671604918f7f..b22ea3dceb36 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -18,8 +18,8 @@ use hir::def::Def; use hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use hir::map::Map; -use hir::{GenericArg, GenericParam, ItemLocalId, LifetimeName, ParamName, Node}; -use ty::{self, TyCtxt, DefIdTree, GenericParamDefKind}; +use hir::{GenericArg, GenericParam, ItemLocalId, LifetimeName, Node, ParamName}; +use ty::{self, DefIdTree, GenericParamDefKind, TyCtxt}; use errors::{Applicability, DiagnosticBuilder}; use rustc::lint; @@ -54,14 +54,12 @@ pub enum LifetimeDefOrigin { impl LifetimeDefOrigin { fn from_param(param: &GenericParam) -> Self { match param.kind { - GenericParamKind::Lifetime { kind } => { - match kind { - LifetimeParamKind::InBand => LifetimeDefOrigin::InBand, - LifetimeParamKind::Explicit => LifetimeDefOrigin::ExplicitOrElided, - LifetimeParamKind::Elided => LifetimeDefOrigin::ExplicitOrElided, - LifetimeParamKind::Error => LifetimeDefOrigin::Error, - } - } + GenericParamKind::Lifetime { kind } => match kind { + LifetimeParamKind::InBand => LifetimeDefOrigin::InBand, + LifetimeParamKind::Explicit => LifetimeDefOrigin::ExplicitOrElided, + LifetimeParamKind::Elided => LifetimeDefOrigin::ExplicitOrElided, + LifetimeParamKind::Error => LifetimeDefOrigin::Error, + }, _ => bug!("expected a lifetime param"), } } @@ -107,12 +105,12 @@ impl Region { let origin = LifetimeDefOrigin::from_param(param); debug!( "Region::late: param={:?} depth={:?} def_id={:?} origin={:?}", - param, - depth, - def_id, - origin, + param, depth, def_id, origin, ); - (param.name.modern(), Region::LateBound(depth, def_id, origin)) + ( + param.name.modern(), + Region::LateBound(depth, def_id, origin), + ) } fn late_anon(index: &Cell) -> Region { @@ -146,23 +144,24 @@ impl Region { fn shifted_out_to_binder(self, binder: ty::DebruijnIndex) -> Region { match self { - Region::LateBound(debruijn, id, origin) => Region::LateBound( - debruijn.shifted_out_to_binder(binder), - id, - origin, - ), - Region::LateBoundAnon(debruijn, index) => Region::LateBoundAnon( - debruijn.shifted_out_to_binder(binder), - index, - ), + Region::LateBound(debruijn, id, origin) => { + Region::LateBound(debruijn.shifted_out_to_binder(binder), id, origin) + } + Region::LateBoundAnon(debruijn, index) => { + Region::LateBoundAnon(debruijn.shifted_out_to_binder(binder), index) + } _ => self, } } fn subst<'a, L>(self, mut params: L, map: &NamedRegionMap) -> Option - where L: Iterator { + where + L: Iterator, + { if let Region::EarlyBound(index, _, _) = self { - params.nth(index as usize).and_then(|lifetime| map.defs.get(&lifetime.id).cloned()) + params + .nth(index as usize) + .and_then(|lifetime| map.defs.get(&lifetime.id).cloned()) } else { Some(self) } @@ -364,17 +363,17 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) { is_late_bound_map: |tcx, id| { let id = LocalDefId::from_def_id(DefId::local(id)); // (*) tcx.resolve_lifetimes(LOCAL_CRATE) - .late_bound - .get(&id) - .cloned() + .late_bound + .get(&id) + .cloned() }, object_lifetime_defaults_map: |tcx, id| { let id = LocalDefId::from_def_id(DefId::local(id)); // (*) tcx.resolve_lifetimes(LOCAL_CRATE) - .object_lifetime_defaults - .get(&id) - .cloned() + .object_lifetime_defaults + .get(&id) + .cloned() }, ..*providers @@ -408,14 +407,16 @@ fn resolve_lifetimes<'tcx>( } for k in named_region_map.late_bound { let hir_id = tcx.hir.node_to_hir_id(k); - let map = rl.late_bound.entry(hir_id.owner_local_def_id()).or_default(); + let map = rl.late_bound + .entry(hir_id.owner_local_def_id()) + .or_default(); Lrc::get_mut(map).unwrap().insert(hir_id.local_id); } for (k, v) in named_region_map.object_lifetime_defaults { let hir_id = tcx.hir.node_to_hir_id(k); let map = rl.object_lifetime_defaults - .entry(hir_id.owner_local_def_id()) - .or_default(); + .entry(hir_id.owner_local_def_id()) + .or_default(); Lrc::get_mut(map) .unwrap() .insert(hir_id.local_id, Lrc::new(v)); @@ -498,13 +499,20 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { }; self.with(scope, |_, this| intravisit::walk_item(this, item)); } - hir::ItemKind::Existential(hir::ExistTy { impl_trait_fn: Some(_), .. }) => { + hir::ItemKind::Existential(hir::ExistTy { + impl_trait_fn: Some(_), + .. + }) => { // currently existential type declarations are just generated from impl Trait // items. doing anything on this node is irrelevant, as we currently don't need // it. } hir::ItemKind::Ty(_, ref generics) - | hir::ItemKind::Existential(hir::ExistTy { impl_trait_fn: None, ref generics, .. }) + | hir::ItemKind::Existential(hir::ExistTy { + impl_trait_fn: None, + ref generics, + .. + }) | hir::ItemKind::Enum(_, ref generics) | hir::ItemKind::Struct(_, ref generics) | hir::ItemKind::Union(_, ref generics) @@ -524,15 +532,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { 0 }; let mut type_count = 0; - let lifetimes = generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::early(&self.tcx.hir, &mut index, param)) - } - GenericParamKind::Type { .. } => { - type_count += 1; - None - } - }).collect(); + let lifetimes = generics + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + Some(Region::early(&self.tcx.hir, &mut index, param)) + } + GenericParamKind::Type { .. } => { + type_count += 1; + None + } + }) + .collect(); let scope = Scope::Binder { lifetimes, next_early_index: index + type_count, @@ -572,12 +584,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let was_in_fn_syntax = self.is_in_fn_syntax; self.is_in_fn_syntax = true; let scope = Scope::Binder { - lifetimes: c.generic_params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::late(&self.tcx.hir, param)) - } - _ => None, - }).collect(), + lifetimes: c.generic_params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + Some(Region::late(&self.tcx.hir, param)) + } + _ => None, + }) + .collect(), s: self.scope, next_early_index, track_lifetime_uses: true, @@ -615,7 +630,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { // If the user wrote an explicit name, use that. self.visit_lifetime(lifetime); } - LifetimeName::Error => { } + LifetimeName::Error => {} } } hir::TyKind::Rptr(ref lifetime_ref, ref mt) => { @@ -635,19 +650,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let (generics, bounds) = match self.tcx.hir.expect_item(item_id.id).node { // named existential types are reached via TyKind::Path // this arm is for `impl Trait` in the types of statics, constants and locals - hir::ItemKind::Existential(hir::ExistTy{ impl_trait_fn: None, .. }) => { + hir::ItemKind::Existential(hir::ExistTy { + impl_trait_fn: None, + .. + }) => { intravisit::walk_ty(self, ty); return; - }, + } // RPIT (return position impl trait) - hir::ItemKind::Existential(hir::ExistTy{ + hir::ItemKind::Existential(hir::ExistTy { ref generics, ref bounds, .. - }) => ( - generics, - bounds, - ), + }) => (generics, bounds), ref i => bug!("impl Trait pointed to non-existential type?? {:#?}", i), }; @@ -778,15 +793,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let mut index = self.next_early_index(); debug!("visit_ty: index = {}", index); let mut type_count = 0; - let lifetimes = generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::early(&self.tcx.hir, &mut index, param)) - } - GenericParamKind::Type { .. } => { - type_count += 1; - None - } - }).collect(); + let lifetimes = generics + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + Some(Region::early(&self.tcx.hir, &mut index, param)) + } + GenericParamKind::Type { .. } => { + type_count += 1; + None + } + }) + .collect(); let scope = Scope::Binder { lifetimes, next_early_index: index + type_count, @@ -829,15 +848,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let mut index = self.next_early_index(); let mut next_early_index = index; debug!("visit_ty: index = {}", index); - let lifetimes = generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::early(&self.tcx.hir, &mut index, param)) - } - GenericParamKind::Type { .. } => { - next_early_index += 1; - None - } - }).collect(); + let lifetimes = generics + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + Some(Region::early(&self.tcx.hir, &mut index, param)) + } + GenericParamKind::Type { .. } => { + next_early_index += 1; + None + } + }) + .collect(); let scope = Scope::Binder { lifetimes, next_early_index, @@ -855,15 +878,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let mut index = self.next_early_index(); let mut next_early_index = index; debug!("visit_ty: index = {}", index); - let lifetimes = generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(Region::early(&self.tcx.hir, &mut index, param)) - } - GenericParamKind::Type { .. } => { - next_early_index += 1; - None - } - }).collect(); + let lifetimes = generics + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + Some(Region::early(&self.tcx.hir, &mut index, param)) + } + GenericParamKind::Type { .. } => { + next_early_index += 1; + None + } + }) + .collect(); let scope = Scope::Binder { lifetimes, @@ -937,13 +964,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { ref bound_generic_params, .. }) => { - let lifetimes: FxHashMap<_, _> = bound_generic_params.iter() + let lifetimes: FxHashMap<_, _> = bound_generic_params + .iter() .filter_map(|param| match param.kind { GenericParamKind::Lifetime { .. } => { Some(Region::late(&self.tcx.hir, param)) } _ => None, - }).collect(); + }) + .collect(); if !lifetimes.is_empty() { self.trait_ref_hack = true; let next_early_index = self.next_early_index(); @@ -993,15 +1022,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { ) { debug!("visit_poly_trait_ref trait_ref={:?}", trait_ref); - if !self.trait_ref_hack - || trait_ref - .bound_generic_params - .iter() - .any(|param| match param.kind { - GenericParamKind::Lifetime { .. } => true, - _ => false, - }) - { + if !self.trait_ref_hack || trait_ref.bound_generic_params.iter().any(|param| { + match param.kind { + GenericParamKind::Lifetime { .. } => true, + _ => false, + } + }) { if self.trait_ref_hack { span_err!( self.tcx.sess, @@ -1012,13 +1038,16 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { } let next_early_index = self.next_early_index(); let scope = Scope::Binder { - lifetimes: trait_ref.bound_generic_params.iter() + lifetimes: trait_ref + .bound_generic_params + .iter() .filter_map(|param| match param.kind { GenericParamKind::Lifetime { .. } => { Some(Region::late(&self.tcx.hir, param)) } _ => None, - }).collect(), + }) + .collect(), s: self.scope, next_early_index, track_lifetime_uses: true, @@ -1083,29 +1112,30 @@ impl ShadowKind { } } -fn check_mixed_explicit_and_in_band_defs( - tcx: TyCtxt<'_, '_, '_>, - params: &P<[hir::GenericParam]>, -) { - let lifetime_params: Vec<_> = params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { kind, .. } => Some((kind, param.span)), - _ => None, - }).collect(); - let explicit = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::Explicit); - let in_band = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::InBand); +fn check_mixed_explicit_and_in_band_defs(tcx: TyCtxt<'_, '_, '_>, params: &P<[hir::GenericParam]>) { + let lifetime_params: Vec<_> = params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { kind, .. } => Some((kind, param.span)), + _ => None, + }) + .collect(); + let explicit = lifetime_params + .iter() + .find(|(kind, _)| *kind == LifetimeParamKind::Explicit); + let in_band = lifetime_params + .iter() + .find(|(kind, _)| *kind == LifetimeParamKind::InBand); - if let (Some((_, explicit_span)), Some((_, in_band_span))) - = (explicit, in_band) { + if let (Some((_, explicit_span)), Some((_, in_band_span))) = (explicit, in_band) { struct_span_err!( tcx.sess, *in_band_span, E0688, "cannot mix in-band and explicit lifetime definitions" - ).span_label( - *in_band_span, - "in-band lifetime definition here", - ).span_label(*explicit_span, "explicit lifetime definition here") - .emit(); + ).span_label(*in_band_span, "in-band lifetime definition here") + .span_label(*explicit_span, "explicit lifetime definition here") + .emit(); } } @@ -1191,8 +1221,9 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body) { fn expression_label(ex: &hir::Expr) -> Option { match ex.node { - hir::ExprKind::While(.., Some(label)) | - hir::ExprKind::Loop(_, Some(label), _) => Some(label.ident), + hir::ExprKind::While(.., Some(label)) | hir::ExprKind::Loop(_, Some(label), _) => { + Some(label.ident) + } _ => None, } } @@ -1245,7 +1276,11 @@ fn compute_object_lifetime_defaults( hir::ItemKind::Struct(_, ref generics) | hir::ItemKind::Union(_, ref generics) | hir::ItemKind::Enum(_, ref generics) - | hir::ItemKind::Existential(hir::ExistTy { ref generics, impl_trait_fn: None, .. }) + | hir::ItemKind::Existential(hir::ExistTy { + ref generics, + impl_trait_fn: None, + .. + }) | hir::ItemKind::Ty(_, ref generics) | hir::ItemKind::Trait(_, _, ref generics, ..) => { let result = object_lifetime_defaults_for_item(tcx, generics); @@ -1257,8 +1292,10 @@ fn compute_object_lifetime_defaults( .map(|set| match *set { Set1::Empty => "BaseDefault".into(), Set1::One(Region::Static) => "'static".into(), - Set1::One(Region::EarlyBound(mut i, _, _)) => { - generics.params.iter().find_map(|param| match param.kind { + Set1::One(Region::EarlyBound(mut i, _, _)) => generics + .params + .iter() + .find_map(|param| match param.kind { GenericParamKind::Lifetime { .. } => { if i == 0 { return Some(param.name.ident().to_string().into()); @@ -1267,8 +1304,8 @@ fn compute_object_lifetime_defaults( None } _ => None, - }).unwrap() - } + }) + .unwrap(), Set1::One(_) => bug!(), Set1::Many => "Ambiguous".into(), }) @@ -1300,66 +1337,70 @@ fn object_lifetime_defaults_for_item( } } - generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => None, - GenericParamKind::Type { .. } => { - let mut set = Set1::Empty; + generics + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => None, + GenericParamKind::Type { .. } => { + let mut set = Set1::Empty; - add_bounds(&mut set, ¶m.bounds); + add_bounds(&mut set, ¶m.bounds); - let param_def_id = tcx.hir.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, - }; + let param_def_id = tcx.hir.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_generic_params.is_empty() { - continue; - } + // Ignore `for<'a> type: ...` as they can change what + // lifetimes mean (although we could "just" handle it). + if !data.bound_generic_params.is_empty() { + continue; + } - let def = match data.bounded_ty.node { - hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => path.def, - _ => continue, - }; + let def = match data.bounded_ty.node { + hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => path.def, + _ => continue, + }; - if def == Def::TyParam(param_def_id) { - add_bounds(&mut set, &data.bounds); - } - } - - Some(match set { - Set1::Empty => Set1::Empty, - Set1::One(name) => { - if name == hir::LifetimeName::Static { - Set1::One(Region::Static) - } else { - generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - Some(( - param.id, - hir::LifetimeName::Param(param.name), - LifetimeDefOrigin::from_param(param), - )) - } - _ => None, - }) - .enumerate() - .find(|&(_, (_, lt_name, _))| lt_name == name) - .map_or(Set1::Many, |(i, (id, _, origin))| { - let def_id = tcx.hir.local_def_id(id); - Set1::One(Region::EarlyBound(i as u32, def_id, origin)) - }) + if def == Def::TyParam(param_def_id) { + add_bounds(&mut set, &data.bounds); } } - Set1::Many => Set1::Many, - }) - } - }) - .collect() + + Some(match set { + Set1::Empty => Set1::Empty, + Set1::One(name) => { + if name == hir::LifetimeName::Static { + Set1::One(Region::Static) + } else { + generics + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => Some(( + param.id, + hir::LifetimeName::Param(param.name), + LifetimeDefOrigin::from_param(param), + )), + _ => None, + }) + .enumerate() + .find(|&(_, (_, lt_name, _))| lt_name == name) + .map_or(Set1::Many, |(i, (id, _, origin))| { + let def_id = tcx.hir.local_def_id(id); + Set1::One(Region::EarlyBound(i as u32, def_id, origin)) + }) + } + } + Set1::Many => Set1::Many, + }) + } + }) + .collect() } impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { @@ -1409,20 +1450,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { // if sole lifetime, remove the `<>` brackets Some(generics.span) } else { - generics.params.iter().enumerate() - .find_map(|(i, param)| { - if param.name.ident() == name { - // We also want to delete a leading or trailing comma - // as appropriate - if i >= generics.params.len() - 1 { - Some(generics.params[i-1].span.shrink_to_hi().to(param.span)) - } else { - Some(param.span.to(generics.params[i+1].span.shrink_to_lo())) - } + generics.params.iter().enumerate().find_map(|(i, param)| { + if param.name.ident() == name { + // We also want to delete a leading or trailing comma + // as appropriate + if i >= generics.params.len() - 1 { + Some(generics.params[i - 1].span.shrink_to_hi().to(param.span)) } else { - None + Some(param.span.to(generics.params[i + 1].span.shrink_to_lo())) } - }) + } else { + None + } + }) } } @@ -1435,7 +1475,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } }; - let mut def_ids: Vec<_> = defined_by.values() + let mut def_ids: Vec<_> = defined_by + .values() .flat_map(|region| match region { Region::EarlyBound(_, def_id, _) | Region::LateBound(_, def_id, _) @@ -1449,21 +1490,28 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { def_ids.sort_by_key(|&def_id| self.tcx.def_path_hash(def_id)); for def_id in def_ids { - debug!("check_uses_for_lifetimes_defined_by_scope: def_id = {:?}", def_id); + debug!( + "check_uses_for_lifetimes_defined_by_scope: def_id = {:?}", + def_id + ); let lifetimeuseset = self.lifetime_uses.remove(&def_id); - debug!("check_uses_for_lifetimes_defined_by_scope: lifetimeuseset = {:?}", - lifetimeuseset); + debug!( + "check_uses_for_lifetimes_defined_by_scope: lifetimeuseset = {:?}", + lifetimeuseset + ); match lifetimeuseset { Some(LifetimeUseSet::One(lifetime)) => { let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap(); debug!("node id first={:?}", node_id); if let Some((id, span, name)) = match self.tcx.hir.get(node_id) { - Node::Lifetime(hir_lifetime) => { - Some((hir_lifetime.id, hir_lifetime.span, hir_lifetime.name.ident())) - } + Node::Lifetime(hir_lifetime) => Some(( + hir_lifetime.id, + hir_lifetime.span, + hir_lifetime.name.ident(), + )), Node::GenericParam(param) => { Some((param.id, param.span, param.name.ident())) } @@ -1487,9 +1535,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { None => { let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap(); if let Some((id, span, name)) = match self.tcx.hir.get(node_id) { - Node::Lifetime(hir_lifetime) => { - Some((hir_lifetime.id, hir_lifetime.span, hir_lifetime.name.ident())) - } + Node::Lifetime(hir_lifetime) => Some(( + hir_lifetime.id, + hir_lifetime.span, + hir_lifetime.name.ident(), + )), Node::GenericParam(param) => { Some((param.id, param.span, param.name.ident())) } @@ -1500,7 +1550,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { lint::builtin::UNUSED_LIFETIMES, id, span, - &format!("lifetime parameter `{}` never used", name) + &format!("lifetime parameter `{}` never used", name), ); if let Some(parent_def_id) = self.tcx.parent(def_id) { if let Some(generics) = self.tcx.hir.get_generics(parent_def_id) { @@ -1510,7 +1560,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { span, "remove it", String::new(), - Applicability::MachineApplicable + Applicability::MachineApplicable, ); } } @@ -1568,19 +1618,23 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } let mut type_count = 0; - let lifetimes = generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => { - if self.map.late_bound.contains(¶m.id) { - Some(Region::late(&self.tcx.hir, param)) - } else { - Some(Region::early(&self.tcx.hir, &mut index, param)) + let lifetimes = generics + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + if self.map.late_bound.contains(¶m.id) { + Some(Region::late(&self.tcx.hir, param)) + } else { + Some(Region::early(&self.tcx.hir, &mut index, param)) + } } - } - GenericParamKind::Type { .. } => { - type_count += 1; - None - } - }).collect(); + GenericParamKind::Type { .. } => { + type_count += 1; + None + } + }) + .collect(); let next_early_index = index + type_count; let scope = Scope::Binder { @@ -1659,7 +1713,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { break None; } - Scope::Binder { ref lifetimes, s, .. } => { + Scope::Binder { + ref lifetimes, s, .. + } => { match lifetime_ref.name { LifetimeName::Param(param_name) => { if let Some(&def) = lifetimes.get(¶m_name.modern()) { @@ -1716,7 +1772,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { "lifetimes used in `fn` or `Fn` syntax must be \ explicitly declared using `<...>` binders" ).span_label(lifetime_ref.span, "in-band lifetime definition") - .emit(); + .emit(); } Region::Static @@ -1738,35 +1794,33 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { "use of undeclared lifetime name `{}`", lifetime_ref ).span_label(lifetime_ref.span, "undeclared lifetime") - .emit(); + .emit(); } } - fn visit_segment_args( - &mut self, - def: Def, - depth: usize, - generic_args: &'tcx hir::GenericArgs, - ) { + fn visit_segment_args(&mut self, def: Def, depth: usize, generic_args: &'tcx hir::GenericArgs) { if generic_args.parenthesized { let was_in_fn_syntax = self.is_in_fn_syntax; self.is_in_fn_syntax = true; - self.visit_fn_like_elision(generic_args.inputs(), - Some(&generic_args.bindings[0].ty)); + self.visit_fn_like_elision(generic_args.inputs(), Some(&generic_args.bindings[0].ty)); self.is_in_fn_syntax = was_in_fn_syntax; return; } let mut elide_lifetimes = true; - let lifetimes = generic_args.args.iter().filter_map(|arg| match arg { - hir::GenericArg::Lifetime(lt) => { - if !lt.is_elided() { - elide_lifetimes = false; + let lifetimes = generic_args + .args + .iter() + .filter_map(|arg| match arg { + hir::GenericArg::Lifetime(lt) => { + if !lt.is_elided() { + elide_lifetimes = false; + } + Some(lt) } - Some(lt) - } - _ => None, - }).collect(); + _ => None, + }) + .collect(); if elide_lifetimes { self.resolve_elided_lifetimes(lifetimes); } else { @@ -1822,33 +1876,37 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { self.xcrate_object_lifetime_defaults .entry(def_id) .or_insert_with(|| { - tcx.generics_of(def_id).params.iter().filter_map(|param| { - match param.kind { - GenericParamDefKind::Type { object_lifetime_default, .. } => { - Some(object_lifetime_default) - } + tcx.generics_of(def_id) + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamDefKind::Type { + object_lifetime_default, + .. + } => Some(object_lifetime_default), GenericParamDefKind::Lifetime => None, - } - }).collect() + }) + .collect() }) }; - unsubst.iter() - .map(|set| match *set { - Set1::Empty => if in_body { - None - } else { - Some(Region::Static) - }, - Set1::One(r) => { - let lifetimes = generic_args.args.iter().filter_map(|arg| match arg { - GenericArg::Lifetime(lt) => Some(lt), - _ => None, - }); - r.subst(lifetimes, map) - } - Set1::Many => None, - }) - .collect() + unsubst + .iter() + .map(|set| match *set { + Set1::Empty => if in_body { + None + } else { + Some(Region::Static) + }, + Set1::One(r) => { + let lifetimes = generic_args.args.iter().filter_map(|arg| match arg { + GenericArg::Lifetime(lt) => Some(lt), + _ => None, + }); + r.subst(lifetimes, map) + } + Set1::Many => None, + }) + .collect() }); let mut i = 0; @@ -1875,11 +1933,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } } - fn visit_fn_like_elision( - &mut self, - inputs: &'tcx [hir::Ty], - output: Option<&'tcx P>, - ) { + fn visit_fn_like_elision(&mut self, inputs: &'tcx [hir::Ty], output: Option<&'tcx P>) { debug!("visit_fn_like_elision: enter"); let mut arg_elide = Elide::FreshLateAnon(Cell::new(0)); let arg_scope = Scope::Elision { @@ -2139,8 +2193,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } } - fn resolve_elided_lifetimes(&mut self, - lifetime_refs: Vec<&'tcx hir::Lifetime>) { + fn resolve_elided_lifetimes(&mut self, lifetime_refs: Vec<&'tcx hir::Lifetime>) { if lifetime_refs.is_empty() { return; } @@ -2298,23 +2351,32 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Scope::Body { .. } | Scope::ObjectLifetimeDefault { lifetime: None, .. } => return, - Scope::ObjectLifetimeDefault { lifetime: Some(l), .. } => break l, + Scope::ObjectLifetimeDefault { + lifetime: Some(l), .. + } => break l, } }; self.insert_lifetime(lifetime_ref, lifetime.shifted(late_depth)); } - fn check_lifetime_params(&mut self, old_scope: ScopeRef<'_>, - params: &'tcx [hir::GenericParam]) { - let lifetimes: Vec<_> = params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => Some((param, param.name)), - _ => None, - }).collect(); + fn check_lifetime_params( + &mut self, + old_scope: ScopeRef<'_>, + params: &'tcx [hir::GenericParam], + ) { + let lifetimes: Vec<_> = params + .iter() + .filter_map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => Some((param, param.name)), + _ => None, + }) + .collect(); for (i, (lifetime_i, lifetime_i_name)) in lifetimes.iter().enumerate() { if let hir::ParamName::Plain(_) = lifetime_i_name { let name = lifetime_i_name.ident().name; - if name == keywords::UnderscoreLifetime.name() || - name == keywords::StaticLifetime.name() { + if name == keywords::UnderscoreLifetime.name() + || name == keywords::StaticLifetime.name() + { let mut err = struct_span_err!( self.tcx.sess, lifetime_i.span, @@ -2340,8 +2402,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { "lifetime name `{}` declared twice in the same scope", lifetime_j.name.ident() ).span_label(lifetime_j.span, "declared twice") - .span_label(lifetime_i.span, "previous declaration here") - .emit(); + .span_label(lifetime_i.span, "previous declaration here") + .emit(); } } @@ -2351,21 +2413,26 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { for bound in &lifetime_i.bounds { match bound { hir::GenericBound::Outlives(lt) => match lt.name { - hir::LifetimeName::Underscore => { - self.tcx.sess.delay_span_bug(lt.span, "use of `'_` in illegal place, but not caught by lowering") - } + hir::LifetimeName::Underscore => self.tcx.sess.delay_span_bug( + lt.span, + "use of `'_` in illegal place, but not caught by lowering", + ), hir::LifetimeName::Static => { self.insert_lifetime(lt, Region::Static); - self.tcx.sess.struct_span_warn( - lifetime_i.span.to(lt.span), - &format!( - "unnecessary lifetime parameter `{}`", + self.tcx + .sess + .struct_span_warn( + lifetime_i.span.to(lt.span), + &format!( + "unnecessary lifetime parameter `{}`", + lifetime_i.name.ident(), + ), + ) + .help(&format!( + "you can use the `'static` lifetime directly, in place of `{}`", lifetime_i.name.ident(), - ), - ).help(&format!( - "you can use the `'static` lifetime directly, in place of `{}`", - lifetime_i.name.ident(), - )).emit(); + )) + .emit(); } hir::LifetimeName::Param(_) | hir::LifetimeName::Implicit => { self.resolve_lifetime_ref(lt); @@ -2373,7 +2440,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { hir::LifetimeName::Error => { // No need to do anything, error already reported. } - } + }, _ => bug!(), } } @@ -2542,8 +2609,10 @@ fn insert_late_bound_lifetimes( decl: &hir::FnDecl, generics: &hir::Generics, ) { - debug!("insert_late_bound_lifetimes(decl={:?}, generics={:?})", - decl, generics); + debug!( + "insert_late_bound_lifetimes(decl={:?}, generics={:?})", + decl, generics + ); let mut constrained_by_input = ConstrainedCollector { regions: FxHashSet(), @@ -2557,8 +2626,10 @@ fn insert_late_bound_lifetimes( }; intravisit::walk_fn_ret_ty(&mut appears_in_output, &decl.output); - debug!("insert_late_bound_lifetimes: constrained_by_input={:?}", - constrained_by_input.regions); + debug!( + "insert_late_bound_lifetimes: constrained_by_input={:?}", + constrained_by_input.regions + ); // Walk the lifetimes that appear in where clauses. // @@ -2574,7 +2645,8 @@ fn insert_late_bound_lifetimes( if !param.bounds.is_empty() { // `'a: 'b` means both `'a` and `'b` are referenced appears_in_where_clause - .regions.insert(hir::LifetimeName::Param(param.name.modern())); + .regions + .insert(hir::LifetimeName::Param(param.name.modern())); } } } @@ -2609,9 +2681,11 @@ fn insert_late_bound_lifetimes( continue; } - debug!("insert_late_bound_lifetimes: lifetime {:?} with id {:?} is late-bound", - param.name.ident(), - param.id); + debug!( + "insert_late_bound_lifetimes: lifetime {:?} with id {:?} is late-bound", + param.name.ident(), + param.id + ); let inserted = map.late_bound.insert(param.id); assert!(inserted, "visited lifetime {:?} twice", param.id);