Skip to content

Commit f8b330d

Browse files
authored
Rollup merge of #72209 - Nemo157:lint-no-mangle-in-unsafe-code, r=nikomatsakis
Add checking for no_mangle to unsafe_code lint fixes #72188 r? `@estebank`
2 parents 0fc6756 + fc8a3ad commit f8b330d

File tree

3 files changed

+159
-20
lines changed

3 files changed

+159
-20
lines changed

compiler/rustc_lint/src/builtin.rs

+46
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,18 @@ impl UnsafeCode {
328328

329329
cx.struct_span_lint(UNSAFE_CODE, span, decorate);
330330
}
331+
332+
fn report_overriden_symbol_name(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) {
333+
self.report_unsafe(cx, span, |lint| {
334+
lint.build(msg)
335+
.note(
336+
"the linker's behavior with multiple libraries exporting duplicate symbol \
337+
names is undefined and Rust cannot provide guarantees when you manually \
338+
override them",
339+
)
340+
.emit();
341+
})
342+
}
331343
}
332344

333345
impl EarlyLintPass for UnsafeCode {
@@ -367,6 +379,40 @@ impl EarlyLintPass for UnsafeCode {
367379
lint.build("implementation of an `unsafe` trait").emit()
368380
}),
369381

382+
ast::ItemKind::Fn(..) => {
383+
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
384+
self.report_overriden_symbol_name(
385+
cx,
386+
attr.span,
387+
"declaration of a `no_mangle` function",
388+
);
389+
}
390+
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
391+
self.report_overriden_symbol_name(
392+
cx,
393+
attr.span,
394+
"declaration of a function with `export_name`",
395+
);
396+
}
397+
}
398+
399+
ast::ItemKind::Static(..) => {
400+
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
401+
self.report_overriden_symbol_name(
402+
cx,
403+
attr.span,
404+
"declaration of a `no_mangle` static",
405+
);
406+
}
407+
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
408+
self.report_overriden_symbol_name(
409+
cx,
410+
attr.span,
411+
"declaration of a static with `export_name`",
412+
);
413+
}
414+
}
415+
370416
_ => {}
371417
}
372418
}

src/test/ui/lint/lint-unsafe-code.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,28 @@ mod allowed_unsafe {
1212
unsafe fn also_allowed() {}
1313
unsafe trait AllowedUnsafe { }
1414
unsafe impl AllowedUnsafe for super::Bar {}
15+
#[no_mangle] fn allowed2() {}
16+
#[export_name = "foo"] fn allowed3() {}
1517
}
1618

1719
macro_rules! unsafe_in_macro {
18-
() => {
20+
() => {{
21+
#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
22+
#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
23+
#[export_name = "bar"] fn bar() {}
24+
//~^ ERROR: declaration of a function with `export_name`
25+
#[export_name = "BAR"] static BAR: u32 = 5;
26+
//~^ ERROR: declaration of a static with `export_name`
1927
unsafe {} //~ ERROR: usage of an `unsafe` block
20-
}
28+
}}
2129
}
2230

31+
#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
32+
#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
33+
34+
#[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name`
35+
#[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name`
36+
2337
unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
2438
unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
2539
unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
+97-18
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,168 @@
1-
error: declaration of an `unsafe` function
2-
--> $DIR/lint-unsafe-code.rs:23:1
1+
error: declaration of a `no_mangle` function
2+
--> $DIR/lint-unsafe-code.rs:31:1
33
|
4-
LL | unsafe fn baz() {}
5-
| ^^^^^^^^^^^^^^^^^^
4+
LL | #[no_mangle] fn foo() {}
5+
| ^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/lint-unsafe-code.rs:3:9
99
|
1010
LL | #![deny(unsafe_code)]
1111
| ^^^^^^^^^^^
12+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
13+
14+
error: declaration of a `no_mangle` static
15+
--> $DIR/lint-unsafe-code.rs:32:1
16+
|
17+
LL | #[no_mangle] static FOO: u32 = 5;
18+
| ^^^^^^^^^^^^
19+
|
20+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
21+
22+
error: declaration of a function with `export_name`
23+
--> $DIR/lint-unsafe-code.rs:34:1
24+
|
25+
LL | #[export_name = "bar"] fn bar() {}
26+
| ^^^^^^^^^^^^^^^^^^^^^^
27+
|
28+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
29+
30+
error: declaration of a static with `export_name`
31+
--> $DIR/lint-unsafe-code.rs:35:1
32+
|
33+
LL | #[export_name = "BAR"] static BAR: u32 = 5;
34+
| ^^^^^^^^^^^^^^^^^^^^^^
35+
|
36+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
37+
38+
error: declaration of an `unsafe` function
39+
--> $DIR/lint-unsafe-code.rs:37:1
40+
|
41+
LL | unsafe fn baz() {}
42+
| ^^^^^^^^^^^^^^^^^^
1243

1344
error: declaration of an `unsafe` trait
14-
--> $DIR/lint-unsafe-code.rs:24:1
45+
--> $DIR/lint-unsafe-code.rs:38:1
1546
|
1647
LL | unsafe trait Foo {}
1748
| ^^^^^^^^^^^^^^^^^^^
1849

