Skip to content

Commit

Permalink
Rollup merge of #100069 - dpaoliello:linkordinal, r=michaelwoerister
Browse files Browse the repository at this point in the history
Add error if link_ordinal used with unsupported link kind

The `link_ordinal` attribute only has an affect if the `raw-dylib` link kind is used, so add an error if it is used with any other link kind.
  • Loading branch information
matthiaskrgr authored Aug 10, 2022
2 parents e10f924 + fda5144 commit 6b5ec41
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
26 changes: 25 additions & 1 deletion compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,31 @@ impl<'tcx> Collector<'tcx> {
.map(|child_item| self.build_dll_import(abi, child_item))
.collect()
}
_ => Vec::new(),
_ => {
for child_item in foreign_mod_items {
if self.tcx.def_kind(child_item.id.def_id).has_codegen_attrs()
&& self
.tcx
.codegen_fn_attrs(child_item.id.def_id)
.link_ordinal
.is_some()
{
let link_ordinal_attr = self
.tcx
.hir()
.attrs(self.tcx.hir().local_def_id_to_hir_id(child_item.id.def_id))
.iter()
.find(|a| a.has_name(sym::link_ordinal))
.unwrap();
sess.span_err(
link_ordinal_attr.span,
"`#[link_ordinal]` is only supported if link kind is `raw-dylib`",
);
}
}

Vec::new()
}
};
self.libs.push(NativeLib {
name: name.map(|(name, _)| name),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(raw_dylib)]
//~^ WARN the feature `raw_dylib` is incomplete

#[link(name = "foo")]
extern "C" {
#[link_ordinal(3)]
//~^ ERROR `#[link_ordinal]` is only supported if link kind is `raw-dylib`
fn foo();
}

#[link(name = "bar", kind = "static")]
extern "C" {
#[link_ordinal(3)]
//~^ ERROR `#[link_ordinal]` is only supported if link kind is `raw-dylib`
fn bar();
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -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-unsupported-link-kind.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: `#[link_ordinal]` is only supported if link kind is `raw-dylib`
--> $DIR/link-ordinal-unsupported-link-kind.rs:6:5
|
LL | #[link_ordinal(3)]
| ^^^^^^^^^^^^^^^^^^

error: `#[link_ordinal]` is only supported if link kind is `raw-dylib`
--> $DIR/link-ordinal-unsupported-link-kind.rs:13:5
|
LL | #[link_ordinal(3)]
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors; 1 warning emitted

0 comments on commit 6b5ec41

Please sign in to comment.