Skip to content

Commit 7ebcdfd

Browse files
committed
Infer all anonymous lifetimes in (assoc) consts as 'static
1 parent b2dfdd2 commit 7ebcdfd

12 files changed

Lines changed: 21 additions & 195 deletions

compiler/rustc_resolve/src/late.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,6 @@ enum LifetimeRibKind {
342342
/// error on default object bounds (e.g., `Box<dyn Foo>`).
343343
AnonymousReportError,
344344

345-
/// Resolves elided lifetimes to `'static` if there are no other lifetimes in scope,
346-
/// otherwise give a warning that the previous behavior of introducing a new early-bound
347-
/// lifetime is a bug and will be removed (if `emit_lint` is enabled).
348-
StaticIfNoLifetimeInScope { emit_lint: bool },
349-
350345
/// Signal we cannot find which should be the anonymous lifetime.
351346
ElisionFailure,
352347

@@ -1373,7 +1368,6 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
13731368
}
13741369
LifetimeRibKind::AnonymousCreateParameter { .. }
13751370
| LifetimeRibKind::AnonymousReportError
1376-
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. }
13771371
| LifetimeRibKind::ImplTrait
13781372
| LifetimeRibKind::Elided(_)
13791373
| LifetimeRibKind::ElisionFailure
@@ -1779,7 +1773,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
17791773
// lifetime would be illegal.
17801774
LifetimeRibKind::Item
17811775
| LifetimeRibKind::AnonymousReportError
1782-
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. }
17831776
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many),
17841777
// An anonymous lifetime is legal here, and bound to the right
17851778
// place, go ahead.
@@ -1843,8 +1836,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18431836
| LifetimeRibKind::Generics { .. }
18441837
| LifetimeRibKind::ElisionFailure
18451838
| LifetimeRibKind::AnonymousReportError
1846-
| LifetimeRibKind::ImplTrait
1847-
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. } => {}
1839+
| LifetimeRibKind::ImplTrait => {}
18481840
}
18491841
}
18501842

@@ -1883,31 +1875,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18831875
self.record_lifetime_use(lifetime.id, res, elision_candidate);
18841876
return;
18851877
}
1886-
LifetimeRibKind::StaticIfNoLifetimeInScope { emit_lint } => {
1887-
let mut lifetimes_in_scope = vec![];
1888-
for rib in self.lifetime_ribs[..i].iter().rev() {
1889-
lifetimes_in_scope.extend(rib.bindings.iter().map(|(ident, _)| ident.span));
1890-
// Consider any anonymous lifetimes, too
1891-
if let LifetimeRibKind::AnonymousCreateParameter { binder, .. } = rib.kind
1892-
&& let Some(extra) = self.r.extra_lifetime_params_map.get(&binder)
1893-
{
1894-
lifetimes_in_scope.extend(extra.iter().map(|(ident, _, _)| ident.span));
1895-
}
1896-
if let LifetimeRibKind::Item = rib.kind {
1897-
break;
1898-
}
1899-
}
1900-
if lifetimes_in_scope.is_empty() {
1901-
self.record_lifetime_use(
1902-
lifetime.id,
1903-
LifetimeRes::Static,
1904-
elision_candidate,
1905-
);
1906-
return;
1907-
} else if emit_lint {
1908-
break;
1909-
}
1910-
}
19111878
LifetimeRibKind::AnonymousReportError => {
19121879
let guar = if elided {
19131880
let suggestion = self.lifetime_ribs[i..].iter().rev().find_map(|rib| {
@@ -2257,8 +2224,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
22572224
//
22582225
// impl Foo for std::cell::Ref<u32> // note lack of '_
22592226
// async fn foo(_: std::cell::Ref<u32>) { ... }
2260-
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. }
2261-
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. } => {
2227+
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. } => {
22622228
let sess = self.r.tcx.sess;
22632229
let subdiag = elided_lifetime_in_path_suggestion(
22642230
sess.source_map(),
@@ -3331,7 +3297,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
33313297
generics.span,
33323298
|this| {
33333299
this.with_lifetime_rib(
3334-
LifetimeRibKind::StaticIfNoLifetimeInScope { emit_lint: false },
3300+
LifetimeRibKind::Elided(LifetimeRes::Static),
33353301
|this| {
33363302
this.visit_generics(generics);
33373303
if rhs_kind.is_type_const()
@@ -3547,10 +3513,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
35473513
generics.span,
35483514
|this| {
35493515
this.with_lifetime_rib(
3550-
LifetimeRibKind::StaticIfNoLifetimeInScope {
3551-
// In impls, it's not a hard error yet due to backcompat.
3552-
emit_lint: true,
3553-
},
3516+
LifetimeRibKind::Elided(LifetimeRes::Static),
35543517
|this| {
35553518
// If this is a trait impl, ensure the const
35563519
// exists in trait
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ check-pass
2+
13
use std::marker::PhantomData;
24

35
struct Foo<'a> {
@@ -6,10 +8,8 @@ struct Foo<'a> {
68

79
impl<'a> Foo<'a> {
810
const FOO: Foo<'_> = Foo { x: PhantomData::<&()> };
9-
//~^ ERROR missing lifetime specifier
1011

1112
const BAR: &() = &();
12-
//~^ ERROR missing lifetime specifier
1313
}
1414

1515
fn main() {}

tests/ui/consts/assoc-const-elided-lifetime.stderr

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
//@check-pass
2+
13
struct Foo<'a>(&'a ());
24

35
impl Foo<'_> {
46
const STATIC: &str = "";
5-
//~^ ERROR missing lifetime specifier
67
}
78

89
trait Bar {
@@ -11,7 +12,6 @@ trait Bar {
1112

1213
impl Bar for Foo<'_> {
1314
const STATIC: &str = "";
14-
//~^ ERROR missing lifetime specifier
1515
}
1616

1717
fn main() {}

tests/ui/consts/static-default-lifetime/elided-lifetime.stderr

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
//@ check-pass
2+
13
#![feature(generic_const_items)]
24

35
struct A;
46
impl A {
57
const GAC_TYPE<T>: &str = "";
68
const GAC_LIFETIME<'a>: &str = "";
7-
//~^ ERROR missing lifetime specifier
89
}
910

1011
trait Trait {
1112
const GAC_TYPE<T>: &str = "";
1213
const GAC_LIFETIME<'a>: &str = "";
13-
//~^ ERROR missing lifetime specifier
1414
}
1515

1616
fn main() {}

tests/ui/consts/static-default-lifetime/generic-associated-const.stderr

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/ui/consts/static-default-lifetime/static-trait-impl.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
//@ check-pass
2+
13
trait Bar<'a> {
24
const STATIC: &'a str;
35
}
46

57
struct A;
68
impl Bar<'_> for A {
79
const STATIC: &str = "";
8-
//~^ ERROR missing lifetime specifier
910
}
1011

1112
struct B;
@@ -15,12 +16,10 @@ impl Bar<'static> for B {
1516

1617
struct C;
1718
impl Bar<'_> for C {
18-
// make ^^ not cause
1919
const STATIC: &'static str = {
2020
struct B;
2121
impl Bar<'static> for B {
2222
const STATIC: &str = "";
23-
// ^ to emit a future incompat warning
2423
}
2524
""
2625
};

tests/ui/consts/static-default-lifetime/static-trait-impl.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/ui/suggestions/missing-lifetime-in-assoc-const-type.default.stderr

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)