Add note about why no_mangle and export_name are unsafe

This commit is contained in:
Wim Looman 2020-06-20 13:34:22 +02:00
parent 79b0ab5195
commit 9ed3661427
2 changed files with 43 additions and 12 deletions

View File

@ -236,6 +236,18 @@ impl UnsafeCode {
cx.struct_span_lint(UNSAFE_CODE, span, decorate);
}
fn report_overriden_symbol_name(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) {
self.report_unsafe(cx, span, |lint| {
lint.build(msg)
.note(
"the linker's behavior with multiple libraries exporting duplicate symbol \
names is undefined and Rust cannot provide guarantees when you manually \
override them",
)
.emit();
})
}
}
impl EarlyLintPass for UnsafeCode {
@ -279,27 +291,35 @@ impl EarlyLintPass for UnsafeCode {
ast::ItemKind::Fn(..) => {
if attr::contains_name(&it.attrs, sym::no_mangle) {
self.report_unsafe(cx, it.ident.span, |lint| {
lint.build("declaration of a `no_mangle` function").emit();
})
self.report_overriden_symbol_name(
cx,
it.ident.span,
"declaration of a `no_mangle` function",
);
}
if attr::contains_name(&it.attrs, sym::export_name) {
self.report_unsafe(cx, it.ident.span, |lint| {
lint.build("declaration of a function with `export_name`").emit();
})
self.report_overriden_symbol_name(
cx,
it.ident.span,
"declaration of a function with `export_name`",
);
}
}
ast::ItemKind::Static(..) => {
if attr::contains_name(&it.attrs, sym::no_mangle) {
self.report_unsafe(cx, it.ident.span, |lint| {
lint.build("declaration of a `no_mangle` static").emit();
})
self.report_overriden_symbol_name(
cx,
it.ident.span,
"declaration of a `no_mangle` static",
);
}
if attr::contains_name(&it.attrs, sym::export_name) {
self.report_unsafe(cx, it.ident.span, |lint| {
lint.build("declaration of a static with `export_name`").emit();
})
self.report_overriden_symbol_name(
cx,
it.ident.span,
"declaration of a static with `export_name`",
);
}
}

View File

@ -9,24 +9,31 @@ note: the lint level is defined here
|
LL | #![deny(unsafe_code)]
| ^^^^^^^^^^^
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
error: declaration of a `no_mangle` static
--> $DIR/lint-unsafe-code.rs:32:21
|
LL | #[no_mangle] static FOO: u32 = 5;
| ^^^
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
error: declaration of a function with `export_name`
--> $DIR/lint-unsafe-code.rs:34:27
|
LL | #[export_name = "bar"] fn bar() {}
| ^^^
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
error: declaration of a static with `export_name`
--> $DIR/lint-unsafe-code.rs:35:31
|
LL | #[export_name = "BAR"] static BAR: u32 = 5;
| ^^^
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
error: declaration of an `unsafe` function
--> $DIR/lint-unsafe-code.rs:37:1
@ -115,6 +122,7 @@ LL | #[no_mangle] fn foo() {}
LL | unsafe_in_macro!()
| ------------------ in this macro invocation
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: declaration of a `no_mangle` static
@ -126,6 +134,7 @@ LL | #[no_mangle] static FOO: u32 = 5;
LL | unsafe_in_macro!()
| ------------------ in this macro invocation
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: declaration of a function with `export_name`
@ -137,6 +146,7 @@ LL | #[export_name = "bar"] fn bar() {}
LL | unsafe_in_macro!()
| ------------------ in this macro invocation
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: declaration of a static with `export_name`
@ -148,6 +158,7 @@ LL | #[export_name = "BAR"] static BAR: u32 = 5;
LL | unsafe_in_macro!()
| ------------------ in this macro invocation
|
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: usage of an `unsafe` block