Skip to content

Commit

Permalink
Only allow immutable statics with #[linkage]
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed May 12, 2024
1 parent b71fa82 commit e4a404a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
} else {
codegen_fn_attrs.linkage = linkage;
}
if tcx.is_mutable_static(did.into()) {
tcx.dcx()
.span_err(attr.span, "mutable statics are not allowed with #[linkage]");
}
}
}
sym::link_section => {
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/linkage-attr/linkage-attr-mutable-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! The symbols are resolved by the linker. It doesn't make sense to change
//! them at runtime, so deny mutable statics with #[linkage].

#![feature(linkage)]

fn main() {
extern "C" {
#[linkage = "weak"] //~ ERROR mutable statics are not allowed with #[linkage]
static mut ABC: *const u8;
}

unsafe {
assert_eq!(ABC as usize, 0);
}
}
8 changes: 8 additions & 0 deletions tests/ui/linkage-attr/linkage-attr-mutable-static.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: mutable statics are not allowed with #[linkage]
--> $DIR/linkage-attr-mutable-static.rs:8:9
|
LL | #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

0 comments on commit e4a404a

Please sign in to comment.