Remove attribute `#[link_args]`

This commit is contained in:
Vadim Petrochenkov 2021-04-03 20:20:18 +03:00
parent 97717a5618
commit 5839bff0ba
21 changed files with 12 additions and 219 deletions

View File

@ -1411,15 +1411,10 @@ fn add_link_script(cmd: &mut dyn Linker, sess: &Session, tmpdir: &Path, crate_ty
}
}
/// Add arbitrary "user defined" args defined from command line and by `#[link_args]` attributes.
/// Add arbitrary "user defined" args defined from command line.
/// FIXME: Determine where exactly these args need to be inserted.
fn add_user_defined_link_args(
cmd: &mut dyn Linker,
sess: &Session,
codegen_results: &CodegenResults,
) {
fn add_user_defined_link_args(cmd: &mut dyn Linker, sess: &Session) {
cmd.args(&sess.opts.cg.link_args);
cmd.args(&*codegen_results.crate_info.link_args);
}
/// Add arbitrary "late link" args defined by the target spec.
@ -1761,7 +1756,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
add_rpath_args(cmd, sess, codegen_results, out_filename);
// OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
add_user_defined_link_args(cmd, sess, codegen_results);
add_user_defined_link_args(cmd, sess);
// NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
cmd.finalize();

View File

@ -754,7 +754,6 @@ impl CrateInfo {
is_no_builtins: Default::default(),
native_libraries: Default::default(),
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
link_args: tcx.link_args(LOCAL_CRATE),
crate_name: Default::default(),
used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),

View File

@ -139,7 +139,6 @@ pub struct CrateInfo {
pub native_libraries: FxHashMap<CrateNum, Vec<NativeLib>>,
pub crate_name: FxHashMap<CrateNum, String>,
pub used_libraries: Vec<NativeLib>,
pub link_args: Lrc<Vec<String>>,
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
pub used_crates_static: Vec<(CrateNum, LibSource)>,
pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,

View File

@ -258,9 +258,6 @@ declare_features! (
// feature-group-start: actual feature gates
// -------------------------------------------------------------------------
/// Allows using the `#[link_args]` attribute.
(active, link_args, "1.0.0", Some(29596), None),
/// Allows defining identifiers beyond ASCII.
(active, non_ascii_idents, "1.0.0", Some(55467), None),

View File

@ -279,11 +279,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
// Linking:
gated!(naked, AssumedUsed, template!(Word), naked_functions, experimental!(naked)),
gated!(
link_args, Normal, template!(NameValueStr: "args"),
"the `link_args` attribute is experimental and not portable across platforms, \
it is recommended to use `#[link(name = \"foo\")] instead",
),
gated!(
link_ordinal, AssumedUsed, template!(List: "ordinal"), raw_dylib,
experimental!(link_ordinal)

View File

@ -128,6 +128,10 @@ declare_features! (
/// Allows comparing raw pointers during const eval.
(removed, const_compare_raw_pointers, "1.46.0", Some(53020), None,
Some("cannot be allowed in const eval in any meaningful way")),
/// Allows using the `#[link_args]` attribute.
(removed, link_args, "1.53.0", Some(29596), None,
Some("removed in favor of using `-C link-arg=ARG` on command line, \
which is available from cargo build scripts with `cargo:rustc-link-arg` now")),
// -------------------------------------------------------------------------
// feature-group-end: removed features

View File

@ -26,7 +26,6 @@ pub use rmeta::{provide, provide_extern};
mod dependency_format;
mod foreign_modules;
mod link_args;
mod native_libs;
mod rmeta;

View File

@ -1,57 +0,0 @@
use rustc_hir as hir;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::{sym, Symbol};
use rustc_target::spec::abi::Abi;
crate fn collect(tcx: TyCtxt<'_>) -> Vec<String> {
let mut collector = Collector { tcx, args: Vec::new() };
tcx.hir().krate().visit_all_item_likes(&mut collector);
for attr in tcx.hir().attrs(hir::CRATE_HIR_ID).iter() {
if attr.has_name(sym::link_args) {
if let Some(linkarg) = attr.value_str() {
collector.add_link_args(linkarg);
}
}
}
collector.args
}
struct Collector<'tcx> {
tcx: TyCtxt<'tcx>,
args: Vec<String>,
}
impl<'tcx> ItemLikeVisitor<'tcx> for Collector<'tcx> {
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
let abi = match it.kind {
hir::ItemKind::ForeignMod { abi, .. } => abi,
_ => return,
};
if abi == Abi::Rust || abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
return;
}
// First, add all of the custom #[link_args] attributes
let sess = &self.tcx.sess;
for m in
self.tcx.hir().attrs(it.hir_id()).iter().filter(|a| sess.check_name(a, sym::link_args))
{
if let Some(linkarg) = m.value_str() {
self.add_link_args(linkarg);
}
}
}
fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {}
fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem<'tcx>) {}
fn visit_foreign_item(&mut self, _it: &'tcx hir::ForeignItem<'tcx>) {}
}
impl<'tcx> Collector<'tcx> {
fn add_link_args(&mut self, args: Symbol) {
self.args.extend(args.as_str().split(' ').filter(|s| !s.is_empty()).map(|s| s.to_string()))
}
}

View File

@ -1,6 +1,5 @@
use crate::creader::{CStore, LoadedMacro};
use crate::foreign_modules;
use crate::link_args;
use crate::native_libs;
use crate::rmeta::{self, encoder};
@ -295,10 +294,6 @@ pub fn provide(providers: &mut Providers) {
foreign_modules::collect(tcx).into_iter().map(|m| (m.def_id, m)).collect();
Lrc::new(modules)
},
link_args: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Lrc::new(link_args::collect(tcx))
},
// Returns a map from a sufficiently visible external item (i.e., an
// external item that is visible from at least one local module) to a

