5
5
use crate :: prelude:: * ;
6
6
#[ cfg( feature = "rustc" ) ]
7
7
use rustc_middle:: { mir, ty} ;
8
- #[ cfg( feature = "rustc" ) ]
9
- use tracing:: trace;
10
8
11
9
#[ derive_group( Serializers ) ]
12
10
#[ derive( AdtInto , Clone , Debug , JsonSchema ) ]
@@ -342,58 +340,20 @@ pub struct Terminator {
342
340
}
343
341
344
342
#[ cfg( feature = "rustc" ) ]
345
- pub ( crate ) fn get_function_from_def_id_and_generics < ' tcx , S : BaseState < ' tcx > + HasOwnerId > (
343
+ /// Compute the
344
+ pub ( crate ) fn get_function_from_def_id_and_generics < ' tcx , S : UnderOwnerState < ' tcx > > (
346
345
s : & S ,
347
346
def_id : rustc_hir:: def_id:: DefId ,
348
347
generics : rustc_middle:: ty:: GenericArgsRef < ' tcx > ,
349
348
) -> ( DefId , Vec < GenericArg > , Vec < ImplExpr > , Option < ImplExpr > ) {
350
- let tcx = s. base ( ) . tcx ;
351
-
352
- // Retrieve the trait requirements for the **method**.
353
- // For instance, if we write:
354
- // ```
355
- // fn foo<T : Bar>(...)
356
- // ^^^
357
- // ```
358
- let mut trait_refs = solve_item_required_traits ( s, def_id, generics) ;
349
+ // Retrieve the trait requirements for the item.
350
+ let trait_refs = solve_item_required_traits ( s, def_id, generics) ;
359
351
352
+ // If this is a trait method call, retreive the impl expr information for that trait.
360
353
// Check if this is a trait method call: retrieve the trait source if
361
- // it is the case (i.e., where does the method come from? Does it refer
362
- // to a top-level implementation? Or the method of a parameter? etc.).
363
- // At the same time, retrieve the trait obligations for this **trait**.
364
- // Remark: the trait obligations for the method are not the same as
365
- // the trait obligations for the trait. More precisely:
366
- //
367
- // ```
368
- // trait Foo<T : Bar> {
369
- // ^^^^^
370
- // trait level trait obligation
371
- // fn baz(...) where T : ... {
372
- // ... ^^^
373
- // method level trait obligation
374
- // }
375
- // }
376
- // ```
377
- //
378
- // Also, a function doesn't need to belong to a trait to have trait
379
- // obligations:
380
- // ```
381
- // fn foo<T : Bar>(...)
382
- // ^^^
383
- // method level trait obligation
384
- // ```
385
- let ( generics, source) = if let Some ( assoc) = tcx. opt_associated_item ( def_id) {
386
- // There is an associated item.
387
- use tracing:: * ;
388
- trace ! ( "def_id: {:?}" , def_id) ;
389
- trace ! ( "assoc: def_id: {:?}" , assoc. def_id) ;
390
- // Retrieve the `DefId` of the trait declaration or the impl block.
391
- let container_def_id = match assoc. container {
392
- rustc_middle:: ty:: AssocItemContainer :: Trait => tcx. trait_of_item ( assoc. def_id ) . unwrap ( ) ,
393
- rustc_middle:: ty:: AssocItemContainer :: Impl => tcx. impl_of_method ( assoc. def_id ) . unwrap ( ) ,
394
- } ;
395
- // The generics are split in two: the arguments of the container (trait decl or impl block)
396
- // and the arguments of the method.
354
+ let ( generics, trait_impl) = if let Some ( tinfo) = self_clause_for_item ( s, def_id, generics) {
355
+ // The generics are split in two: the arguments of the container (trait decl or impl
356
+ // block) and the arguments of the method.
397
357
//
398
358
// For instance, if we have:
399
359
// ```
@@ -408,44 +368,16 @@ pub(crate) fn get_function_from_def_id_and_generics<'tcx, S: BaseState<'tcx> + H
408
368
// ```
409
369
// The generics for the call to `baz` will be the concatenation: `<T, u32, U>`, which we
410
370
// split into `<T, u32>` and `<U>`.
411
- //
412
- // If we have:
413
- // ```
414
- // impl<T: Ord> Map<T> {
415
- // pub fn insert<U: Clone>(&mut self, x: U) { ... }
416
- // }
417
- // pub fn test(mut tree: Map<u32>) {
418
- // tree.insert(false);
419
- // }
420
- // ```
421
- // The generics for `insert` are `<u32>` for the impl and `<bool>` for the method.
422
- match assoc. container {
423
- rustc_middle:: ty:: AssocItemContainer :: Trait => {
424
- let num_container_generics = tcx. generics_of ( container_def_id) . own_params . len ( ) ;
425
- // Retrieve the trait information
426
- let impl_expr = self_clause_for_item ( s, & assoc, generics) . unwrap ( ) ;
427
- // Return only the method generics; the trait generics are included in `impl_expr`.
428
- let method_generics = & generics[ num_container_generics..] ;
429
- ( method_generics. sinto ( s) , Some ( impl_expr) )
430
- }
431
- rustc_middle:: ty:: AssocItemContainer :: Impl => {
432
- // Solve the trait constraints of the impl block.
433
- let container_generics = tcx. generics_of ( container_def_id) ;
434
- let container_generics = generics. truncate_to ( tcx, container_generics) ;
435
- // Prepend the container trait refs.
436
- let mut combined_trait_refs =
437
- solve_item_required_traits ( s, container_def_id, container_generics) ;
438
- combined_trait_refs. extend ( std:: mem:: take ( & mut trait_refs) ) ;
439
- trait_refs = combined_trait_refs;
440
- ( generics. sinto ( s) , None )
441
- }
442
- }
371
+ let num_trait_generics = tinfo. r#trait . hax_skip_binder_ref ( ) . generic_args . len ( ) ;
372
+ // Return only the method generics; the trait generics are included in `trait_impl_expr`.
373
+ let method_generics = & generics[ num_trait_generics..] ;
374
+ ( method_generics. sinto ( s) , Some ( tinfo) )
443
375
} else {
444
376
// Regular function call
445
377
( generics. sinto ( s) , None )
446
378
} ;
447
379
448
- ( def_id. sinto ( s) , generics, trait_refs, source )
380
+ ( def_id. sinto ( s) , generics, trait_refs, trait_impl )
449
381
}
450
382
451
383
#[ cfg( feature = "rustc" ) ]
@@ -965,7 +897,8 @@ pub enum AggregateKind {
965
897
generics. sinto( s) ,
966
898
trait_refs,
967
899
annot. sinto( s) ,
968
- fid. sinto( s) )
900
+ fid. sinto( s) ,
901
+ )
969
902
} ) ]
970
903
Adt (
971
904
DefId ,
0 commit comments