From 06a0269c110975d72a7312aab593abbb66c47f27 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Thu, 14 May 2020 17:31:06 +0200 Subject: [PATCH 1/6] Add checking for no_mangle to unsafe_code lint --- compiler/rustc_lint/src/builtin.rs | 16 ++++++ src/test/ui/lint/lint-unsafe-code.rs | 10 +++- src/test/ui/lint/lint-unsafe-code.stderr | 70 ++++++++++++++++++------ 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ea624b9ed3003..ed6dab2f0a6e6 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -277,6 +277,22 @@ impl EarlyLintPass for UnsafeCode { }) } + ast::ItemKind::Fn(..) => { + if attr::contains_name(&it.attrs, sym::no_mangle) { + self.report_unsafe(cx, it.span, |lint| { + lint.build("declaration of a `no_mangle` function").emit(); + }) + } + } + + ast::ItemKind::Static(..) => { + if attr::contains_name(&it.attrs, sym::no_mangle) { + self.report_unsafe(cx, it.span, |lint| { + lint.build("declaration of a `no_mangle` static").emit(); + }) + } + } + _ => {} } } diff --git a/src/test/ui/lint/lint-unsafe-code.rs b/src/test/ui/lint/lint-unsafe-code.rs index 735f33f601f9d..79c44c57fc9e5 100644 --- a/src/test/ui/lint/lint-unsafe-code.rs +++ b/src/test/ui/lint/lint-unsafe-code.rs @@ -12,14 +12,20 @@ mod allowed_unsafe { unsafe fn also_allowed() {} unsafe trait AllowedUnsafe { } unsafe impl AllowedUnsafe for super::Bar {} + #[no_mangle] fn allowed2() {} } macro_rules! unsafe_in_macro { - () => { + () => {{ + #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function + #[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static unsafe {} //~ ERROR: usage of an `unsafe` block - } + }} } +#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function +#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static + unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait diff --git a/src/test/ui/lint/lint-unsafe-code.stderr b/src/test/ui/lint/lint-unsafe-code.stderr index 0b2b9fab3925e..a674bdf0748d8 100644 --- a/src/test/ui/lint/lint-unsafe-code.stderr +++ b/src/test/ui/lint/lint-unsafe-code.stderr @@ -1,8 +1,8 @@ -error: declaration of an `unsafe` function - --> $DIR/lint-unsafe-code.rs:23:1 +error: declaration of a `no_mangle` function + --> $DIR/lint-unsafe-code.rs:26:14 | -LL | unsafe fn baz() {} - | ^^^^^^^^^^^^^^^^^^ +LL | #[no_mangle] fn foo() {} + | ^^^^^^^^^^^ | note: the lint level is defined here --> $DIR/lint-unsafe-code.rs:3:9 @@ -10,80 +10,114 @@ note: the lint level is defined here LL | #![deny(unsafe_code)] | ^^^^^^^^^^^ +error: declaration of a `no_mangle` static + --> $DIR/lint-unsafe-code.rs:27:14 + | +LL | #[no_mangle] static FOO: u32 = 5; + | ^^^^^^^^^^^^^^^^^^^^ + +error: declaration of an `unsafe` function + --> $DIR/lint-unsafe-code.rs:29:1 + | +LL | unsafe fn baz() {} + | ^^^^^^^^^^^^^^^^^^ + error: declaration of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:24:1 + --> $DIR/lint-unsafe-code.rs:30:1 | LL | unsafe trait Foo {} | ^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:25:1 + --> $DIR/lint-unsafe-code.rs:31:1 | LL | unsafe impl Foo for Bar {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: declaration of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:28:5 + --> $DIR/lint-unsafe-code.rs:34:5 | LL | unsafe fn baz(&self); | ^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:29:5 + --> $DIR/lint-unsafe-code.rs:35:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:30:5 + --> $DIR/lint-unsafe-code.rs:36:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:34:5 + --> $DIR/lint-unsafe-code.rs:40:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:35:5 + --> $DIR/lint-unsafe-code.rs:41:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:54:5 + --> $DIR/lint-unsafe-code.rs:60:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:65:5 + --> $DIR/lint-unsafe-code.rs:71:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:71:5 + --> $DIR/lint-unsafe-code.rs:77:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:75:5 + --> $DIR/lint-unsafe-code.rs:81:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-unsafe-code.rs:86:5 + --> $DIR/lint-unsafe-code.rs:92:5 | LL | unsafe {} | ^^^^^^^^^ +error: declaration of a `no_mangle` function + --> $DIR/lint-unsafe-code.rs:20:22 + | +LL | #[no_mangle] fn foo() {} + | ^^^^^^^^^^^ +... +LL | unsafe_in_macro!() + | ------------------ in this macro invocation + | + = 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 + --> $DIR/lint-unsafe-code.rs:21:22 + | +LL | #[no_mangle] static FOO: u32 = 5; + | ^^^^^^^^^^^^^^^^^^^^ +... +LL | unsafe_in_macro!() + | ------------------ in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + error: usage of an `unsafe` block - --> $DIR/lint-unsafe-code.rs:19:9 + --> $DIR/lint-unsafe-code.rs:22:9 | LL | unsafe {} | ^^^^^^^^^ @@ -93,5 +127,5 @@ LL | unsafe_in_macro!() | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 14 previous errors +error: aborting due to 18 previous errors From 66b2f9acfcb6203090ca2321ce37bb7ae6c07210 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Fri, 15 May 2020 17:36:19 +0200 Subject: [PATCH 2/6] Add checking for export_name to unsafe_code lint --- compiler/rustc_lint/src/builtin.rs | 10 ++++ src/test/ui/lint/lint-unsafe-code.rs | 8 +++ src/test/ui/lint/lint-unsafe-code.stderr | 72 +++++++++++++++++------- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ed6dab2f0a6e6..784468aac2a49 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -283,6 +283,11 @@ impl EarlyLintPass for UnsafeCode { lint.build("declaration of a `no_mangle` function").emit(); }) } + if attr::contains_name(&it.attrs, sym::export_name) { + self.report_unsafe(cx, it.span, |lint| { + lint.build("declaration of a function with `export_name`").emit(); + }) + } } ast::ItemKind::Static(..) => { @@ -291,6 +296,11 @@ impl EarlyLintPass for UnsafeCode { lint.build("declaration of a `no_mangle` static").emit(); }) } + if attr::contains_name(&it.attrs, sym::export_name) { + self.report_unsafe(cx, it.span, |lint| { + lint.build("declaration of a static with `export_name`").emit(); + }) + } } _ => {} diff --git a/src/test/ui/lint/lint-unsafe-code.rs b/src/test/ui/lint/lint-unsafe-code.rs index 79c44c57fc9e5..4ac02b51f62fe 100644 --- a/src/test/ui/lint/lint-unsafe-code.rs +++ b/src/test/ui/lint/lint-unsafe-code.rs @@ -13,12 +13,17 @@ mod allowed_unsafe { unsafe trait AllowedUnsafe { } unsafe impl AllowedUnsafe for super::Bar {} #[no_mangle] fn allowed2() {} + #[export_name = "foo"] fn allowed3() {} } macro_rules! unsafe_in_macro { () => {{ #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function #[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static + #[export_name = "bar"] fn bar() {} + //~^ ERROR: declaration of a function with `export_name` + #[export_name = "BAR"] static BAR: u32 = 5; + //~^ ERROR: declaration of a static with `export_name` unsafe {} //~ ERROR: usage of an `unsafe` block }} } @@ -26,6 +31,9 @@ macro_rules! unsafe_in_macro { #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function #[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static +#[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name` +#[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name` + unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait diff --git a/src/test/ui/lint/lint-unsafe-code.stderr b/src/test/ui/lint/lint-unsafe-code.stderr index a674bdf0748d8..aadd02277ef81 100644 --- a/src/test/ui/lint/lint-unsafe-code.stderr +++ b/src/test/ui/lint/lint-unsafe-code.stderr @@ -1,5 +1,5 @@ error: declaration of a `no_mangle` function - --> $DIR/lint-unsafe-code.rs:26:14 + --> $DIR/lint-unsafe-code.rs:31:14 | LL | #[no_mangle] fn foo() {} | ^^^^^^^^^^^ @@ -11,91 +11,103 @@ LL | #![deny(unsafe_code)] | ^^^^^^^^^^^ error: declaration of a `no_mangle` static - --> $DIR/lint-unsafe-code.rs:27:14 + --> $DIR/lint-unsafe-code.rs:32:14 | LL | #[no_mangle] static FOO: u32 = 5; | ^^^^^^^^^^^^^^^^^^^^ +error: declaration of a function with `export_name` + --> $DIR/lint-unsafe-code.rs:34:24 + | +LL | #[export_name = "bar"] fn bar() {} + | ^^^^^^^^^^^ + +error: declaration of a static with `export_name` + --> $DIR/lint-unsafe-code.rs:35:24 + | +LL | #[export_name = "BAR"] static BAR: u32 = 5; + | ^^^^^^^^^^^^^^^^^^^^ + error: declaration of an `unsafe` function - --> $DIR/lint-unsafe-code.rs:29:1 + --> $DIR/lint-unsafe-code.rs:37:1 | LL | unsafe fn baz() {} | ^^^^^^^^^^^^^^^^^^ error: declaration of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:30:1 + --> $DIR/lint-unsafe-code.rs:38:1 | LL | unsafe trait Foo {} | ^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:31:1 + --> $DIR/lint-unsafe-code.rs:39:1 | LL | unsafe impl Foo for Bar {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: declaration of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:34:5 + --> $DIR/lint-unsafe-code.rs:42:5 | LL | unsafe fn baz(&self); | ^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:35:5 + --> $DIR/lint-unsafe-code.rs:43:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:36:5 + --> $DIR/lint-unsafe-code.rs:44:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:40:5 + --> $DIR/lint-unsafe-code.rs:48:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:41:5 + --> $DIR/lint-unsafe-code.rs:49:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:60:5 + --> $DIR/lint-unsafe-code.rs:68:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:71:5 + --> $DIR/lint-unsafe-code.rs:79:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:77:5 + --> $DIR/lint-unsafe-code.rs:85:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:81:5 + --> $DIR/lint-unsafe-code.rs:89:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-unsafe-code.rs:92:5 + --> $DIR/lint-unsafe-code.rs:100:5 | LL | unsafe {} | ^^^^^^^^^ error: declaration of a `no_mangle` function - --> $DIR/lint-unsafe-code.rs:20:22 + --> $DIR/lint-unsafe-code.rs:21:22 | LL | #[no_mangle] fn foo() {} | ^^^^^^^^^^^ @@ -106,7 +118,7 @@ LL | unsafe_in_macro!() = 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 - --> $DIR/lint-unsafe-code.rs:21:22 + --> $DIR/lint-unsafe-code.rs:22:22 | LL | #[no_mangle] static FOO: u32 = 5; | ^^^^^^^^^^^^^^^^^^^^ @@ -116,8 +128,30 @@ LL | unsafe_in_macro!() | = 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` + --> $DIR/lint-unsafe-code.rs:23:32 + | +LL | #[export_name = "bar"] fn bar() {} + | ^^^^^^^^^^^ +... +LL | unsafe_in_macro!() + | ------------------ in this macro invocation + | + = 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` + --> $DIR/lint-unsafe-code.rs:25:32 + | +LL | #[export_name = "BAR"] static BAR: u32 = 5; + | ^^^^^^^^^^^^^^^^^^^^ +... +LL | unsafe_in_macro!() + | ------------------ in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + error: usage of an `unsafe` block - --> $DIR/lint-unsafe-code.rs:22:9 + --> $DIR/lint-unsafe-code.rs:27:9 | LL | unsafe {} | ^^^^^^^^^ @@ -127,5 +161,5 @@ LL | unsafe_in_macro!() | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 18 previous errors +error: aborting due to 22 previous errors From 79b0ab5195f0b7f9e05881e775219eea9cc410f6 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 20 Jun 2020 13:31:24 +0200 Subject: [PATCH 3/6] Scope no_mangle and export_name warnings to the declarations name --- compiler/rustc_lint/src/builtin.rs | 8 +++--- src/test/ui/lint/lint-unsafe-code.stderr | 32 ++++++++++++------------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 784468aac2a49..89190072a722a 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -279,12 +279,12 @@ impl EarlyLintPass for UnsafeCode { ast::ItemKind::Fn(..) => { if attr::contains_name(&it.attrs, sym::no_mangle) { - self.report_unsafe(cx, it.span, |lint| { + self.report_unsafe(cx, it.ident.span, |lint| { lint.build("declaration of a `no_mangle` function").emit(); }) } if attr::contains_name(&it.attrs, sym::export_name) { - self.report_unsafe(cx, it.span, |lint| { + self.report_unsafe(cx, it.ident.span, |lint| { lint.build("declaration of a function with `export_name`").emit(); }) } @@ -292,12 +292,12 @@ impl EarlyLintPass for UnsafeCode { ast::ItemKind::Static(..) => { if attr::contains_name(&it.attrs, sym::no_mangle) { - self.report_unsafe(cx, it.span, |lint| { + self.report_unsafe(cx, it.ident.span, |lint| { lint.build("declaration of a `no_mangle` static").emit(); }) } if attr::contains_name(&it.attrs, sym::export_name) { - self.report_unsafe(cx, it.span, |lint| { + self.report_unsafe(cx, it.ident.span, |lint| { lint.build("declaration of a static with `export_name`").emit(); }) } diff --git a/src/test/ui/lint/lint-unsafe-code.stderr b/src/test/ui/lint/lint-unsafe-code.stderr index aadd02277ef81..b97c78aef2fd0 100644 --- a/src/test/ui/lint/lint-unsafe-code.stderr +++ b/src/test/ui/lint/lint-unsafe-code.stderr @@ -1,8 +1,8 @@ error: declaration of a `no_mangle` function - --> $DIR/lint-unsafe-code.rs:31:14 + --> $DIR/lint-unsafe-code.rs:31:17 | LL | #[no_mangle] fn foo() {} - | ^^^^^^^^^^^ + | ^^^ | note: the lint level is defined here --> $DIR/lint-unsafe-code.rs:3:9 @@ -11,22 +11,22 @@ LL | #![deny(unsafe_code)] | ^^^^^^^^^^^ error: declaration of a `no_mangle` static - --> $DIR/lint-unsafe-code.rs:32:14 + --> $DIR/lint-unsafe-code.rs:32:21 | LL | #[no_mangle] static FOO: u32 = 5; - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^ error: declaration of a function with `export_name` - --> $DIR/lint-unsafe-code.rs:34:24 + --> $DIR/lint-unsafe-code.rs:34:27 | LL | #[export_name = "bar"] fn bar() {} - | ^^^^^^^^^^^ + | ^^^ error: declaration of a static with `export_name` - --> $DIR/lint-unsafe-code.rs:35:24 + --> $DIR/lint-unsafe-code.rs:35:31 | LL | #[export_name = "BAR"] static BAR: u32 = 5; - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^ error: declaration of an `unsafe` function --> $DIR/lint-unsafe-code.rs:37:1 @@ -107,10 +107,10 @@ LL | unsafe {} | ^^^^^^^^^ error: declaration of a `no_mangle` function - --> $DIR/lint-unsafe-code.rs:21:22 + --> $DIR/lint-unsafe-code.rs:21:25 | LL | #[no_mangle] fn foo() {} - | ^^^^^^^^^^^ + | ^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation @@ -118,10 +118,10 @@ LL | unsafe_in_macro!() = 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 - --> $DIR/lint-unsafe-code.rs:22:22 + --> $DIR/lint-unsafe-code.rs:22:29 | LL | #[no_mangle] static FOO: u32 = 5; - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation @@ -129,10 +129,10 @@ LL | unsafe_in_macro!() = 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` - --> $DIR/lint-unsafe-code.rs:23:32 + --> $DIR/lint-unsafe-code.rs:23:35 | LL | #[export_name = "bar"] fn bar() {} - | ^^^^^^^^^^^ + | ^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation @@ -140,10 +140,10 @@ LL | unsafe_in_macro!() = 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` - --> $DIR/lint-unsafe-code.rs:25:32 + --> $DIR/lint-unsafe-code.rs:25:39 | LL | #[export_name = "BAR"] static BAR: u32 = 5; - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation From 9ed3661427670346b8071ee32a6577892e8ea506 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 20 Jun 2020 13:34:22 +0200 Subject: [PATCH 4/6] 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 89190072a722a..4bc55b8717e01 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 b97c78aef2fd0..fa22498dc0f37 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 From 7636de33cf7935835d3be4fc504b1acc218dec6c Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Tue, 4 Aug 2020 13:02:17 +0200 Subject: [PATCH 5/6] Point to no_mangle/export_name attribute when linting --- compiler/rustc_lint/src/builtin.rs | 16 ++++++------ src/test/ui/lint/lint-unsafe-code.stderr | 32 ++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 4bc55b8717e01..0a477fa5f2b72 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -290,34 +290,34 @@ impl EarlyLintPass for UnsafeCode { } ast::ItemKind::Fn(..) => { - if attr::contains_name(&it.attrs, sym::no_mangle) { + if let Some(attr) = attr::find_by_name(&it.attrs, sym::no_mangle) { self.report_overriden_symbol_name( cx, - it.ident.span, + attr.span, "declaration of a `no_mangle` function", ); } - if attr::contains_name(&it.attrs, sym::export_name) { + if let Some(attr) = attr::find_by_name(&it.attrs, sym::export_name) { self.report_overriden_symbol_name( cx, - it.ident.span, + attr.span, "declaration of a function with `export_name`", ); } } ast::ItemKind::Static(..) => { - if attr::contains_name(&it.attrs, sym::no_mangle) { + if let Some(attr) = attr::find_by_name(&it.attrs, sym::no_mangle) { self.report_overriden_symbol_name( cx, - it.ident.span, + attr.span, "declaration of a `no_mangle` static", ); } - if attr::contains_name(&it.attrs, sym::export_name) { + if let Some(attr) = attr::find_by_name(&it.attrs, sym::export_name) { self.report_overriden_symbol_name( cx, - it.ident.span, + attr.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 fa22498dc0f37..a8ef047e517b4 100644 --- a/src/test/ui/lint/lint-unsafe-code.stderr +++ b/src/test/ui/lint/lint-unsafe-code.stderr @@ -1,8 +1,8 @@ error: declaration of a `no_mangle` function - --> $DIR/lint-unsafe-code.rs:31:17 + --> $DIR/lint-unsafe-code.rs:31:1 | LL | #[no_mangle] fn foo() {} - | ^^^ + | ^^^^^^^^^^^^ | note: the lint level is defined here --> $DIR/lint-unsafe-code.rs:3:9 @@ -12,26 +12,26 @@ 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 + --> $DIR/lint-unsafe-code.rs:32:1 | 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 + --> $DIR/lint-unsafe-code.rs:34:1 | 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 + --> $DIR/lint-unsafe-code.rs:35:1 | 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 @@ -114,10 +114,10 @@ LL | unsafe {} | ^^^^^^^^^ error: declaration of a `no_mangle` function - --> $DIR/lint-unsafe-code.rs:21:25 + --> $DIR/lint-unsafe-code.rs:21:9 | LL | #[no_mangle] fn foo() {} - | ^^^ + | ^^^^^^^^^^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation @@ -126,10 +126,10 @@ LL | unsafe_in_macro!() = 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 - --> $DIR/lint-unsafe-code.rs:22:29 + --> $DIR/lint-unsafe-code.rs:22:9 | LL | #[no_mangle] static FOO: u32 = 5; - | ^^^ + | ^^^^^^^^^^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation @@ -138,10 +138,10 @@ LL | unsafe_in_macro!() = 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` - --> $DIR/lint-unsafe-code.rs:23:35 + --> $DIR/lint-unsafe-code.rs:23:9 | LL | #[export_name = "bar"] fn bar() {} - | ^^^ + | ^^^^^^^^^^^^^^^^^^^^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation @@ -150,10 +150,10 @@ LL | unsafe_in_macro!() = 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` - --> $DIR/lint-unsafe-code.rs:25:39 + --> $DIR/lint-unsafe-code.rs:25:9 | LL | #[export_name = "BAR"] static BAR: u32 = 5; - | ^^^ + | ^^^^^^^^^^^^^^^^^^^^^^ ... LL | unsafe_in_macro!() | ------------------ in this macro invocation From fc8a3ad66c7026e782ee54bd3849cc860983b69a Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sun, 30 Aug 2020 21:51:30 +0200 Subject: [PATCH 6/6] Update for moved function from #74932 --- compiler/rustc_lint/src/builtin.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 0a477fa5f2b72..d768775e0cf39 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -290,14 +290,14 @@ impl EarlyLintPass for UnsafeCode { } ast::ItemKind::Fn(..) => { - if let Some(attr) = attr::find_by_name(&it.attrs, sym::no_mangle) { + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) { self.report_overriden_symbol_name( cx, attr.span, "declaration of a `no_mangle` function", ); } - if let Some(attr) = attr::find_by_name(&it.attrs, sym::export_name) { + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { self.report_overriden_symbol_name( cx, attr.span, @@ -307,14 +307,14 @@ impl EarlyLintPass for UnsafeCode { } ast::ItemKind::Static(..) => { - if let Some(attr) = attr::find_by_name(&it.attrs, sym::no_mangle) { + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) { self.report_overriden_symbol_name( cx, attr.span, "declaration of a `no_mangle` static", ); } - if let Some(attr) = attr::find_by_name(&it.attrs, sym::export_name) { + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { self.report_overriden_symbol_name( cx, attr.span,