diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 6be78c52f99..92cd8c2b611 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -338,6 +338,17 @@ impl UnsafeCode { .emit(); }) } + + fn report_overridden_symbol_section(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) { + self.report_unsafe(cx, span, |lint| { + lint.build(msg) + .note( + "the program's behavior with overridden link sections on items is unpredictable \ + and Rust cannot provide guarantees when you manually override them", + ) + .emit(); + }) + } } impl EarlyLintPass for UnsafeCode { @@ -385,6 +396,7 @@ impl EarlyLintPass for UnsafeCode { "declaration of a `no_mangle` function", ); } + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { self.report_overridden_symbol_name( cx, @@ -392,6 +404,14 @@ impl EarlyLintPass for UnsafeCode { "declaration of a function with `export_name`", ); } + + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) { + self.report_overridden_symbol_section( + cx, + attr.span, + "declaration of a function with `link_section`", + ); + } } ast::ItemKind::Static(..) => { @@ -402,6 +422,7 @@ impl EarlyLintPass for UnsafeCode { "declaration of a `no_mangle` static", ); } + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { self.report_overridden_symbol_name( cx, @@ -409,6 +430,14 @@ impl EarlyLintPass for UnsafeCode { "declaration of a static with `export_name`", ); } + + if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) { + self.report_overridden_symbol_section( + cx, + attr.span, + "declaration of a static with `link_section`", + ); + } } _ => {} diff --git a/src/test/ui/lint/lint-unsafe-code.rs b/src/test/ui/lint/lint-unsafe-code.rs index c30f21bbf8f..b72e4c3a9e7 100644 --- a/src/test/ui/lint/lint-unsafe-code.rs +++ b/src/test/ui/lint/lint-unsafe-code.rs @@ -48,6 +48,9 @@ impl AssocFnTrait for AssocFnFoo { #[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` +#[link_section = ".example_section"] fn uwu() {} //~ ERROR: declaration of a function with `link_section` +#[link_section = ".example_section"] static UWU: u32 = 5; //~ ERROR: declaration of a static with `link_section` + struct AssocFnBar; impl AssocFnBar { diff --git a/src/test/ui/lint/lint-unsafe-code.stderr b/src/test/ui/lint/lint-unsafe-code.stderr index b6895ac8da8..8dde05fc4de 100644 --- a/src/test/ui/lint/lint-unsafe-code.stderr +++ b/src/test/ui/lint/lint-unsafe-code.stderr @@ -51,8 +51,24 @@ 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 a function with `link_section` + --> $DIR/lint-unsafe-code.rs:51:1 + | +LL | #[link_section = ".example_section"] fn uwu() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: the program's behavior with overridden link sections on items is unpredictable and Rust cannot provide guarantees when you manually override them + +error: declaration of a static with `link_section` + --> $DIR/lint-unsafe-code.rs:52:1 + | +LL | #[link_section = ".example_section"] static UWU: u32 = 5; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: the program's behavior with overridden link sections on items is unpredictable and Rust cannot provide guarantees when you manually override them + error: declaration of a method with `export_name` - --> $DIR/lint-unsafe-code.rs:54:5 + --> $DIR/lint-unsafe-code.rs:57:5 | LL | #[export_name = "bar"] fn bar() {} | ^^^^^^^^^^^^^^^^^^^^^^ @@ -60,7 +76,7 @@ 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 method with `export_name` - --> $DIR/lint-unsafe-code.rs:58:5 + --> $DIR/lint-unsafe-code.rs:61:5 | LL | #[export_name = "bar"] fn foo() {} | ^^^^^^^^^^^^^^^^^^^^^^ @@ -68,79 +84,79 @@ LL | #[export_name = "bar"] fn foo() {} = 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:61:1 + --> $DIR/lint-unsafe-code.rs:64:1 | LL | unsafe fn baz() {} | ^^^^^^^^^^^^^^^^^^ error: declaration of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:62:1 + --> $DIR/lint-unsafe-code.rs:65:1 | LL | unsafe trait Foo {} | ^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:63:1 + --> $DIR/lint-unsafe-code.rs:66:1 | LL | unsafe impl Foo for Bar {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: declaration of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:66:5 + --> $DIR/lint-unsafe-code.rs:69:5 | LL | unsafe fn baz(&self); | ^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:67:5 + --> $DIR/lint-unsafe-code.rs:70:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:68:5 + --> $DIR/lint-unsafe-code.rs:71:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:72:5 + --> $DIR/lint-unsafe-code.rs:75:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:73:5 + --> $DIR/lint-unsafe-code.rs:76:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:92:5 + --> $DIR/lint-unsafe-code.rs:95:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:103:5 + --> $DIR/lint-unsafe-code.rs:106:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:109:5 + --> $DIR/lint-unsafe-code.rs:112:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:113:5 + --> $DIR/lint-unsafe-code.rs:116:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-unsafe-code.rs:124:5 + --> $DIR/lint-unsafe-code.rs:127:5 | LL | unsafe {} | ^^^^^^^^^ @@ -204,5 +220,5 @@ LL | unsafe_in_macro!() | = note: this error originates in the macro `unsafe_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 26 previous errors +error: aborting due to 28 previous errors