Skip to content

Commit 1d00c54

Browse files
committed
Auto merge of #21495 - richo:unexported-unmangled-lint, r=alexcrichton
The usecase is that functions made visible to systems outside of the rust ecosystem require the symbol to be visible. This adds a lint for functions that are not exported, but also not mangled. It has some gotchas: [ ]: There is fallout in core that needs taking care of [ ]: I'm not convinced the error message is correct [ ]: It has no tests ~~However, there's an underlying issue which I'd like feedback on- which is that my belief that that non-pub functions would not have their symbols exported, however that seems not to be the case in the first case that this lint turned up in rustc (`rust_fail`), which intuition suggests has been working.~~ This seems to be a separate bug in rust, wherein the symbols are exported in binaries, but not in rlibs or dylibs. This lint would catch that case.
2 parents 3fbfad3 + ff25fd6 commit 1d00c54

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/librustc/lint/builtin.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,35 @@ impl LintPass for HardwiredLints {
20372037
}
20382038
}
20392039

2040+
declare_lint! {
2041+
PRIVATE_NO_MANGLE_FNS,
2042+
Warn,
2043+
"functions marked #[no_mangle] should be exported"
2044+
}
2045+
2046+
#[derive(Copy)]
2047+
pub struct PrivateNoMangleFns;
2048+
2049+
impl LintPass for PrivateNoMangleFns {
2050+
fn get_lints(&self) -> LintArray {
2051+
lint_array!(PRIVATE_NO_MANGLE_FNS)
2052+
}
2053+
2054+
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
2055+
match it.node {
2056+
ast::ItemFn(..) => {
2057+
if attr::contains_name(it.attrs.as_slice(), "no_mangle") &&
2058+
!cx.exported_items.contains(&it.id) {
2059+
let msg = format!("function {} is marked #[no_mangle], but not exported",
2060+
it.ident);
2061+
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice());
2062+
}
2063+
},
2064+
_ => {},
2065+
}
2066+
}
2067+
}
2068+
20402069
/// Forbids using the `#[feature(...)]` attribute
20412070
#[derive(Copy)]
20422071
pub struct UnstableFeatures;

src/librustc/lint/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl LintStore {
213213
UnstableFeatures,
214214
Stability,
215215
UnconditionalRecursion,
216+
PrivateNoMangleFns,
216217
);
217218

218219
add_builtin_with_new!(sess,

src/libstd/rt/unwind.rs

+5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pub fn panicking() -> bool {
160160
// An uninlined, unmangled function upon which to slap yer breakpoints
161161
#[inline(never)]
162162
#[no_mangle]
163+
#[allow(private_no_mangle_fns)]
163164
fn rust_panic(cause: Box<Any + Send>) -> ! {
164165
rtdebug!("begin_unwind()");
165166

@@ -237,6 +238,7 @@ pub mod eabi {
237238

238239
#[lang="eh_personality"]
239240
#[no_mangle] // referenced from rust_try.ll
241+
#[allow(private_no_mangle_fns)]
240242
extern fn rust_eh_personality(
241243
version: c_int,
242244
actions: uw::_Unwind_Action,
@@ -290,6 +292,7 @@ pub mod eabi {
290292

291293
#[lang="eh_personality"]
292294
#[no_mangle] // referenced from rust_try.ll
295+
#[allow(private_no_mangle_fns)]
293296
pub extern "C" fn rust_eh_personality(
294297
version: c_int,
295298
actions: uw::_Unwind_Action,
@@ -343,6 +346,7 @@ pub mod eabi {
343346

344347
#[lang="eh_personality"]
345348
#[no_mangle] // referenced from rust_try.ll
349+
#[allow(private_no_mangle_fns)]
346350
extern "C" fn rust_eh_personality(
347351
state: uw::_Unwind_State,
348352
ue_header: *mut uw::_Unwind_Exception,
@@ -432,6 +436,7 @@ pub mod eabi {
432436

433437
#[lang="eh_personality"]
434438
#[no_mangle] // referenced from rust_try.ll
439+
#[allow(private_no_mangle_fns)]
435440
extern "C" fn rust_eh_personality(
436441
exceptionRecord: *mut EXCEPTION_RECORD,
437442
establisherFrame: *mut c_void,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:-F private_no_mangle_fns
12+
13+
// FIXME(#19495) no_mangle'ing main ICE's.
14+
#[no_mangle]
15+
fn foo() { //~ ERROR function foo is marked #[no_mangle], but not exported
16+
}
17+
18+
#[no_mangle]
19+
pub fn bar() {
20+
}
21+
22+
fn main() {
23+
foo();
24+
bar();
25+
}

0 commit comments

Comments
 (0)