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

Add warning for use of lifetime parameter with 'static bound #40734

Merged
merged 2 commits into from
Mar 26, 2017
Merged
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
4 changes: 4 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl Lifetime {
pub fn is_elided(&self) -> bool {
self.name == keywords::Invalid.name()
}

pub fn is_static(&self) -> bool {
self.name == keywords::StaticLifetime.name()
}
}

/// A lifetime definition, eg `'a: 'b+'c+'d`
Expand Down
18 changes: 14 additions & 4 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use syntax::ast;
use syntax::attr;
use syntax::ptr::P;
use syntax::symbol::keywords;
use syntax_pos::Span;
use syntax_pos::{mk_sp, Span};
use errors::DiagnosticBuilder;
use util::nodemap::{NodeMap, NodeSet, FxHashSet, FxHashMap, DefIdMap};
use rustc_back::slice;
Expand Down Expand Up @@ -434,7 +434,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
self.resolve_elided_lifetimes(slice::ref_slice(lifetime_ref));
return;
}
if lifetime_ref.name == keywords::StaticLifetime.name() {
if lifetime_ref.is_static() {
self.insert_lifetime(lifetime_ref, Region::Static);
return;
}
Expand Down Expand Up @@ -1434,7 +1434,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
let lifetime_i = &lifetimes[i];

for lifetime in lifetimes {
if lifetime.lifetime.name == keywords::StaticLifetime.name() {
if lifetime.lifetime.is_static() {
let lifetime = lifetime.lifetime;
let mut err = struct_span_err!(self.sess, lifetime.span, E0262,
"invalid lifetime parameter name: `{}`", lifetime.name);
Expand Down Expand Up @@ -1464,7 +1464,17 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
self.check_lifetime_def_for_shadowing(old_scope, &lifetime_i.lifetime);

for bound in &lifetime_i.bounds {
self.resolve_lifetime_ref(bound);
if !bound.is_static() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I chose to put the common path first, even though its a negated condition. I can change if this is not preferred style though.

Copy link
Member

Choose a reason for hiding this comment

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

Looks good!

self.resolve_lifetime_ref(bound);
} else {
self.insert_lifetime(bound, Region::Static);
let full_span = mk_sp(lifetime_i.lifetime.span.lo, bound.span.hi);
self.sess.struct_span_warn(full_span,
&format!("unnecessary lifetime parameter `{}`", lifetime_i.lifetime.name))
.help(&format!("you can use the `'static` lifetime directly, in place \
of `{}`", lifetime_i.lifetime.name))
.emit();
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/static-lifetime-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2017 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.

fn f<'a: 'static>(_: &'a i32) {} //~WARN unnecessary lifetime parameter `'a`

fn main() {
let x = 0;
f(&x); //~ERROR does not live long enough
}