Skip to content
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

Warn no_mangle on generic functions #30269

Merged
merged 1 commit into from
Dec 16, 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
27 changes: 20 additions & 7 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,26 +971,39 @@ declare_lint! {
"const items will not have their symbols exported"
}

declare_lint! {
NO_MANGLE_GENERIC_ITEMS,
Warn,
"generic items must be mangled"
}

#[derive(Copy, Clone)]
pub struct InvalidNoMangleItems;

impl LintPass for InvalidNoMangleItems {
fn get_lints(&self) -> LintArray {
lint_array!(PRIVATE_NO_MANGLE_FNS,
PRIVATE_NO_MANGLE_STATICS,
NO_MANGLE_CONST_ITEMS)
NO_MANGLE_CONST_ITEMS,
NO_MANGLE_GENERIC_ITEMS)
}
}

impl LateLintPass for InvalidNoMangleItems {
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
match it.node {
hir::ItemFn(..) => {
if attr::contains_name(&it.attrs, "no_mangle") &&
!cx.access_levels.is_reachable(it.id) {
let msg = format!("function {} is marked #[no_mangle], but not exported",
it.name);
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
hir::ItemFn(_, _, _, _, ref generics, _) => {
if attr::contains_name(&it.attrs, "no_mangle") {
if !cx.access_levels.is_reachable(it.id) {
let msg = format!("function {} is marked #[no_mangle], but not exported",
it.name);
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
}
if generics.is_parameterized() {
cx.span_lint(NO_MANGLE_GENERIC_ITEMS,
it.span,
"generic functions must be mangled");
}
}
},
hir::ItemStatic(..) => {
Expand Down
8 changes: 5 additions & 3 deletions src/test/compile-fail/generic-no-mangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-test this should fail to compile (#15844)
#![deny(no_mangle_generic_items)]

#[no_mangle]
fn foo<T>() {} //~ ERROR generic functions must be mangled
pub fn foo<T>() {} //~ ERROR generic functions must be mangled

#[no_mangle]
extern fn foo<T>() {} //~ ERROR generic functions must be mangled
pub extern fn bar<T>() {} //~ ERROR generic functions must be mangled

fn main() {}