提交 1d00c545 编写于 作者: B bors

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.
......@@ -2037,6 +2037,35 @@ fn get_lints(&self) -> LintArray {
}
}
declare_lint! {
PRIVATE_NO_MANGLE_FNS,
Warn,
"functions marked #[no_mangle] should be exported"
}
#[derive(Copy)]
pub struct PrivateNoMangleFns;
impl LintPass for PrivateNoMangleFns {
fn get_lints(&self) -> LintArray {
lint_array!(PRIVATE_NO_MANGLE_FNS)
}
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
match it.node {
ast::ItemFn(..) => {
if attr::contains_name(it.attrs.as_slice(), "no_mangle") &&
!cx.exported_items.contains(&it.id) {
let msg = format!("function {} is marked #[no_mangle], but not exported",
it.ident);
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg.as_slice());
}
},
_ => {},
}
}
}
/// Forbids using the `#[feature(...)]` attribute
#[derive(Copy)]
pub struct UnstableFeatures;
......
......@@ -213,6 +213,7 @@ pub fn register_builtin(&mut self, sess: Option<&Session>) {
UnstableFeatures,
Stability,
UnconditionalRecursion,
PrivateNoMangleFns,
);
add_builtin_with_new!(sess,
......
......@@ -160,6 +160,7 @@ pub fn panicking() -> bool {
// An uninlined, unmangled function upon which to slap yer breakpoints
#[inline(never)]
#[no_mangle]
#[allow(private_no_mangle_fns)]
fn rust_panic(cause: Box<Any + Send>) -> ! {
rtdebug!("begin_unwind()");
......@@ -237,6 +238,7 @@ fn __gcc_personality_v0(version: c_int,
#[lang="eh_personality"]
#[no_mangle] // referenced from rust_try.ll
#[allow(private_no_mangle_fns)]
extern fn rust_eh_personality(
version: c_int,
actions: uw::_Unwind_Action,
......@@ -290,6 +292,7 @@ fn __gcc_personality_sj0(version: c_int,
#[lang="eh_personality"]
#[no_mangle] // referenced from rust_try.ll
#[allow(private_no_mangle_fns)]
pub extern "C" fn rust_eh_personality(
version: c_int,
actions: uw::_Unwind_Action,
......@@ -343,6 +346,7 @@ fn __gcc_personality_v0(state: uw::_Unwind_State,
#[lang="eh_personality"]
#[no_mangle] // referenced from rust_try.ll
#[allow(private_no_mangle_fns)]
extern "C" fn rust_eh_personality(
state: uw::_Unwind_State,
ue_header: *mut uw::_Unwind_Exception,
......@@ -432,6 +436,7 @@ fn _GCC_specific_handler(
#[lang="eh_personality"]
#[no_mangle] // referenced from rust_try.ll
#[allow(private_no_mangle_fns)]
extern "C" fn rust_eh_personality(
exceptionRecord: *mut EXCEPTION_RECORD,
establisherFrame: *mut c_void,
......
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags:-F private_no_mangle_fns
// FIXME(#19495) no_mangle'ing main ICE's.
#[no_mangle]
fn foo() { //~ ERROR function foo is marked #[no_mangle], but not exported
}
#[no_mangle]
pub fn bar() {
}
fn main() {
foo();
bar();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册