Skip to content

Commit

Permalink
Auto merge of #96885 - petrochenkov:linkstrict2, r=cjgillot,luqmana
Browse files Browse the repository at this point in the history
rustc: Stricter checking for #[link] attributes

A subset of #94962 that doesn't touch library renaming/reordering/deduplication.

`#[link]` attributes are checked for all kinds of unexpected arguments inside them.
I also tried to make wording for these errors more consistent, that's why some existing errors are changed, including errors for command line `-l` options.
Spans are also made more precise where possible.
bors committed May 15, 2022
2 parents e1ec326 + 4fa24bc commit 10b3a0d
Showing 61 changed files with 774 additions and 535 deletions.
31 changes: 0 additions & 31 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
@@ -396,37 +396,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}

// Check for unstable modifiers on `#[link(..)]` attribute
if attr.has_name(sym::link) {
for nested_meta in attr.meta_item_list().unwrap_or_default() {
if nested_meta.has_name(sym::modifiers) {
if let Some(modifiers) = nested_meta.value_str() {
for modifier in modifiers.as_str().split(',') {
if let Some(modifier) = modifier.strip_prefix(&['+', '-']) {
macro_rules! gate_modifier { ($($name:literal => $feature:ident)*) => {
$(if modifier == $name {
let msg = concat!("`#[link(modifiers=\"", $name, "\")]` is unstable");
gate_feature_post!(
self,
$feature,
nested_meta.name_value_literal_span().unwrap(),
msg
);
})*
}}

gate_modifier!(
"bundle" => native_link_modifiers_bundle
"verbatim" => native_link_modifiers_verbatim
"as-needed" => native_link_modifiers_as_needed
);
}
}
}
}
}
}

// Emit errors for non-staged-api crates.
if !self.features.staged_api {
if attr.has_name(sym::rustc_deprecated)
5 changes: 5 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0455.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Some linking kinds are target-specific and not supported on all platforms.

Linking with `kind=framework` is only supported when targeting macOS,
as frameworks are specific to that operating system.

Similarly, `kind=raw-dylib` is only supported when targeting Windows-like
platforms.

Erroneous code example:

```ignore (should-compile_fail-but-cannot-doctest-conditionally-without-macos)
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/error_codes/E0458.md
Original file line number Diff line number Diff line change
@@ -12,3 +12,4 @@ Please specify a valid "kind" value, from one of the following:
* static
* dylib
* framework
* raw-dylib
2 changes: 2 additions & 0 deletions compiler/rustc_metadata/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(crate_visibility_modifier)]
#![feature(decl_macro)]
#![feature(drain_filter)]
#![feature(generators)]
#![feature(let_chains)]
#![feature(let_else)]
#![feature(nll)]
#![feature(once_cell)]
514 changes: 274 additions & 240 deletions compiler/rustc_metadata/src/native_libs.rs

Large diffs are not rendered by default.

34 changes: 20 additions & 14 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ use rustc_session::lint::builtin::{
use rustc_session::parse::feature_err;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_target::spec::abi::Abi;
use std::collections::hash_map::Entry;

pub(crate) fn target_from_impl_item<'tcx>(
@@ -1317,22 +1318,27 @@ impl CheckAttrVisitor<'_> {

/// Checks if `#[link]` is applied to an item other than a foreign module.
fn check_link(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
match target {
Target::ForeignMod => {}
_ => {
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
let mut diag = lint.build("attribute should be applied to an `extern` block");
diag.warn(
"this was previously accepted by the compiler but is \
being phased out; it will become a hard error in \
a future release!",
);
if target == Target::ForeignMod
&& let hir::Node::Item(item) = self.tcx.hir().get(hir_id)
&& let Item { kind: ItemKind::ForeignMod { abi, .. }, .. } = item
&& !matches!(abi, Abi::Rust | Abi::RustIntrinsic | Abi::PlatformIntrinsic)
{
return;
}

diag.span_label(span, "not an `extern` block");
diag.emit();
});
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
let mut diag =
lint.build("attribute should be applied to an `extern` block with non-Rust ABI");
diag.warn(
"this was previously accepted by the compiler but is \
being phased out; it will become a hard error in \
a future release!",
);
if target != Target::ForeignMod {
diag.span_label(span, "not an `extern` block");
}
}
diag.emit();
});
}

/// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
104 changes: 45 additions & 59 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
@@ -1937,33 +1937,27 @@ fn parse_native_lib_kind(
};

