@@ -610,54 +610,59 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
610
610
}
611
611
612
612
ast:: ExprIndex ( ref base, ref index) => {
613
- if let Ok ( val) = const_eval:: eval_const_expr_partial ( cx. tcx ( ) , & e, None ) {
614
- const_val_for_idx ( cx, val, param_substs, ety)
615
- } else {
616
- let ( bv, bt) = const_expr ( cx, & * * base, param_substs) ;
617
- let iv = try!( eval_const_index ( tcx, idx) ) ;
618
- let ( arr, len) = match bt. sty {
619
- ty:: ty_vec( _, Some ( u) ) => ( bv, C_uint ( cx, u) ) ,
620
- ty:: ty_vec( _, None ) | ty:: ty_str => {
621
- let e1 = const_get_elt ( cx, bv, & [ 0 ] ) ;
622
- ( const_deref_ptr ( cx, e1) , const_get_elt ( cx, bv, & [ 1 ] ) )
623
- }
624
- ty:: ty_rptr( _, mt) => match mt. ty . sty {
625
- ty:: ty_vec( _, Some ( u) ) => {
626
- ( const_deref_ptr ( cx, bv) , C_uint ( cx, u) )
627
- } ,
628
- _ => cx. sess ( ) . span_bug ( base. span ,
629
- & format ! ( "index-expr base must be a vector \
630
- or string type, found {}",
631
- ty_to_string( cx. tcx( ) , bt) ) )
613
+ match const_eval:: eval_const_expr_partial ( cx. tcx ( ) , & e, None ) {
614
+ Ok ( val) => const_val_for_idx ( cx, val, param_substs, ety) ,
615
+ Err ( NonConstPath ) => {
616
+ let ( bv, bt) = const_expr ( cx, & * * base, param_substs) ;
617
+ let iv = try!( const_eval:: eval_const_index ( cx. tcx ( ) , index) ) ;
618
+ let ( arr, len) = match bt. sty {
619
+ ty:: ty_vec( _, Some ( u) ) => ( bv, C_uint ( cx, u) ) ,
620
+ ty:: ty_vec( _, None ) | ty:: ty_str => {
621
+ let e1 = const_get_elt ( cx, bv, & [ 0 ] ) ;
622
+ ( const_deref_ptr ( cx, e1) , const_get_elt ( cx, bv, & [ 1 ] ) )
623
+ }
624
+ ty:: ty_rptr( _, mt) => match mt. ty . sty {
625
+ ty:: ty_vec( _, Some ( u) ) => {
626
+ ( const_deref_ptr ( cx, bv) , C_uint ( cx, u) )
632
627
} ,
633
628
_ => cx. sess ( ) . span_bug ( base. span ,
634
629
& format ! ( "index-expr base must be a vector \
635
630
or string type, found {}",
636
631
ty_to_string( cx. tcx( ) , bt) ) )
637
- } ;
638
-
639
- let len = llvm:: LLVMConstIntGetZExtValue ( len) as u64 ;
640
- let len = match bt. sty {
641
- ty:: ty_uniq( ty) | ty:: ty_rptr( _, ty:: mt { ty, ..} ) => match ty. sty {
642
- ty:: ty_str => {
643
- assert ! ( len > 0 ) ;
644
- len - 1
645
- }
646
- _ => len
647
- } ,
632
+ } ,
633
+ _ => cx. sess ( ) . span_bug ( base. span ,
634
+ & format ! ( "index-expr base must be a vector \
635
+ or string type, found {}",
636
+ ty_to_string( cx. tcx( ) , bt) ) )
637
+ } ;
638
+
639
+ let len = llvm:: LLVMConstIntGetZExtValue ( len) as u64 ;
640
+ let len = match bt. sty {
641
+ ty:: ty_uniq( ty) | ty:: ty_rptr( _, ty:: mt { ty, ..} ) => match ty. sty {
642
+ ty:: ty_str => {
643
+ assert ! ( len > 0 ) ;
644
+ len - 1
645
+ }
648
646
_ => len
649
- } ;
650
- if iv >= len {
651
- // FIXME #3170: report this earlier on in the const-eval
652
- // pass. Reporting here is a bit late.
653
- // Since some static-eval expressions aren't const-evaluable
654
- // this has to stay
655
- cx. sess ( ) . span_err ( e. span ,
656
- "const index-expr is out of bounds" ) ;
657
- C_undef ( type_of:: type_of ( cx, bt) . element_type ( ) )
658
- } else {
659
- const_get_elt ( cx, arr, & [ iv as c_uint ] )
660
- }
647
+ } ,
648
+ _ => len
649
+ } ;
650
+ if iv >= len {
651
+ // FIXME #3170: report this earlier on in the const-eval
652
+ // pass. Reporting here is a bit late.
653
+ // Since some static-eval expressions aren't const-evaluable
654
+ // this has to stay
655
+ cx. sess ( ) . span_err ( e. span ,
656
+ "const index-expr is out of bounds" ) ;
657
+ C_undef ( type_of:: type_of ( cx, bt) . element_type ( ) )
658
+ } else {
659
+ const_get_elt ( cx, arr, & [ iv as c_uint ] )
660
+ }
661
+ } ,
662
+ Err ( err) => cx. sess ( ) . span_fatal (
663
+ e. span ,
664
+ & format ! ( "constant indexing failed: {}" , err. description( ) ) ,
665
+ ) ,
661
666
}
662
667
} ,
663
668
0 commit comments