@@ -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 ;
@@ -2693,20 +2694,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26932694 let ty = self . lower_ty ( ty) ;
26942695 let pat_ty = match pat. kind {
26952696 hir:: TyPatKind :: Range ( start, end, include_end) => {
2696- let ty = match ty. kind ( ) {
2697- ty:: Int ( _) | ty:: Uint ( _) | ty:: Char => ty,
2698- _ => Ty :: new_error (
2699- tcx,
2700- self . dcx ( ) . emit_err ( InvalidBaseType {
2697+ let ( ty, start, end) = match ty. kind ( ) {
2698+ ty:: Int ( _) | ty:: Uint ( _) | ty:: Char => {
2699+ let ( start, end) = self . lower_ty_pat_range ( ty, start, end) ;
2700+ ( ty, start, end)
2701+ }
2702+ _ => {
2703+ let guar = self . dcx ( ) . emit_err ( InvalidBaseType {
27012704 ty,
27022705 pat : "range" ,
27032706 ty_span,
27042707 pat_span : pat. span ,
2705- } ) ,
2706- ) ,
2708+ } ) ;
2709+ let errc = ty:: Const :: new_error ( tcx, guar) ;
2710+ ( Ty :: new_error ( tcx, guar) , errc, errc)
2711+ }
27072712 } ;
2708- let start = start. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) ;
2709- let end = end. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) ;
27102713
27112714 let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
27122715 Ty :: new_pat ( tcx, ty, pat)
@@ -2723,6 +2726,70 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27232726 result_ty
27242727 }
27252728
2729+ fn lower_ty_pat_range (
2730+ & self ,
2731+ base : Ty < ' tcx > ,
2732+ start : Option < & ConstArg < ' tcx > > ,
2733+ end : Option < & ConstArg < ' tcx > > ,
2734+ ) -> ( ty:: Const < ' tcx > , ty:: Const < ' tcx > ) {
2735+ let tcx = self . tcx ( ) ;
2736+ let size = match base. kind ( ) {
2737+ ty:: Int ( i) => {
2738+ i. bit_width ( ) . map_or ( tcx. data_layout . pointer_size , |bits| Size :: from_bits ( bits) )
2739+ }
2740+ ty:: Uint ( ui) => {
2741+ ui. bit_width ( ) . map_or ( tcx. data_layout . pointer_size , |bits| Size :: from_bits ( bits) )
2742+ }
2743+ ty:: Char => Size :: from_bytes ( 4 ) ,
2744+ _ => unreachable ! ( ) ,
2745+ } ;
2746+ let start =
2747+ start. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) . unwrap_or_else ( || {
2748+ match base. kind ( ) {
2749+ ty:: Char | ty:: Uint ( _) => ty:: Const :: new_value (
2750+ tcx,
2751+ ty:: ValTree :: from_scalar_int ( ty:: ScalarInt :: null ( size) ) ,
2752+ base,
2753+ ) ,
2754+ ty:: Int ( _) => ty:: Const :: new_value (
2755+ tcx,
2756+ ty:: ValTree :: from_scalar_int (
2757+ ty:: ScalarInt :: truncate_from_int ( size. signed_int_min ( ) , size) . 0 ,
2758+ ) ,
2759+ base,
2760+ ) ,
2761+ _ => unreachable ! ( ) ,
2762+ }
2763+ } ) ;
2764+ let end = end. map ( |expr| self . lower_const_arg ( expr, FeedConstTy :: No ) ) . unwrap_or_else (
2765+ || match base. kind ( ) {
2766+ ty:: Char => ty:: Const :: new_value (
2767+ tcx,
2768+ ty:: ValTree :: from_scalar_int (
2769+ ty:: ScalarInt :: truncate_from_uint ( char:: MAX , size) . 0 ,
2770+ ) ,
2771+ base,
2772+ ) ,
2773+ ty:: Uint ( _) => ty:: Const :: new_value (
2774+ tcx,
2775+ ty:: ValTree :: from_scalar_int (
2776+ ty:: ScalarInt :: truncate_from_uint ( size. unsigned_int_max ( ) , size) . 0 ,
2777+ ) ,
2778+ base,
2779+ ) ,
2780+ ty:: Int ( _) => ty:: Const :: new_value (
2781+ tcx,
2782+ ty:: ValTree :: from_scalar_int (
2783+ ty:: ScalarInt :: truncate_from_int ( size. signed_int_max ( ) , size) . 0 ,
2784+ ) ,
2785+ base,
2786+ ) ,
2787+ _ => unreachable ! ( ) ,
2788+ } ,
2789+ ) ;
2790+ ( start, end)
2791+ }
2792+
27262793 /// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
27272794 #[ instrument( level = "debug" , skip( self ) , ret) ]
27282795 fn lower_opaque_ty ( & self , def_id : LocalDefId , in_trait : bool ) -> Ty < ' tcx > {
0 commit comments