let kind = match kind {
"dylib" => NativeLibKind::Dylib { as_needed: None },
"framework" => NativeLibKind::Framework { as_needed: None },
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
"static-nobundle" => {
early_warn(
error_format,
"library kind `static-nobundle` has been superseded by specifying \
`-bundle` on library kind `static`. Try `static:-bundle`",
modifier `-bundle` with library kind `static`. Try `static:-bundle`",
);
if modifiers.is_some() {
early_error(
error_format,
"linking modifier can't be used with library kind `static-nobundle`",
)
}
if !nightly_options::match_is_nightly_build(matches) {
early_error(
error_format,
"library kind `static-nobundle` are currently unstable and only accepted on \
the nightly compiler",
"library kind `static-nobundle` is unstable \
and only accepted on the nightly compiler",
);
}
NativeLibKind::Static { bundle: Some(false), whole_archive: None }
}
s => early_error(
"dylib" => NativeLibKind::Dylib { as_needed: None },
"framework" => NativeLibKind::Framework { as_needed: None },
_ => early_error(
error_format,
&format!("unknown library kind `{s}`, expected one of dylib, framework, or static"),
&format!("unknown library kind `{kind}`, expected one of: static, dylib, framework"),
),
};
match modifiers {
@@ -1978,94 +1972,83 @@ fn parse_native_lib_modifiers(
error_format: ErrorOutputType,
matches: &getopts::Matches,
) -> (NativeLibKind, Option<bool>) {
let report_unstable_modifier = |modifier| {
if !nightly_options::is_unstable_enabled(matches) {
let why = if nightly_options::match_is_nightly_build(matches) {
" and only accepted on the nightly compiler"
} else {
", the `-Z unstable-options` flag must also be passed to use it"
};
early_error(
error_format,
&format!("{modifier} linking modifier is currently unstable{why}"),
)
}
};

let mut has_duplicate_modifiers = false;
let mut verbatim = None;
for modifier in modifiers.split(',') {
let (modifier, value) = match modifier.strip_prefix(&['+', '-']) {
Some(m) => (m, modifier.starts_with('+')),
None => early_error(
error_format,
"invalid linking modifier syntax, expected '+' or '-' prefix \
before one of: bundle, verbatim, whole-archive, as-needed",
before one of: bundle, verbatim, whole-archive, as-needed",
),
};

let report_unstable_modifier = || {
if !nightly_options::is_unstable_enabled(matches) {
let why = if nightly_options::match_is_nightly_build(matches) {
" and only accepted on the nightly compiler"
} else {
", the `-Z unstable-options` flag must also be passed to use it"
};
early_error(
error_format,
&format!("linking modifier `{modifier}` is unstable{why}"),
)
}
};
let assign_modifier = |dst: &mut Option<bool>| {
if dst.is_some() {
let msg = format!("multiple `{modifier}` modifiers in a single `-l` option");
early_error(error_format, &msg)
} else {
*dst = Some(value);
}
};
match (modifier, &mut kind) {
("bundle", NativeLibKind::Static { bundle, .. }) => {
report_unstable_modifier(modifier);
if bundle.is_some() {
has_duplicate_modifiers = true;
}
*bundle = Some(value);
report_unstable_modifier();
assign_modifier(bundle)
}
("bundle", _) => early_error(
error_format,
"bundle linking modifier is only compatible with \
`static` linking kind",
"linking modifier `bundle` is only compatible with `static` linking kind",
),

("verbatim", _) => {
report_unstable_modifier(modifier);
if verbatim.is_some() {
has_duplicate_modifiers = true;
}
verbatim = Some(value);
report_unstable_modifier();
assign_modifier(&mut verbatim)
}

("whole-archive", NativeLibKind::Static { whole_archive, .. }) => {
if whole_archive.is_some() {
has_duplicate_modifiers = true;
}
*whole_archive = Some(value);
assign_modifier(whole_archive)
}
("whole-archive", _) => early_error(
error_format,
"whole-archive linking modifier is only compatible with \
`static` linking kind",
"linking modifier `whole-archive` is only compatible with `static` linking kind",
),

("as-needed", NativeLibKind::Dylib { as_needed })
| ("as-needed", NativeLibKind::Framework { as_needed }) => {
report_unstable_modifier(modifier);
if as_needed.is_some() {
has_duplicate_modifiers = true;
}
*as_needed = Some(value);
report_unstable_modifier();
assign_modifier(as_needed)
}
("as-needed", _) => early_error(
error_format,
"as-needed linking modifier is only compatible with \
`dylib` and `framework` linking kinds",
"linking modifier `as-needed` is only compatible with \
`dylib` and `framework` linking kinds",
),

// Note: this error also excludes the case with empty modifier
// string, like `modifiers = ""`.
_ => early_error(
error_format,
&format!(
"unrecognized linking modifier `{modifier}`, expected one \
of: bundle, verbatim, whole-archive, as-needed"
"unknown linking modifier `{modifier}`, expected one \
of: bundle, verbatim, whole-archive, as-needed"
),
),
}
}
if has_duplicate_modifiers {
report_unstable_modifier("duplicating")
}

(kind, verbatim)
}
@@ -2093,6 +2076,9 @@ fn parse_libs(matches: &getopts::Matches, error_format: ErrorOutputType) -> Vec<
None => (name, None),
Some((name, new_name)) => (name.to_string(), Some(new_name.to_owned())),
};
if name.is_empty() {
early_error(error_format, "library name must not be empty");
}
NativeLib { name, new_name, kind, verbatim }
})
.collect()
3 changes: 2 additions & 1 deletion src/doc/rustc/src/command-line-arguments.md
Original file line number Diff line number Diff line change
@@ -52,7 +52,8 @@ where `KIND` may be one of:
If the kind is specified, then linking modifiers can be attached to it.
Modifiers are specified as a comma-delimited string with each modifier prefixed with
either a `+` or `-` to indicate that the modifier is enabled or disabled, respectively.
The last boolean value specified for a given modifier wins. \
Specifying multiple `modifiers` arguments in a single `link` attribute,
or multiple identical modifiers in the same `modifiers` argument is not currently supported. \
Example: `-l static:+whole-archive=mylib`.

