@@ -98,7 +98,7 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit)
9898        ast:: LitBool ( b)  => C_bool ( cx,  b) , 
9999        ast:: LitStr ( ref  s,  _)  => C_str_slice ( cx,  ( * s) . clone ( ) ) , 
100100        ast:: LitByteStr ( ref  data)  => { 
101-             addr_of ( cx,  C_bytes ( cx,  & data[ ..] ) ,  "byte_str" ) 
101+             addr_of ( cx,  C_bytes ( cx,  & data[ ..] ) ,  1 ,   "byte_str" ) 
102102        } 
103103    } 
104104} 
@@ -111,6 +111,7 @@ pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
111111
112112fn  addr_of_mut ( ccx :  & CrateContext , 
113113               cv :  ValueRef , 
114+                align :  machine:: llalign , 
114115               kind :  & str ) 
115116               -> ValueRef  { 
116117    unsafe  { 
@@ -122,6 +123,7 @@ fn addr_of_mut(ccx: &CrateContext,
122123            ccx. sess ( ) . bug ( & format ! ( "symbol `{}` is already defined" ,  name) ) ; 
123124        } ) ; 
124125        llvm:: LLVMSetInitializer ( gv,  cv) ; 
126+         llvm:: LLVMSetAlignment ( gv,  align) ; 
125127        SetLinkage ( gv,  InternalLinkage ) ; 
126128        SetUnnamedAddr ( gv,  true ) ; 
127129        gv
@@ -130,13 +132,23 @@ fn addr_of_mut(ccx: &CrateContext,
130132
131133pub  fn  addr_of ( ccx :  & CrateContext , 
132134               cv :  ValueRef , 
135+                align :  machine:: llalign , 
133136               kind :  & str ) 
134137               -> ValueRef  { 
135138    match  ccx. const_globals ( ) . borrow ( ) . get ( & cv)  { 
136-         Some ( & gv)  => return  gv, 
139+         Some ( & gv)  => { 
140+             unsafe  { 
141+                 // Upgrade the alignment in cases where the same constant is used with different 
142+                 // alignment requirements 
143+                 if  align > llvm:: LLVMGetAlignment ( gv)  { 
144+                     llvm:: LLVMSetAlignment ( gv,  align) ; 
145+                 } 
146+             } 
147+             return  gv; 
148+         } 
137149        None  => { } 
138150    } 
139-     let  gv = addr_of_mut ( ccx,  cv,  kind) ; 
151+     let  gv = addr_of_mut ( ccx,  cv,  align ,   kind) ; 
140152    unsafe  { 
141153        llvm:: LLVMSetGlobalConstant ( gv,  True ) ; 
142154    } 
@@ -254,11 +266,11 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
254266        Some ( & val)  => return  val, 
255267        None  => { } 
256268    } 
269+     let  ty = monomorphize:: apply_param_substs ( ccx. tcx ( ) ,  param_substs, 
270+                                               & ccx. tcx ( ) . expr_ty ( expr) ) ; 
257271    let  val = if  qualif. intersects ( check_const:: ConstQualif :: NON_STATIC_BORROWS )  { 
258272        // Avoid autorefs as they would create global instead of stack 
259273        // references, even when only the latter are correct. 
260-         let  ty = monomorphize:: apply_param_substs ( ccx. tcx ( ) ,  param_substs, 
261-                                                   & ccx. tcx ( ) . expr_ty ( expr) ) ; 
262274        const_expr_unadjusted ( ccx,  expr,  ty,  param_substs,  None ) 
263275    }  else  { 
264276        const_expr ( ccx,  expr,  param_substs,  None ) . 0 
@@ -274,7 +286,7 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
274286        } 
275287    } ; 
276288
277-     let  lvalue = addr_of ( ccx,  val,  "const" ) ; 
289+     let  lvalue = addr_of ( ccx,  val,  type_of :: align_of ( ccx ,  ty ) ,   "const" ) ; 
278290    ccx. const_values ( ) . borrow_mut ( ) . insert ( key,  lvalue) ; 
279291    lvalue
280292} 
@@ -314,7 +326,7 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
314326                if  adj. autoderefs  == 0  { 
315327                    // Don't copy data to do a deref+ref 
316328                    // (i.e., skip the last auto-deref). 
317-                     llconst = addr_of ( cx,  llconst,  "autoref" ) ; 
329+                     llconst = addr_of ( cx,  llconst,  type_of :: align_of ( cx ,  ty ) ,   "autoref" ) ; 
318330                    ty = cx. tcx ( ) . mk_imm_ref ( cx. tcx ( ) . mk_region ( ty:: ReStatic ) ,  ty) ; 
319331                } 
320332            }  else  { 
@@ -720,13 +732,13 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
720732            }  else  { 
721733                // If this isn't the address of a static, then keep going through 
722734                // normal constant evaluation. 
723-                 let  ( v,  _ )  = const_expr ( cx,  & * * sub,  param_substs,  fn_args) ; 
724-                 addr_of ( cx,  v,  "ref" ) 
735+                 let  ( v,  ty )  = const_expr ( cx,  & * * sub,  param_substs,  fn_args) ; 
736+                 addr_of ( cx,  v,  type_of :: align_of ( cx ,  ty ) ,   "ref" ) 
725737            } 
726738        } , 
727739        hir:: ExprAddrOf ( hir:: MutMutable ,  ref  sub)  => { 
728-             let  ( v,  _ )  = const_expr ( cx,  & * * sub,  param_substs,  fn_args) ; 
729-             addr_of_mut ( cx,  v,  "ref_mut_slice" ) 
740+             let  ( v,  ty )  = const_expr ( cx,  & * * sub,  param_substs,  fn_args) ; 
741+             addr_of_mut ( cx,  v,  type_of :: align_of ( cx ,  ty ) ,   "ref_mut_slice" ) 
730742        } , 
731743        hir:: ExprTup ( ref  es)  => { 
732744            let  repr = adt:: represent_type ( cx,  ety) ; 
@@ -934,6 +946,7 @@ pub fn trans_static(ccx: &CrateContext,
934946            ccx. statics_to_rauw ( ) . borrow_mut ( ) . push ( ( g,  new_g) ) ; 
935947            new_g
936948        } ; 
949+         llvm:: LLVMSetAlignment ( g,  type_of:: align_of ( ccx,  ty) ) ; 
937950        llvm:: LLVMSetInitializer ( g,  v) ; 
938951
939952        // As an optimization, all shared statics which do not have interior 
0 commit comments