1950
error: implementation of an `unsafe` trait
20-
--> $DIR/lint-unsafe-code.rs:25:1
51+
--> $DIR/lint-unsafe-code.rs:39:1
2152
|
2253
LL | unsafe impl Foo for Bar {}
2354
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2455

2556
error: declaration of an `unsafe` method
26-
--> $DIR/lint-unsafe-code.rs:28:5
57+
--> $DIR/lint-unsafe-code.rs:42:5
2758
|
2859
LL | unsafe fn baz(&self);
2960
| ^^^^^^^^^^^^^^^^^^^^^
3061

3162
error: implementation of an `unsafe` method
32-
--> $DIR/lint-unsafe-code.rs:29:5
63+
--> $DIR/lint-unsafe-code.rs:43:5
3364
|
3465
LL | unsafe fn provided(&self) {}
3566
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3667

3768
error: implementation of an `unsafe` method
38-
--> $DIR/lint-unsafe-code.rs:30:5
69+
--> $DIR/lint-unsafe-code.rs:44:5
3970
|
4071
LL | unsafe fn provided_override(&self) {}
4172
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4273

4374
error: implementation of an `unsafe` method
44-
--> $DIR/lint-unsafe-code.rs:34:5
75+
--> $DIR/lint-unsafe-code.rs:48:5
4576
|
4677
LL | unsafe fn baz(&self) {}
4778
| ^^^^^^^^^^^^^^^^^^^^^^^
4879

4980
error: implementation of an `unsafe` method
50-
--> $DIR/lint-unsafe-code.rs:35:5
81+
--> $DIR/lint-unsafe-code.rs:49:5
5182
|
5283
LL | unsafe fn provided_override(&self) {}
5384
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5485

5586
error: implementation of an `unsafe` method
56-
--> $DIR/lint-unsafe-code.rs:54:5
87+
--> $DIR/lint-unsafe-code.rs:68:5
5788
|
5889
LL | unsafe fn provided_override(&self) {}
5990
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6091

6192
error: implementation of an `unsafe` method
62-
--> $DIR/lint-unsafe-code.rs:65:5
93+
--> $DIR/lint-unsafe-code.rs:79:5
6394
|
6495
LL | unsafe fn provided(&self) {}
6596
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6697

6798
error: implementation of an `unsafe` method
68-
--> $DIR/lint-unsafe-code.rs:71:5
99+
--> $DIR/lint-unsafe-code.rs:85:5
69100
|
70101
LL | unsafe fn provided(&self) {}
71102
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72103

73104
error: implementation of an `unsafe` method
74-
--> $DIR/lint-unsafe-code.rs:75:5
105+
--> $DIR/lint-unsafe-code.rs:89:5
75106
|
76107
LL | unsafe fn baz(&self) {}
77108
| ^^^^^^^^^^^^^^^^^^^^^^^
78109

79110
error: usage of an `unsafe` block
80-
--> $DIR/lint-unsafe-code.rs:86:5
111+
--> $DIR/lint-unsafe-code.rs:100:5
81112
|
82113
LL | unsafe {}
83114
| ^^^^^^^^^
84115

116+
error: declaration of a `no_mangle` function
117+
--> $DIR/lint-unsafe-code.rs:21:9
118+
|
119+
LL | #[no_mangle] fn foo() {}
120+
| ^^^^^^^^^^^^
121+
...
122+
LL | unsafe_in_macro!()
123+
| ------------------ in this macro invocation
124+
|
125+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
126+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
127+
128+
error: declaration of a `no_mangle` static
129+
--> $DIR/lint-unsafe-code.rs:22:9
130+
|
131+
LL | #[no_mangle] static FOO: u32 = 5;
132+
| ^^^^^^^^^^^^
133+
...
134+
LL | unsafe_in_macro!()
135+
| ------------------ in this macro invocation
136+
|
137+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
138+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
139+
140+
error: declaration of a function with `export_name`
141+
--> $DIR/lint-unsafe-code.rs:23:9
142+
|
143+
LL | #[export_name = "bar"] fn bar() {}
144+
| ^^^^^^^^^^^^^^^^^^^^^^
145+
...
146+
LL | unsafe_in_macro!()
147+
| ------------------ in this macro invocation
148+
|
149+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
150+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
151+
152+
error: declaration of a static with `export_name`
153+
--> $DIR/lint-unsafe-code.rs:25:9
154+
|
155+
LL | #[export_name = "BAR"] static BAR: u32 = 5;
156+
| ^^^^^^^^^^^^^^^^^^^^^^
157+
...
158+
LL | unsafe_in_macro!()
159+
| ------------------ in this macro invocation
160+
|
161+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
162+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
163+
85164
error: usage of an `unsafe` block
86-
--> $DIR/lint-unsafe-code.rs:19:9
165+
--> $DIR/lint-unsafe-code.rs:27:9
87166
|
88167
LL | unsafe {}
89168
| ^^^^^^^^^
@@ -93,5 +172,5 @@ LL | unsafe_in_macro!()
93172
|
94173
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
95174

96-
error: aborting due to 14 previous errors
175+
error: aborting due to 22 previous errors
97176

0 commit comments

Comments
 (0)