From 9ed3661427670346b8071ee32a6577892e8ea506 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 20 Jun 2020 13:34:22 +0200 Subject: [PATCH] Add note about why no_mangle and export_name are unsafe --- compiler/rustc_lint/src/builtin.rs | 44 +++++++++++++++++------- src/test/ui/lint/lint-unsafe-code.stderr | 11 ++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 89190072a72..4bc55b8717e 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -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`", + ); } } diff --git a/src/test/ui/lint/lint-unsafe-code.stderr b/src/test/ui/lint/lint-unsafe-code.stderr index b97c78aef2f..fa22498dc0f 100644 --- a/src/test/ui/lint/lint-unsafe-code.stderr +++ b/src/test/ui/lint/lint-unsafe-code.stderr @@ -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