Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow static mut definitions with #[linkage] #125920

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,21 +324,22 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
let linkage = Some(linkage_by_name(tcx, did, val.as_str()));
if tcx.is_foreign_item(did) {
codegen_fn_attrs.import_linkage = linkage;

if tcx.is_mutable_static(did.into()) {
let mut diag = tcx.dcx().struct_span_err(
attr.span,
"extern mutable statics are not allowed with `#[linkage]`",
);
diag.note(
"marking the extern static mutable would allow changing which symbol \
the static references rather than make the target of the symbol \
mutable",
);
diag.emit();
}
} else {
codegen_fn_attrs.linkage = linkage;
}
if tcx.is_mutable_static(did.into()) {
let mut diag = tcx.dcx().struct_span_err(
attr.span,
"mutable statics are not allowed with `#[linkage]`",
);
diag.note(
"making the static mutable would allow changing which symbol the \
static references rather than make the target of the symbol \
mutable",
);
diag.emit();
}
}
}
sym::link_section => {
Expand Down
15 changes: 12 additions & 3 deletions tests/ui/linkage-attr/linkage-attr-mutable-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
#![feature(linkage)]

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

unsafe {
assert_eq!(ABC as usize, 0);
assert_eq!(EXTERN_WEAK as usize, 0);
}

// static mut is fine here as this is a definition rather than declaration.
#[linkage = "weak"]
static mut WEAK_DEF: u8 = 42;

unsafe {
assert_eq!(WEAK_DEF, 0);
}
}
10 changes: 5 additions & 5 deletions tests/ui/linkage-attr/linkage-attr-mutable-static.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: mutable statics are not allowed with `#[linkage]`
--> $DIR/linkage-attr-mutable-static.rs:8:9
error: extern mutable statics are not allowed with `#[linkage]`
--> $DIR/linkage-attr-mutable-static.rs:9:9
|
LL | #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^
LL | #[linkage = "extern_weak"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: making the static mutable would allow changing which symbol the static references rather than make the target of the symbol mutable
= note: marking the extern static mutable would allow changing which symbol the static references rather than make the target of the symbol mutable

error: aborting due to 1 previous error

Loading