The kind of library and the modifiers can also be specified in a [`#[link]`
2 changes: 1 addition & 1 deletion src/test/ui/empty/empty-linkname.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[link(name = "")] //~ ERROR: given with empty name
#[link(name = "")] //~ ERROR: link name must not be empty
extern "C" {}

fn main() {}
6 changes: 3 additions & 3 deletions src/test/ui/empty/empty-linkname.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0454]: `#[link(name = "")]` given with empty name
--> $DIR/empty-linkname.rs:1:1
error[E0454]: link name must not be empty
--> $DIR/empty-linkname.rs:1:15
|
LL | #[link(name = "")]
| ^^^^^^^^^^^^^^^^^^ empty name given
| ^^ empty link name

error: aborting due to previous error

6 changes: 3 additions & 3 deletions src/test/ui/error-codes/E0454.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0454]: `#[link(name = "")]` given with empty name
--> $DIR/E0454.rs:1:1
error[E0454]: link name must not be empty
--> $DIR/E0454.rs:1:15
|
LL | #[link(name = "")] extern "C" {}
| ^^^^^^^^^^^^^^^^^^ empty name given
| ^^ empty link name

error: aborting due to previous error

10 changes: 4 additions & 6 deletions src/test/ui/error-codes/E0458.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
error[E0458]: unknown kind: `wonderful_unicorn`
--> $DIR/E0458.rs:1:8
error[E0458]: unknown link kind `wonderful_unicorn`, expected one of: static, dylib, framework, raw-dylib
--> $DIR/E0458.rs:1:15
|
LL | #[link(kind = "wonderful_unicorn")] extern "C" {}
| -------^^^^^^^^^^^^^^^^^^^^^^^^^^--
| |
| unknown kind
| ^^^^^^^^^^^^^^^^^^^ unknown link kind

error[E0459]: `#[link(...)]` specified without `name = "foo"`
error[E0459]: `#[link]` attribute requires a `name = "string"` argument
--> $DIR/E0458.rs:1:1
|
LL | #[link(kind = "wonderful_unicorn")] extern "C" {}
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0459.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0459]: `#[link(...)]` specified without `name = "foo"`
error[E0459]: `#[link]` attribute requires a `name = "string"` argument
--> $DIR/E0459.rs:1:1
|
LL | #[link(kind = "dylib")] extern "C" {}
6 changes: 3 additions & 3 deletions src/test/ui/feature-gates/feature-gate-link_cfg.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0658]: kind="link_cfg" is unstable
--> $DIR/feature-gate-link_cfg.rs:1:1
error[E0658]: link cfg is unstable
--> $DIR/feature-gate-link_cfg.rs:1:22
|
LL | #[link(name = "foo", cfg(foo))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^
|
= note: see issue #37406 <https://github.com/rust-lang/rust/issues/37406> for more information
= help: add `#![feature(link_cfg)]` to the crate attributes to enable
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[link(name = "foo", modifiers = "+as-needed")]
//~^ ERROR: `#[link(modifiers="as-needed")]` is unstable
#[link(name = "foo", kind = "dylib", modifiers = "+as-needed")]
//~^ ERROR: linking modifier `as-needed` is unstable
extern "C" {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0658]: `#[link(modifiers="as-needed")]` is unstable
--> $DIR/feature-gate-native_link_modifiers_as_needed.rs:1:34
error[E0658]: linking modifier `as-needed` is unstable
--> $DIR/feature-gate-native_link_modifiers_as_needed.rs:1:50
|
LL | #[link(name = "foo", modifiers = "+as-needed")]
| ^^^^^^^^^^^^
LL | #[link(name = "foo", kind = "dylib", modifiers = "+as-needed")]
| ^^^^^^^^^^^^
|
= note: see issue #81490 <https://github.com/rust-lang/rust/issues/81490> for more information
= help: add `#![feature(native_link_modifiers_as_needed)]` to the crate attributes to enable
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: bundle linking modifier is currently unstable and only accepted on the nightly compiler
error: linking modifier `bundle` is unstable and only accepted on the nightly compiler

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[link(name = "foo", modifiers = "+bundle")]
//~^ ERROR: `#[link(modifiers="bundle")]` is unstable
#[link(name = "foo", kind = "static", modifiers = "+bundle")]
//~^ ERROR: linking modifier `bundle` is unstable
extern "C" {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0658]: `#[link(modifiers="bundle")]` is unstable
--> $DIR/feature-gate-native_link_modifiers_bundle.rs:1:34
error[E0658]: linking modifier `bundle` is unstable
--> $DIR/feature-gate-native_link_modifiers_bundle.rs:1:51
|
LL | #[link(name = "foo", modifiers = "+bundle")]
| ^^^^^^^^^
LL | #[link(name = "foo", kind = "static", modifiers = "+bundle")]
| ^^^^^^^^^
|
= note: see issue #81490 <https://github.com/rust-lang/rust/issues/81490> for more information
= help: add `#![feature(native_link_modifiers_bundle)]` to the crate attributes to enable
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[link(name = "foo", modifiers = "+verbatim")]
//~^ ERROR: `#[link(modifiers="verbatim")]` is unstable
//~^ ERROR: linking modifier `verbatim` is unstable
extern "C" {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0658]: `#[link(modifiers="verbatim")]` is unstable
error[E0658]: linking modifier `verbatim` is unstable
--> $DIR/feature-gate-native_link_modifiers_verbatim.rs:1:34
|
LL | #[link(name = "foo", modifiers = "+verbatim")]
2 changes: 1 addition & 1 deletion src/test/ui/feature-gates/feature-gate-raw-dylib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// only-windows
#[link(name = "foo", kind = "raw-dylib")]
//~^ ERROR: kind="raw-dylib" is unstable
//~^ ERROR: link kind `raw-dylib` is unstable
extern "C" {}

