Skip to content
Merged
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
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) mod loop_match;
pub(crate) mod macro_attrs;
pub(crate) mod must_use;
pub(crate) mod no_implicit_prelude;
pub(crate) mod no_link;
pub(crate) mod non_exhaustive;
pub(crate) mod path;
pub(crate) mod pin_v2;
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/no_link.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use super::prelude::*;

pub(crate) struct NoLinkParser;
impl<S: Stage> NoArgsAttributeParser<S> for NoLinkParser {
const PATH: &[Symbol] = &[sym::no_link];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
Allow(Target::ExternCrate),
Warn(Target::Field),
Warn(Target::Arm),
Warn(Target::MacroDef),
]);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoLink;
}
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use crate::attributes::macro_attrs::{
};
use crate::attributes::must_use::MustUseParser;
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
use crate::attributes::no_link::NoLinkParser;
use crate::attributes::non_exhaustive::NonExhaustiveParser;
use crate::attributes::path::PathParser as PathAttributeParser;
use crate::attributes::pin_v2::PinV2Parser;
Expand Down Expand Up @@ -240,6 +241,7 @@ attribute_parsers!(
Single<WithoutArgs<MayDangleParser>>,
Single<WithoutArgs<NoCoreParser>>,
Single<WithoutArgs<NoImplicitPreludeParser>>,
Single<WithoutArgs<NoLinkParser>>,
Single<WithoutArgs<NoMangleParser>>,
Single<WithoutArgs<NoStdParser>>,
Single<WithoutArgs<NonExhaustiveParser>>,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,9 @@ pub enum AttributeKind {
/// Represents `#[no_implicit_prelude]`
NoImplicitPrelude(Span),

/// Represents `#[no_link]`
NoLink,

/// Represents `#[no_mangle]`
NoMangle(Span),

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/attrs/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl AttributeKind {
Naked(..) => No,
NoCore(..) => No,
NoImplicitPrelude(..) => No,
NoLink => No,
NoMangle(..) => Yes, // Needed for rustdoc
NoStd(..) => No,
NonExhaustive(..) => Yes, // Needed for rustdoc
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,6 @@ passes_has_incoherent_inherent_impl =
`rustc_has_incoherent_inherent_impls` attribute should be applied to types or traits
.label = only adts, extern types and traits are supported

passes_ignored_attr_with_macro =
`#[{$sym}]` is ignored on struct fields, match arms and macro defs
.warn = {-passes_previously_accepted}
.note = {-passes_see_issue(issue: "80564")}

passes_ignored_derived_impls =
`{$name}` has {$trait_list_len ->
[one] a derived impl
Expand Down Expand Up @@ -379,10 +374,6 @@ passes_must_not_suspend =
`must_not_suspend` attribute should be applied to a struct, enum, union, or trait
.label = is not a struct, enum, union, or trait

passes_no_link =
attribute should be applied to an `extern crate` item
.label = not an `extern crate` item

passes_no_main_function =
`main` function not found in crate `{$crate_name}`
.here_is_main = here is a function named `main`
Expand Down
28 changes: 1 addition & 27 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::LinkSection { .. }
| AttributeKind::MacroUse { .. }
| AttributeKind::MacroEscape( .. )
| AttributeKind::NoLink
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
| AttributeKind::RustcScalableVector { .. }
Expand Down Expand Up @@ -299,7 +300,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_diagnostic_on_const(attr.span(), hir_id, target, item)
}
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
[sym::rustc_no_implicit_autorefs, ..] => {
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
}
Expand Down Expand Up @@ -456,15 +456,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_mix_no_mangle_export(hir_id, attrs);
}

fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr_span: Span, sym: &str) {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr_span,
errors::IgnoredAttrWithMacro { sym },
);
}

fn check_eii_impl(&self, impls: &[EiiImpl], target: Target) {
for EiiImpl { span, inner_span, eii_macro, impl_marked_unsafe, is_default: _ } in impls {
match target {
Expand Down Expand Up @@ -1198,23 +1189,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
);
}

/// Checks if `#[no_link]` is applied to an `extern crate`.
fn check_no_link(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
match target {
Target::ExternCrate => {}
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
// `#[no_link]` attribute with just a lint, because we previously
// erroneously allowed it and some crates used it accidentally, to be compatible
// with crates depending on them, we can't throw an error here.
Target::Field | Target::Arm | Target::MacroDef => {
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "no_link");
}
_ => {
self.dcx().emit_err(errors::NoLink { attr_span: attr.span(), span });
}
}
}

