@@ -53,7 +53,11 @@ pub(crate) fn codegen_tls_ref<'tcx>(
53
53
let call = fx. bcx . ins ( ) . call ( func_ref, & [ ] ) ;
54
54
fx. bcx . func . dfg . first_result ( call)
55
55
} 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
+ ) ;
57
61
let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
58
62
if fx. clif_comments . enabled ( ) {
59
63
fx. add_comment ( local_data_id, format ! ( "tls {:?}" , def_id) ) ;
@@ -164,7 +168,11 @@ pub(crate) fn codegen_const_value<'tcx>(
164
168
}
165
169
GlobalAlloc :: Static ( def_id) => {
166
170
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
+ ) ;
168
176
let local_data_id =
169
177
fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
170
178
if fx. clif_comments . enabled ( ) {
@@ -232,21 +240,19 @@ fn data_id_for_static(
232
240
module : & mut dyn Module ,
233
241
def_id : DefId ,
234
242
definition : bool ,
243
+ definition_writable : bool ,
235
244
) -> DataId {
236
245
let attrs = tcx. codegen_fn_attrs ( def_id) ;
237
246
238
247
let instance = Instance :: mono ( tcx, def_id) . polymorphize ( tcx) ;
239
248
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 ( ) ;
247
249
248
250
if let Some ( import_linkage) = attrs. import_linkage {
249
251
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 ( ) ;
250
256
251
257
let linkage = if import_linkage == rustc_middle:: mir:: mono:: Linkage :: ExternalWeak
252
258
|| import_linkage == rustc_middle:: mir:: mono:: Linkage :: WeakAny
@@ -259,7 +265,7 @@ fn data_id_for_static(
259
265
let data_id = match module. declare_data (
260
266
symbol_name,
261
267
linkage,
262
- is_mutable ,
268
+ false ,
263
269
attrs. flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL ) ,
264
270
) {
265
271
Ok ( data_id) => data_id,
@@ -307,7 +313,7 @@ fn data_id_for_static(
307
313
let data_id = match module. declare_data (
308
314
symbol_name,
309
315
linkage,
310
- is_mutable ,
316
+ definition_writable ,
311
317
attrs. flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL ) ,
312
318
) {
313
319
Ok ( data_id) => data_id,
@@ -341,7 +347,13 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
341
347
342
348
let alloc = tcx. eval_static_initializer ( def_id) . unwrap ( ) ;
343
349
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
+ ) ;
345
357
( data_id, alloc, section_name)
346
358
}
347
359
} ;
@@ -421,7 +433,11 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
421
433
// Don't push a `TodoItem::Static` here, as it will cause statics used by
422
434
// multiple crates to be duplicated between them. It isn't necessary anyway,
423
435
// 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
+ )
425
441
}
426
442
} ;
427
443
0 commit comments