@@ -53,7 +53,11 @@ pub(crate) fn codegen_tls_ref<'tcx>(
5353 let call = fx. bcx . ins ( ) . call ( func_ref, & [ ] ) ;
5454 fx. bcx . func . dfg . first_result ( call)
5555 } else {
56- let data_id = data_id_for_static ( fx. tcx , fx. module , def_id, false ) ;
56+ let data_id = data_id_for_static (
57+ fx. tcx , fx. module , def_id, false ,
58+ // For a declaration the stated mutability doesn't matter.
59+ false ,
60+ ) ;
5761 let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
5862 if fx. clif_comments . enabled ( ) {
5963 fx. add_comment ( local_data_id, format ! ( "tls {:?}" , def_id) ) ;
@@ -164,7 +168,11 @@ pub(crate) fn codegen_const_value<'tcx>(
164168 }
165169 GlobalAlloc :: Static ( def_id) => {
166170 assert ! ( fx. tcx. is_static( def_id) ) ;
167- let data_id = data_id_for_static ( fx. tcx , fx. module , def_id, false ) ;
171+ let data_id = data_id_for_static (
172+ fx. tcx , fx. module , def_id, false ,
173+ // For a declaration the stated mutability doesn't matter.
174+ false ,
175+ ) ;
168176 let local_data_id =
169177 fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
170178 if fx. clif_comments . enabled ( ) {
@@ -232,21 +240,19 @@ fn data_id_for_static(
232240 module : & mut dyn Module ,
233241 def_id : DefId ,
234242 definition : bool ,
243+ definition_writable : bool ,
235244) -> DataId {
236245 let attrs = tcx. codegen_fn_attrs ( def_id) ;
237246
238247 let instance = Instance :: mono ( tcx, def_id) . polymorphize ( tcx) ;
239248 let symbol_name = tcx. symbol_name ( instance) . name ;
240- let ty = instance. ty ( tcx, ParamEnv :: reveal_all ( ) ) ;
241- let is_mutable = if tcx. is_mutable_static ( def_id) {
242- true
243- } else {
244- !ty. is_freeze ( tcx, ParamEnv :: reveal_all ( ) )
245- } ;
246- let align = tcx. layout_of ( ParamEnv :: reveal_all ( ) . and ( ty) ) . unwrap ( ) . align . pref . bytes ( ) ;
247249
248250 if let Some ( import_linkage) = attrs. import_linkage {
249251 assert ! ( !definition) ;
252+ assert ! ( !tcx. is_mutable_static( def_id) ) ;
253+
254+ let ty = instance. ty ( tcx, ParamEnv :: reveal_all ( ) ) ;
255+ let align = tcx. layout_of ( ParamEnv :: reveal_all ( ) . and ( ty) ) . unwrap ( ) . align . pref . bytes ( ) ;
250256
251257 let linkage = if import_linkage == rustc_middle:: mir:: mono:: Linkage :: ExternalWeak
252258 || import_linkage == rustc_middle:: mir:: mono:: Linkage :: WeakAny
@@ -259,7 +265,7 @@ fn data_id_for_static(
259265 let data_id = match module. declare_data (
260266 symbol_name,
261267 linkage,
262- is_mutable ,
268+ false ,
263269 attrs. flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL ) ,
264270 ) {
265271 Ok ( data_id) => data_id,
@@ -307,7 +313,7 @@ fn data_id_for_static(
307313 let data_id = match module. declare_data (
308314 symbol_name,
309315 linkage,
310- is_mutable ,
316+ definition_writable ,
311317 attrs. flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL ) ,
312318 ) {
313319 Ok ( data_id) => data_id,
@@ -341,7 +347,13 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
341347
342348 let alloc = tcx. eval_static_initializer ( def_id) . unwrap ( ) ;
343349
344- let data_id = data_id_for_static ( tcx, module, def_id, true ) ;
350+ let data_id = data_id_for_static (
351+ tcx,
352+ module,
353+ def_id,
354+ true ,
355+ alloc. inner ( ) . mutability == Mutability :: Mut ,
356+ ) ;
345357 ( data_id, alloc, section_name)
346358 }
347359 } ;
@@ -421,7 +433,11 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
421433 // Don't push a `TodoItem::Static` here, as it will cause statics used by
422434 // multiple crates to be duplicated between them. It isn't necessary anyway,
423435 // as it will get pushed by `codegen_static` when necessary.
424- data_id_for_static ( tcx, module, def_id, false )
436+ data_id_for_static (
437+ tcx, module, def_id, false ,
438+ // For a declaration the stated mutability doesn't matter.
439+ false ,
440+ )
425441 }
426442 } ;
427443
0 commit comments