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