Skip to content

Commit 73d5d89

Browse files
committed
lint: Warn about no-mangled statics that are not exported
1 parent 51ed1ec commit 73d5d89

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/librustc/lint/builtin.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,12 @@ declare_lint! {
20652065
"functions marked #[no_mangle] should be exported"
20662066
}
20672067

2068+
declare_lint! {
2069+
PRIVATE_NO_MANGLE_STATICS,
2070+
Warn,
2071+
"statics marked #[no_mangle] should be exported"
2072+
}
2073+
20682074
declare_lint! {
20692075
NO_MANGLE_CONST_ITEMS,
20702076
Deny,
@@ -2077,6 +2083,7 @@ pub struct InvalidNoMangleItems;
20772083
impl LintPass for InvalidNoMangleItems {
20782084
fn get_lints(&self) -> LintArray {
20792085
lint_array!(PRIVATE_NO_MANGLE_FNS,
2086+
PRIVATE_NO_MANGLE_STATICS,
20802087
NO_MANGLE_CONST_ITEMS)
20812088
}
20822089

@@ -2090,6 +2097,14 @@ impl LintPass for InvalidNoMangleItems {
20902097
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice());
20912098
}
20922099
},
2100+
ast::ItemStatic(..) => {
2101+
if attr::contains_name(it.attrs.as_slice(), "no_mangle") &&
2102+
!cx.exported_items.contains(&it.id) {
2103+
let msg = format!("static {} is marked #[no_mangle], but not exported",
2104+
it.ident);
2105+
cx.span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg.as_slice());
2106+
}
2107+
},
20932108
ast::ItemConst(..) => {
20942109
if attr::contains_name(it.attrs.as_slice(), "no_mangle") {
20952110
let msg = "const items should never be #[no_mangle]";

src/test/compile-fail/lint-unexported-no-mangle.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags:-F private_no_mangle_fns -F no_mangle_const_items
11+
// compile-flags:-F private_no_mangle_fns -F no_mangle_const_items -F private_no_mangle_statics
1212

1313
// FIXME(#19495) no_mangle'ing main ICE's.
1414
#[no_mangle]
@@ -26,6 +26,14 @@ pub const PUB_FOO: u64 = 1; //~ ERROR const items should never be #[no_mangle]
2626
pub fn bar() {
2727
}
2828

29+
#[no_mangle]
30+
pub static BAR: u64 = 1;
31+
32+
#[allow(dead_code)]
33+
#[no_mangle]
34+
static PRIVATE_BAR: u64 = 1; //~ ERROR static PRIVATE_BAR is marked #[no_mangle], but not exported
35+
36+
2937
fn main() {
3038
foo();
3139
bar();

0 commit comments

Comments
 (0)