Skip to content

Commit 38e5a96

Browse files
committed
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 rust-lang#44366.
1 parent f98939c commit 38e5a96

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

src/librustc_lint/builtin.rs

+35-15
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::collections::HashSet;
4444
use syntax::ast;
4545
use syntax::attr;
4646
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
47-
use syntax_pos::{Span, SyntaxContext};
47+
use syntax_pos::{BytePos, Span, SyntaxContext};
4848
use syntax::symbol::keywords;
4949

5050
use rustc::hir::{self, PatKind};
@@ -1133,35 +1133,55 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
11331133
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
11341134
match it.node {
11351135
hir::ItemFn(.., ref generics, _) => {
1136-
if attr::contains_name(&it.attrs, "no_mangle") &&
1137-
!attr::contains_name(&it.attrs, "linkage") {
1136+
if let Some(no_mangle_attr) = attr::find_by_name(&it.attrs, "no_mangle") {
1137+
if attr::contains_name(&it.attrs, "linkage") {
1138+
return;
1139+
}
11381140
if !cx.access_levels.is_reachable(it.id) {
1139-
let msg = format!("function {} is marked #[no_mangle], but not exported",
1140-
it.name);
1141-
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
1141+
let msg = "function is marked #[no_mangle], but not exported";
1142+
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg);
1143+
let insertion_span = it.span.with_hi(it.span.lo());
1144+
err.span_suggestion(insertion_span,
1145+
"try making it public",
1146+
"pub ".to_owned());
1147+
err.emit();
11421148
}
11431149
if generics.is_type_parameterized() {
1144-
cx.span_lint(NO_MANGLE_GENERIC_ITEMS,
1145-
it.span,
1146-
"functions generic over types must be mangled");
1150+
let mut err = cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS,
1151+
it.span,
1152+
"functions generic over \
1153+
types must be mangled");
1154+
err.span_suggestion_short(no_mangle_attr.span,
1155+
"remove this attribute",
1156+
"".to_owned());
1157+
err.emit();
11471158
}
11481159
}
11491160
}
11501161
hir::ItemStatic(..) => {
11511162
if attr::contains_name(&it.attrs, "no_mangle") &&
11521163
!cx.access_levels.is_reachable(it.id) {
1153-
let msg = format!("static {} is marked #[no_mangle], but not exported",
1154-
it.name);
1155-
cx.span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, &msg);
1164+
let msg = "static is marked #[no_mangle], but not exported";
1165+
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg);
1166+
let insertion_span = it.span.with_hi(it.span.lo());
1167+
err.span_suggestion(insertion_span,
1168+
"try making it public",
1169+
"pub ".to_owned());
1170+
err.emit();
11561171
}
11571172
}
11581173
hir::ItemConst(..) => {
11591174
if attr::contains_name(&it.attrs, "no_mangle") {
11601175
// Const items do not refer to a particular location in memory, and therefore
11611176
// don't have anything to attach a symbol to
1162-
let msg = "const items should never be #[no_mangle], consider instead using \
1163-
`pub static`";
1164-
cx.span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
1177+
let msg = "const items should never be #[no_mangle]";
1178+
let mut err = cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
1179+
// `const` is 5 chars
1180+
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + 5));
1181+
err.span_suggestion(const_span,
1182+
"try a static value",
1183+
"pub static".to_owned());
1184+
err.emit();
11651185
}
11661186
}
11671187
_ => {}

src/test/compile-fail/feature-gate/issue-43106-gating-of-builtin-attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ mod no_mangle {
424424
mod inner { #![no_mangle="3500"] }
425425

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

429429
#[no_mangle = "3500"] struct S;
430430

src/test/compile-fail/lint-unexported-no-mangle.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

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

13-
// FIXME(#19495) no_mangle'ing main ICE's.
1413
#[no_mangle]
15-
fn foo() { //~ ERROR function foo is marked #[no_mangle], but not exported
14+
fn foo() { //~ ERROR function is marked #[no_mangle], but not exported
1615
}
1716

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

3231
#[allow(dead_code)]
3332
#[no_mangle]
34-
static PRIVATE_BAR: u64 = 1; //~ ERROR static PRIVATE_BAR is marked #[no_mangle], but not exported
33+
static PRIVATE_BAR: u64 = 1; //~ ERROR static is marked #[no_mangle], but not exported
3534

3635

3736
fn main() {

0 commit comments

Comments
 (0)