fn main() {}
6 changes: 3 additions & 3 deletions src/test/ui/feature-gates/feature-gate-raw-dylib.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0658]: kind="raw-dylib" is unstable
--> $DIR/feature-gate-raw-dylib.rs:2:1
error[E0658]: link kind `raw-dylib` is unstable
--> $DIR/feature-gate-raw-dylib.rs:2:29
|
LL | #[link(name = "foo", kind = "raw-dylib")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^
|
= note: see issue #58713 <https://github.com/rust-lang/rust/issues/58713> for more information
= help: add `#![feature(raw_dylib)]` to the crate attributes to enable
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
warning: library kind `static-nobundle` has been superseded by specifying `-bundle` on library kind `static`. Try `static:-bundle`
warning: library kind `static-nobundle` has been superseded by specifying modifier `-bundle` with library kind `static`. Try `static:-bundle`

4 changes: 2 additions & 2 deletions src/test/ui/feature-gates/feature-gate-static-nobundle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[link(name = "foo", kind = "static-nobundle")]
//~^ WARNING: library kind `static-nobundle` has been superseded by specifying modifier `-bundle` with library kind `static`
//~^^ ERROR: kind="static-nobundle" is unstable
//~^ WARNING: link kind `static-nobundle` has been superseded by specifying modifier `-bundle` with link kind `static`
//~^^ ERROR: link kind `static-nobundle` is unstable
extern "C" {}

fn main() {}
12 changes: 6 additions & 6 deletions src/test/ui/feature-gates/feature-gate-static-nobundle.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
warning: library kind `static-nobundle` has been superseded by specifying modifier `-bundle` with library kind `static`
--> $DIR/feature-gate-static-nobundle.rs:1:22
warning: link kind `static-nobundle` has been superseded by specifying modifier `-bundle` with link kind `static`
--> $DIR/feature-gate-static-nobundle.rs:1:29
|
LL | #[link(name = "foo", kind = "static-nobundle")]
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^

error[E0658]: kind="static-nobundle" is unstable
--> $DIR/feature-gate-static-nobundle.rs:1:22
error[E0658]: link kind `static-nobundle` is unstable
--> $DIR/feature-gate-static-nobundle.rs:1:29
|
LL | #[link(name = "foo", kind = "static-nobundle")]
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #37403 <https://github.com/rust-lang/rust/issues/37403> for more information
= help: add `#![feature(static_nobundle)]` to the crate attributes to enable
Original file line number Diff line number Diff line change
@@ -581,6 +581,10 @@ mod link {
//~^ WARN attribute should be applied to an `extern` block
//~| WARN this was previously accepted
//~| NOTE not an `extern` block

#[link()] extern "Rust" {}
//~^ WARN attribute should be applied to an `extern` block
//~| WARN this was previously accepted
}

struct StructForDeprecated;
128 changes: 68 additions & 60 deletions src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-43925.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[link(name = "foo", cfg("rlib"))] //~ ERROR invalid argument for `cfg(..)`
#[link(name = "foo", cfg("rlib"))] //~ ERROR link cfg must have a single predicate argument
extern "C" {}

