@@ -2016,36 +2016,48 @@ impl<'tcx> TyCtxt<'tcx> {
20162016 true
20172017 }
20182018
2019+ /// With `cfg(debug_assertions)`, assert that args are compatible with their generics,
2020+ /// and print out the args if not.
20192021 pub fn debug_assert_args_compatible ( self , def_id : DefId , args : & ' tcx [ ty:: GenericArg < ' tcx > ] ) {
2020- #[ cfg( not( debug_assertions) ) ]
2021- {
2022- return ;
2023- }
2024-
2025- if !self . check_args_compatible ( def_id, args) {
2026- if let DefKind :: AssocTy = self . def_kind ( def_id)
2027- && let DefKind :: Impl { of_trait : false } = self . def_kind ( self . parent ( def_id) )
2028- {
2029- bug ! ( )
2030- } else {
2031- bug ! (
2032- "args not compatible with generics for {}: args={:#?}, generics={:#?}" ,
2033- self . def_path_str( def_id) ,
2034- args,
2035- ty:: GenericArgs :: identity_for_item( self , def_id)
2036- ) ;
2022+ if cfg ! ( debug_assertions) {
2023+ if !self . check_args_compatible ( def_id, args) {
2024+ if let DefKind :: AssocTy = self . def_kind ( def_id)
2025+ && let DefKind :: Impl { of_trait : false } = self . def_kind ( self . parent ( def_id) )
2026+ {
2027+ bug ! (
2028+ "args not compatible with generics for {}: args={:#?}, generics={:#?}" ,
2029+ self . def_path_str( def_id) ,
2030+ args,
2031+ // Make `[Self, GAT_ARGS...]` (this could be simplified)
2032+ self . mk_args_from_iter(
2033+ [ self . types. self_param. into( ) ] . into_iter( ) . chain(
2034+ self . generics_of( def_id)
2035+ . own_args( ty:: GenericArgs :: identity_for_item( self , def_id) )
2036+ . iter( )
2037+ . copied( )
2038+ )
2039+ )
2040+ ) ;
2041+ } else {
2042+ bug ! (
2043+ "args not compatible with generics for {}: args={:#?}, generics={:#?}" ,
2044+ self . def_path_str( def_id) ,
2045+ args,
2046+ ty:: GenericArgs :: identity_for_item( self , def_id)
2047+ ) ;
2048+ }
20372049 }
20382050 }
20392051 }
20402052
20412053 #[ inline( always) ]
20422054 pub ( crate ) fn check_and_mk_args (
20432055 self ,
2044- _def_id : DefId ,
2056+ def_id : DefId ,
20452057 args : impl IntoIterator < Item : Into < GenericArg < ' tcx > > > ,
20462058 ) -> GenericArgsRef < ' tcx > {
20472059 let args = self . mk_args_from_iter ( args. into_iter ( ) . map ( Into :: into) ) ;
2048- self . debug_assert_args_compatible ( _def_id , args) ;
2060+ self . debug_assert_args_compatible ( def_id , args) ;
20492061 args
20502062 }
20512063
0 commit comments