mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-08 13:18:32 +00:00
code suggestions for no-mangle lints
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 #44366.
This commit is contained in:
parent
f98939c6fd
commit
38e5a964f2
@ -44,7 +44,7 @@ use std::collections::HashSet;
|
|||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::attr;
|
use syntax::attr;
|
||||||
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
|
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 syntax::symbol::keywords;
|
||||||
|
|
||||||
use rustc::hir::{self, PatKind};
|
use rustc::hir::{self, PatKind};
|
||||||
@ -1133,35 +1133,55 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
|
|||||||
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
||||||
match it.node {
|
match it.node {
|
||||||
hir::ItemFn(.., ref generics, _) => {
|
hir::ItemFn(.., ref generics, _) => {
|
||||||
if attr::contains_name(&it.attrs, "no_mangle") &&
|
if let Some(no_mangle_attr) = attr::find_by_name(&it.attrs, "no_mangle") {
|
||||||
!attr::contains_name(&it.attrs, "linkage") {
|
if attr::contains_name(&it.attrs, "linkage") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if !cx.access_levels.is_reachable(it.id) {
|
if !cx.access_levels.is_reachable(it.id) {
|
||||||
let msg = format!("function {} is marked #[no_mangle], but not exported",
|
let msg = "function is marked #[no_mangle], but not exported";
|
||||||
it.name);
|
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg);
|
||||||
cx.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() {
|
if generics.is_type_parameterized() {
|
||||||
cx.span_lint(NO_MANGLE_GENERIC_ITEMS,
|
let mut err = cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS,
|
||||||
it.span,
|
it.span,
|
||||||
"functions generic over types must be mangled");
|
"functions generic over \
|
||||||
|
types must be mangled");
|
||||||
|
err.span_suggestion_short(no_mangle_attr.span,
|
||||||
|
"remove this attribute",
|
||||||
|
"".to_owned());
|
||||||
|
err.emit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hir::ItemStatic(..) => {
|
hir::ItemStatic(..) => {
|
||||||
if attr::contains_name(&it.attrs, "no_mangle") &&
|
if attr::contains_name(&it.attrs, "no_mangle") &&
|
||||||
!cx.access_levels.is_reachable(it.id) {
|
!cx.access_levels.is_reachable(it.id) {
|
||||||
let msg = format!("static {} is marked #[no_mangle], but not exported",
|
let msg = "static is marked #[no_mangle], but not exported";
|
||||||
it.name);
|
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg);
|
||||||
cx.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(..) => {
|
hir::ItemConst(..) => {
|
||||||
if attr::contains_name(&it.attrs, "no_mangle") {
|
if attr::contains_name(&it.attrs, "no_mangle") {
|
||||||
// Const items do not refer to a particular location in memory, and therefore
|
// Const items do not refer to a particular location in memory, and therefore
|
||||||
// don't have anything to attach a symbol to
|
// don't have anything to attach a symbol to
|
||||||
let msg = "const items should never be #[no_mangle], consider instead using \
|
let msg = "const items should never be #[no_mangle]";
|
||||||
`pub static`";
|
let mut err = cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
|
||||||
cx.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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -424,7 +424,7 @@ mod no_mangle {
|
|||||||
mod inner { #![no_mangle="3500"] }
|
mod inner { #![no_mangle="3500"] }
|
||||||
|
|
||||||
#[no_mangle = "3500"] fn f() { }
|
#[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;
|
#[no_mangle = "3500"] struct S;
|
||||||
|
|
||||||
|
@ -10,9 +10,8 @@
|
|||||||
|
|
||||||
// compile-flags:-F private_no_mangle_fns -F no_mangle_const_items -F private_no_mangle_statics
|
// 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]
|
#[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)]
|
#[allow(dead_code)]
|
||||||
@ -31,7 +30,7 @@ pub static BAR: u64 = 1;
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[no_mangle]
|
#[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() {
|
fn main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user