View File

@ -1253,11 +1253,6 @@ rustc_queries! {
desc { |tcx| "native_library_kind({})", tcx.def_path_str(def_id) }
}
query link_args(_: CrateNum) -> Lrc<Vec<String>> {
eval_always
desc { "looking up link arguments for a crate" }
}
/// Does lifetime resolution, but does not descend into trait items. This
/// should only be used for resolving lifetimes of on trait definitions,
/// and is used to avoid cycles. Importantly, `resolve_lifetimes` still visits

View File

@ -281,7 +281,6 @@
#![feature(intra_doc_pointers)]
#![feature(iter_zip)]
#![feature(lang_items)]
#![feature(link_args)]
#![feature(linkage)]
#![feature(llvm_asm)]
#![feature(log_syntax)]

View File

@ -1,32 +0,0 @@
# `link_args`
The tracking issue for this feature is: [#29596]
[#29596]: https://github.com/rust-lang/rust/issues/29596
------------------------
You can tell `rustc` how to customize linking, and that is via the `link_args`
attribute. This attribute is applied to `extern` blocks and specifies raw flags
which need to get passed to the linker when producing an artifact. An example
usage would be:
```rust,no_run
#![feature(link_args)]
#[link_args = "-foo -bar -baz"]
extern "C" {}
# fn main() {}
```
Note that this feature is currently hidden behind the `feature(link_args)` gate
because this is not a sanctioned way of performing linking. Right now `rustc`
shells out to the system linker (`gcc` on most systems, `link.exe` on MSVC), so
it makes sense to provide extra command line arguments, but this will not
always be the case. In the future `rustc` may use LLVM directly to link native
libraries, in which case `link_args` will have no meaning. You can achieve the
same effect as the `link_args` attribute with the `-C link-args` argument to
`rustc`.
It is highly recommended to *not* use this attribute, and rather use the more
formal `#[link(...)]` attribute on `extern` blocks instead.

View File

@ -12,7 +12,6 @@
#![allow(warnings)]
#![feature(rustc_attrs)]
#![feature(unboxed_closures)]
#![feature(link_args)]
#![crate_type = "rlib"]
// Change function name --------------------------------------------------------
@ -146,21 +145,6 @@ extern "C" {
pub fn add_function2();
}
// Change link-args ------------------------------------------------------------
#[cfg(cfail1)]
#[link_args = "-foo -bar"]
extern "C" {
pub fn change_link_args(c: i32);
}
#[cfg(not(cfail1))]
#[rustc_dirty(cfg = "cfail2", except = "hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg = "cfail3")]
#[link_args = "-foo -bar -baz"]
extern "C" {
pub fn change_link_args(c: i32);
}
// Change link-name ------------------------------------------------------------
#[cfg(cfail1)]
#[link(name = "foo")]

View File

@ -6,5 +6,5 @@ RUSTC_FLAGS = -C linker-flavor=ld -C link-arg=a -C link-args="b c" -C link-args=
RUSTC_FLAGS_PRE = -C linker-flavor=ld -Z pre-link-arg=a -Z pre-link-args="b c" -Z pre-link-args="d e" -Z pre-link-arg=f
all:
$(RUSTC) $(RUSTC_FLAGS) empty.rs 2>&1 | $(CGREP) '"a" "b" "c" "d" "e" "f" "g"'
$(RUSTC) $(RUSTC_FLAGS) empty.rs 2>&1 | $(CGREP) '"a" "b" "c" "d" "e" "f"'
$(RUSTC) $(RUSTC_FLAGS_PRE) empty.rs 2>&1 | $(CGREP) '"a" "b" "c" "d" "e" "f"'

View File

@ -1,6 +1 @@
#![feature(link_args)]
#[link_args = "g"]
extern "C" {}
fn main() {}

View File

@ -1,17 +0,0 @@
// Test that `#[link_args]` attribute is gated by `link_args`
// feature gate, both when it occurs where expected (atop
// `extern { }` blocks) and where unexpected.
// sidestep warning (which is correct, but misleading for
// purposes of this test)
#![allow(unused_attributes)]
#![link_args = "-l unexpected_use_as_inner_attr_on_mod"]
//~^ ERROR the `link_args` attribute is experimental
#[link_args = "-l expected_use_case"]
//~^ ERROR the `link_args` attribute is experimental
extern "C" {}
#[link_args = "-l unexected_use_on_non_extern_item"]
//~^ ERROR: the `link_args` attribute is experimental
fn main() {}

View File

@ -1,30 +0,0 @@
error[E0658]: the `link_args` attribute is experimental and not portable across platforms, it is recommended to use `#[link(name = "foo")] instead
--> $DIR/feature-gate-link_args.rs:11:1
|
LL | #[link_args = "-l expected_use_case"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #29596 <https://github.com/rust-lang/rust/issues/29596> for more information
= help: add `#![feature(link_args)]` to the crate attributes to enable
error[E0658]: the `link_args` attribute is experimental and not portable across platforms, it is recommended to use `#[link(name = "foo")] instead
--> $DIR/feature-gate-link_args.rs:15:1
|
LL | #[link_args = "-l unexected_use_on_non_extern_item"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #29596 <https://github.com/rust-lang/rust/issues/29596> for more information
= help: add `#![feature(link_args)]` to the crate attributes to enable
error[E0658]: the `link_args` attribute is experimental and not portable across platforms, it is recommended to use `#[link(name = "foo")] instead
--> $DIR/feature-gate-link_args.rs:8:1
|
LL | #![link_args = "-l unexpected_use_as_inner_attr_on_mod"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #29596 <https://github.com/rust-lang/rust/issues/29596> for more information
= help: add `#![feature(link_args)]` to the crate attributes to enable
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0658`.

View File

@ -1,13 +0,0 @@
// run-pass
#![allow(unused_attributes)]
// ignore-windows
// ignore-wasm32-bare no libs to link
// ignore-sgx no libs to link
#![feature(link_args)]
#[link_args = "-lc -lm"]
#[link_args = " -lc"]
#[link_args = "-lc "]
extern "C" {}
fn main() {}

View File

@ -1,14 +0,0 @@
// build-fail
// dont-check-compiler-stderr
// ignore-msvc due to linker-flavor=ld
// error-pattern:aFdEfSeVEEE
// compile-flags: -C linker-flavor=ld
/* Make sure invalid link_args are printed to stderr. */
#![feature(link_args)]
#[link_args = "aFdEfSeVEEE"]
extern "C" {}
fn main() {}

View File

@ -1,8 +1,8 @@
// compile-flags: -Clink-arg=-nostartfiles
// ignore-macos
// ignore-windows
#![feature(lang_items, link_args, start, libc)]
#![link_args = "-nostartfiles"]
#![feature(lang_items, start, libc)]
#![no_std]
use core::panic::PanicInfo;

View File

@ -1,9 +1,9 @@
// compile-flags: -Clink-arg=-nostartfiles
// ignore-macos
// ignore-windows
#![warn(clippy::empty_loop)]
#![feature(lang_items, link_args, start, libc)]
#![link_args = "-nostartfiles"]
#![feature(lang_items, start, libc)]
#![no_std]
use core::panic::PanicInfo;