fn main() {}
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-43925.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: invalid argument for `cfg(..)`
--> $DIR/issue-43925.rs:1:26
error: link cfg must have a single predicate argument
--> $DIR/issue-43925.rs:1:22
|
LL | #[link(name = "foo", cfg("rlib"))]
| ^^^^^^
| ^^^^^^^^^^^

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-43926.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[link(name = "foo", cfg())] //~ ERROR `cfg()` must have an argument
#[link(name = "foo", cfg())] //~ ERROR link cfg must have a single predicate argument
extern "C" {}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-43926.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `cfg()` must have an argument
error: link cfg must have a single predicate argument
--> $DIR/issue-43926.rs:1:22
|
LL | #[link(name = "foo", cfg())]
7 changes: 0 additions & 7 deletions src/test/ui/linkage-attr/bad-extern-link-attrs.rs

This file was deleted.

24 changes: 0 additions & 24 deletions src/test/ui/linkage-attr/bad-extern-link-attrs.stderr

This file was deleted.

8 changes: 8 additions & 0 deletions src/test/ui/linkage-attr/link-attr-validation-early.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Top-level ill-formed
#[link] //~ ERROR attribute must be of the form
//~| WARN this was previously accepted
#[link = "foo"] //~ ERROR attribute must be of the form
//~| WARN this was previously accepted
extern "C" {}

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/linkage-attr/link-attr-validation-early.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: attribute must be of the form `#[link(name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...")]`
--> $DIR/link-attr-validation-early.rs:2:1
|
LL | #[link]
| ^^^^^^^
|
= note: `#[deny(ill_formed_attribute_input)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>

error: attribute must be of the form `#[link(name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...")]`
--> $DIR/link-attr-validation-early.rs:4:1
|
LL | #[link = "foo"]
| ^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>

error: aborting due to 2 previous errors

40 changes: 40 additions & 0 deletions src/test/ui/linkage-attr/link-attr-validation-late.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![feature(native_link_modifiers_verbatim)]
#![feature(link_cfg)]

// Top-level ill-formed
#[link(name = "...", "literal")] //~ ERROR unexpected `#[link]` argument
#[link(name = "...", unknown)] //~ ERROR unexpected `#[link]` argument
extern "C" {}

// Duplicate arguments
#[link(name = "foo", name = "bar")] //~ ERROR multiple `name` arguments
#[link(name = "...", kind = "dylib", kind = "bar")] //~ ERROR multiple `kind` arguments
#[link(name = "...", modifiers = "+verbatim", modifiers = "bar")] //~ ERROR multiple `modifiers` arguments
#[link(name = "...", cfg(FALSE), cfg(FALSE))] //~ ERROR multiple `cfg` arguments
#[link(wasm_import_module = "foo", wasm_import_module = "bar")] //~ ERROR multiple `wasm_import_module` arguments
extern "C" {}

// Ill-formed arguments
#[link(name)] //~ ERROR link name must be of the form `name = "string"`
//~| ERROR `#[link]` attribute requires a `name = "string"` argument
#[link(name())] //~ ERROR link name must be of the form `name = "string"`
//~| ERROR `#[link]` attribute requires a `name = "string"` argument
#[link(name = "...", kind)] //~ ERROR link kind must be of the form `kind = "string"`
#[link(name = "...", kind())] //~ ERROR link kind must be of the form `kind = "string"`
#[link(name = "...", modifiers)] //~ ERROR link modifiers must be of the form `modifiers = "string"`
#[link(name = "...", modifiers())] //~ ERROR link modifiers must be of the form `modifiers = "string"`
#[link(name = "...", cfg)] //~ ERROR link cfg must be of the form `cfg(/* predicate */)`
#[link(name = "...", cfg = "literal")] //~ ERROR link cfg must be of the form `cfg(/* predicate */)`
#[link(name = "...", cfg("literal"))] //~ ERROR link cfg must have a single predicate argument
#[link(name = "...", wasm_import_module)] //~ ERROR wasm import module must be of the form `wasm_import_module = "string"`
#[link(name = "...", wasm_import_module())] //~ ERROR wasm import module must be of the form `wasm_import_module = "string"`
extern "C" {}

// Basic modifier validation
#[link(name = "...", modifiers = "")] //~ ERROR invalid linking modifier syntax, expected '+' or '-' prefix
#[link(name = "...", modifiers = "no-plus-minus")] //~ ERROR invalid linking modifier syntax, expected '+' or '-' prefix
#[link(name = "...", modifiers = "+unknown")] //~ ERROR unknown linking modifier `unknown`
#[link(name = "...", modifiers = "+verbatim,+verbatim")] //~ ERROR multiple `verbatim` modifiers
extern "C" {}

fn main() {}
147 changes: 147 additions & 0 deletions src/test/ui/linkage-attr/link-attr-validation-late.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
error: unexpected `#[link]` argument, expected one of: name, kind, modifiers, cfg, wasm_import_module
--> $DIR/link-attr-validation-late.rs:5:22
|
LL | #[link(name = "...", "literal")]
| ^^^^^^^^^

error: unexpected `#[link]` argument, expected one of: name, kind, modifiers, cfg, wasm_import_module
--> $DIR/link-attr-validation-late.rs:6:22
|
LL | #[link(name = "...", unknown)]
| ^^^^^^^

