@@ -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 ;
@@ -2443,20 +2444,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24432444 let ty = self . lower_ty ( ty) ;
24442445 let pat_ty = match pat. kind {
24452446 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 {
24512454 ty,
24522455 pat : "range" ,
24532456 ty_span,
24542457 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+ }
24572462 } ;
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 ) ) ;
24602463
24612464 let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
24622465 Ty :: new_pat ( tcx, ty, pat)
@@ -2473,6 +2476,70 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24732476 result_ty
24742477 }
24752478
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+
24762543 /// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
24772544 #[ instrument( level = "debug" , skip( self ) , ret) ]
24782545 fn lower_opaque_ty ( & self , def_id : LocalDefId , in_trait : bool ) -> Ty < ' tcx > {
0 commit comments