/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
fn check_rustc_legacy_const_generics(
&self,
Expand Down
15 changes: 0 additions & 15 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ pub(crate) struct OuterCrateLevelAttrSuggestion {
#[diag(passes_inner_crate_level_attr)]
pub(crate) struct InnerCrateLevelAttr;

#[derive(LintDiagnostic)]
#[diag(passes_ignored_attr_with_macro)]
pub(crate) struct IgnoredAttrWithMacro<'a> {
pub sym: &'a str,
}

#[derive(Diagnostic)]
#[diag(passes_should_be_applied_to_fn)]
pub(crate) struct AttrShouldBeAppliedToFn {
Expand Down Expand Up @@ -254,15 +248,6 @@ pub(crate) struct Link {
pub span: Option<Span>,
}

#[derive(Diagnostic)]
#[diag(passes_no_link)]
pub(crate) struct NoLink {
#[primary_span]
pub attr_span: Span,
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_rustc_legacy_const_generics_only)]
pub(crate) struct RustcLegacyConstGenericsOnly {
Expand Down
17 changes: 9 additions & 8 deletions tests/ui/attributes/malformed-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,6 @@ error: malformed `thread_local` attribute input
LL | #[thread_local()]
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[thread_local]`

error: malformed `no_link` attribute input
--> $DIR/malformed-attrs.rs:214:1
|
LL | #[no_link()]
| ^^^^^^^^^^^^ help: must be of the form: `#[no_link]`
|
= note: for more information, visit <https://doc.rust-lang.org/reference/items/extern-crates.html#the-no_link-attribute>

error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
--> $DIR/malformed-attrs.rs:105:1
|
Expand Down Expand Up @@ -626,6 +618,15 @@ LL | #[non_exhaustive = 1]
| | didn't expect any arguments here
| help: must be of the form: `#[non_exhaustive]`

error[E0565]: malformed `no_link` attribute input
--> $DIR/malformed-attrs.rs:214:1
|
LL | #[no_link()]
| ^^^^^^^^^--^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[no_link]`

error[E0539]: malformed `macro_use` attribute input
--> $DIR/malformed-attrs.rs:216:1
|
Expand Down
46 changes: 28 additions & 18 deletions tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//~ NOTE: not an `extern crate` item
// This is testing whether various builtin attributes signals an
// error or warning when put in "weird" places.
//
Expand Down Expand Up @@ -29,7 +28,7 @@
//~| WARN cannot be used on crates
//~| WARN previously accepted
#![no_link]
//~^ ERROR: attribute should be applied to an `extern crate` item
//~^ ERROR: `#[no_link]` attribute cannot be used on crates
#![export_name = "2200"]
//~^ ERROR: attribute cannot be used on
//~| NOTE takes precedence
Expand Down Expand Up @@ -60,29 +59,40 @@ mod inline {
}

#[no_link]
//~^ ERROR attribute should be applied to an `extern crate` item
//~^ ERROR `#[no_link]` attribute cannot be used on modules
mod no_link {
//~^ NOTE not an `extern crate` item

mod inner { #![no_link] }
//~^ ERROR attribute should be applied to an `extern crate` item
//~| NOTE not an `extern crate` item

#[no_link] fn f() { }
//~^ ERROR attribute should be applied to an `extern crate` item
//~| NOTE not an `extern crate` item
//~^ ERROR `#[no_link]` attribute cannot be used on modules

#[no_link] fn f() {
//~^ ERROR `#[no_link]` attribute cannot be used on functions
match () {
#[no_link]
//~^ WARN `#[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!
_ => ()
}
}

#[no_link] struct S;
//~^ ERROR attribute should be applied to an `extern crate` item
//~| NOTE not an `extern crate` item
#[no_link]
//~^ ERROR `#[no_link]` attribute cannot be used on structs
struct S {
#[no_link]
//~^ WARN `#[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!
field: ()
}

#[no_link]type T = S;
//~^ ERROR attribute should be applied to an `extern crate` item
//~| NOTE not an `extern crate` item
//~^ ERROR `#[no_link]` attribute cannot be used on type aliases

#[no_link] impl S { }
//~^ ERROR attribute should be applied to an `extern crate` item
//~| NOTE not an `extern crate` item
//~^ ERROR `#[no_link]` attribute cannot be used on inherent impl blocks

#[no_link]
//~^ WARN `#[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!
macro_rules! m{() => {}}
}

#[export_name = "2200"]
Expand Down
Loading
Loading