@@ -27,7 +27,7 @@ use rustc::ty::subst::{Subst, InternalSubsts};
27
27
use rustc:: ty:: util:: Discr ;
28
28
use rustc:: ty:: util:: IntTypeExt ;
29
29
use rustc:: ty:: subst:: UnpackedKind ;
30
- use rustc:: ty:: { self , AdtKind , ToPolyTraitRef , Ty , TyCtxt } ;
30
+ use rustc:: ty:: { self , AdtKind , DefIdTree , ToPolyTraitRef , Ty , TyCtxt } ;
31
31
use rustc:: ty:: { ReprOptions , ToPredicate } ;
32
32
use rustc:: util:: captures:: Captures ;
33
33
use rustc:: util:: nodemap:: FxHashMap ;
@@ -1349,65 +1349,61 @@ pub fn checked_type_of<'a, 'tcx>(
1349
1349
1350
1350
match path {
1351
1351
QPath :: Resolved ( _, ref path) => {
1352
- let mut arg_index = 0 ;
1353
- let mut found_const = false ;
1354
- for seg in & path. segments {
1355
- if let Some ( generic_args) = & seg. args {
1356
- let args = & generic_args. args ;
1357
- for arg in args {
1358
- if let GenericArg :: Const ( ct) = arg {
1359
- if ct. value . hir_id == hir_id {
1360
- found_const = true ;
1361
- break ;
1362
- }
1363
- arg_index += 1 ;
1364
- }
1365
- }
1366
- }
1367
- }
1368
- // Sanity check to make sure everything is as expected.
1369
- if !found_const {
1370
- if !fail {
1371
- return None ;
1372
- }
1373
- bug ! ( "no arg matching AnonConst in path" )
1374
- }
1375
- match path. res {
1376
- // We've encountered an `AnonConst` in some path, so we need to
1377
- // figure out which generic parameter it corresponds to and return
1378
- // the relevant type.
1379
- Res :: Def ( DefKind :: Struct , def_id)
1380
- | Res :: Def ( DefKind :: Union , def_id)
1381
- | Res :: Def ( DefKind :: Enum , def_id)
1382
- | Res :: Def ( DefKind :: Fn , def_id) => {
1383
- let generics = tcx. generics_of ( def_id) ;
1384
- let mut param_index = 0 ;
1385
- for param in & generics. params {
1386
- if let ty:: GenericParamDefKind :: Const = param. kind {
1387
- if param_index == arg_index {
1388
- return Some ( tcx. type_of ( param. def_id ) ) ;
1389
- }
1390
- param_index += 1 ;
1391
- }
1392
- }
1393
- // This is no generic parameter associated with the arg. This is
1394
- // probably from an extra arg where one is not needed.
1395
- return Some ( tcx. types . err ) ;
1396
- }
1397
- Res :: Err => tcx. types . err ,
1398
- x => {
1352
+ let arg_index = path. segments . iter ( )
1353
+ . filter_map ( |seg| seg. args . as_ref ( ) )
1354
+ . map ( |generic_args| generic_args. args . as_ref ( ) )
1355
+ . find_map ( |args| {
1356
+ args. iter ( )
1357
+ . filter ( |arg| arg. is_const ( ) )
1358
+ . enumerate ( )
1359
+ . filter ( |( _, arg) | arg. id ( ) == hir_id)
1360
+ . map ( |( index, _) | index)
1361
+ . next ( )
1362
+ } )
1363
+ . or_else ( || {
1399
1364
if !fail {
1400
- return None ;
1365
+ None
1366
+ } else {
1367
+ bug ! ( "no arg matching AnonConst in path" )
1401
1368
}
1369
+ } ) ?;
1370
+
1371
+ // We've encountered an `AnonConst` in some path, so we need to
1372
+ // figure out which generic parameter it corresponds to and return
1373
+ // the relevant type.
1374
+ let generics = match path. res {
1375
+ Res :: Def ( DefKind :: Ctor ( ..) , def_id) =>
1376
+ tcx. generics_of ( tcx. parent ( def_id) . unwrap ( ) ) ,
1377
+ Res :: Def ( _, def_id) =>
1378
+ tcx. generics_of ( def_id) ,
1379
+ Res :: Err =>
1380
+ return Some ( tcx. types . err ) ,
1381
+ _ if !fail =>
1382
+ return None ,
1383
+ x => {
1402
1384
tcx. sess . delay_span_bug (
1403
1385
DUMMY_SP ,
1404
1386
& format ! (
1405
1387
"unexpected const parent path def {:?}" , x
1406
1388
) ,
1407
1389
) ;
1408
- tcx. types . err
1390
+ return Some ( tcx. types . err ) ;
1409
1391
}
1410
- }
1392
+ } ;
1393
+
1394
+ generics. params . iter ( )
1395
+ . filter ( |param| {
1396
+ if let ty:: GenericParamDefKind :: Const = param. kind {
1397
+ true
1398
+ } else {
1399
+ false
1400
+ }
1401
+ } )
1402
+ . nth ( arg_index)
1403
+ . map ( |param| tcx. type_of ( param. def_id ) )
1404
+ // This is no generic parameter associated with the arg. This is
1405
+ // probably from an extra arg where one is not needed.
1406
+ . unwrap_or ( tcx. types . err )
1411
1407
}
1412
1408
x => {
1413
1409
if !fail {
0 commit comments