lint: Warn about no-mangled statics that are not exported
This commit is contained in:
parent
51ed1ecefd
commit
73d5d89567
2 changed files with 24 additions and 1 deletions
|
@ -2065,6 +2065,12 @@ declare_lint! {
|
||||||
"functions marked #[no_mangle] should be exported"
|
"functions marked #[no_mangle] should be exported"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare_lint! {
|
||||||
|
PRIVATE_NO_MANGLE_STATICS,
|
||||||
|
Warn,
|
||||||
|
"statics marked #[no_mangle] should be exported"
|
||||||
|
}
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
NO_MANGLE_CONST_ITEMS,
|
NO_MANGLE_CONST_ITEMS,
|
||||||
Deny,
|
Deny,
|
||||||
|
@ -2077,6 +2083,7 @@ pub struct InvalidNoMangleItems;
|
||||||
impl LintPass for InvalidNoMangleItems {
|
impl LintPass for InvalidNoMangleItems {
|
||||||
fn get_lints(&self) -> LintArray {
|
fn get_lints(&self) -> LintArray {
|
||||||
lint_array!(PRIVATE_NO_MANGLE_FNS,
|
lint_array!(PRIVATE_NO_MANGLE_FNS,
|
||||||
|
PRIVATE_NO_MANGLE_STATICS,
|
||||||
NO_MANGLE_CONST_ITEMS)
|
NO_MANGLE_CONST_ITEMS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2090,6 +2097,14 @@ impl LintPass for InvalidNoMangleItems {
|
||||||
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice());
|
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice());
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
ast::ItemStatic(..) => {
|
||||||
|
if attr::contains_name(it.attrs.as_slice(), "no_mangle") &&
|
||||||
|
!cx.exported_items.contains(&it.id) {
|
||||||
|
let msg = format!("static {} is marked #[no_mangle], but not exported",
|
||||||
|
it.ident);
|
||||||
|
cx.span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg.as_slice());
|
||||||
|
}
|
||||||
|
},
|
||||||
ast::ItemConst(..) => {
|
ast::ItemConst(..) => {
|
||||||
if attr::contains_name(it.attrs.as_slice(), "no_mangle") {
|
if attr::contains_name(it.attrs.as_slice(), "no_mangle") {
|
||||||
let msg = "const items should never be #[no_mangle]";
|
let msg = "const items should never be #[no_mangle]";
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
// compile-flags:-F private_no_mangle_fns -F no_mangle_const_items
|
// compile-flags:-F private_no_mangle_fns -F no_mangle_const_items -F private_no_mangle_statics
|
||||||
|
|
||||||
// FIXME(#19495) no_mangle'ing main ICE's.
|
// FIXME(#19495) no_mangle'ing main ICE's.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -26,6 +26,14 @@ pub const PUB_FOO: u64 = 1; //~ ERROR const items should never be #[no_mangle]
|
||||||
pub fn bar() {
|
pub fn bar() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub static BAR: u64 = 1;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[no_mangle]
|
||||||
|
static PRIVATE_BAR: u64 = 1; //~ ERROR static PRIVATE_BAR is marked #[no_mangle], but not exported
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
foo();
|
foo();
|
||||||
bar();
|
bar();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue