Skip to content

Commit 0d1b832

Browse files
committed
check link ordinal make sure target is foreign function
1 parent 792bc5a commit 0d1b832

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,6 @@ passes-rustc-lint-opt-ty = `#[rustc_lint_opt_ty]` should be applied to a struct
262262
263263
passes-rustc-lint-opt-deny-field-access = `#[rustc_lint_opt_deny_field_access]` should be applied to a field
264264
.label = not a field
265+
266+
passes-link-ordinal = attribute should be applied to a foreign function
267+
.label = not a foreign function

compiler/rustc_passes/src/check_attr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ impl CheckAttrVisitor<'_> {
146146
| sym::stable
147147
| sym::rustc_allowed_through_unstable_modules
148148
| sym::rustc_promotable => self.check_stability_promotable(&attr, span, target),
149+
sym::link_ordinal => {
150+
self.check_link_ordinal(&attr, span, target)
151+
},
149152
_ => true,
150153
};
151154
is_valid &= attr_is_valid;
@@ -1861,6 +1864,16 @@ impl CheckAttrVisitor<'_> {
18611864
}
18621865
}
18631866

1867+
fn check_link_ordinal(&self, attr: &Attribute, _span: Span, target: Target) -> bool {
1868+
match target {
1869+
Target::ForeignFn => true,
1870+
_ => {
1871+
self.tcx.sess.emit_err(errors::LinkOrdinal { attr_span: attr.span });
1872+
false
1873+
}
1874+
}
1875+
}
1876+
18641877
fn check_deprecated(&self, hir_id: HirId, attr: &Attribute, _span: Span, target: Target) {
18651878
match target {
18661879
Target::Closure | Target::Expression | Target::Statement | Target::Arm => {

compiler/rustc_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,13 @@ pub struct ConstTrait {
551551
pub attr_span: Span,
552552
}
553553

554+
#[derive(SessionDiagnostic)]
555+
#[error(passes::link_ordinal)]
556+
pub struct LinkOrdinal {
557+
#[primary_span]
558+
pub attr_span: Span
559+
}
560+
554561
#[derive(SessionDiagnostic)]
555562
#[error(passes::stability_promotable)]
556563
pub struct StabilityPromotable {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(raw_dylib)]
2+
//~^ WARN the feature `raw_dylib` is incomplete
3+
4+
#[link_ordinal(123)]
5+
//~^ ERROR attribute should be applied to a foreign function
6+
struct Foo {}
7+
8+
#[link_ordinal(123)]
9+
//~^ ERROR attribute should be applied to a foreign function
10+
fn test() {}
11+
12+
#[link(name = "exporter", kind = "raw-dylib")]
13+
extern {
14+
#[link_ordinal(13)]
15+
fn imported_function();
16+
}
17+
18+
fn main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
warning: the feature `raw_dylib` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/link-ordinal-not-foreign-fn.rs:1:12
3+
|
4+
LL | #![feature(raw_dylib)]
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
= note: see issue #58713 <https://github.com/rust-lang/rust/issues/58713> for more information
9+
10+
error: attribute should be applied to a foreign function
11+
--> $DIR/link-ordinal-not-foreign-fn.rs:4:1
12+
|
13+
LL | #[link_ordinal(123)]
14+
| ^^^^^^^^^^^^^^^^^^^^
15+
16+
error: attribute should be applied to a foreign function
17+
--> $DIR/link-ordinal-not-foreign-fn.rs:8:1
18+
|
19+
LL | #[link_ordinal(123)]
20+
| ^^^^^^^^^^^^^^^^^^^^
21+
22+
error: aborting due to 2 previous errors; 1 warning emitted
23+

0 commit comments

Comments
 (0)