@@ -310,9 +310,10 @@ enum LifetimeRibKind {
310310 /// error on default object bounds (e.g., `Box<dyn Foo>`).
311311 AnonymousReportError ,
312312
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 ) ,
316317
317318 /// Signal we cannot find which should be the anonymous lifetime.
318319 ElisionFailure ,
@@ -1212,7 +1213,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
12121213 }
12131214 LifetimeRibKind :: AnonymousCreateParameter { .. }
12141215 | LifetimeRibKind :: AnonymousReportError
1215- | LifetimeRibKind :: AnonymousWarn ( _)
1216+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _)
12161217 | LifetimeRibKind :: Elided ( _)
12171218 | LifetimeRibKind :: ElisionFailure
12181219 | LifetimeRibKind :: ConcreteAnonConst ( _)
@@ -1580,7 +1581,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15801581 // lifetime would be illegal.
15811582 LifetimeRibKind :: Item
15821583 | LifetimeRibKind :: AnonymousReportError
1583- | LifetimeRibKind :: AnonymousWarn ( _)
1584+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _)
15841585 | LifetimeRibKind :: ElisionFailure => Some ( LifetimeUseSet :: Many ) ,
15851586 // An anonymous lifetime is legal here, and bound to the right
15861587 // place, go ahead.
@@ -1643,7 +1644,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16431644 | LifetimeRibKind :: Generics { .. }
16441645 | LifetimeRibKind :: ElisionFailure
16451646 | LifetimeRibKind :: AnonymousReportError
1646- | LifetimeRibKind :: AnonymousWarn ( _) => { }
1647+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _) => { }
16471648 }
16481649 }
16491650
@@ -1677,16 +1678,36 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16771678 self . record_lifetime_res ( lifetime. id , res, elision_candidate) ;
16781679 return ;
16791680 }
1680- LifetimeRibKind :: AnonymousWarn ( node_id) => {
1681- self . r . lint_buffer . buffer_lint (
1682- lint:: builtin:: ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT ,
1683- node_id,
1684- lifetime. ident . span ,
1685- lint:: BuiltinLintDiag :: AssociatedConstElidedLifetime {
1686- elided,
1687- span : lifetime. ident . span ,
1688- } ,
1689- ) ;
1681+ LifetimeRibKind :: StaticIfNoLifetimeInScope ( node_id) => {
1682+ let mut lifetimes_in_scope = vec ! [ ] ;
1683+ for rib in & self . lifetime_ribs [ ..i] {
1684+ lifetimes_in_scope. extend ( rib. bindings . iter ( ) . map ( |( ident, _) | ident. span ) ) ;
1685+ // Consider any anonymous lifetimes, too
1686+ if let LifetimeRibKind :: AnonymousCreateParameter { binder, .. } = rib. kind
1687+ && let Some ( extra) = self . r . extra_lifetime_params_map . get ( & binder)
1688+ {
1689+ lifetimes_in_scope. extend ( extra. iter ( ) . map ( |( ident, _, _) | ident. span ) ) ;
1690+ }
1691+ }
1692+ if lifetimes_in_scope. is_empty ( ) {
1693+ self . record_lifetime_res (
1694+ lifetime. id ,
1695+ LifetimeRes :: Static ,
1696+ elision_candidate,
1697+ ) ;
1698+ return ;
1699+ } else {
1700+ self . r . lint_buffer . buffer_lint (
1701+ lint:: builtin:: ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT ,
1702+ node_id,
1703+ lifetime. ident . span ,
1704+ lint:: BuiltinLintDiag :: AssociatedConstElidedLifetime {
1705+ elided,
1706+ span : lifetime. ident . span ,
1707+ lifetimes_in_scope : lifetimes_in_scope. into ( ) ,
1708+ } ,
1709+ ) ;
1710+ }
16901711 }
16911712 LifetimeRibKind :: AnonymousReportError => {
16921713 if elided {
@@ -1904,7 +1925,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
19041925 // impl Foo for std::cell::Ref<u32> // note lack of '_
19051926 // async fn foo(_: std::cell::Ref<u32>) { ... }
19061927 LifetimeRibKind :: AnonymousCreateParameter { report_in_path : true , .. }
1907- | LifetimeRibKind :: AnonymousWarn ( _) => {
1928+ | LifetimeRibKind :: StaticIfNoLifetimeInScope ( _) => {
19081929 let sess = self . r . tcx . sess ;
19091930 let subdiag = rustc_errors:: elided_lifetime_in_path_suggestion (
19101931 sess. source_map ( ) ,
@@ -3030,30 +3051,33 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
30303051 kind : LifetimeBinderKind :: ConstItem ,
30313052 } ,
30323053 |this| {
3033- this. with_lifetime_rib ( LifetimeRibKind :: AnonymousWarn ( item. id ) , |this| {
3034- // If this is a trait impl, ensure the const
3035- // exists in trait
3036- this. check_trait_item (
3037- item. id ,
3038- item. ident ,
3039- & item. kind ,
3040- ValueNS ,
3041- item. span ,
3042- seen_trait_items,
3043- |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
3044- ) ;
3054+ this. with_lifetime_rib (
3055+ LifetimeRibKind :: StaticIfNoLifetimeInScope ( item. id ) ,
3056+ |this| {
3057+ // If this is a trait impl, ensure the const
3058+ // exists in trait
3059+ this. check_trait_item (
3060+ item. id ,
3061+ item. ident ,
3062+ & item. kind ,
3063+ ValueNS ,
3064+ item. span ,
3065+ seen_trait_items,
3066+ |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
3067+ ) ;
30453068
3046- this. visit_generics ( generics) ;
3047- this. visit_ty ( ty) ;
3048- if let Some ( expr) = expr {
3049- // We allow arbitrary const expressions inside of associated consts,
3050- // even if they are potentially not const evaluatable.
3051- //
3052- // Type parameters can already be used and as associated consts are
3053- // not used as part of the type system, this is far less surprising.
3054- this. resolve_const_body ( expr, None ) ;
3055- }
3056- } ) ;
3069+ this. visit_generics ( generics) ;
3070+ this. visit_ty ( ty) ;
3071+ if let Some ( expr) = expr {
3072+ // We allow arbitrary const expressions inside of associated consts,
3073+ // even if they are potentially not const evaluatable.
3074+ //
3075+ // Type parameters can already be used and as associated consts are
3076+ // not used as part of the type system, this is far less surprising.
3077+ this. resolve_const_body ( expr, None ) ;
3078+ }
3079+ } ,
3080+ ) ;
30573081 } ,
30583082 ) ;
30593083 }
0 commit comments