Skip to content

Commit 55ffc33

Browse files
committed
Warn no_mangle on generic functions
1 parent 7b77f67 commit 55ffc33

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/librustc_lint/builtin.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -971,26 +971,39 @@ declare_lint! {
971971
"const items will not have their symbols exported"
972972
}
973973

974+
declare_lint! {
975+
NO_MANGLE_GENERIC_ITEMS,
976+
Warn,
977+
"generic items must be mangled"
978+
}
979+
974980
#[derive(Copy, Clone)]
975981
pub struct InvalidNoMangleItems;
976982

977983
impl LintPass for InvalidNoMangleItems {
978984
fn get_lints(&self) -> LintArray {
979985
lint_array!(PRIVATE_NO_MANGLE_FNS,
980986
PRIVATE_NO_MANGLE_STATICS,
981-
NO_MANGLE_CONST_ITEMS)
987+
NO_MANGLE_CONST_ITEMS,
988+
NO_MANGLE_GENERIC_ITEMS)
982989
}
983990
}
984991

985992
impl LateLintPass for InvalidNoMangleItems {
986993
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
987994
match it.node {
988-
hir::ItemFn(..) => {
989-
if attr::contains_name(&it.attrs, "no_mangle") &&
990-
!cx.access_levels.is_reachable(it.id) {
991-
let msg = format!("function {} is marked #[no_mangle], but not exported",
992-
it.name);
993-
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
995+
hir::ItemFn(_, _, _, _, ref generics, _) => {
996+
if attr::contains_name(&it.attrs, "no_mangle") {
997+
if !cx.access_levels.is_reachable(it.id) {
998+
let msg = format!("function {} is marked #[no_mangle], but not exported",
999+
it.name);
1000+
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
1001+
}
1002+
if generics.is_parameterized() {
1003+
cx.span_lint(NO_MANGLE_GENERIC_ITEMS,
1004+
it.span,
1005+
"generic functions must be mangled");
1006+
}
9941007
}
9951008
},
9961009
hir::ItemStatic(..) => {

src/test/compile-fail/generic-no-mangle.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

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

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

1616
#[no_mangle]
17-
extern fn foo<T>() {} //~ ERROR generic functions must be mangled
17+
pub extern fn bar<T>() {} //~ ERROR generic functions must be mangled
18+
19+
fn main() {}

0 commit comments

Comments
 (0)