@@ -21,8 +21,9 @@ pub mod generics;
21
21
mod lint;
22
22
23
23
use std:: assert_matches:: assert_matches;
24
- use std:: slice;
24
+ use std:: { char , slice} ;
25
25
26
+ use rustc_abi:: Size ;
26
27
use rustc_ast:: TraitObjectSyntax ;
27
28
use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
28
29
use rustc_errors:: codes:: * ;
@@ -31,7 +32,7 @@ use rustc_errors::{
31
32
} ;
32
33
use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , Namespace , Res } ;
33
34
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
34
- use rustc_hir:: { self as hir, AnonConst , GenericArg , GenericArgs , HirId } ;
35
+ use rustc_hir:: { self as hir, AnonConst , ConstArg , GenericArg , GenericArgs , HirId } ;
35
36
use rustc_infer:: infer:: { InferCtxt , TyCtxtInferExt } ;
36
37
use rustc_infer:: traits:: ObligationCause ;
37
38
use rustc_middle:: middle:: stability:: AllowUnstable ;
@@ -2453,20 +2454,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2453
2454
let ty = self . lower_ty ( ty) ;
2454
2455
let pat_ty = match pat. kind {
2455
2456
hir:: TyPatKind :: Range ( start, end, include_end) => {
2456
- let ty = match ty. kind ( ) {
2457
- ty:: Int ( _) | ty:: Uint ( _) | ty:: Char => ty,
2458
- _ => Ty :: new_error (
2459
- tcx,
2460
- self . dcx ( ) . emit_err ( InvalidBaseType {
2457
+ let ( ty, start, end) = match ty. kind ( ) {
2458
+ ty:: Int ( _) | ty:: Uint ( _) | ty:: Char => {
2459
+ let ( start, end) = self . lower_ty_pat_range ( ty, start, end) ;
2460
+ ( ty, start, end)
2461
+ }
2462
+ _ => {
2463
+ let guar = self . dcx ( ) . emit_err ( InvalidBaseType {
2461
2464
ty,
2462
2465
pat : "range" ,
2463
2466
ty_span,
2464
2467
pat_span : pat. span ,
2465
- } ) ,
2466
- ) ,
2468
+ } ) ;
2469
+ let errc = ty:: Const :: new_error ( tcx, guar) ;
2470
+ ( Ty :: new_error ( tcx, guar) , errc, errc)
2471
+ }
2467
2472
} ;
2468
- let start = start. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) ;
2469
- let end = end. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) ;
2470
2473
2471
2474
let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
2472
2475
Ty :: new_pat ( tcx, ty, pat)
@@ -2483,6 +2486,70 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2483
2486
result_ty
2484
2487
}
2485
2488
2489
+ fn lower_ty_pat_range (
2490
+ & self ,
2491
+ base : Ty < ' tcx > ,
2492
+ start : Option < & ConstArg < ' tcx > > ,
2493
+ end : Option < & ConstArg < ' tcx > > ,
2494
+ ) -> ( ty:: Const < ' tcx > , ty:: Const < ' tcx > ) {
2495
+ let tcx = self . tcx ( ) ;
2496
+ let size = match base. kind ( ) {
2497
+ ty:: Int ( i) => {
2498
+ i. bit_width ( ) . map_or ( tcx. data_layout . pointer_size , |bits| Size :: from_bits ( bits) )
2499
+ }
2500
+ ty:: Uint ( ui) => {
2501
+ ui. bit_width ( ) . map_or ( tcx. data_layout . pointer_size , |bits| Size :: from_bits ( bits) )
2502
+ }
2503
+ ty:: Char => Size :: from_bytes ( 4 ) ,
2504
+ _ => unreachable ! ( ) ,
2505
+ } ;
2506
+ let start =
2507
+ start. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) . unwrap_or_else ( || {
2508
+ match base. kind ( ) {
2509
+ ty:: Char | ty:: Uint ( _) => ty:: Const :: new_value (
2510
+ tcx,
2511
+ ty:: ValTree :: from_scalar_int ( ty:: ScalarInt :: null ( size) ) ,
2512
+ base,
2513
+ ) ,
2514
+ ty:: Int ( _) => ty:: Const :: new_value (
2515
+ tcx,
2516
+ ty:: ValTree :: from_scalar_int (
2517
+ ty:: ScalarInt :: truncate_from_int ( size. signed_int_min ( ) , size) . 0 ,
2518
+ ) ,
2519
+ base,
2520
+ ) ,
2521
+ _ => unreachable ! ( ) ,
2522
+ }
2523
+ } ) ;
2524
+ let end = end. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) . unwrap_or_else (
2525
+ || match base. kind ( ) {
2526
+ ty:: Char => ty:: Const :: new_value (
2527
+ tcx,
2528
+ ty:: ValTree :: from_scalar_int (
2529
+ ty:: ScalarInt :: truncate_from_uint ( char:: MAX , size) . 0 ,
2530
+ ) ,
2531
+ base,
2532
+ ) ,
2533
+ ty:: Uint ( _) => ty:: Const :: new_value (
2534
+ tcx,
2535
+ ty:: ValTree :: from_scalar_int (
2536
+ ty:: ScalarInt :: truncate_from_uint ( size. unsigned_int_max ( ) , size) . 0 ,
2537
+ ) ,
2538
+ base,
2539
+ ) ,
2540
+ ty:: Int ( _) => ty:: Const :: new_value (
2541
+ tcx,
2542
+ ty:: ValTree :: from_scalar_int (
2543
+ ty:: ScalarInt :: truncate_from_int ( size. signed_int_max ( ) , size) . 0 ,
2544
+ ) ,
2545
+ base,
2546
+ ) ,
2547
+ _ => unreachable ! ( ) ,
2548
+ } ,
2549
+ ) ;
2550
+ ( start, end)
2551
+ }
2552
+
2486
2553
/// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
2487
2554
#[ instrument( level = "debug" , skip( self ) , ret) ]
2488
2555
fn lower_opaque_ty ( & self , def_id : LocalDefId , in_trait : bool ) -> Ty < ' tcx > {
0 commit comments