Skip to content

Commit

Permalink
Add note about why no_mangle and export_name are unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Aug 30, 2020
1 parent 79b0ab5 commit 9ed3661
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
44 changes: 32 additions & 12 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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`",
);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/lint/lint-unsafe-code.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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
Expand Down

0 comments on commit 9ed3661

Please sign in to comment.