@@ -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 ;
@@ -2455,20 +2456,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24552456 let ty = self . lower_ty ( ty) ;
24562457 let pat_ty = match pat. kind {
24572458 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 {
24632466 ty,
24642467 pat : "range" ,
24652468 ty_span,
24662469 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+ }
24692474 } ;
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 ) ) ;
24722475
24732476 let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
24742477 Ty :: new_pat ( tcx, ty, pat)
@@ -2485,6 +2488,70 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24852488 result_ty
24862489 }
24872490
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+
24882555 /// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
24892556 #[ instrument( level = "debug" , skip( self ) , ret) ]
24902557 fn lower_opaque_ty ( & self , def_id : LocalDefId , in_trait : bool ) -> Ty < ' tcx > {
0 commit comments