@@ -6,6 +6,12 @@ use rustc_ast::ast::LitKind;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: def_id:: DefIdSet ;
8
8
use rustc_hir:: {
9
+ def:: Res ,
10
+ PrimTy ,
11
+ QPath ,
12
+ TypeBindingKind ,
13
+ lang_items:: LangItem ,
14
+ GenericBound ,
9
15
def_id:: DefId , AssocItemKind , BinOpKind , Expr , ExprKind , FnRetTy , ImplItem , ImplItemKind , ImplicitSelfKind , Item ,
10
16
ItemKind , Mutability , Node , TraitItemRef , TyKind , UnOp ,
11
17
} ;
@@ -256,7 +262,87 @@ enum LenOutput<'tcx> {
256
262
Option ( DefId ) ,
257
263
Result ( DefId , Ty < ' tcx > ) ,
258
264
}
259
- fn parse_len_output < ' tcx > ( cx : & LateContext < ' _ > , sig : FnSig < ' tcx > ) -> Option < LenOutput < ' tcx > > {
265
+
266
+ fn extract_future_output < ' tcx > (
267
+ cx : & LateContext < ' tcx > ,
268
+ ty : Ty < ' tcx > ,
269
+ ) -> Option < Res > {
270
+
271
+ if let ty:: Alias ( _, alias_ty) = ty. kind ( ) {
272
+ let def_id = alias_ty. def_id ;
273
+ let hir_map = cx. tcx . hir ( ) ;
274
+ let item = hir_map. get_if_local ( def_id) ;
275
+ let item = item. unwrap ( ) ;
276
+
277
+ if let Node :: Item ( item) = item {
278
+ if let ItemKind :: OpaqueTy ( opaque) = & item. kind {
279
+ let bounds = opaque. bounds ;
280
+ let bound = & bounds[ 0 ] ;
281
+ if let GenericBound :: LangItemTrait ( item, _, _, generic_args) = bound {
282
+ if item != & LangItem :: Future {
283
+ return None ;
284
+ }
285
+
286
+ let bindings = generic_args. bindings ;
287
+ if bindings. len ( ) != 1 {
288
+ return None ;
289
+ }
290
+
291
+ let binding = & bindings[ 0 ] ;
292
+ let kind = & binding. kind ;
293
+
294
+ if let TypeBindingKind :: Equality { term : rustc_hir:: Term :: Ty ( term_ty) } = kind {
295
+ if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = & term_ty. kind {
296
+ let segments = & path. segments ;
297
+ let segment = & segments[ 0 ] ;
298
+ let res = segment. res ;
299
+
300
+ return Some ( res) ;
301
+ }
302
+ }
303
+ }
304
+ }
305
+ }
306
+ }
307
+
308
+ return None ;
309
+ }
310
+
311
+ // fn future_output_ty<'tcx>(trait_ref: &'tcx TraitRef<'tcx>) -> Option<&'tcx Ty<'tcx>> {
312
+ // if_chain! {
313
+ // if let Some(segment) = trait_ref.path.segments.last();
314
+ // if let Some(args) = segment.args;
315
+ // if args.bindings.len() == 1;
316
+ // let binding = &args.bindings[0];
317
+ // if binding.ident.name == sym::Output;
318
+ // if let TypeBindingKind::Equality { term: Term::Ty(output) } = binding.kind;
319
+ // then {
320
+ // return Some(output);
321
+ // }
322
+ // }
323
+
324
+ // None
325
+ // }
326
+
327
+ fn parse_len_output < ' tcx > ( cx : & LateContext < ' tcx > , sig : FnSig < ' tcx > ) -> Option < LenOutput < ' tcx > > {
328
+
329
+ if let Some ( res) = extract_future_output ( cx, sig. output ( ) ) {
330
+
331
+ if matches ! ( res, Res :: PrimTy ( PrimTy :: Uint ( _) ) ) || matches ! ( res, Res :: PrimTy ( PrimTy :: Int ( _) ) ) {
332
+ return Some ( LenOutput :: Integral ) ;
333
+ }
334
+
335
+ if let Res :: Def ( _, def_id) = res {
336
+ if cx. tcx . is_diagnostic_item ( sym:: Option , def_id) {
337
+ return Some ( LenOutput :: Option ( def_id) ) ;
338
+ } else if cx. tcx . is_diagnostic_item ( sym:: Result , def_id) {
339
+ return Some ( LenOutput :: Result ( def_id, cx. tcx . ty_error ( ) ) ) ;
340
+ }
341
+ }
342
+
343
+ panic ! ( ) ;
344
+ }
345
+
260
346
match * sig. output ( ) . kind ( ) {
261
347
ty:: Int ( _) | ty:: Uint ( _) => Some ( LenOutput :: Integral ) ,
262
348
ty:: Adt ( adt, subs) if cx. tcx . is_diagnostic_item ( sym:: Option , adt. did ( ) ) => {
@@ -271,7 +357,19 @@ fn parse_len_output<'tcx>(cx: &LateContext<'_>, sig: FnSig<'tcx>) -> Option<LenO
271
357
}
272
358
273
359
impl < ' tcx > LenOutput < ' tcx > {
274
- fn matches_is_empty_output ( self , ty : Ty < ' tcx > ) -> bool {
360
+ fn matches_is_empty_output ( self ,
361
+ cx : & LateContext < ' tcx > ,
362
+ ty : Ty < ' tcx > ) -> bool {
363
+
364
+ if let Some ( future_output) = extract_future_output ( cx, ty) {
365
+ return match ( self , future_output) {
366
+ ( _, Res :: PrimTy ( PrimTy :: Bool ) ) => true ,
367
+ ( Self :: Option ( _) , Res :: Def ( _, def_id) ) if cx. tcx . is_diagnostic_item ( sym:: Option , def_id) => true ,
368
+ ( Self :: Result ( _, _) , Res :: Def ( _, def_id) ) if cx. tcx . is_diagnostic_item ( sym:: Result , def_id) => true ,
369
+ _ => false ,
370
+ }
371
+ }
372
+
275
373
match ( self , ty. kind ( ) ) {
276
374
( _, & ty:: Bool ) => true ,
277
375
( Self :: Option ( id) , & ty:: Adt ( adt, subs) ) if id == adt. did ( ) => subs. type_at ( 0 ) . is_bool ( ) ,
@@ -301,9 +399,14 @@ impl<'tcx> LenOutput<'tcx> {
301
399
}
302
400
303
401
/// Checks if the given signature matches the expectations for `is_empty`
304
- fn check_is_empty_sig < ' tcx > ( sig : FnSig < ' tcx > , self_kind : ImplicitSelfKind , len_output : LenOutput < ' tcx > ) -> bool {
402
+ fn check_is_empty_sig < ' tcx > (
403
+ cx : & LateContext < ' tcx > ,
404
+ sig : FnSig < ' tcx > ,
405
+ self_kind : ImplicitSelfKind ,
406
+ len_output : LenOutput < ' tcx >
407
+ ) -> bool {
305
408
match & * * sig. inputs_and_output {
306
- [ arg, res] if len_output. matches_is_empty_output ( * res) => {
409
+ [ arg, res] if len_output. matches_is_empty_output ( cx , * res) => {
307
410
matches ! (
308
411
( arg. kind( ) , self_kind) ,
309
412
( ty:: Ref ( _, _, Mutability :: Not ) , ImplicitSelfKind :: ImmRef )
@@ -352,6 +455,7 @@ fn check_for_is_empty<'tcx>(
352
455
Some ( is_empty)
353
456
if !( is_empty. fn_has_self_parameter
354
457
&& check_is_empty_sig (
458
+ cx,
355
459
cx. tcx . fn_sig ( is_empty. def_id ) . subst_identity ( ) . skip_binder ( ) ,
356
460
self_kind,
357
461
output,
0 commit comments