@@ -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 ;
@@ -2693,20 +2694,22 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2693
2694
let ty = self . lower_ty ( ty) ;
2694
2695
let pat_ty = match pat. kind {
2695
2696
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 {
2701
2704
ty,
2702
2705
pat : "range" ,
2703
2706
ty_span,
2704
2707
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
+ }
2707
2712
} ;
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 ) ) ;
2710
2713
2711
2714
let pat = tcx. mk_pat ( ty:: PatternKind :: Range { start, end, include_end } ) ;
2712
2715
Ty :: new_pat ( tcx, ty, pat)
@@ -2723,6 +2726,70 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2723
2726
result_ty
2724
2727
}
2725
2728
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
+
2726
2793
/// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
2727
2794
#[ instrument( level = "debug" , skip( self ) , ret) ]
2728
2795
fn lower_opaque_ty ( & self , def_id : LocalDefId , in_trait : bool ) -> Ty < ' tcx > {
0 commit comments