Skip to content

Commit 07dc3eb

Browse files
committed
Allow static mut definitions with #[linkage]
Unlike static declarations with #[linkage], for definitions rustc doesn't rewrite it to add an extra indirection.
1 parent 1d52972 commit 07dc3eb

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -324,21 +324,22 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
324324
let linkage = Some(linkage_by_name(tcx, did, val.as_str()));
325325
if tcx.is_foreign_item(did) {
326326
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+
}
327340
} else {
328341
codegen_fn_attrs.linkage = linkage;
329342
}
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-
}
342343
}
343344
}
344345
sym::link_section => {

tests/ui/linkage-attr/linkage-attr-mutable-static.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
#![feature(linkage)]
55

66
fn main() {
7+
#[rustfmt::skip]
78
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;
1011
}
1112

1213
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);
1423
}
1524
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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
33
|
4-
LL | #[linkage = "weak"]
5-
| ^^^^^^^^^^^^^^^^^^^
4+
LL | #[linkage = "extern_weak"]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
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
88

99
error: aborting due to 1 previous error
1010

0 commit comments

Comments
 (0)