@@ -9,8 +9,7 @@ use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, Param, PatKind, QPath, Saf
9
9
use rustc_infer:: infer:: TyCtxtInferExt ;
10
10
use rustc_lint:: { LateContext , LateLintPass } ;
11
11
use rustc_middle:: ty:: {
12
- self , Binder , ClosureKind , FnSig , GenericArg , GenericArgKind , List , Region , RegionKind , Ty , TypeVisitableExt ,
13
- TypeckResults ,
12
+ self , Binder , ClosureKind , FnSig , GenericArg , GenericArgKind , List , Region , Ty , TypeVisitableExt , TypeckResults ,
14
13
} ;
15
14
use rustc_session:: declare_lint_pass;
16
15
use rustc_span:: symbol:: sym;
@@ -176,6 +175,17 @@ fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tc
176
175
}
177
176
} ,
178
177
} ;
178
+ if let Some ( outer) = outer_receiver
179
+ && ty_has_static ( sig. output ( ) )
180
+ && let generic_args = typeck. node_args ( outer. hir_id )
181
+ // Given a closure in `T.method(|| f())`, where `fn f() -> U where U: 'static`, `T.method(f)`
182
+ // will succeed iff `T: 'static`.
183
+ // But the region of `T` is always erased by `typeck.expr_ty()`. So we have a hack like this.
184
+ // && dbg!(generic_args).len() > 0
185
+ && generic_args. len ( ) > 0
186
+ {
187
+ return ;
188
+ }
179
189
if check_sig ( closure_sig, sig)
180
190
&& let generic_args = typeck. node_args ( callee. hir_id )
181
191
// Given some trait fn `fn f() -> ()` and some type `T: Trait`, `T::f` is not
@@ -275,7 +285,7 @@ fn check_sig<'tcx>(closure_sig: FnSig<'tcx>, call_sig: FnSig<'tcx>) -> bool {
275
285
/// This is needed because rustc is unable to late bind early-bound regions in a function signature.
276
286
fn has_late_bound_to_non_late_bound_regions ( from_sig : FnSig < ' _ > , to_sig : FnSig < ' _ > ) -> bool {
277
287
fn check_region ( from_region : Region < ' _ > , to_region : Region < ' _ > ) -> bool {
278
- matches ! ( from_region. kind ( ) , RegionKind :: ReBound ( .. ) ) && !matches ! ( to_region. kind ( ) , RegionKind :: ReBound ( .. ) )
288
+ from_region. is_bound ( ) && !to_region. is_bound ( )
279
289
}
280
290
281
291
fn check_subs ( from_subs : & [ GenericArg < ' _ > ] , to_subs : & [ GenericArg < ' _ > ] ) -> bool {
@@ -328,3 +338,8 @@ fn has_late_bound_to_non_late_bound_regions(from_sig: FnSig<'_>, to_sig: FnSig<'
328
338
. zip ( to_sig. inputs_and_output )
329
339
. any ( |( from_ty, to_ty) | check_ty ( from_ty, to_ty) )
330
340
}
341
+
342
+ fn ty_has_static ( ty : Ty < ' _ > ) -> bool {
343
+ ty. walk ( )
344
+ . any ( |arg| matches ! ( arg. unpack( ) , GenericArgKind :: Lifetime ( re) if re. is_static( ) ) )
345
+ }
0 commit comments