3 files changed +30
-20
lines changed Original file line number Diff line number Diff line change @@ -324,21 +324,22 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
324
324
let linkage = Some ( linkage_by_name ( tcx, did, val. as_str ( ) ) ) ;
325
325
if tcx. is_foreign_item ( did) {
326
326
codegen_fn_attrs. import_linkage = linkage;
327
+
328
+ if tcx. is_mutable_static ( did. into ( ) ) {
329
+ let mut diag = tcx. dcx ( ) . struct_span_err (
330
+ attr. span ,
331
+ "extern mutable statics are not allowed with `#[linkage]`" ,
332
+ ) ;
333
+ diag. note (
334
+ "marking the extern static mutable would allow changing which symbol \
335
+ the static references rather than make the target of the symbol \
336
+ mutable",
337
+ ) ;
338
+ diag. emit ( ) ;
339
+ }
327
340
} else {
328
341
codegen_fn_attrs. linkage = linkage;
329
342
}
330
- if tcx. is_mutable_static ( did. into ( ) ) {
331
- let mut diag = tcx. dcx ( ) . struct_span_err (
332
- attr. span ,
333
- "mutable statics are not allowed with `#[linkage]`" ,
334
- ) ;
335
- diag. note (
336
- "making the static mutable would allow changing which symbol the \
337
- static references rather than make the target of the symbol \
338
- mutable",
339
- ) ;
340
- diag. emit ( ) ;
341
- }
342
343
}
343
344
}
344
345
sym:: link_section => {
Original file line number Diff line number Diff line change 4
4
#![ feature( linkage) ]
5
5
6
6
fn main ( ) {
7
+ #[ rustfmt:: skip]
7
8
extern "C" {
8
- #[ linkage = "weak " ] //~ ERROR mutable statics are not allowed with `#[linkage]`
9
- static mut ABC : * const u8 ;
9
+ #[ linkage = "extern_weak " ] //~ ERROR extern mutable statics are not allowed with `#[linkage]`
10
+ static mut EXTERN_WEAK : * const u8 ;
10
11
}
11
12
12
13
unsafe {
13
- assert_eq ! ( ABC as usize , 0 ) ;
14
+ assert_eq ! ( EXTERN_WEAK as usize , 0 ) ;
15
+ }
16
+
17
+ // static mut is fine here as this is a definition rather than declaration.
18
+ #[ linkage = "weak" ]
19
+ static mut WEAK_DEF : u8 = 42 ;
20
+
21
+ unsafe {
22
+ assert_eq ! ( WEAK_DEF , 0 ) ;
14
23
}
15
24
}
Original file line number Diff line number Diff line change 1
- error: mutable statics are not allowed with `#[linkage]`
2
- --> $DIR/linkage-attr-mutable-static.rs:8 :9
1
+ error: extern mutable statics are not allowed with `#[linkage]`
2
+ --> $DIR/linkage-attr-mutable-static.rs:9 :9
3
3
|
4
- LL | #[linkage = "weak "]
5
- | ^^^^^^^^^^^^^^^^^^^
4
+ LL | #[linkage = "extern_weak "]
5
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
6
6
|
7
- = note: making the static mutable would allow changing which symbol the static references rather than make the target of the symbol mutable
7
+ = note: marking the extern static mutable would allow changing which symbol the static references rather than make the target of the symbol mutable
8
8
9
9
error: aborting due to 1 previous error
10
10
0 commit comments