3
3
#![ allow( clippy:: module_name_repetitions) ]
4
4
5
5
use core:: ops:: ControlFlow ;
6
+ use itertools:: Itertools ;
6
7
use rustc_ast:: ast:: Mutability ;
7
8
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
8
9
use rustc_hir as hir;
@@ -13,17 +14,19 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
13
14
use rustc_infer:: infer:: TyCtxtInferExt ;
14
15
use rustc_lint:: LateContext ;
15
16
use rustc_middle:: mir:: interpret:: { ConstValue , Scalar } ;
17
+ use rustc_middle:: traits:: EvaluationResult ;
16
18
use rustc_middle:: ty:: layout:: ValidityRequirement ;
17
19
use rustc_middle:: ty:: {
18
- self , AdtDef , AliasTy , AssocKind , Binder , BoundRegion , FnSig , GenericArg , GenericArgKind , GenericArgsRef , IntTy ,
19
- List , ParamEnv , Region , RegionKind , Ty , TyCtxt , TypeSuperVisitable , TypeVisitable , TypeVisitableExt , TypeVisitor ,
20
- UintTy , VariantDef , VariantDiscr ,
20
+ self , AdtDef , AliasTy , AssocKind , Binder , BoundRegion , FnSig , GenericArg , GenericArgKind , GenericArgsRef ,
21
+ GenericParamDefKind , IntTy , List , ParamEnv , Region , RegionKind , ToPredicate , TraitRef , Ty , TyCtxt ,
22
+ TypeSuperVisitable , TypeVisitable , TypeVisitableExt , TypeVisitor , UintTy , VariantDef , VariantDiscr ,
21
23
} ;
22
24
use rustc_span:: symbol:: Ident ;
23
25
use rustc_span:: { sym, Span , Symbol , DUMMY_SP } ;
24
26
use rustc_target:: abi:: { Size , VariantIdx } ;
25
- use rustc_trait_selection:: infer :: InferCtxtExt ;
27
+ use rustc_trait_selection:: traits :: query :: evaluate_obligation :: InferCtxtExt as _ ;
26
28
use rustc_trait_selection:: traits:: query:: normalize:: QueryNormalizeExt ;
29
+ use rustc_trait_selection:: traits:: { Obligation , ObligationCause } ;
27
30
use std:: iter;
28
31
29
32
use crate :: { match_def_path, path_res, paths} ;
@@ -207,15 +210,9 @@ pub fn implements_trait<'tcx>(
207
210
cx : & LateContext < ' tcx > ,
208
211
ty : Ty < ' tcx > ,
209
212
trait_id : DefId ,
210
- ty_params : & [ GenericArg < ' tcx > ] ,
213
+ args : & [ GenericArg < ' tcx > ] ,
211
214
) -> bool {
212
- implements_trait_with_env (
213
- cx. tcx ,
214
- cx. param_env ,
215
- ty,
216
- trait_id,
217
- ty_params. iter ( ) . map ( |& arg| Some ( arg) ) ,
218
- )
215
+ implements_trait_with_env_from_iter ( cx. tcx , cx. param_env , ty, trait_id, args. iter ( ) . map ( |& x| Some ( x) ) )
219
216
}
220
217
221
218
/// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context.
@@ -224,7 +221,18 @@ pub fn implements_trait_with_env<'tcx>(
224
221
param_env : ParamEnv < ' tcx > ,
225
222
ty : Ty < ' tcx > ,
226
223
trait_id : DefId ,
227
- ty_params : impl IntoIterator < Item = Option < GenericArg < ' tcx > > > ,
224
+ args : & [ GenericArg < ' tcx > ] ,
225
+ ) -> bool {
226
+ implements_trait_with_env_from_iter ( tcx, param_env, ty, trait_id, args. iter ( ) . map ( |& x| Some ( x) ) )
227
+ }
228
+
229
+ /// Same as `implements_trait_from_env` but takes the arguments as an iterator.
230
+ pub fn implements_trait_with_env_from_iter < ' tcx > (
231
+ tcx : TyCtxt < ' tcx > ,
232
+ param_env : ParamEnv < ' tcx > ,
233
+ ty : Ty < ' tcx > ,
234
+ trait_id : DefId ,
235
+ args : impl IntoIterator < Item = impl Into < Option < GenericArg < ' tcx > > > > ,
228
236
) -> bool {
229
237
// Clippy shouldn't have infer types
230
238
assert ! ( !ty. has_infer( ) ) ;
@@ -233,19 +241,37 @@ pub fn implements_trait_with_env<'tcx>(
233
241
if ty. has_escaping_bound_vars ( ) {
234
242
return false ;
235
243
}
244
+
236
245
let infcx = tcx. infer_ctxt ( ) . build ( ) ;
237
- let orig = TypeVariableOrigin {
238
- kind : TypeVariableOriginKind :: MiscVariable ,
239
- span : DUMMY_SP ,
240
- } ;
241
- let ty_params = tcx. mk_args_from_iter (
242
- ty_params
246
+ let trait_ref = TraitRef :: new (
247
+ tcx,
248
+ trait_id,
249
+ Some ( GenericArg :: from ( ty) )
243
250
. into_iter ( )
244
- . map ( |arg| arg. unwrap_or_else ( || infcx. next_ty_var ( orig) . into ( ) ) ) ,
251
+ . chain ( args. into_iter ( ) . map ( |arg| {
252
+ arg. into ( ) . unwrap_or_else ( || {
253
+ let orig = TypeVariableOrigin {
254
+ kind : TypeVariableOriginKind :: MiscVariable ,
255
+ span : DUMMY_SP ,
256
+ } ;
257
+ infcx. next_ty_var ( orig) . into ( )
258
+ } )
259
+ } ) ) ,
245
260
) ;
261
+
262
+ debug_assert_eq ! ( tcx. def_kind( trait_id) , DefKind :: Trait ) ;
263
+ #[ cfg( debug_assertions) ]
264
+ assert_generic_args_match ( tcx, trait_id, trait_ref. args ) ;
265
+
266
+ let obligation = Obligation {
267
+ cause : ObligationCause :: dummy ( ) ,
268
+ param_env,
269
+ recursion_depth : 0 ,
270
+ predicate : ty:: Binder :: dummy ( trait_ref) . without_const ( ) . to_predicate ( tcx) ,
271
+ } ;
246
272
infcx
247
- . type_implements_trait ( trait_id , [ ty . into ( ) ] . into_iter ( ) . chain ( ty_params ) , param_env )
248
- . must_apply_modulo_regions ( )
273
+ . evaluate_obligation ( & obligation )
274
+ . is_ok_and ( EvaluationResult :: must_apply_modulo_regions )
249
275
}
250
276
251
277
/// Checks whether this type implements `Drop`.
@@ -393,6 +419,11 @@ pub fn is_type_lang_item(cx: &LateContext<'_>, ty: Ty<'_>, lang_item: hir::LangI
393
419
}
394
420
}
395
421
422
+ /// Gets the diagnostic name of the type, if it has one
423
+ pub fn type_diagnostic_name ( cx : & LateContext < ' _ > , ty : Ty < ' _ > ) -> Option < Symbol > {
424
+ ty. ty_adt_def ( ) . and_then ( |adt| cx. tcx . get_diagnostic_name ( adt. did ( ) ) )
425
+ }
426
+
396
427
/// Return `true` if the passed `typ` is `isize` or `usize`.
397
428
pub fn is_isize_or_usize ( typ : Ty < ' _ > ) -> bool {
398
429
matches ! ( typ. kind( ) , ty:: Int ( IntTy :: Isize ) | ty:: Uint ( UintTy :: Usize ) )
@@ -1014,12 +1045,60 @@ pub fn approx_ty_size<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> u64 {
1014
1045
}
1015
1046
}
1016
1047
1048
+ /// Asserts that the given arguments match the generic parameters of the given item.
1049
+ #[ allow( dead_code) ]
1050
+ fn assert_generic_args_match < ' tcx > ( tcx : TyCtxt < ' tcx > , did : DefId , args : & [ GenericArg < ' tcx > ] ) {
1051
+ let g = tcx. generics_of ( did) ;
1052
+ let parent = g. parent . map ( |did| tcx. generics_of ( did) ) ;
1053
+ let count = g. parent_count + g. params . len ( ) ;
1054
+ let params = parent
1055
+ . map_or ( [ ] . as_slice ( ) , |p| p. params . as_slice ( ) )
1056
+ . iter ( )
1057
+ . chain ( & g. params )
1058
+ . map ( |x| & x. kind ) ;
1059
+
1060
+ assert ! (
1061
+ count == args. len( ) ,
1062
+ "wrong number of arguments for `{did:?}`: expected `{count}`, found {}\n \
1063
+ note: the expected arguments are: `[{}]`\n \
1064
+ the given arguments are: `{args:#?}`",
1065
+ args. len( ) ,
1066
+ params. clone( ) . map( GenericParamDefKind :: descr) . format( ", " ) ,
1067
+ ) ;
1068
+
1069
+ if let Some ( ( idx, ( param, arg) ) ) =
1070
+ params
1071
+ . clone ( )
1072
+ . zip ( args. iter ( ) . map ( |& x| x. unpack ( ) ) )
1073
+ . enumerate ( )
1074
+ . find ( |( _, ( param, arg) ) | match ( param, arg) {
1075
+ ( GenericParamDefKind :: Lifetime , GenericArgKind :: Lifetime ( _) )
1076
+ | ( GenericParamDefKind :: Type { .. } , GenericArgKind :: Type ( _) )
1077
+ | ( GenericParamDefKind :: Const { .. } , GenericArgKind :: Const ( _) ) => false ,
1078
+ (
1079
+ GenericParamDefKind :: Lifetime
1080
+ | GenericParamDefKind :: Type { .. }
1081
+ | GenericParamDefKind :: Const { .. } ,
1082
+ _,
1083
+ ) => true ,
1084
+ } )
1085
+ {
1086
+ panic ! (
1087
+ "incorrect argument for `{did:?}` at index `{idx}`: expected a {}, found `{arg:?}`\n \
1088
+ note: the expected arguments are `[{}]`\n \
1089
+ the given arguments are `{args:#?}`",
1090
+ param. descr( ) ,
1091
+ params. clone( ) . map( GenericParamDefKind :: descr) . format( ", " ) ,
1092
+ ) ;
1093
+ }
1094
+ }
1095
+
1017
1096
/// Makes the projection type for the named associated type in the given impl or trait impl.
1018
1097
///
1019
1098
/// This function is for associated types which are "known" to exist, and as such, will only return
1020
1099
/// `None` when debug assertions are disabled in order to prevent ICE's. With debug assertions
1021
1100
/// enabled this will check that the named associated type exists, the correct number of
1022
- /// substitutions are given, and that the correct kinds of substitutions are given (lifetime,
1101
+ /// arguments are given, and that the correct kinds of arguments are given (lifetime,
1023
1102
/// constant or type). This will not check if type normalization would succeed.
1024
1103
pub fn make_projection < ' tcx > (
1025
1104
tcx : TyCtxt < ' tcx > ,
@@ -1043,49 +1122,7 @@ pub fn make_projection<'tcx>(
1043
1122
return None ;
1044
1123
} ;
1045
1124
#[ cfg( debug_assertions) ]
1046
- {
1047
- let generics = tcx. generics_of ( assoc_item. def_id ) ;
1048
- let generic_count = generics. parent_count + generics. params . len ( ) ;
1049
- let params = generics
1050
- . parent
1051
- . map_or ( [ ] . as_slice ( ) , |id| & * tcx. generics_of ( id) . params )
1052
- . iter ( )
1053
- . chain ( & generics. params )
1054
- . map ( |x| & x. kind ) ;
1055
-
1056
- debug_assert ! (
1057
- generic_count == args. len( ) ,
1058
- "wrong number of args for `{:?}`: found `{}` expected `{generic_count}`.\n \
1059
- note: the expected parameters are: {:#?}\n \
1060
- the given arguments are: `{args:#?}`",
1061
- assoc_item. def_id,
1062
- args. len( ) ,
1063
- params. map( ty:: GenericParamDefKind :: descr) . collect:: <Vec <_>>( ) ,
1064
- ) ;
1065
-
1066
- if let Some ( ( idx, ( param, arg) ) ) = params
1067
- . clone ( )
1068
- . zip ( args. iter ( ) . map ( GenericArg :: unpack) )
1069
- . enumerate ( )
1070
- . find ( |( _, ( param, arg) ) | {
1071
- !matches ! (
1072
- ( param, arg) ,
1073
- ( ty:: GenericParamDefKind :: Lifetime , GenericArgKind :: Lifetime ( _) )
1074
- | ( ty:: GenericParamDefKind :: Type { .. } , GenericArgKind :: Type ( _) )
1075
- | ( ty:: GenericParamDefKind :: Const { .. } , GenericArgKind :: Const ( _) )
1076
- )
1077
- } )
1078
- {
1079
- debug_assert ! (
1080
- false ,
1081
- "mismatched subst type at index {idx}: expected a {}, found `{arg:?}`\n \
1082
- note: the expected parameters are {:#?}\n \
1083
- the given arguments are {args:#?}",
1084
- param. descr( ) ,
1085
- params. map( ty:: GenericParamDefKind :: descr) . collect:: <Vec <_>>( )
1086
- ) ;
1087
- }
1088
- }
1125
+ assert_generic_args_match ( tcx, assoc_item. def_id , args) ;
1089
1126
1090
1127
Some ( tcx. mk_alias_ty ( assoc_item. def_id , args) )
1091
1128
}
@@ -1100,7 +1137,7 @@ pub fn make_projection<'tcx>(
1100
1137
/// Normalizes the named associated type in the given impl or trait impl.
1101
1138
///
1102
1139
/// This function is for associated types which are "known" to be valid with the given
1103
- /// substitutions , and as such, will only return `None` when debug assertions are disabled in order
1140
+ /// arguments , and as such, will only return `None` when debug assertions are disabled in order
1104
1141
/// to prevent ICE's. With debug assertions enabled this will check that type normalization
1105
1142
/// succeeds as well as everything checked by `make_projection`.
1106
1143
pub fn make_normalized_projection < ' tcx > (
@@ -1112,17 +1149,12 @@ pub fn make_normalized_projection<'tcx>(
1112
1149
) -> Option < Ty < ' tcx > > {
1113
1150
fn helper < ' tcx > ( tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > , ty : AliasTy < ' tcx > ) -> Option < Ty < ' tcx > > {
1114
1151
#[ cfg( debug_assertions) ]
1115
- if let Some ( ( i, subst) ) = ty
1116
- . args
1117
- . iter ( )
1118
- . enumerate ( )
1119
- . find ( |( _, subst) | subst. has_late_bound_regions ( ) )
1120
- {
1152
+ if let Some ( ( i, arg) ) = ty. args . iter ( ) . enumerate ( ) . find ( |( _, arg) | arg. has_late_bound_regions ( ) ) {
1121
1153
debug_assert ! (
1122
1154
false ,
1123
1155
"args contain late-bound region at index `{i}` which can't be normalized.\n \
1124
1156
use `TyCtxt::erase_late_bound_regions`\n \
1125
- note: subst is `{subst :#?}`",
1157
+ note: arg is `{arg :#?}`",
1126
1158
) ;
1127
1159
return None ;
1128
1160
}
@@ -1190,17 +1222,12 @@ pub fn make_normalized_projection_with_regions<'tcx>(
1190
1222
) -> Option < Ty < ' tcx > > {
1191
1223
fn helper < ' tcx > ( tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > , ty : AliasTy < ' tcx > ) -> Option < Ty < ' tcx > > {
1192
1224
#[ cfg( debug_assertions) ]
1193
- if let Some ( ( i, subst) ) = ty
1194
- . args
1195
- . iter ( )
1196
- . enumerate ( )
1197
- . find ( |( _, subst) | subst. has_late_bound_regions ( ) )
1198
- {
1225
+ if let Some ( ( i, arg) ) = ty. args . iter ( ) . enumerate ( ) . find ( |( _, arg) | arg. has_late_bound_regions ( ) ) {
1199
1226
debug_assert ! (
1200
1227
false ,
1201
1228
"args contain late-bound region at index `{i}` which can't be normalized.\n \
1202
1229
use `TyCtxt::erase_late_bound_regions`\n \
1203
- note: subst is `{subst :#?}`",
1230
+ note: arg is `{arg :#?}`",
1204
1231
) ;
1205
1232
return None ;
1206
1233
}
0 commit comments