@@ -311,6 +311,10 @@ enum LifetimeRibKind {
311311 /// error on default object bounds (e.g., `Box<dyn Foo>`).
312312 AnonymousReportError ,
313313
314+ /// Resolves elided lifetimes to `'static`, but gives a warning that this behavior
315+ /// is a bug and will be reverted soon.
316+ AnonymousWarnToStatic ( NodeId ) ,
317+
314318 /// Signal we cannot find which should be the anonymous lifetime.
315319 ElisionFailure ,
316320
@@ -1148,6 +1152,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
11481152 }
11491153 LifetimeRibKind :: AnonymousCreateParameter { .. }
11501154 | LifetimeRibKind :: AnonymousReportError
1155+ | LifetimeRibKind :: AnonymousWarnToStatic ( _)
11511156 | LifetimeRibKind :: Elided ( _)
11521157 | LifetimeRibKind :: ElisionFailure
11531158 | LifetimeRibKind :: ConcreteAnonConst ( _)
@@ -1515,6 +1520,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15151520 // lifetime would be illegal.
15161521 LifetimeRibKind :: Item
15171522 | LifetimeRibKind :: AnonymousReportError
1523+ | LifetimeRibKind :: AnonymousWarnToStatic ( _)
15181524 | LifetimeRibKind :: ElisionFailure => Some ( LifetimeUseSet :: Many ) ,
15191525 // An anonymous lifetime is legal here, and bound to the right
15201526 // place, go ahead.
@@ -1576,7 +1582,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15761582 | LifetimeRibKind :: Elided ( _)
15771583 | LifetimeRibKind :: Generics { .. }
15781584 | LifetimeRibKind :: ElisionFailure
1579- | LifetimeRibKind :: AnonymousReportError => { }
1585+ | LifetimeRibKind :: AnonymousReportError
1586+ | LifetimeRibKind :: AnonymousWarnToStatic ( _) => { }
15801587 }
15811588 }
15821589
@@ -1616,6 +1623,25 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16161623 self . record_lifetime_res ( lifetime. id , res, elision_candidate) ;
16171624 return ;
16181625 }
1626+ LifetimeRibKind :: AnonymousWarnToStatic ( node_id) => {
1627+ self . record_lifetime_res ( lifetime. id , LifetimeRes :: Static , elision_candidate) ;
1628+ let msg = if elided {
1629+ "`&` without an explicit lifetime name cannot be used here"
1630+ } else {
1631+ "`'_` cannot be used here"
1632+ } ;
1633+ self . r . lint_buffer . buffer_lint_with_diagnostic (
1634+ lint:: builtin:: ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT ,
1635+ node_id,
1636+ lifetime. ident . span ,
1637+ msg,
1638+ lint:: BuiltinLintDiagnostics :: AssociatedConstElidedLifetime {
1639+ elided,
1640+ span : lifetime. ident . span ,
1641+ } ,
1642+ ) ;
1643+ return ;
1644+ }
16191645 LifetimeRibKind :: AnonymousReportError => {
16201646 let ( msg, note) = if elided {
16211647 (
@@ -1811,7 +1837,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
18111837 //
18121838 // impl Foo for std::cell::Ref<u32> // note lack of '_
18131839 // async fn foo(_: std::cell::Ref<u32>) { ... }
1814- LifetimeRibKind :: AnonymousCreateParameter { report_in_path : true , .. } => {
1840+ LifetimeRibKind :: AnonymousCreateParameter { report_in_path : true , .. }
1841+ | LifetimeRibKind :: AnonymousWarnToStatic ( _) => {
18151842 let sess = self . r . tcx . sess ;
18161843 let mut err = rustc_errors:: struct_span_err!(
18171844 sess,
@@ -2898,7 +2925,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28982925 match & item. kind {
28992926 AssocItemKind :: Const ( box ast:: ConstItem { generics, ty, expr, .. } ) => {
29002927 debug ! ( "resolve_implementation AssocItemKind::Const" ) ;
2901-
29022928 self . with_generic_param_rib (
29032929 & generics. params ,
29042930 RibKind :: AssocItem ,
@@ -2908,28 +2934,33 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
29082934 kind : LifetimeBinderKind :: ConstItem ,
29092935 } ,
29102936 |this| {
2911- // If this is a trait impl, ensure the const
2912- // exists in trait
2913- this. check_trait_item (
2914- item. id ,
2915- item. ident ,
2916- & item. kind ,
2917- ValueNS ,
2918- item. span ,
2919- seen_trait_items,
2920- |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
2921- ) ;
2937+ this. with_lifetime_rib (
2938+ LifetimeRibKind :: AnonymousWarnToStatic ( item. id ) ,
2939+ |this| {
2940+ // If this is a trait impl, ensure the const
2941+ // exists in trait
2942+ this. check_trait_item (
2943+ item. id ,
2944+ item. ident ,
2945+ & item. kind ,
2946+ ValueNS ,
2947+ item. span ,
2948+ seen_trait_items,
2949+ |i, s, c| ConstNotMemberOfTrait ( i, s, c) ,
2950+ ) ;
29222951
2923- this. visit_generics ( generics) ;
2924- this. visit_ty ( ty) ;
2925- if let Some ( expr) = expr {
2926- // We allow arbitrary const expressions inside of associated consts,
2927- // even if they are potentially not const evaluatable.
2928- //
2929- // Type parameters can already be used and as associated consts are
2930- // not used as part of the type system, this is far less surprising.
2931- this. resolve_const_body ( expr, None ) ;
2932- }
2952+ this. visit_generics ( generics) ;
2953+ this. visit_ty ( ty) ;
2954+ if let Some ( expr) = expr {
2955+ // We allow arbitrary const expressions inside of associated consts,
2956+ // even if they are potentially not const evaluatable.
2957+ //
2958+ // Type parameters can already be used and as associated consts are
2959+ // not used as part of the type system, this is far less surprising.
2960+ this. resolve_const_body ( expr, None ) ;
2961+ }
2962+ } ,
2963+ ) ;
29332964 } ,
29342965 ) ;
29352966 }
0 commit comments