Skip to content

Commit aa0a5a8

Browse files
committed
Auto merge of #46254 - Dylan-DPC:ellided-lifetime, r=nikomatsakis
elided lifetime Closes #45992 Hey Having a problem with my config so decided to make a WIP PR nevertheless. Will add some more tests.
2 parents 8d04b8f + accd997 commit aa0a5a8

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

src/librustc/lint/builtin.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ declare_lint! {
246246
"raw pointer to an inference variable"
247247
}
248248

249+
declare_lint! {
250+
pub ELIDED_LIFETIME_IN_PATH,
251+
Allow,
252+
"hidden lifetime parameters are deprecated, try `Foo<'_>`"
253+
}
254+
249255
/// Does nothing as a lint pass, but registers some `Lint`s
250256
/// which are used by other parts of the compiler.
251257
#[derive(Copy, Clone)]
@@ -291,7 +297,9 @@ impl LintPass for HardwiredLints {
291297
UNUSED_MUT,
292298
COERCE_NEVER,
293299
SINGLE_USE_LIFETIME,
294-
TYVAR_BEHIND_RAW_POINTER
300+
TYVAR_BEHIND_RAW_POINTER,
301+
ELIDED_LIFETIME_IN_PATH
302+
295303
)
296304
}
297305
}

src/librustc/middle/resolve_lifetime.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
737737

738738
fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
739739
if lifetime_ref.is_elided() {
740-
self.resolve_elided_lifetimes(slice::from_ref(lifetime_ref));
740+
self.resolve_elided_lifetimes(slice::from_ref(lifetime_ref), false);
741741
return;
742742
}
743743
if lifetime_ref.is_static() {
@@ -1444,7 +1444,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14441444
}
14451445

14461446
if params.lifetimes.iter().all(|l| l.is_elided()) {
1447-
self.resolve_elided_lifetimes(&params.lifetimes);
1447+
self.resolve_elided_lifetimes(&params.lifetimes, true);
14481448
} else {
14491449
for l in &params.lifetimes {
14501450
self.visit_lifetime(l);
@@ -1803,14 +1803,24 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18031803
}
18041804
}
18051805

1806-
fn resolve_elided_lifetimes(&mut self, lifetime_refs: &'tcx [hir::Lifetime]) {
1806+
fn resolve_elided_lifetimes(&mut self, lifetime_refs: &'tcx [hir::Lifetime], deprecated: bool) {
18071807
if lifetime_refs.is_empty() {
18081808
return;
18091809
}
18101810

18111811
let span = lifetime_refs[0].span;
1812+
let id = lifetime_refs[0].id;
18121813
let mut late_depth = 0;
18131814
let mut scope = self.scope;
1815+
if deprecated {
1816+
self.tcx
1817+
.struct_span_lint_node(
1818+
lint::builtin::ELIDED_LIFETIME_IN_PATH,
1819+
id,
1820+
span,
1821+
&format!("hidden lifetime parameters are deprecated, try `Foo<'_>`"))
1822+
.emit();
1823+
}
18141824
let error = loop {
18151825
match *scope {
18161826
// Do not assign any resolution, it will be inferred.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 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+
#![allow(warnings)]
11+
#![allow(unused_variables, dead_code, unused, bad_style)]
12+
#![deny(elided_lifetime_in_path)]
13+
14+
struct Foo<'a> { x: &'a u32 }
15+
fn foo(x: &Foo) {
16+
//~^ ERROR: hidden lifetime parameters are deprecated, try `Foo<'_>`
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: hidden lifetime parameters are deprecated, try `Foo<'_>`
2+
--> $DIR/ellided-lifetimes.rs:15:12
3+
|
4+
15 | fn foo(x: &Foo) {
5+
| ^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/ellided-lifetimes.rs:12:9
9+
|
10+
12 | #![deny(elided_lifetime_in_path)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)