From 0d1b832667e4e9cb0d44eefb5218a0545b42483b Mon Sep 17 00:00:00 2001 From: yukang Date: Wed, 3 Aug 2022 11:30:27 +0800 Subject: [PATCH 1/3] check link ordinal make sure target is foreign function --- .../locales/en-US/passes.ftl | 3 +++ compiler/rustc_passes/src/check_attr.rs | 13 +++++++++++ compiler/rustc_passes/src/errors.rs | 7 ++++++ .../link-ordinal-not-foreign-fn.rs | 18 +++++++++++++++ .../link-ordinal-not-foreign-fn.stderr | 23 +++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs create mode 100644 src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr diff --git a/compiler/rustc_error_messages/locales/en-US/passes.ftl b/compiler/rustc_error_messages/locales/en-US/passes.ftl index b17eb9c2d260e..2802a0d2c6439 100644 --- a/compiler/rustc_error_messages/locales/en-US/passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/passes.ftl @@ -262,3 +262,6 @@ passes-rustc-lint-opt-ty = `#[rustc_lint_opt_ty]` should be applied to a struct passes-rustc-lint-opt-deny-field-access = `#[rustc_lint_opt_deny_field_access]` should be applied to a field .label = not a field + +passes-link-ordinal = attribute should be applied to a foreign function + .label = not a foreign function \ No newline at end of file diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index fde12b9eee6b9..bbf6c72ef3650 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -146,6 +146,9 @@ impl CheckAttrVisitor<'_> { | sym::stable | sym::rustc_allowed_through_unstable_modules | sym::rustc_promotable => self.check_stability_promotable(&attr, span, target), + sym::link_ordinal => { + self.check_link_ordinal(&attr, span, target) + }, _ => true, }; is_valid &= attr_is_valid; @@ -1861,6 +1864,16 @@ impl CheckAttrVisitor<'_> { } } + fn check_link_ordinal(&self, attr: &Attribute, _span: Span, target: Target) -> bool { + match target { + Target::ForeignFn => true, + _ => { + self.tcx.sess.emit_err(errors::LinkOrdinal { attr_span: attr.span }); + false + } + } + } + fn check_deprecated(&self, hir_id: HirId, attr: &Attribute, _span: Span, target: Target) { match target { Target::Closure | Target::Expression | Target::Statement | Target::Arm => { diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 5feb0e2956b74..6cb53ab32848b 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -551,6 +551,13 @@ pub struct ConstTrait { pub attr_span: Span, } +#[derive(SessionDiagnostic)] +#[error(passes::link_ordinal)] +pub struct LinkOrdinal { + #[primary_span] + pub attr_span: Span +} + #[derive(SessionDiagnostic)] #[error(passes::stability_promotable)] pub struct StabilityPromotable { diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs new file mode 100644 index 0000000000000..c86756be4f640 --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs @@ -0,0 +1,18 @@ +#![feature(raw_dylib)] +//~^ WARN the feature `raw_dylib` is incomplete + +#[link_ordinal(123)] +//~^ ERROR attribute should be applied to a foreign function +struct Foo {} + +#[link_ordinal(123)] +//~^ ERROR attribute should be applied to a foreign function +fn test() {} + +#[link(name = "exporter", kind = "raw-dylib")] +extern { + #[link_ordinal(13)] + fn imported_function(); +} + +fn main() {} diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr new file mode 100644 index 0000000000000..c3c399d575981 --- /dev/null +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr @@ -0,0 +1,23 @@ +warning: the feature `raw_dylib` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/link-ordinal-not-foreign-fn.rs:1:12 + | +LL | #![feature(raw_dylib)] + | ^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #58713 for more information + +error: attribute should be applied to a foreign function + --> $DIR/link-ordinal-not-foreign-fn.rs:4:1 + | +LL | #[link_ordinal(123)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: attribute should be applied to a foreign function + --> $DIR/link-ordinal-not-foreign-fn.rs:8:1 + | +LL | #[link_ordinal(123)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors; 1 warning emitted + From 4a5e83c939706e208d09eda477f3e4785ed6de02 Mon Sep 17 00:00:00 2001 From: yukang Date: Wed, 3 Aug 2022 12:19:21 +0800 Subject: [PATCH 2/3] fix tidy --- compiler/rustc_passes/src/check_attr.rs | 4 +--- compiler/rustc_passes/src/errors.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index bbf6c72ef3650..9fee28a403578 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -146,9 +146,7 @@ impl CheckAttrVisitor<'_> { | sym::stable | sym::rustc_allowed_through_unstable_modules | sym::rustc_promotable => self.check_stability_promotable(&attr, span, target), - sym::link_ordinal => { - self.check_link_ordinal(&attr, span, target) - }, + sym::link_ordinal => self.check_link_ordinal(&attr, span, target), _ => true, }; is_valid &= attr_is_valid; diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 6cb53ab32848b..cd465380867cf 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -555,7 +555,7 @@ pub struct ConstTrait { #[error(passes::link_ordinal)] pub struct LinkOrdinal { #[primary_span] - pub attr_span: Span + pub attr_span: Span, } #[derive(SessionDiagnostic)] From e614bbcd301eb4c3dbb65a4aa3147dc0b6ff197c Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 4 Aug 2022 09:28:59 +0800 Subject: [PATCH 3/3] link_ordinal is available for foreign static --- .../rustc_error_messages/locales/en-US/passes.ftl | 4 ++-- compiler/rustc_passes/src/check_attr.rs | 2 +- .../link-ordinal-not-foreign-fn.rs | 11 +++++++++-- .../link-ordinal-not-foreign-fn.stderr | 12 +++++++++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/passes.ftl b/compiler/rustc_error_messages/locales/en-US/passes.ftl index 2802a0d2c6439..d8056c77f0f42 100644 --- a/compiler/rustc_error_messages/locales/en-US/passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/passes.ftl @@ -263,5 +263,5 @@ passes-rustc-lint-opt-ty = `#[rustc_lint_opt_ty]` should be applied to a struct passes-rustc-lint-opt-deny-field-access = `#[rustc_lint_opt_deny_field_access]` should be applied to a field .label = not a field -passes-link-ordinal = attribute should be applied to a foreign function - .label = not a foreign function \ No newline at end of file +passes-link-ordinal = attribute should be applied to a foreign function or static + .label = not a foreign function or static \ No newline at end of file diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 9fee28a403578..8a8d88d7bf4ad 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1864,7 +1864,7 @@ impl CheckAttrVisitor<'_> { fn check_link_ordinal(&self, attr: &Attribute, _span: Span, target: Target) -> bool { match target { - Target::ForeignFn => true, + Target::ForeignFn | Target::ForeignStatic => true, _ => { self.tcx.sess.emit_err(errors::LinkOrdinal { attr_span: attr.span }); false diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs index c86756be4f640..5d273d52a928e 100644 --- a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs @@ -2,17 +2,24 @@ //~^ WARN the feature `raw_dylib` is incomplete #[link_ordinal(123)] -//~^ ERROR attribute should be applied to a foreign function +//~^ ERROR attribute should be applied to a foreign function or static struct Foo {} #[link_ordinal(123)] -//~^ ERROR attribute should be applied to a foreign function +//~^ ERROR attribute should be applied to a foreign function or static fn test() {} +#[link_ordinal(42)] +//~^ ERROR attribute should be applied to a foreign function or static +static mut imported_val: i32 = 123; + #[link(name = "exporter", kind = "raw-dylib")] extern { #[link_ordinal(13)] fn imported_function(); + + #[link_ordinal(42)] + static mut imported_variable: i32; } fn main() {} diff --git a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr index c3c399d575981..8fa2f16f44df9 100644 --- a/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr +++ b/src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr @@ -7,17 +7,23 @@ LL | #![feature(raw_dylib)] = note: `#[warn(incomplete_features)]` on by default = note: see issue #58713 for more information -error: attribute should be applied to a foreign function +error: attribute should be applied to a foreign function or static --> $DIR/link-ordinal-not-foreign-fn.rs:4:1 | LL | #[link_ordinal(123)] | ^^^^^^^^^^^^^^^^^^^^ -error: attribute should be applied to a foreign function +error: attribute should be applied to a foreign function or static --> $DIR/link-ordinal-not-foreign-fn.rs:8:1 | LL | #[link_ordinal(123)] | ^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 2 previous errors; 1 warning emitted +error: attribute should be applied to a foreign function or static + --> $DIR/link-ordinal-not-foreign-fn.rs:12:1 + | +LL | #[link_ordinal(42)] + | ^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors; 1 warning emitted