Skip to content

lint: Deny #[no_mangle] const items #21881

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

Merged
merged 3 commits into from
Feb 12, 2015
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
37 changes: 34 additions & 3 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,12 +2065,26 @@ declare_lint! {
"functions marked #[no_mangle] should be exported"
}

declare_lint! {
PRIVATE_NO_MANGLE_STATICS,
Warn,
"statics marked #[no_mangle] should be exported"
}

declare_lint! {
NO_MANGLE_CONST_ITEMS,
Deny,
"const items will not have their symbols exported"
}

#[derive(Copy)]
pub struct PrivateNoMangleFns;
pub struct InvalidNoMangleItems;

impl LintPass for PrivateNoMangleFns {
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) {
Expand All @@ -2083,6 +2097,23 @@ impl LintPass for PrivateNoMangleFns {
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(..) => {
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);
}
}
_ => {},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl LintStore {
UnstableFeatures,
Stability,
UnconditionalRecursion,
PrivateNoMangleFns,
InvalidNoMangleItems,
);

add_builtin_with_new!(sess,
Expand Down
17 changes: 16 additions & 1 deletion src/test/compile-fail/lint-unexported-no-mangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down