Skip to content

Commit 661e033

Browse files
committed
in which the private no-mangle lints receive a valued lesson in humility
The incompetent fool who added these suggestions in 38e5a96 apparently thought it was safe to assume that, because the offending function or static was unreachable, it would therefore have not have any existing visibility modifiers, making it safe for us to unconditionally suggest inserting `pub`. This isn't true. This resolves rust-lang#47383.
1 parent 79a521b commit 661e033

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

src/librustc_lint/builtin.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1154,9 +1154,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
11541154
let msg = "function is marked #[no_mangle], but not exported";
11551155
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg);
11561156
let insertion_span = it.span.with_hi(it.span.lo());
1157-
err.span_suggestion(insertion_span,
1158-
"try making it public",
1159-
"pub ".to_owned());
1157+
if it.vis == hir::Visibility::Inherited {
1158+
err.span_suggestion(insertion_span,
1159+
"try making it public",
1160+
"pub ".to_owned());
1161+
}
11601162
err.emit();
11611163
}
11621164
if generics.is_type_parameterized() {
@@ -1177,9 +1179,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
11771179
let msg = "static is marked #[no_mangle], but not exported";
11781180
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg);
11791181
let insertion_span = it.span.with_hi(it.span.lo());
1180-
err.span_suggestion(insertion_span,
1181-
"try making it public",
1182-
"pub ".to_owned());
1182+
if it.vis == hir::Visibility::Inherited {
1183+
err.span_suggestion(insertion_span,
1184+
"try making it public",
1185+
"pub ".to_owned());
1186+
}
11831187
err.emit();
11841188
}
11851189
}

src/test/ui/lint/suggestions.rs

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ pub fn defiant<T>(_t: T) {}
2424
fn rio_grande() {} // should suggest `pub`
2525
//~^ WARN function is marked
2626

27+
mod badlands {
28+
// The private-no-mangle lints shouldn't suggest inserting `pub` when the
29+
// item is already `pub` (but triggered the lint because, e.g., it's in a
30+
// private module). (Issue #47383)
31+
#[no_mangle] pub static DAUNTLESS: bool = true;
32+
//~^ WARN static is marked
33+
#[no_mangle] pub fn val_jean() {}
34+
//~^ WARN function is marked
35+
}
36+
2737
struct Equinox {
2838
warp_factor: f32,
2939
}

src/test/ui/lint/suggestions.stderr

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: unnecessary parentheses around assigned value
2-
--> $DIR/suggestions.rs:36:21
2+
--> $DIR/suggestions.rs:46:21
33
|
4-
36 | let mut a = (1); // should suggest no `mut`, no parens
4+
46 | let mut a = (1); // should suggest no `mut`, no parens
55
| ^^^ help: remove these parentheses
66
|
77
note: lint level defined here
@@ -11,17 +11,17 @@ note: lint level defined here
1111
| ^^^^^^^^^^^^^
1212

1313
warning: use of deprecated attribute `no_debug`: the `#[no_debug]` attribute was an experimental feature that has been deprecated due to lack of demand. See https://github.com/rust-lang/rust/issues/29721
14-
--> $DIR/suggestions.rs:31:1
14+
--> $DIR/suggestions.rs:41:1
1515
|
16-
31 | #[no_debug] // should suggest removal of deprecated attribute
16+
41 | #[no_debug] // should suggest removal of deprecated attribute
1717
| ^^^^^^^^^^^ help: remove this attribute
1818
|
1919
= note: #[warn(deprecated)] on by default
2020

2121
warning: variable does not need to be mutable
22-
--> $DIR/suggestions.rs:36:13
22+
--> $DIR/suggestions.rs:46:13
2323
|
24-
36 | let mut a = (1); // should suggest no `mut`, no parens
24+
46 | let mut a = (1); // should suggest no `mut`, no parens
2525
| ---^^
2626
| |
2727
| help: remove this `mut`
@@ -72,18 +72,30 @@ warning: function is marked #[no_mangle], but not exported
7272
|
7373
= note: #[warn(private_no_mangle_fns)] on by default
7474

75+
warning: static is marked #[no_mangle], but not exported
76+
--> $DIR/suggestions.rs:31:18
77+
|
78+
31 | #[no_mangle] pub static DAUNTLESS: bool = true;
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80+
81+
warning: function is marked #[no_mangle], but not exported
82+
--> $DIR/suggestions.rs:33:18
83+
|
84+
33 | #[no_mangle] pub fn val_jean() {}
85+
| ^^^^^^^^^^^^^^^^^^^^
86+
7587
warning: denote infinite loops with `loop { ... }`
76-
--> $DIR/suggestions.rs:34:5
88+
--> $DIR/suggestions.rs:44:5
7789
|
78-
34 | while true { // should suggest `loop`
90+
44 | while true { // should suggest `loop`
7991
| ^^^^^^^^^^ help: use `loop`
8092
|
8193
= note: #[warn(while_true)] on by default
8294

8395
warning: the `warp_factor:` in this pattern is redundant
84-
--> $DIR/suggestions.rs:41:23
96+
--> $DIR/suggestions.rs:51:23
8597
|
86-
41 | Equinox { warp_factor: warp_factor } => {} // should suggest shorthand
98+
51 | Equinox { warp_factor: warp_factor } => {} // should suggest shorthand
8799
| ------------^^^^^^^^^^^^
88100
| |
89101
| help: remove this

0 commit comments

Comments
 (0)