Skip to content

Commit 51ed1ec

Browse files
committed
lint: Deny #[no_mangle] const items
This renames the PrivateNoMangleFns lint to allow both to happen in a single pass, since they do roughly the same work.
1 parent eaf4c5c commit 51ed1ec

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/librustc/lint/builtin.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,12 +2065,19 @@ declare_lint! {
20652065
"functions marked #[no_mangle] should be exported"
20662066
}
20672067

2068+
declare_lint! {
2069+
NO_MANGLE_CONST_ITEMS,
2070+
Deny,
2071+
"const items will not have their symbols exported"
2072+
}
2073+
20682074
#[derive(Copy)]
2069-
pub struct PrivateNoMangleFns;
2075+
pub struct InvalidNoMangleItems;
20702076

2071-
impl LintPass for PrivateNoMangleFns {
2077+
impl LintPass for InvalidNoMangleItems {
20722078
fn get_lints(&self) -> LintArray {
2073-
lint_array!(PRIVATE_NO_MANGLE_FNS)
2079+
lint_array!(PRIVATE_NO_MANGLE_FNS,
2080+
NO_MANGLE_CONST_ITEMS)
20742081
}
20752082

20762083
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
@@ -2083,6 +2090,12 @@ impl LintPass for PrivateNoMangleFns {
20832090
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice());
20842091
}
20852092
},
2093+
ast::ItemConst(..) => {
2094+
if attr::contains_name(it.attrs.as_slice(), "no_mangle") {
2095+
let msg = "const items should never be #[no_mangle]";
2096+
cx.span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
2097+
}
2098+
}
20862099
_ => {},
20872100
}
20882101
}

src/librustc/lint/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl LintStore {
213213
UnstableFeatures,
214214
Stability,
215215
UnconditionalRecursion,
216-
PrivateNoMangleFns,
216+
InvalidNoMangleItems,
217217
);
218218

219219
add_builtin_with_new!(sess,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
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
11+
// compile-flags:-F private_no_mangle_fns -F no_mangle_const_items
1212

1313
// FIXME(#19495) no_mangle'ing main ICE's.
1414
#[no_mangle]
1515
fn foo() { //~ ERROR function foo is marked #[no_mangle], but not exported
1616
}
1717

18+
#[allow(dead_code)]
19+
#[no_mangle]
20+
const FOO: u64 = 1; //~ ERROR const items should never be #[no_mangle]
21+
22+
#[no_mangle]
23+
pub const PUB_FOO: u64 = 1; //~ ERROR const items should never be #[no_mangle]
24+
1825
#[no_mangle]
1926
pub fn bar() {
2027
}

0 commit comments

Comments
 (0)