Skip to content

Check link ordinal to make sure it is targetted for foreign function #100091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/passes.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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 or static
.label = not a foreign function or static
11 changes: 11 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +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),
_ => true,
};
is_valid &= attr_is_valid;
Expand Down Expand Up @@ -1861,6 +1862,16 @@ impl CheckAttrVisitor<'_> {
}
}

fn check_link_ordinal(&self, attr: &Attribute, _span: Span, target: Target) -> bool {
match target {
Target::ForeignFn | Target::ForeignStatic => 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 => {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![feature(raw_dylib)]
//~^ WARN the feature `raw_dylib` is incomplete

#[link_ordinal(123)]
//~^ 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 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() {}
29 changes: 29 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 <https://github.com/rust-lang/rust/issues/58713> for more information

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 or static
--> $DIR/link-ordinal-not-foreign-fn.rs:8:1
|
LL | #[link_ordinal(123)]
| ^^^^^^^^^^^^^^^^^^^^

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