@@ -34,6 +34,7 @@ use middle::ty::{self, Ty};
34
34
use util:: ppaux:: { Repr , ty_to_string} ;
35
35
36
36
use std:: iter:: repeat;
37
+ use libc:: c_uint;
37
38
use syntax:: { ast, ast_util} ;
38
39
use syntax:: parse:: token;
39
40
use syntax:: ptr:: P ;
@@ -673,14 +674,40 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
673
674
_ => break ,
674
675
}
675
676
}
676
- let opt_def = cx. tcx ( ) . def_map . borrow ( ) . get ( & cur. id ) . map ( |d| d. full_def ( ) ) ;
677
- if let Some ( def:: DefStatic ( def_id, _) ) = opt_def {
678
- get_static_val ( cx, def_id, ety)
679
- } else {
680
- // If this isn't the address of a static, then keep going through
681
- // normal constant evaluation.
682
- let ( v, _) = const_expr ( cx, & * * sub, param_substs) ;
677
+ if let ast:: ExprIndex ( ref base, ref index) = cur. node {
678
+ let ( bv, bt) = const_expr ( cx, & * * base, param_substs) ;
679
+ let iv = match const_eval:: eval_const_expr_partial ( cx. tcx ( ) , & * * index, None ) {
680
+ Ok ( const_eval:: const_int( i) ) => i as u64 ,
681
+ Ok ( const_eval:: const_uint( u) ) => u,
682
+ _ => unreachable ! ( ) ,
683
+ } ;
684
+ let arr = match bt. sty {
685
+ ty:: ty_vec( _, Some ( _) ) => bv,
686
+ ty:: ty_vec( _, None ) | ty:: ty_str => {
687
+ let e1 = const_get_elt ( cx, bv, & [ 0 ] ) ;
688
+ const_deref_ptr ( cx, e1)
689
+ } ,
690
+ ty:: ty_rptr( _, mt) => if let ty:: ty_vec( _, Some ( _) ) = mt. ty . sty {
691
+ const_deref_ptr ( cx, bv)
692
+ } else {
693
+ unreachable ! ( )
694
+ } ,
695
+ _ => unreachable ! ( ) ,
696
+ } ;
697
+ // UB if const_eval wasn't called on this
698
+ // since no len check happening here
699
+ let v = const_get_elt ( cx, arr, & [ iv as c_uint ] ) ;
683
700
addr_of ( cx, v, "ref" )
701
+ } else {
702
+ let opt_def = cx. tcx ( ) . def_map . borrow ( ) . get ( & cur. id ) . map ( |d| d. full_def ( ) ) ;
703
+ if let Some ( def:: DefStatic ( def_id, _) ) = opt_def {
704
+ get_static_val ( cx, def_id, ety)
705
+ } else {
706
+ // If this isn't the address of a static, then keep going through
707
+ // normal constant evaluation.
708
+ let ( v, _) = const_expr ( cx, & * * sub, param_substs) ;
709
+ addr_of ( cx, v, "ref" )
710
+ }
684
711
}
685
712
}
686
713
ast:: ExprAddrOf ( ast:: MutMutable , ref sub) => {
0 commit comments