Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/no_link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use rustc_feature::AttributeStability;
use rustc_lint_defs::builtin::NO_LINK_ATTRIBUTES;

use super::prelude::*;
use crate::diagnostics::NoLinkAttrUsed;

pub(crate) struct NoLinkParser;
impl NoArgsAttributeParser for NoLinkParser {
Expand All @@ -14,4 +16,8 @@ impl NoArgsAttributeParser for NoLinkParser {
]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoLink;

fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
cx.emit_lint(NO_LINK_ATTRIBUTES, NoLinkAttrUsed, attr_span);
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_attr_parsing/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,3 +2132,7 @@ pub(crate) enum MacroExport {
#[note("declarative macros follow the same exporting rules as regular items")]
OnDeclMacro,
}

#[derive(Diagnostic)]
#[diag("use of deprecated `no_link` attribute")]
pub(crate) struct NoLinkAttrUsed;
33 changes: 33 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub mod hardwired {
NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
NON_CONTIGUOUS_RANGE_ENDPOINTS,
NON_EXHAUSTIVE_OMITTED_PATTERNS,
NO_LINK_ATTRIBUTES,
OUT_OF_SCOPE_MACRO_CALLS,
OVERLAPPING_RANGE_ENDPOINTS,
PARTIAL_STACK_PROTECTOR,
Expand Down Expand Up @@ -6019,3 +6020,35 @@ declare_lint! {
r#"detects default field values on types that have const parameters"#,
@feature_gate = default_field_values;
}

declare_lint! {
/// The `no_link_attributes` lint detects that you used `#[no_link]` on an
/// `extern crate` item.
///
/// ### Example
///
/// ```rust,compile_fail
/// #[no_link]
/// extern crate std;
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// This is a pre-1.0 attribute that accidentally got stabilized. It was only meant
/// for compiler plugins which we no longer support, not for regular crates. It is
/// rather fragile and can easily lead to linker errors depending on where rustc
/// decides to codegen functions and statics, which varies between rustc versions. It
/// might also be unsound if a crate for example expects a global constructor it
/// defined to run. Using it doesn't have an effect on the size of the produced
/// binaries except when it defines a `#[used]` static given that we remove all unused
/// functions and statics at link time already.
pub NO_LINK_ATTRIBUTES,
Deny,
"detects usage of `#[no_link]` on `extern crate` items",
@future_incompatible = FutureIncompatibleInfo {
reason: fcw!(FutureReleaseError #164032),
report_in_deps: true,
};
}
2 changes: 2 additions & 0 deletions tests/ui/attributes/malformed-attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ static mut TLS: u8 = 42;

#[no_link()]
//~^ ERROR malformed
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
#[macro_use = 1]
//~^ ERROR malformed
extern crate wloop;
Expand Down
37 changes: 29 additions & 8 deletions tests/ui/attributes/malformed-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LL | #[cfg_attr(predicate, attr1, attr2, ...)]
| ++++++++++++++++++++++++++++++

error[E0463]: can't find crate for `wloop`
--> $DIR/malformed-attrs.rs:212:1
--> $DIR/malformed-attrs.rs:214:1
|
LL | extern crate wloop;
| ^^^^^^^^^^^^^^^^^^^ can't find crate
Expand Down Expand Up @@ -783,7 +783,7 @@ LL + #[no_link]
|

error[E0539]: malformed `macro_use` attribute input
--> $DIR/malformed-attrs.rs:210:3
--> $DIR/malformed-attrs.rs:212:3
|
LL | #[macro_use = 1]
| ^^^^^^^^^^---
Expand All @@ -801,7 +801,7 @@ LL + #[macro_use(name1, name2, ...)]
|

error[E0539]: malformed `macro_export` attribute input
--> $DIR/malformed-attrs.rs:215:3
--> $DIR/malformed-attrs.rs:217:3
|
LL | #[macro_export = 18]
| ^^^^^^^^^^^^^----
Expand All @@ -818,7 +818,7 @@ LL + #[macro_export(local_inner_macros)]
|

error[E0658]: the `allow_internal_unsafe` attribute side-steps the `unsafe_code` lint
--> $DIR/malformed-attrs.rs:217:3
--> $DIR/malformed-attrs.rs:219:3
|
LL | #[allow_internal_unsafe = 1]
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -827,7 +827,7 @@ LL | #[allow_internal_unsafe = 1]
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0565]: malformed `allow_internal_unsafe` attribute input
--> $DIR/malformed-attrs.rs:217:3
--> $DIR/malformed-attrs.rs:219:3
|
LL | #[allow_internal_unsafe = 1]
| ^^^^^^^^^^^^^^^^^^^^^^---
Expand Down Expand Up @@ -964,8 +964,18 @@ LL | #[automatically_derived = 18]
= help: the `automatically_derived` attribute can only be applied to trait impl blocks
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

error: use of deprecated `no_link` attribute
--> $DIR/malformed-attrs.rs:208:1
|
LL | #[no_link()]
| ^^^^^^^^^^^^
|
= 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 #164032 <https://github.com/rust-lang/rust/issues/164032>
= note: `#[deny(no_link_attributes)]` (part of `#[deny(future_incompatible)]`) on by default

error: valid forms for the attribute are `ignore` and `ignore = "reason"`
--> $DIR/malformed-attrs.rs:224:3
--> $DIR/malformed-attrs.rs:226:3
|
LL | #[ignore = 1]
| ^^^^^^^^^^
Expand All @@ -984,7 +994,7 @@ LL | #[coroutine = 63] || {}
= note: expected unit type `()`
found coroutine `{coroutine@$DIR/malformed-attrs.rs:117:23: 117:25}`

error: aborting due to 74 previous errors; 8 warnings emitted
error: aborting due to 75 previous errors; 8 warnings emitted

Some errors have detailed explanations: E0308, E0463, E0539, E0565, E0658, E0805.
For more information about an error, try `rustc --explain E0308`.
Expand All @@ -1010,9 +1020,20 @@ LL | #[ignore()]
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

Future breakage diagnostic:
error: use of deprecated `no_link` attribute
--> $DIR/malformed-attrs.rs:208:1
|
LL | #[no_link()]
| ^^^^^^^^^^^^
|
= 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 #164032 <https://github.com/rust-lang/rust/issues/164032>
= note: `#[deny(no_link_attributes)]` (part of `#[deny(future_incompatible)]`) on by default

Future breakage diagnostic:
error: valid forms for the attribute are `ignore` and `ignore = "reason"`
--> $DIR/malformed-attrs.rs:224:3
--> $DIR/malformed-attrs.rs:226:3
|
LL | #[ignore = 1]
| ^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui/attributes/no_link/auxiliary/no_link-crate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ no-prefer-dynamic

#![crate_type = "rlib"]
#![warn(no_link_attributes)]

#[macro_use] #[no_link] extern crate empty_crate_1 as t1;
#[macro_use] extern crate empty_crate_2 as t2;
4 changes: 4 additions & 0 deletions tests/ui/attributes/no_link/no-link-struct.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//@ check-pass
//@ aux-build:empty-struct.rs

#![warn(no_link_attributes)]

#[no_link]
//~^ WARN use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
extern crate empty_struct;

fn main() {
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/attributes/no_link/no-link-struct.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
warning: use of deprecated `no_link` attribute
--> $DIR/no-link-struct.rs:6:1
|
LL | #[no_link]
| ^^^^^^^^^^
|
= 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 #164032 <https://github.com/rust-lang/rust/issues/164032>
note: the lint level is defined here
--> $DIR/no-link-struct.rs:4:9
|
LL | #![warn(no_link_attributes)]
| ^^^^^^^^^^^^^^^^^^

warning: 1 warning emitted

Future incompatibility report: Future breakage diagnostic:
warning: use of deprecated `no_link` attribute
--> $DIR/no-link-struct.rs:6:1
|
LL | #[no_link]
| ^^^^^^^^^^
|
= 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 #164032 <https://github.com/rust-lang/rust/issues/164032>
note: the lint level is defined here
--> $DIR/no-link-struct.rs:4:9
|
LL | #![warn(no_link_attributes)]
| ^^^^^^^^^^^^^^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
//~| WARN previously accepted
#![no_link]
//~^ ERROR: the `no_link` attribute cannot be used on crates
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE `#[deny(no_link_attributes)]` (part of `#[deny(future_incompatible)]`) on by default
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>
#![export_name = "2200"]
//~^ ERROR: attribute cannot be used on
//~| NOTE takes precedence
Expand Down Expand Up @@ -58,38 +62,65 @@ mod inline {

#[no_link]
//~^ ERROR the `no_link` attribute cannot be used on modules
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>
mod no_link {
mod inner { #![no_link] }
//~^ ERROR the `no_link` attribute cannot be used on modules
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>

#[no_link] fn f() {
//~^ ERROR the `no_link` attribute cannot be used on functions
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>
match () {
#[no_link]
//~^ WARN the `no_link` attribute cannot be used on match arms [unused_attributes]
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>
_ => ()
}
}

#[no_link]
//~^ ERROR the `no_link` attribute cannot be used on structs
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>
struct S {
#[no_link]
//~^ WARN the `no_link` attribute cannot be used on struct fields [unused_attributes]
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>
field: ()
}

#[no_link]type T = S;
//~^ ERROR the `no_link` attribute cannot be used on type aliases
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>

#[no_link] impl S { }
//~^ ERROR the `no_link` attribute cannot be used on inherent impl blocks
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>

#[no_link]
//~^ WARN the `no_link` attribute cannot be used on macro defs
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
//~| ERROR use of deprecated `no_link` attribute
//~| WARN this was previously accepted by the compiler
//~| NOTE for more information, see issue #164032 <https://github.com/rust-lang/rust/issues/164032>
macro_rules! m{() => {}}
}

Expand Down
Loading
Loading