Skip to content

Commit 5651759

Browse files
committed
Auto merge of #100091 - chenyukang:add-check-for-link-ordinal, r=michaelwoerister
Check link ordinal to make sure it is targetted for foreign function Fix #100009, when link ordinal is not target for foreign functions, emit an error. cc `@dpaoliello`
2 parents 24cf45a + e614bbc commit 5651759

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

Diff for: compiler/rustc_error_messages/locales/en-US/passes.ftl

+3
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 or static
267+
.label = not a foreign function or static

Diff for: compiler/rustc_passes/src/check_attr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ 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 => self.check_link_ordinal(&attr, span, target),
149150
_ => true,
150151
};
151152
is_valid &= attr_is_valid;
@@ -1893,6 +1894,16 @@ impl CheckAttrVisitor<'_> {
18931894
}
18941895
}
18951896

1897+
fn check_link_ordinal(&self, attr: &Attribute, _span: Span, target: Target) -> bool {
1898+
match target {
1899+
Target::ForeignFn | Target::ForeignStatic => true,
1900+
_ => {
1901+
self.tcx.sess.emit_err(errors::LinkOrdinal { attr_span: attr.span });
1902+
false
1903+
}
1904+
}
1905+
}
1906+
18961907
fn check_deprecated(&self, hir_id: HirId, attr: &Attribute, _span: Span, target: Target) {
18971908
match target {
18981909
Target::Closure | Target::Expression | Target::Statement | Target::Arm => {

Diff for: compiler/rustc_passes/src/errors.rs

+7
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 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 or static
6+
struct Foo {}
7+
8+
#[link_ordinal(123)]
9+
//~^ ERROR attribute should be applied to a foreign function or static
10+
fn test() {}
11+
12+
#[link_ordinal(42)]
13+
//~^ ERROR attribute should be applied to a foreign function or static
14+
static mut imported_val: i32 = 123;
15+
16+
#[link(name = "exporter", kind = "raw-dylib")]
17+
extern {
18+
#[link_ordinal(13)]
19+
fn imported_function();
20+
21+
#[link_ordinal(42)]
22+
static mut imported_variable: i32;
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 or static
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 or static
17+
--> $DIR/link-ordinal-not-foreign-fn.rs:8:1
18+
|
19+
LL | #[link_ordinal(123)]
20+
| ^^^^^^^^^^^^^^^^^^^^
21+
22+
error: attribute should be applied to a foreign function or static
23+
--> $DIR/link-ordinal-not-foreign-fn.rs:12:1
24+
|
25+
LL | #[link_ordinal(42)]
26+
| ^^^^^^^^^^^^^^^^^^^
27+
28+
error: aborting due to 3 previous errors; 1 warning emitted
29+

0 commit comments

Comments
 (0)