error: multiple `name` arguments in a single `#[link]` attribute
--> $DIR/link-attr-validation-late.rs:10:22
|
LL | #[link(name = "foo", name = "bar")]
| ^^^^^^^^^^^^

error: multiple `kind` arguments in a single `#[link]` attribute
--> $DIR/link-attr-validation-late.rs:11:38
|
LL | #[link(name = "...", kind = "dylib", kind = "bar")]
| ^^^^^^^^^^^^

error: multiple `modifiers` arguments in a single `#[link]` attribute
--> $DIR/link-attr-validation-late.rs:12:47
|
LL | #[link(name = "...", modifiers = "+verbatim", modifiers = "bar")]
| ^^^^^^^^^^^^^^^^^

error: multiple `cfg` arguments in a single `#[link]` attribute
--> $DIR/link-attr-validation-late.rs:13:34
|
LL | #[link(name = "...", cfg(FALSE), cfg(FALSE))]
| ^^^^^^^^^^

error: multiple `wasm_import_module` arguments in a single `#[link]` attribute
--> $DIR/link-attr-validation-late.rs:14:36
|
LL | #[link(wasm_import_module = "foo", wasm_import_module = "bar")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: link name must be of the form `name = "string"`
--> $DIR/link-attr-validation-late.rs:18:8
|
LL | #[link(name)]
| ^^^^

error[E0459]: `#[link]` attribute requires a `name = "string"` argument
--> $DIR/link-attr-validation-late.rs:18:1
|
LL | #[link(name)]
| ^^^^^^^^^^^^^ missing `name` argument

error: link name must be of the form `name = "string"`
--> $DIR/link-attr-validation-late.rs:20:8
|
LL | #[link(name())]
| ^^^^^^

error[E0459]: `#[link]` attribute requires a `name = "string"` argument
--> $DIR/link-attr-validation-late.rs:20:1
|
LL | #[link(name())]
| ^^^^^^^^^^^^^^^ missing `name` argument

error: link kind must be of the form `kind = "string"`
--> $DIR/link-attr-validation-late.rs:22:22
|
LL | #[link(name = "...", kind)]
| ^^^^

error: link kind must be of the form `kind = "string"`
--> $DIR/link-attr-validation-late.rs:23:22
|
LL | #[link(name = "...", kind())]
| ^^^^^^

error: link modifiers must be of the form `modifiers = "string"`
--> $DIR/link-attr-validation-late.rs:24:22
|
LL | #[link(name = "...", modifiers)]
| ^^^^^^^^^

error: link modifiers must be of the form `modifiers = "string"`
--> $DIR/link-attr-validation-late.rs:25:22
|
LL | #[link(name = "...", modifiers())]
| ^^^^^^^^^^^

error: link cfg must be of the form `cfg(/* predicate */)`
--> $DIR/link-attr-validation-late.rs:26:22
|
LL | #[link(name = "...", cfg)]
| ^^^

error: link cfg must be of the form `cfg(/* predicate */)`
--> $DIR/link-attr-validation-late.rs:27:22
|
LL | #[link(name = "...", cfg = "literal")]
| ^^^^^^^^^^^^^^^

error: link cfg must have a single predicate argument
--> $DIR/link-attr-validation-late.rs:28:22
|
LL | #[link(name = "...", cfg("literal"))]
| ^^^^^^^^^^^^^^

error: wasm import module must be of the form `wasm_import_module = "string"`
--> $DIR/link-attr-validation-late.rs:29:22
|
LL | #[link(name = "...", wasm_import_module)]
| ^^^^^^^^^^^^^^^^^^

error: wasm import module must be of the form `wasm_import_module = "string"`
--> $DIR/link-attr-validation-late.rs:30:22
|
LL | #[link(name = "...", wasm_import_module())]
| ^^^^^^^^^^^^^^^^^^^^

error: invalid linking modifier syntax, expected '+' or '-' prefix before one of: bundle, verbatim, whole-archive, as-needed
--> $DIR/link-attr-validation-late.rs:34:34
|
LL | #[link(name = "...", modifiers = "")]
| ^^

error: invalid linking modifier syntax, expected '+' or '-' prefix before one of: bundle, verbatim, whole-archive, as-needed
--> $DIR/link-attr-validation-late.rs:35:34
|
LL | #[link(name = "...", modifiers = "no-plus-minus")]
| ^^^^^^^^^^^^^^^

error: unknown linking modifier `unknown`, expected one of: bundle, verbatim, whole-archive, as-needed
--> $DIR/link-attr-validation-late.rs:36:34
|
LL | #[link(name = "...", modifiers = "+unknown")]
| ^^^^^^^^^^

error: multiple `verbatim` modifiers in a single `modifiers` argument
--> $DIR/link-attr-validation-late.rs:37:34
|
LL | #[link(name = "...", modifiers = "+verbatim,+verbatim")]
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 24 previous errors

