@@ -1130,20 +1130,39 @@ fn report_assoc_ty_on_inherent_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, span:
1130
1130
}
1131
1131
1132
1132
fn type_of < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , def_id : DefId ) -> Ty < ' tcx > {
1133
+ checked_type_of ( tcx, def_id, true ) . unwrap ( )
1134
+ }
1135
+
1136
+ pub fn checked_type_of < ' a , ' tcx > (
1137
+ tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
1138
+ def_id : DefId ,
1139
+ fail : bool ,
1140
+ ) -> Option < Ty < ' tcx > > {
1133
1141
use rustc:: hir:: * ;
1134
1142
1135
- let hir_id = tcx. hir ( ) . as_local_hir_id ( def_id) . unwrap ( ) ;
1143
+ let hir_id = match tcx. hir ( ) . as_local_hir_id ( def_id) {
1144
+ Some ( hir_id) => hir_id,
1145
+ None => {
1146
+ if !fail {
1147
+ return None ;
1148
+ }
1149
+ bug ! ( "invalid node" ) ;
1150
+ }
1151
+ } ;
1136
1152
1137
1153
let icx = ItemCtxt :: new ( tcx, def_id) ;
1138
1154
1139
- match tcx. hir ( ) . get_by_hir_id ( hir_id) {
1155
+ Some ( match tcx. hir ( ) . get_by_hir_id ( hir_id) {
1140
1156
Node :: TraitItem ( item) => match item. node {
1141
1157
TraitItemKind :: Method ( ..) => {
1142
1158
let substs = InternalSubsts :: identity_for_item ( tcx, def_id) ;
1143
1159
tcx. mk_fn_def ( def_id, substs)
1144
1160
}
1145
1161
TraitItemKind :: Const ( ref ty, _) | TraitItemKind :: Type ( _, Some ( ref ty) ) => icx. to_ty ( ty) ,
1146
1162
TraitItemKind :: Type ( _, None ) => {
1163
+ if !fail {
1164
+ return None ;
1165
+ }
1147
1166
span_bug ! ( item. span, "associated type missing default" ) ;
1148
1167
}
1149
1168
} ,
@@ -1225,6 +1244,9 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Ty<'tcx> {
1225
1244
| ItemKind :: GlobalAsm ( ..)
1226
1245
| ItemKind :: ExternCrate ( ..)
1227
1246
| ItemKind :: Use ( ..) => {
1247
+ if !fail {
1248
+ return None ;
1249
+ }
1228
1250
span_bug ! (
1229
1251
item. span,
1230
1252
"compute_type_of_item: unexpected item type: {:?}" ,
@@ -1264,7 +1286,7 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Ty<'tcx> {
1264
1286
..
1265
1287
} ) => {
1266
1288
if gen. is_some ( ) {
1267
- return tcx. typeck_tables_of ( def_id) . node_type ( hir_id) ;
1289
+ return Some ( tcx. typeck_tables_of ( def_id) . node_type ( hir_id) ) ;
1268
1290
}
1269
1291
1270
1292
let substs = ty:: ClosureSubsts {
@@ -1342,6 +1364,9 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Ty<'tcx> {
1342
1364
}
1343
1365
// Sanity check to make sure everything is as expected.
1344
1366
if !found_const {
1367
+ if !fail {
1368
+ return None ;
1369
+ }
1345
1370
bug ! ( "no arg matching AnonConst in path" )
1346
1371
}
1347
1372
match path. def {
@@ -1367,14 +1392,27 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Ty<'tcx> {
1367
1392
return tcx. types . err ;
1368
1393
}
1369
1394
Def :: Err => tcx. types . err ,
1370
- x => bug ! ( "unexpected const parent path def {:?}" , x) ,
1395
+ x => {
1396
+ if !fail {
1397
+ return None ;
1398
+ }
1399
+ bug ! ( "unexpected const parent path def {:?}" , x) ;
1400
+ }
1401
+ }
1402
+ }
1403
+ x => {
1404
+ if !fail {
1405
+ return None ;
1371
1406
}
1407
+ bug ! ( "unexpected const parent path {:?}" , x) ;
1372
1408
}
1373
- x => bug ! ( "unexpected const parent path {:?}" , x) ,
1374
1409
}
1375
1410
}
1376
1411
1377
1412
x => {
1413
+ if !fail {
1414
+ return None ;
1415
+ }
1378
1416
bug ! ( "unexpected const parent in type_of_def_id(): {:?}" , x) ;
1379
1417
}
1380
1418
}
@@ -1385,13 +1423,21 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Ty<'tcx> {
1385
1423
hir:: GenericParamKind :: Const { ref ty, .. } => {
1386
1424
icx. to_ty ( ty)
1387
1425
}
1388
- x => bug ! ( "unexpected non-type Node::GenericParam: {:?}" , x) ,
1426
+ x => {
1427
+ if !fail {
1428
+ return None ;
1429
+ }
1430
+ bug ! ( "unexpected non-type Node::GenericParam: {:?}" , x)
1431
+ } ,
1389
1432
} ,
1390
1433
1391
1434
x => {
1435
+ if !fail {
1436
+ return None ;
1437
+ }
1392
1438
bug ! ( "unexpected sort of node in type_of_def_id(): {:?}" , x) ;
1393
1439
}
1394
- }
1440
+ } )
1395
1441
}
1396
1442
1397
1443
fn find_existential_constraints < ' a , ' tcx > (
0 commit comments