@@ -21,8 +21,9 @@ pub mod generics;
2121mod lint;
2222
2323use std:: assert_matches:: assert_matches;
24- use std:: slice;
24+ use std:: { char , slice} ;
2525
26+ use rustc_abi:: Size ;
2627use rustc_ast:: TraitObjectSyntax ;
2728use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
2829use rustc_errors:: codes:: * ;
@@ -31,7 +32,7 @@ use rustc_errors::{
3132} ;
3233use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , Namespace , Res } ;
3334use 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 } ;
3536use rustc_infer:: infer:: { InferCtxt , TyCtxtInferExt } ;
3637use rustc_infer:: traits:: ObligationCause ;
3738use rustc_middle:: middle:: stability:: AllowUnstable ;
@@ -2453,20 +2454,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24532454 let ty = self . lower_ty ( ty) ;
24542455 let pat_ty = match pat. kind {
24552456 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 {
24612464 ty,
24622465 pat : "range" ,
24632466 ty_span,
24642467 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+ }
24672472 } ;
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 ) ) ;
24702473
24712474 let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
24722475 Ty :: new_pat ( tcx, ty, pat)
@@ -2483,6 +2486,70 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24832486 result_ty
24842487 }
24852488
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+
24862553 /// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
24872554 #[ instrument( level = "debug" , skip( self ) , ret) ]
24882555 fn lower_opaque_ty ( & self , def_id : LocalDefId , in_trait : bool ) -> Ty < ' tcx > {
0 commit comments