For more information about this error, try `rustc --explain E0459`.
2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-bad-form.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// compile-flags:-l static=
// error-pattern: empty library name given via `-l`
// error-pattern: library name must not be empty

fn main() {
}
4 changes: 1 addition & 3 deletions src/test/ui/manual/manual-link-bad-form.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
error: empty library name given via `-l`

error: aborting due to previous error
error: library name must not be empty

2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-bad-kind.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// compile-flags:-l bar=foo
// error-pattern: unknown library kind `bar`, expected one of dylib, framework, or static
// error-pattern: unknown library kind `bar`, expected one of: static, dylib, framework

fn main() {
}
2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-bad-kind.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind `bar`, expected one of dylib, framework, or static
error: unknown library kind `bar`, expected one of: static, dylib, framework

2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-framework.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ignore-macos
// ignore-ios
// compile-flags:-l framework=foo
// error-pattern: native frameworks are only available on macOS targets
// error-pattern: library kind `framework` is only supported on Apple targets

fn main() {
}
2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-framework.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: native frameworks are only available on macOS targets
error: library kind `framework` is only supported on Apple targets

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-unsupported-kind.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// compile-flags:-l raw-dylib=foo
// error-pattern: unknown library kind `raw-dylib`, expected one of dylib, framework, or static
// error-pattern: unknown library kind `raw-dylib`, expected one of: static, dylib, framework

fn main() {
}
2 changes: 1 addition & 1 deletion src/test/ui/manual/manual-link-unsupported-kind.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind `raw-dylib`, expected one of dylib, framework, or static
error: unknown library kind `raw-dylib`, expected one of: static, dylib, framework

2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Unspecified kind should fail with an error

// compile-flags: -l =mylib
// error-pattern: unknown library kind ``, expected one of dylib, framework, or static
// error-pattern: unknown library kind ``, expected one of: static, dylib, framework

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-1.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind ``, expected one of dylib, framework, or static
error: unknown library kind ``, expected one of: static, dylib, framework

2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Unspecified kind should fail with an error

// compile-flags: -l :+bundle=mylib
// error-pattern: unknown library kind ``, expected one of dylib, framework, or static
// error-pattern: unknown library kind ``, expected one of: static, dylib, framework

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/native-library-link-flags/empty-kind-2.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: unknown library kind ``, expected one of dylib, framework, or static
error: unknown library kind ``, expected one of: static, dylib, framework

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: duplicating linking modifier is currently unstable and only accepted on the nightly compiler
error: multiple `whole-archive` modifiers in a single `-l` option

5 changes: 3 additions & 2 deletions src/test/ui/native-library-link-flags/modifiers-override.rs
Original file line number Diff line number Diff line change
@@ -3,12 +3,13 @@
#![feature(native_link_modifiers_bundle)]

#[link(name = "foo")]
#[link( //~ ERROR multiple `modifiers` arguments in a single `#[link]` attribute
#[link(
name = "bar",
kind = "static",
modifiers = "+whole-archive,-whole-archive",
//~^ ERROR same modifier is used multiple times in a single `modifiers` argument
//~^ ERROR multiple `whole-archive` modifiers in a single `modifiers` argument
modifiers = "+bundle"
//~^ ERROR multiple `modifiers` arguments in a single `#[link]` attribute
)]
extern "C" {}
//~^ ERROR overriding linking modifiers from command line is not supported
26 changes: 10 additions & 16 deletions src/test/ui/native-library-link-flags/modifiers-override.stderr
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
error: same modifier is used multiple times in a single `modifiers` argument
--> $DIR/modifiers-override.rs:9:5
error: multiple `modifiers` arguments in a single `#[link]` attribute
--> $DIR/modifiers-override.rs:11:5
|
LL | modifiers = "+whole-archive,-whole-archive",
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | modifiers = "+bundle"
| ^^^^^^^^^^^^^^^^^^^^^

error: multiple `modifiers` arguments in a single `#[link]` attribute
--> $DIR/modifiers-override.rs:6:1
error: multiple `whole-archive` modifiers in a single `modifiers` argument
--> $DIR/modifiers-override.rs:9:17
|
LL | / #[link(
LL | | name = "bar",
LL | | kind = "static",
LL | | modifiers = "+whole-archive,-whole-archive",
LL | |
LL | | modifiers = "+bundle"
LL | | )]
| |__^
LL | modifiers = "+whole-archive,-whole-archive",
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: overriding linking modifiers from command line is not supported
--> $DIR/modifiers-override.rs:13:1
--> $DIR/modifiers-override.rs:14:1
|
LL | extern "C" {}
| ^^^^^^^^^^^^^

error: overriding linking modifiers from command line is not supported
--> $DIR/modifiers-override.rs:13:1
--> $DIR/modifiers-override.rs:14:1
|
LL | extern "C" {}
| ^^^^^^^^^^^^^
2 changes: 1 addition & 1 deletion src/test/ui/osx-frameworks.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@

