@@ -310,9 +310,10 @@ enum LifetimeRibKind {
310
310
/// error on default object bounds (e.g., `Box<dyn Foo>`).
311
311
AnonymousReportError ,
312
312
313
- /// Resolves elided lifetimes to `'static`, but gives a warning that this behavior
314
- /// is a bug and will be reverted soon.
315
- AnonymousWarn ( NodeId ) ,
313
+ /// Resolves elided lifetimes to `'static` if there are no other lifetimes in scope,
314
+ /// otherwise give a warning that the previous behavior of introducing a new early-bound
315
+ /// lifetime is a bug and will be removed.
316
+ StaticIfNoLifetimeInScope ( NodeId ) ,
316
317
317
318
/// Signal we cannot find which should be the anonymous lifetime.
318
319
ElisionFailure ,
@@ -1209,7 +1210,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
1209
1210
}
1210
1211
LifetimeRibKind :: AnonymousCreateParameter { .. }
1211
1212
| LifetimeRibKind :: AnonymousReportError
1212
- | LifetimeRibKind :: AnonymousWarn ( _)
1213
+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _)
1213
1214
| LifetimeRibKind :: Elided ( _)
1214
1215
| LifetimeRibKind :: ElisionFailure
1215
1216
| LifetimeRibKind :: ConcreteAnonConst ( _)
@@ -1577,7 +1578,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1577
1578
// lifetime would be illegal.
1578
1579
LifetimeRibKind :: Item
1579
1580
| LifetimeRibKind :: AnonymousReportError
1580
- | LifetimeRibKind :: AnonymousWarn ( _)
1581
+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _)
1581
1582
| LifetimeRibKind :: ElisionFailure => Some ( LifetimeUseSet :: Many ) ,
1582
1583
// An anonymous lifetime is legal here, and bound to the right
1583
1584
// place, go ahead.
@@ -1640,7 +1641,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1640
1641
| LifetimeRibKind :: Generics { .. }
1641
1642
| LifetimeRibKind :: ElisionFailure
1642
1643
| LifetimeRibKind :: AnonymousReportError
1643
- | LifetimeRibKind :: AnonymousWarn ( _) => { }
1644
+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _) => { }
1644
1645
}
1645
1646
}
1646
1647
@@ -1674,22 +1675,42 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1674
1675
self . record_lifetime_res ( lifetime. id , res, elision_candidate) ;
1675
1676
return ;
1676
1677
}
1677
- LifetimeRibKind :: AnonymousWarn ( node_id) => {
1678
- let msg = if elided {
1679
- "`&` without an explicit lifetime name cannot be used here"
1678
+ LifetimeRibKind :: StaticIfNoLifetimeInScope ( node_id) => {
1679
+ let mut lifetimes_in_scope = vec ! [ ] ;
1680
+ for rib in & self . lifetime_ribs [ ..i] {
1681
+ lifetimes_in_scope. extend ( rib. bindings . iter ( ) . map ( |( ident, _) | ident. span ) ) ;
1682
+ // Consider any anonymous lifetimes, too
1683
+ if let LifetimeRibKind :: AnonymousCreateParameter { binder, .. } = rib. kind
1684
+ && let Some ( extra) = self . r . extra_lifetime_params_map . get ( & binder)
1685
+ {
1686
+ lifetimes_in_scope. extend ( extra. iter ( ) . map ( |( ident, _, _) | ident. span ) ) ;
1687
+ }
1688
+ }
1689
+ if lifetimes_in_scope. is_empty ( ) {
1690
+ self . record_lifetime_res (
1691
+ lifetime. id ,
1692
+ LifetimeRes :: Static ,
1693
+ elision_candidate,
1694
+ ) ;
1695
+ return ;
1680
1696
} else {
1681
- "`'_` cannot be used here"
1682
- } ;
1683
- self . r . lint_buffer . buffer_lint_with_diagnostic (
1684
- lint:: builtin:: ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT ,
1685
- node_id,
1686
- lifetime. ident . span ,
1687
- msg,
1688
- lint:: BuiltinLintDiag :: AssociatedConstElidedLifetime {
1689
- elided,
1690
- span : lifetime. ident . span ,
1691
- } ,
1692
- ) ;
1697
+ let msg = if elided {
1698
+ "`&` without an explicit lifetime name cannot be used here"
1699
+ } else {
1700
+ "`'_` cannot be used here"
1701
+ } ;
1702
+ self . r . lint_buffer . buffer_lint_with_diagnostic (
1703
+ lint:: builtin:: ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT ,
1704
+ node_id,
1705
+ lifetime. ident . span ,
1706
+ msg,
1707
+ lint:: BuiltinLintDiag :: AssociatedConstElidedLifetime {
1708
+ elided,
1709
+ span : lifetime. ident . span ,
1710
+ lifetimes_in_scope : lifetimes_in_scope. into ( ) ,
1711
+ } ,
1712
+ ) ;
1713
+ }
1693
1714
}
1694
1715
LifetimeRibKind :: AnonymousReportError => {
1695
1716
if elided {
@@ -1882,7 +1903,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
1882
1903
// impl Foo for std::cell::Ref<u32> // note lack of '_
1883
1904
// async fn foo(_: std::cell::Ref<u32>) { ... }
1884
1905
LifetimeRibKind :: AnonymousCreateParameter { report_in_path : true , .. }
1885
- | LifetimeRibKind :: AnonymousWarn ( _) => {
1906
+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _) => {
1886
1907
let sess = self . r . tcx . sess ;
1887
1908
let subdiag = rustc_errors:: elided_lifetime_in_path_suggestion (
1888
1909
sess. source_map ( ) ,
@@ -3009,30 +3030,33 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3009
3030
kind : LifetimeBinderKind :: ConstItem ,
3010
3031
} ,
3011
3032
|this| {
3012
- this. with_lifetime_rib ( LifetimeRibKind :: AnonymousWarn ( item. id ) , |this| {
3013
- // If this is a trait impl, ensure the const
3014
- // exists in trait
3015
- this. check_trait_item (
3016
- item. id ,
3017
- item. ident ,
3018
- & item. kind ,
3019
- ValueNS ,
3020
- item. span ,
3021
- seen_trait_items,
3022
- |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
3023
- ) ;
3033
+ this. with_lifetime_rib (
3034
+ LifetimeRibKind :: StaticIfNoLifetimeInScope ( item. id ) ,
3035
+ |this| {
3036
+ // If this is a trait impl, ensure the const
3037
+ // exists in trait
3038
+ this. check_trait_item (
3039
+ item. id ,
3040
+ item. ident ,
3041
+ & item. kind ,
3042
+ ValueNS ,
3043
+ item. span ,
3044
+ seen_trait_items,
3045
+ |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
3046
+ ) ;
3024
3047
3025
- this. visit_generics ( generics) ;
3026
- this. visit_ty ( ty) ;
3027
- if let Some ( expr) = expr {
3028
- // We allow arbitrary const expressions inside of associated consts,
3029
- // even if they are potentially not const evaluatable.
3030
- //
3031
- // Type parameters can already be used and as associated consts are
3032
- // not used as part of the type system, this is far less surprising.
3033
- this. resolve_const_body ( expr, None ) ;
3034
- }
3035
- } ) ;
3048
+ this. visit_generics ( generics) ;
3049
+ this. visit_ty ( ty) ;
3050
+ if let Some ( expr) = expr {
3051
+ // We allow arbitrary const expressions inside of associated consts,
3052
+ // even if they are potentially not const evaluatable.
3053
+ //
3054
+ // Type parameters can already be used and as associated consts are
3055
+ // not used as part of the type system, this is far less surprising.
3056
+ this. resolve_const_body ( expr, None ) ;
3057
+ }
3058
+ } ,
3059
+ ) ;
3036
3060
} ,
3037
3061
) ;
3038
3062
}
0 commit comments