@@ -115,6 +115,18 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
115
115
let tr_base = self . trans_lvalue ( bcx, & projection. base ) ;
116
116
let projected_ty = tr_base. ty . projection_ty ( tcx, & projection. elem ) ;
117
117
let projected_ty = bcx. monomorphize ( & projected_ty) ;
118
+
119
+ let project_index = |llindex| {
120
+ let element = if let ty:: TySlice ( _) = tr_base. ty . to_ty ( tcx) . sty {
121
+ // Slices already point to the array element type.
122
+ bcx. inbounds_gep ( tr_base. llval , & [ llindex] )
123
+ } else {
124
+ let zero = common:: C_uint ( bcx. ccx ( ) , 0u64 ) ;
125
+ bcx. inbounds_gep ( tr_base. llval , & [ zero, llindex] )
126
+ } ;
127
+ ( element, ptr:: null_mut ( ) )
128
+ } ;
129
+
118
130
let ( llprojected, llextra) = match projection. elem {
119
131
mir:: ProjectionElem :: Deref => {
120
132
let base_ty = tr_base. ty . to_ty ( tcx) ;
@@ -153,30 +165,21 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
153
165
}
154
166
mir:: ProjectionElem :: Index ( ref index) => {
155
167
let index = self . trans_operand ( bcx, index) ;
156
- let llindex = self . prepare_index ( bcx, index. immediate ( ) ) ;
157
- let zero = common:: C_uint ( bcx. ccx ( ) , 0u64 ) ;
158
- ( bcx. inbounds_gep ( tr_base. llval , & [ zero, llindex] ) ,
159
- ptr:: null_mut ( ) )
168
+ project_index ( self . prepare_index ( bcx, index. immediate ( ) ) )
160
169
}
161
170
mir:: ProjectionElem :: ConstantIndex { offset,
162
171
from_end : false ,
163
172
min_length : _ } => {
164
173
let lloffset = common:: C_u32 ( bcx. ccx ( ) , offset) ;
165
- let llindex = self . prepare_index ( bcx, lloffset) ;
166
- let zero = common:: C_uint ( bcx. ccx ( ) , 0u64 ) ;
167
- ( bcx. inbounds_gep ( tr_base. llval , & [ zero, llindex] ) ,
168
- ptr:: null_mut ( ) )
174
+ project_index ( self . prepare_index ( bcx, lloffset) )
169
175
}
170
176
mir:: ProjectionElem :: ConstantIndex { offset,
171
177
from_end : true ,
172
178
min_length : _ } => {
173
179
let lloffset = common:: C_u32 ( bcx. ccx ( ) , offset) ;
174
180
let lllen = self . lvalue_len ( bcx, tr_base) ;
175
181
let llindex = bcx. sub ( lllen, lloffset) ;
176
- let llindex = self . prepare_index ( bcx, llindex) ;
177
- let zero = common:: C_uint ( bcx. ccx ( ) , 0u64 ) ;
178
- ( bcx. inbounds_gep ( tr_base. llval , & [ zero, llindex] ) ,
179
- ptr:: null_mut ( ) )
182
+ project_index ( self . prepare_index ( bcx, llindex) )
180
183
}
181
184
mir:: ProjectionElem :: Downcast ( ..) => {
182
185
( tr_base. llval , tr_base. llextra )
0 commit comments