@@ -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 ;
@@ -2443,20 +2444,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2443
2444
let ty = self . lower_ty ( ty) ;
2444
2445
let pat_ty = match pat. kind {
2445
2446
hir:: TyPatKind :: Range ( start, end, include_end) => {
2446
- let ty = match ty. kind ( ) {
2447
- ty:: Int ( _) | ty:: Uint ( _) | ty:: Char => ty,
2448
- _ => Ty :: new_error (
2449
- tcx,
2450
- self . dcx ( ) . emit_err ( InvalidBaseType {
2447
+ let ( ty, start, end) = match ty. kind ( ) {
2448
+ ty:: Int ( _) | ty:: Uint ( _) | ty:: Char => {
2449
+ let ( start, end) = self . lower_ty_pat_range ( ty, start, end) ;
2450
+ ( ty, start, end)
2451
+ }
2452
+ _ => {
2453
+ let guar = self . dcx ( ) . emit_err ( InvalidBaseType {
2451
2454
ty,
2452
2455
pat : "range" ,
2453
2456
ty_span,
2454
2457
pat_span : pat. span ,
2455
- } ) ,
2456
- ) ,
2458
+ } ) ;
2459
+ let errc = ty:: Const :: new_error ( tcx, guar) ;
2460
+ ( Ty :: new_error ( tcx, guar) , errc, errc)
2461
+ }
2457
2462
} ;
2458
- let start = start. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) ;
2459
- let end = end. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) ;
2460
2463
2461
2464
let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
2462
2465
Ty :: new_pat ( tcx, ty, pat)
@@ -2473,6 +2476,70 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2473
2476
result_ty
2474
2477
}
2475
2478
2479
+ fn lower_ty_pat_range (
2480
+ & self ,
2481
+ base : Ty < ' tcx > ,
2482
+ start : Option < & ConstArg < ' tcx > > ,
2483
+ end : Option < & ConstArg < ' tcx > > ,
2484
+ ) -> ( ty:: Const < ' tcx > , ty:: Const < ' tcx > ) {
2485
+ let tcx = self . tcx ( ) ;
2486
+ let size = match base. kind ( ) {
2487
+ ty:: Int ( i) => {
2488
+ i. bit_width ( ) . map_or ( tcx. data_layout . pointer_size , |bits| Size :: from_bits ( bits) )
2489
+ }
2490
+ ty:: Uint ( ui) => {
2491
+ ui. bit_width ( ) . map_or ( tcx. data_layout . pointer_size , |bits| Size :: from_bits ( bits) )
2492
+ }
2493
+ ty:: Char => Size :: from_bytes ( 4 ) ,
2494
+ _ => unreachable ! ( ) ,
2495
+ } ;
2496
+ let start =
2497
+ start. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) . unwrap_or_else ( || {
2498
+ match base. kind ( ) {
2499
+ ty:: Char | ty:: Uint ( _) => ty:: Const :: new_value (
2500
+ tcx,
2501
+ ty:: ValTree :: from_scalar_int ( ty:: ScalarInt :: null ( size) ) ,
2502
+ base,
2503
+ ) ,
2504
+ ty:: Int ( _) => ty:: Const :: new_value (
2505
+ tcx,
2506
+ ty:: ValTree :: from_scalar_int (
2507
+ ty:: ScalarInt :: truncate_from_int ( size. signed_int_min ( ) , size) . 0 ,
2508
+ ) ,
2509
+ base,
2510
+ ) ,
2511
+ _ => unreachable ! ( ) ,
2512
+ }
2513
+ } ) ;
2514
+ let end = end. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) . unwrap_or_else (
2515
+ || match base. kind ( ) {
2516
+ ty:: Char => ty:: Const :: new_value (
2517
+ tcx,
2518
+ ty:: ValTree :: from_scalar_int (
2519
+ ty:: ScalarInt :: truncate_from_uint ( char:: MAX , size) . 0 ,
2520
+ ) ,
2521
+ base,
2522
+ ) ,
2523
+ ty:: Uint ( _) => ty:: Const :: new_value (
2524
+ tcx,
2525
+ ty:: ValTree :: from_scalar_int (
2526
+ ty:: ScalarInt :: truncate_from_uint ( size. unsigned_int_max ( ) , size) . 0 ,
2527
+ ) ,
2528
+ base,
2529
+ ) ,
2530
+ ty:: Int ( _) => ty:: Const :: new_value (
2531
+ tcx,
2532
+ ty:: ValTree :: from_scalar_int (
2533
+ ty:: ScalarInt :: truncate_from_int ( size. signed_int_max ( ) , size) . 0 ,
2534
+ ) ,
2535
+ base,
2536
+ ) ,
2537
+ _ => unreachable ! ( ) ,
2538
+ } ,
2539
+ ) ;
2540
+ ( start, end)
2541
+ }
2542
+
2476
2543
/// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
2477
2544
#[ instrument( level = "debug" , skip( self ) , ret) ]
2478
2545
fn lower_opaque_ty ( & self , def_id : LocalDefId , in_trait : bool ) -> Ty < ' tcx > {
0 commit comments