@@ -1954,33 +1954,90 @@ impl<'tcx> TyCtxt<'tcx> {
1954
1954
if pred. kind ( ) != binder { self . mk_predicate ( binder) } else { pred }
1955
1955
}
1956
1956
1957
+ pub fn check_args_compatible ( self , def_id : DefId , args : & ' tcx [ ty:: GenericArg < ' tcx > ] ) -> bool {
1958
+ self . check_args_compatible_inner ( def_id, args, false )
1959
+ }
1960
+
1961
+ fn check_args_compatible_inner (
1962
+ self ,
1963
+ def_id : DefId ,
1964
+ args : & ' tcx [ ty:: GenericArg < ' tcx > ] ,
1965
+ nested : bool ,
1966
+ ) -> bool {
1967
+ let generics = self . generics_of ( def_id) ;
1968
+
1969
+ // IATs themselves have a weird arg setup (self + own args), but nested items *in* IATs
1970
+ // (namely: opaques, i.e. ATPITs) do not.
1971
+ let own_args = if !nested
1972
+ && let DefKind :: AssocTy = self . def_kind ( def_id)
1973
+ && let DefKind :: Impl { of_trait : false } = self . def_kind ( self . parent ( def_id) )
1974
+ {
1975
+ if generics. params . len ( ) + 1 != args. len ( ) {
1976
+ return false ;
1977
+ }
1978
+
1979
+ if !matches ! ( args[ 0 ] . unpack( ) , ty:: GenericArgKind :: Type ( _) ) {
1980
+ return false ;
1981
+ }
1982
+
1983
+ & args[ 1 ..]
1984
+ } else {
1985
+ if generics. count ( ) != args. len ( ) {
1986
+ return false ;
1987
+ }
1988
+
1989
+ let ( parent_args, own_args) = args. split_at ( generics. parent_count ) ;
1990
+
1991
+ if let Some ( parent) = generics. parent
1992
+ && !self . check_args_compatible_inner ( parent, parent_args, true )
1993
+ {
1994
+ return false ;
1995
+ }
1996
+
1997
+ own_args
1998
+ } ;
1999
+
2000
+ for ( param, arg) in std:: iter:: zip ( & generics. params , own_args) {
2001
+ match ( & param. kind , arg. unpack ( ) ) {
2002
+ ( ty:: GenericParamDefKind :: Type { .. } , ty:: GenericArgKind :: Type ( _) )
2003
+ | ( ty:: GenericParamDefKind :: Lifetime , ty:: GenericArgKind :: Lifetime ( _) )
2004
+ | ( ty:: GenericParamDefKind :: Const { .. } , ty:: GenericArgKind :: Const ( _) ) => { }
2005
+ _ => return false ,
2006
+ }
2007
+ }
2008
+
2009
+ true
2010
+ }
2011
+
2012
+ pub fn assert_args_compatible ( self , def_id : DefId , args : & ' tcx [ ty:: GenericArg < ' tcx > ] ) {
2013
+ if !self . check_args_compatible ( def_id, args) {
2014
+ if let DefKind :: AssocTy = self . def_kind ( def_id)
2015
+ && let DefKind :: Impl { of_trait : false } = self . def_kind ( self . parent ( def_id) )
2016
+ {
2017
+ bug ! ( )
2018
+ } else {
2019
+ bug ! (
2020
+ "args not compatible with generics for {}: args={:#?}, generics={:#?}" ,
2021
+ self . def_path_str( def_id) ,
2022
+ args,
2023
+ ty:: GenericArgs :: identity_for_item( self , def_id)
2024
+ ) ;
2025
+ }
2026
+ }
2027
+ }
2028
+
1957
2029
#[ inline( always) ]
1958
2030
pub ( crate ) fn check_and_mk_args (
1959
2031
self ,
1960
2032
_def_id : DefId ,
1961
2033
args : impl IntoIterator < Item : Into < GenericArg < ' tcx > > > ,
1962
2034
) -> GenericArgsRef < ' tcx > {
1963
- let args = args. into_iter ( ) . map ( Into :: into) ;
2035
+ let args = self . mk_args_from_iter ( args. into_iter ( ) . map ( Into :: into) ) ;
1964
2036
#[ cfg( debug_assertions) ]
1965
2037
{
1966
- let generics = self . generics_of ( _def_id) ;
1967
-
1968
- let n = if let DefKind :: AssocTy = self . def_kind ( _def_id)
1969
- && let DefKind :: Impl { of_trait : false } = self . def_kind ( self . parent ( _def_id) )
1970
- {
1971
- // If this is an inherent projection.
1972
- generics. params . len ( ) + 1
1973
- } else {
1974
- generics. count ( )
1975
- } ;
1976
- assert_eq ! (
1977
- ( n, Some ( n) ) ,
1978
- args. size_hint( ) ,
1979
- "wrong number of generic parameters for {_def_id:?}: {:?}" ,
1980
- args. collect:: <Vec <_>>( ) ,
1981
- ) ;
2038
+ self . assert_args_compatible ( _def_id, args) ;
1982
2039
}
1983
- self . mk_args_from_iter ( args)
2040
+ args
1984
2041
}
1985
2042
1986
2043
#[ inline]
0 commit comments