Skip to content

Commit 289ae60

Browse files
committed
Auto merge of #21881 - richo:lint-no-mangle-const, r=kmcallister
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](https://github.com/rust-lang/rfcs/blob/cc53afbe5dea41e1f7d1c3dce71e013abe025211/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)
2 parents 5936278 + b6f55ef commit 289ae60

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/librustc/lint/builtin.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -2080,12 +2080,26 @@ declare_lint! {
20802080
"functions marked #[no_mangle] should be exported"
20812081
}
20822082

2083+
declare_lint! {
2084+
PRIVATE_NO_MANGLE_STATICS,
2085+
Warn,
2086+
"statics marked #[no_mangle] should be exported"
2087+
}
2088+
2089+
declare_lint! {
2090+
NO_MANGLE_CONST_ITEMS,
2091+
Deny,
2092+
"const items will not have their symbols exported"
2093+
}
2094+
20832095
#[derive(Copy)]
2084-
pub struct PrivateNoMangleFns;
2096+
pub struct InvalidNoMangleItems;
20852097

2086-
impl LintPass for PrivateNoMangleFns {
2098+
impl LintPass for InvalidNoMangleItems {
20872099
fn get_lints(&self) -> LintArray {
2088-
lint_array!(PRIVATE_NO_MANGLE_FNS)
2100+
lint_array!(PRIVATE_NO_MANGLE_FNS,
2101+
PRIVATE_NO_MANGLE_STATICS,
2102+
NO_MANGLE_CONST_ITEMS)
20892103
}
20902104

20912105
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
@@ -2098,6 +2112,23 @@ impl LintPass for PrivateNoMangleFns {
20982112
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
20992113
}
21002114
},
2115+
ast::ItemStatic(..) => {
2116+
if attr::contains_name(it.attrs.as_slice(), "no_mangle") &&
2117+
!cx.exported_items.contains(&it.id) {
2118+
let msg = format!("static {} is marked #[no_mangle], but not exported",
2119+
it.ident);
2120+
cx.span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg.as_slice());
2121+
}
2122+
},
2123+
ast::ItemConst(..) => {
2124+
if attr::contains_name(it.attrs.as_slice(), "no_mangle") {
2125+
// Const items do not refer to a particular location in memory, and therefore
2126+
// don't have anything to attach a symbol to
2127+
let msg = "const items should never be #[no_mangle], consider instead using \
2128+
`pub static`";
2129+
cx.span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
2130+
}
2131+
}
21012132
_ => {},
21022133
}
21032134
}

src/librustc/lint/context.rs

+1-1
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

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,32 @@
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 -F private_no_mangle_statics
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
}
2128

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+
2237
fn main() {
2338
foo();
2439
bar();

0 commit comments

Comments
 (0)