rollup merge of #21881: richo/lint-no-mangle-const

This renames the PrivateNoMangleFns lint to allow both to happen in a
single pass, since they do roughly the same work.

Closes #21856

Open questions:

[ ]: Do the tests actually pass (I'm running make check and running out the door now)
[ ]: Is the name of this lint ok. it seems to mostly be fine with [convention](cc53afbe5d/text/0344-conventions-galore.md (lints))
[ ]: I'm not super thrilled about the warning text

r? @kmcallister (Shamelessly nominating because you were looking at my other ticket)
This commit is contained in:
Alex Crichton 2015-02-11 14:02:00 -08:00
commit 8b8331ad09
3 changed files with 52 additions and 6 deletions

View File

@ -2080,12 +2080,26 @@ fn get_lints(&self) -> LintArray {
"functions marked #[no_mangle] should be exported"
}
#[derive(Copy)]
pub struct PrivateNoMangleFns;
declare_lint! {
PRIVATE_NO_MANGLE_STATICS,
Warn,
"statics marked #[no_mangle] should be exported"
}
impl LintPass for PrivateNoMangleFns {
declare_lint! {
NO_MANGLE_CONST_ITEMS,
Deny,
"const items will not have their symbols exported"
}
#[derive(Copy)]
pub struct InvalidNoMangleItems;
impl LintPass for InvalidNoMangleItems {
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)
}
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
@ -2098,6 +2112,23 @@ fn check_item(&mut self, cx: &Context, it: &ast::Item) {
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
}
},
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(..) => {
if attr::contains_name(it.attrs.as_slice(), "no_mangle") {
// Const items do not refer to a particular location in memory, and therefore
// don't have anything to attach a symbol to
let msg = "const items should never be #[no_mangle], consider instead using \
`pub static`";
cx.span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
}
}
_ => {},
}
}

View File

@ -213,7 +213,7 @@ macro_rules! add_lint_group {
UnstableFeatures,
Stability,
UnconditionalRecursion,
PrivateNoMangleFns,
InvalidNoMangleItems,
);
add_builtin_with_new!(sess,

View File

@ -8,17 +8,32 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags:-F private_no_mangle_fns
// 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.
#[no_mangle]
fn foo() { //~ ERROR function foo is marked #[no_mangle], but not exported
}
#[allow(dead_code)]
#[no_mangle]
const FOO: u64 = 1; //~ ERROR const items should never be #[no_mangle]
#[no_mangle]
pub const PUB_FOO: u64 = 1; //~ ERROR const items should never be #[no_mangle]
#[no_mangle]
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() {
foo();
bar();