Skip to content

Commit

Permalink
code suggestions for no-mangle lints
Browse files Browse the repository at this point in the history
At reviewer's suggestion, we remove the function/static name from the
main lint message. While we're correspondingly adjusting the
expectations of a compile-fail test, we remove an obsolete FIXME
comment, another quantum of progress towards resolving the fabulous
metabug rust-lang#44366.
  • Loading branch information
zackmdavis committed Oct 16, 2017
1 parent f98939c commit 38e5a96
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
50 changes: 35 additions & 15 deletions src/librustc_lint/builtin.rs
Expand Up @@ -44,7 +44,7 @@ use std::collections::HashSet;
use syntax::ast;
use syntax::attr;
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
use syntax_pos::{Span, SyntaxContext};
use syntax_pos::{BytePos, Span, SyntaxContext};
use syntax::symbol::keywords;

use rustc::hir::{self, PatKind};
Expand Down Expand Up @@ -1133,35 +1133,55 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
match it.node {
hir::ItemFn(.., ref generics, _) => {
if attr::contains_name(&it.attrs, "no_mangle") &&
!attr::contains_name(&it.attrs, "linkage") {
if let Some(no_mangle_attr) = attr::find_by_name(&it.attrs, "no_mangle") {
if attr::contains_name(&it.attrs, "linkage") {
return;
}
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);
let msg = "function is marked #[no_mangle], but not exported";
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg);
let insertion_span = it.span.with_hi(it.span.lo());
err.span_suggestion(insertion_span,
"try making it public",
"pub ".to_owned());
err.emit();
}
if generics.is_type_parameterized() {
cx.span_lint(NO_MANGLE_GENERIC_ITEMS,
it.span,
"functions generic over types must be mangled");
let mut err = cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS,
it.span,
"functions generic over \
types must be mangled");
err.span_suggestion_short(no_mangle_attr.span,
"remove this attribute",
"".to_owned());
err.emit();
}
}
}
hir::ItemStatic(..) => {
if attr::contains_name(&it.attrs, "no_mangle") &&
!cx.access_levels.is_reachable(it.id) {
let msg = format!("static {} is marked #[no_mangle], but not exported",
it.name);
cx.span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, &msg);
let msg = "static is marked #[no_mangle], but not exported";
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg);
let insertion_span = it.span.with_hi(it.span.lo());
err.span_suggestion(insertion_span,
"try making it public",
"pub ".to_owned());
err.emit();
}
}
hir::ItemConst(..) => {
if attr::contains_name(&it.attrs, "no_mangle") {
// Const items do not refer to a particular location in memory, and therefore
// don't have anything to attach a symbol to
let msg = "const items should never be #[no_mangle], consider instead using \
`pub static`";
cx.span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
let msg = "const items should never be #[no_mangle]";
let mut err = cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
// `const` is 5 chars
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + 5));
err.span_suggestion(const_span,
"try a static value",
"pub static".to_owned());
err.emit();
}
}
_ => {}
Expand Down
Expand Up @@ -424,7 +424,7 @@ mod no_mangle {
mod inner { #![no_mangle="3500"] }

#[no_mangle = "3500"] fn f() { }
//~^ WARN function f is marked #[no_mangle], but not exported
//~^ WARN function is marked #[no_mangle], but not exported

#[no_mangle = "3500"] struct S;

Expand Down
5 changes: 2 additions & 3 deletions src/test/compile-fail/lint-unexported-no-mangle.rs
Expand Up @@ -10,9 +10,8 @@

// compile-flags:-F private_no_mangle_fns -F no_mangle_const_items -F private_no_mangle_statics

// FIXME(#19495) no_mangle'ing main ICE's.
#[no_mangle]
fn foo() { //~ ERROR function foo is marked #[no_mangle], but not exported
fn foo() { //~ ERROR function is marked #[no_mangle], but not exported
}

#[allow(dead_code)]
Expand All @@ -31,7 +30,7 @@ pub static BAR: u64 = 1;

#[allow(dead_code)]
#[no_mangle]
static PRIVATE_BAR: u64 = 1; //~ ERROR static PRIVATE_BAR is marked #[no_mangle], but not exported
static PRIVATE_BAR: u64 = 1; //~ ERROR static is marked #[no_mangle], but not exported


fn main() {
Expand Down

0 comments on commit 38e5a96

Please sign in to comment.