Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest replacing named lifetime with 'static #51513

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,13 @@ impl GenericParam {
_ => false,
}
}

pub fn span(&self) -> Span {
match *self {
GenericParam::Lifetime(ref lifetime) => lifetime.lifetime.span,
GenericParam::Type(ref typaram) => typaram.span,
}
}
}

pub trait GenericParamsExt {
Expand Down Expand Up @@ -1662,6 +1669,137 @@ pub struct Ty {
pub hir_id: HirId,
}

impl Ty {
pub fn lifetimes(&self) -> Vec<Lifetime> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use a visitor for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what is this used for? It seems like it doesn't handle shadowing (which, I guess, we disallow at present, so maybe that's ok).

(I was thinking of types like for<'a> fn(&'a u32), which mention 'a, but not the same 'a as might be in scope outside... but as I said at present we do disallow such shadowing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only used to gather all the lifetime spans in the argument and return tys that match the name being checked.

// FIXME(estebank): expand to all `Ty_`s
match self.node {
TyRptr(lifetime, ref mut_ty) => {
let mut lifetimes = vec![lifetime];
lifetimes.extend(mut_ty.ty.lifetimes());
lifetimes
}
TyPtr(ref mut_ty) => {
let mut lifetimes = vec![];
lifetimes.extend(mut_ty.ty.lifetimes());
lifetimes
}
TySlice(ref ty) |
TyArray(ref ty, _) => ty.lifetimes(),
TyBareFn(ref bare_fn) => {
let mut lifetimes = vec![];
for ty in &bare_fn.decl.inputs {
lifetimes.extend(ty.lifetimes());
}
if let FunctionRetTy::Return(ref ty) = bare_fn.decl.output {
lifetimes.extend(ty.lifetimes());
}
lifetimes
}
TyPath(ref path) => {
match *path {
QPath::Resolved(ref ty, ref path) => {
let mut lifetimes = vec![];
if let &Some(ref ty) = ty {
lifetimes.extend(ty.lifetimes());
}
for segment in &path.segments {
if let Some(ref params) = segment.parameters {
for lifetime in &params.lifetimes {
lifetimes.push(*lifetime);
}
for ty in &params.types {
lifetimes.extend(ty.lifetimes());
}
}
}
lifetimes
}
QPath::TypeRelative(ref ty, ref path_segment) => {
let mut lifetimes = vec![];
lifetimes.extend(ty.lifetimes());
if let Some(ref params) = path_segment.parameters {
for lifetime in &params.lifetimes {
lifetimes.push(*lifetime);
}
for ty in &params.types {
lifetimes.extend(ty.lifetimes());
}
}
lifetimes
}
}
}
TyTup(ref tys) => {
let mut lifetimes = vec![];
for ty in tys {
lifetimes.extend(ty.lifetimes());
}
lifetimes
}
TyTraitObject(ref poly_trait_refs, lifetime) => {
let mut lifetimes = vec![lifetime];
for poly_trait_ref in poly_trait_refs {
for param in &poly_trait_ref.bound_generic_params {
if let GenericParam::Lifetime(lifetime_def) = param {
lifetimes.push(lifetime_def.lifetime);
}
}
for segment in &poly_trait_ref.trait_ref.path.segments {
if let Some(ref params) = segment.parameters {
for lifetime in &params.lifetimes {
lifetimes.push(*lifetime);
}
for ty in &params.types {
lifetimes.extend(ty.lifetimes());
}
}
}
}
lifetimes
}
TyImplTraitExistential(ref exist_ty, ref ex_lifetimes) => {
let mut lifetimes = vec![];
lifetimes.extend(ex_lifetimes);
for param in &exist_ty.generics.params {
if let GenericParam::Lifetime(lifetime_def) = param {
lifetimes.push(lifetime_def.lifetime);
}
}
for param in &exist_ty.generics.params {
if let GenericParam::Lifetime(lifetime_def) = param {
lifetimes.push(lifetime_def.lifetime);
}
}
for bound in &exist_ty.bounds {
match bound {
TyParamBound::RegionTyParamBound(lifetime) => lifetimes.push(*lifetime),

TyParamBound::TraitTyParamBound(ref poly_trait_ref, _) => {
for param in &poly_trait_ref.bound_generic_params {
if let GenericParam::Lifetime(lifetime_def) = param {
lifetimes.push(lifetime_def.lifetime);
}
for segment in &poly_trait_ref.trait_ref.path.segments {
if let Some(ref params) = segment.parameters {
for lifetime in &params.lifetimes {
lifetimes.push(*lifetime);
}
for ty in &params.types {
lifetimes.extend(ty.lifetimes());
}
}
}
}
}
}
}
lifetimes
}
TyNever | TyErr | TyInfer | TyTypeof(_) => vec![],
}
}
}

impl fmt::Debug for Ty {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "type({})",
Expand Down
Loading