Skip to content

Commit 9ed3661

Browse files
committed
Add note about why no_mangle and export_name are unsafe
1 parent 79b0ab5 commit 9ed3661

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ impl UnsafeCode {
236236

237237
cx.struct_span_lint(UNSAFE_CODE, span, decorate);
238238
}
239+
240+
fn report_overriden_symbol_name(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) {
241+
self.report_unsafe(cx, span, |lint| {
242+
lint.build(msg)
243+
.note(
244+
"the linker's behavior with multiple libraries exporting duplicate symbol \
245+
names is undefined and Rust cannot provide guarantees when you manually \
246+
override them",
247+
)
248+
.emit();
249+
})
250+
}
239251
}
240252

241253
impl EarlyLintPass for UnsafeCode {
@@ -279,27 +291,35 @@ impl EarlyLintPass for UnsafeCode {
279291

280292
ast::ItemKind::Fn(..) => {
281293
if attr::contains_name(&it.attrs, sym::no_mangle) {
282-
self.report_unsafe(cx, it.ident.span, |lint| {
283-
lint.build("declaration of a `no_mangle` function").emit();
284-
})
294+
self.report_overriden_symbol_name(
295+
cx,
296+
it.ident.span,
297+
"declaration of a `no_mangle` function",
298+
);
285299
}
286300
if attr::contains_name(&it.attrs, sym::export_name) {
287-
self.report_unsafe(cx, it.ident.span, |lint| {
288-
lint.build("declaration of a function with `export_name`").emit();
289-
})
301+
self.report_overriden_symbol_name(
302+
cx,
303+
it.ident.span,
304+
"declaration of a function with `export_name`",
305+
);
290306
}
291307
}
292308

293309
ast::ItemKind::Static(..) => {
294310
if attr::contains_name(&it.attrs, sym::no_mangle) {
295-
self.report_unsafe(cx, it.ident.span, |lint| {
296-
lint.build("declaration of a `no_mangle` static").emit();
297-
})
311+
self.report_overriden_symbol_name(
312+
cx,
313+
it.ident.span,
314+
"declaration of a `no_mangle` static",
315+
);
298316
}
299317
if attr::contains_name(&it.attrs, sym::export_name) {
300-
self.report_unsafe(cx, it.ident.span, |lint| {
301-
lint.build("declaration of a static with `export_name`").emit();
302-
})
318+
self.report_overriden_symbol_name(
319+
cx,
320+
it.ident.span,
321+
"declaration of a static with `export_name`",
322+
);
303323
}
304324
}
305325

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,31 @@ note: the lint level is defined here
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
1213

1314
error: declaration of a `no_mangle` static
1415
--> $DIR/lint-unsafe-code.rs:32:21
1516
|
1617
LL | #[no_mangle] static FOO: u32 = 5;
1718
| ^^^
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
1821

1922
error: declaration of a function with `export_name`
2023
--> $DIR/lint-unsafe-code.rs:34:27
2124
|
2225
LL | #[export_name = "bar"] fn bar() {}
2326
| ^^^
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
2429

2530
error: declaration of a static with `export_name`
2631
--> $DIR/lint-unsafe-code.rs:35:31
2732
|
2833
LL | #[export_name = "BAR"] static BAR: u32 = 5;
2934
| ^^^
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
3037

3138
error: declaration of an `unsafe` function
3239
--> $DIR/lint-unsafe-code.rs:37:1
@@ -115,6 +122,7 @@ LL | #[no_mangle] fn foo() {}
115122
LL | unsafe_in_macro!()
116123
| ------------------ in this macro invocation
117124
|
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
118126
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
119127

120128
error: declaration of a `no_mangle` static
@@ -126,6 +134,7 @@ LL | #[no_mangle] static FOO: u32 = 5;
126134
LL | unsafe_in_macro!()
127135
| ------------------ in this macro invocation
128136
|
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
129138
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
130139

131140
error: declaration of a function with `export_name`
@@ -137,6 +146,7 @@ LL | #[export_name = "bar"] fn bar() {}
137146
LL | unsafe_in_macro!()
138147
| ------------------ in this macro invocation
139148
|
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
140150
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
141151

142152
error: declaration of a static with `export_name`
@@ -148,6 +158,7 @@ LL | #[export_name = "BAR"] static BAR: u32 = 5;
148158
LL | unsafe_in_macro!()
149159
| ------------------ in this macro invocation
150160
|
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
151162
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
152163

153164
error: usage of an `unsafe` block

0 commit comments

Comments
 (0)