#[link(name = "foo", kind = "framework")]
extern "C" {}
//~^^ ERROR: native frameworks are only available on macOS
//~^^ ERROR: link kind `framework` is only supported on Apple targets

fn main() {}
6 changes: 3 additions & 3 deletions src/test/ui/osx-frameworks.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0455]: native frameworks are only available on macOS targets
--> $DIR/osx-frameworks.rs:3:1
error[E0455]: link kind `framework` is only supported on Apple targets
--> $DIR/osx-frameworks.rs:3:29
|
LL | #[link(name = "foo", kind = "framework")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^

error: aborting due to previous error

9 changes: 9 additions & 0 deletions src/test/ui/rfc-1717-dllimport/rename-modifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// compile-flags: -l dylib=foo:bar
// error-pattern: overriding linking modifiers from command line is not supported

#![feature(native_link_modifiers_as_needed)]

#![crate_type = "lib"]

#[link(name = "foo", kind = "dylib", modifiers = "-as-needed")]
extern "C" {}
8 changes: 8 additions & 0 deletions src/test/ui/rfc-1717-dllimport/rename-modifiers.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: overriding linking modifiers from command line is not supported
--> $DIR/rename-modifiers.rs:9:1
|
LL | extern "C" {}
| ^^^^^^^^^^^^^

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/rfc-2627-raw-dylib/raw-dylib-windows-only.rs
Original file line number Diff line number Diff line change
@@ -3,5 +3,5 @@
#![feature(raw_dylib)]
//~^ WARNING: the feature `raw_dylib` is incomplete
#[link(name = "foo", kind = "raw-dylib")]
//~^ ERROR: `#[link(...)]` with `kind = "raw-dylib"` only supported on Windows
//~^ ERROR: link kind `raw-dylib` is only supported on Windows targets
extern "C" {}
7 changes: 4 additions & 3 deletions src/test/ui/rfc-2627-raw-dylib/raw-dylib-windows-only.stderr
Original file line number Diff line number Diff line change
@@ -7,11 +7,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(...)]` with `kind = "raw-dylib"` only supported on Windows
--> $DIR/raw-dylib-windows-only.rs:5:1
error[E0455]: link kind `raw-dylib` is only supported on Windows targets
--> $DIR/raw-dylib-windows-only.rs:5:29
|
LL | #[link(name = "foo", kind = "raw-dylib")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0455`.
11 changes: 11 additions & 0 deletions src/test/ui/wasm/wasm-import-module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(link_cfg)]

#[link(name = "...", wasm_import_module)] //~ ERROR: must be of the form
extern "C" {}

@@ -7,4 +9,13 @@ extern "C" {}
#[link(name = "...", wasm_import_module())] //~ ERROR: must be of the form
extern "C" {}

#[link(wasm_import_module = "foo", name = "bar")] //~ ERROR: `wasm_import_module` is incompatible with other arguments
extern "C" {}

#[link(wasm_import_module = "foo", kind = "dylib")] //~ ERROR: `wasm_import_module` is incompatible with other arguments
extern "C" {}

#[link(wasm_import_module = "foo", cfg(FALSE))] //~ ERROR: `wasm_import_module` is incompatible with other arguments
extern "C" {}

fn main() {}
32 changes: 25 additions & 7 deletions src/test/ui/wasm/wasm-import-module.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
error: must be of the form `#[link(wasm_import_module = "...")]`
--> $DIR/wasm-import-module.rs:1:22
error: wasm import module must be of the form `wasm_import_module = "string"`
--> $DIR/wasm-import-module.rs:3:22
|
LL | #[link(name = "...", wasm_import_module)]
| ^^^^^^^^^^^^^^^^^^

error: must be of the form `#[link(wasm_import_module = "...")]`
--> $DIR/wasm-import-module.rs:4:22
error: wasm import module must be of the form `wasm_import_module = "string"`
--> $DIR/wasm-import-module.rs:6:22
|
LL | #[link(name = "...", wasm_import_module(x))]
| ^^^^^^^^^^^^^^^^^^^^^

error: must be of the form `#[link(wasm_import_module = "...")]`
--> $DIR/wasm-import-module.rs:7:22
error: wasm import module must be of the form `wasm_import_module = "string"`
--> $DIR/wasm-import-module.rs:9:22
|
LL | #[link(name = "...", wasm_import_module())]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors
error: `wasm_import_module` is incompatible with other arguments in `#[link]` attributes
--> $DIR/wasm-import-module.rs:12:8
|
LL | #[link(wasm_import_module = "foo", name = "bar")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `wasm_import_module` is incompatible with other arguments in `#[link]` attributes
--> $DIR/wasm-import-module.rs:15:8
|
LL | #[link(wasm_import_module = "foo", kind = "dylib")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `wasm_import_module` is incompatible with other arguments in `#[link]` attributes
--> $DIR/wasm-import-module.rs:18:8
|
LL | #[link(wasm_import_module = "foo", cfg(FALSE))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 6 previous errors

0 comments on commit 10b3a0d

Please sign in to comment.