Skip to content

Commit 09f601b

Browse files
committed
Port #[no_link] to use attribute parser
1 parent 95a27ad commit 09f601b

File tree

14 files changed

+134
-164
lines changed

14 files changed

+134
-164
lines changed

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub(crate) mod loop_match;
4747
pub(crate) mod macro_attrs;
4848
pub(crate) mod must_use;
4949
pub(crate) mod no_implicit_prelude;
50+
pub(crate) mod no_link;
5051
pub(crate) mod non_exhaustive;
5152
pub(crate) mod path;
5253
pub(crate) mod pin_v2;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::prelude::*;
2+
3+
pub(crate) struct NoLinkParser;
4+
impl<S: Stage> NoArgsAttributeParser<S> for NoLinkParser {
5+
const PATH: &[Symbol] = &[sym::no_link];
6+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
7+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
8+
Allow(Target::ExternCrate),
9+
Warn(Target::Field),
10+
Warn(Target::Arm),
11+
Warn(Target::MacroDef),
12+
]);
13+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoLink;
14+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::attributes::macro_attrs::{
5050
};
5151
use crate::attributes::must_use::MustUseParser;
5252
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
53+
use crate::attributes::no_link::NoLinkParser;
5354
use crate::attributes::non_exhaustive::NonExhaustiveParser;
5455
use crate::attributes::path::PathParser as PathAttributeParser;
5556
use crate::attributes::pin_v2::PinV2Parser;
@@ -240,6 +241,7 @@ attribute_parsers!(
240241
Single<WithoutArgs<MayDangleParser>>,
241242
Single<WithoutArgs<NoCoreParser>>,
242243
Single<WithoutArgs<NoImplicitPreludeParser>>,
244+
Single<WithoutArgs<NoLinkParser>>,
243245
Single<WithoutArgs<NoMangleParser>>,
244246
Single<WithoutArgs<NoStdParser>>,
245247
Single<WithoutArgs<NonExhaustiveParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ pub enum AttributeKind {
803803
/// Represents `#[no_implicit_prelude]`
804804
NoImplicitPrelude(Span),
805805

806+
/// Represents `#[no_link]`
807+
NoLink,
808+
806809
/// Represents `#[no_mangle]`
807810
NoMangle(Span),
808811

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl AttributeKind {
7070
Naked(..) => No,
7171
NoCore(..) => No,
7272
NoImplicitPrelude(..) => No,
73+
NoLink => No,
7374
NoMangle(..) => Yes, // Needed for rustdoc
7475
NoStd(..) => No,
7576
NonExhaustive(..) => Yes, // Needed for rustdoc

compiler/rustc_metadata/src/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
1515
use rustc_data_structures::unord::UnordMap;
1616
use rustc_expand::base::SyntaxExtension;
1717
use rustc_fs_util::try_canonicalize;
18-
use rustc_hir as hir;
1918
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE, LocalDefId, StableCrateId};
2019
use rustc_hir::definitions::Definitions;
20+
use rustc_hir::{self as hir};
2121
use rustc_index::IndexVec;
2222
use rustc_middle::bug;
2323
use rustc_middle::ty::data_structures::IndexSet;

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,6 @@ passes_has_incoherent_inherent_impl =
240240
`rustc_has_incoherent_inherent_impls` attribute should be applied to types or traits
241241
.label = only adts, extern types and traits are supported
242242
243-
passes_ignored_attr_with_macro =
244-
`#[{$sym}]` is ignored on struct fields, match arms and macro defs
245-
.warn = {-passes_previously_accepted}
246-
.note = {-passes_see_issue(issue: "80564")}
247-
248243
passes_ignored_derived_impls =
249244
`{$name}` has {$trait_list_len ->
250245
[one] a derived impl
@@ -379,10 +374,6 @@ passes_must_not_suspend =
379374
`must_not_suspend` attribute should be applied to a struct, enum, union, or trait
380375
.label = is not a struct, enum, union, or trait
381376
382-
passes_no_link =
383-
attribute should be applied to an `extern crate` item
384-
.label = not an `extern crate` item
385-
386377
passes_no_main_function =
387378
`main` function not found in crate `{$crate_name}`
388379
.here_is_main = here is a function named `main`

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
250250
| AttributeKind::LinkSection { .. }
251251
| AttributeKind::MacroUse { .. }
252252
| AttributeKind::MacroEscape( .. )
253+
| AttributeKind::NoLink
253254
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
254255
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
255256
| AttributeKind::RustcScalableVector { .. }
@@ -299,7 +300,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
299300
self.check_diagnostic_on_const(attr.span(), hir_id, target, item)
300301
}
301302
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
302-
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
303303
[sym::rustc_no_implicit_autorefs, ..] => {
304304
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
305305
}
@@ -456,15 +456,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
456456
self.check_mix_no_mangle_export(hir_id, attrs);
457457
}
458458

459-
fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr_span: Span, sym: &str) {
460-
self.tcx.emit_node_span_lint(
461-
UNUSED_ATTRIBUTES,
462-
hir_id,
463-
attr_span,
464-
errors::IgnoredAttrWithMacro { sym },
465-
);
466-
}
467-
468459
fn check_eii_impl(&self, impls: &[EiiImpl], target: Target) {
469460
for EiiImpl { span, inner_span, eii_macro, impl_marked_unsafe, is_default: _ } in impls {
470461
match target {
@@ -1198,23 +1189,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11981189
);
11991190
}
12001191

1201-
/// Checks if `#[no_link]` is applied to an `extern crate`.
1202-
fn check_no_link(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
1203-
match target {
1204-
Target::ExternCrate => {}
1205-
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
1206-
// `#[no_link]` attribute with just a lint, because we previously
1207-
// erroneously allowed it and some crates used it accidentally, to be compatible
1208-
// with crates depending on them, we can't throw an error here.
1209-
Target::Field | Target::Arm | Target::MacroDef => {
1210-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "no_link");
1211-
}
1212-
_ => {
1213-
self.dcx().emit_err(errors::NoLink { attr_span: attr.span(), span });
1214-
}
1215-
}
1216-
}
1217-
12181192
/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
12191193
fn check_rustc_legacy_const_generics(
12201194
&self,

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ pub(crate) struct OuterCrateLevelAttrSuggestion {
8080
#[diag(passes_inner_crate_level_attr)]
8181
pub(crate) struct InnerCrateLevelAttr;
8282

83-
#[derive(LintDiagnostic)]
84-
#[diag(passes_ignored_attr_with_macro)]
85-
pub(crate) struct IgnoredAttrWithMacro<'a> {
86-
pub sym: &'a str,
87-
}
88-
8983
#[derive(Diagnostic)]
9084
#[diag(passes_should_be_applied_to_fn)]
9185
pub(crate) struct AttrShouldBeAppliedToFn {
@@ -254,15 +248,6 @@ pub(crate) struct Link {
254248
pub span: Option<Span>,
255249
}
256250

257-
#[derive(Diagnostic)]
258-
#[diag(passes_no_link)]
259-
pub(crate) struct NoLink {
260-
#[primary_span]
261-
pub attr_span: Span,
262-
#[label]
263-
pub span: Span,
264-
}
265-
266251
#[derive(Diagnostic)]
267252
#[diag(passes_rustc_legacy_const_generics_only)]
268253
pub(crate) struct RustcLegacyConstGenericsOnly {

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,6 @@ error: malformed `thread_local` attribute input
147147
LL | #[thread_local()]
148148
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[thread_local]`
149149

150-
error: malformed `no_link` attribute input
151-
--> $DIR/malformed-attrs.rs:214:1
152-
|
153-
LL | #[no_link()]
154-
| ^^^^^^^^^^^^ help: must be of the form: `#[no_link]`
155-
|
156-
= note: for more information, visit <https://doc.rust-lang.org/reference/items/extern-crates.html#the-no_link-attribute>
157-
158150
error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
159151
--> $DIR/malformed-attrs.rs:105:1
160152
|
@@ -626,6 +618,15 @@ LL | #[non_exhaustive = 1]
626618
| | didn't expect any arguments here
627619
| help: must be of the form: `#[non_exhaustive]`
628620

621+
error[E0565]: malformed `no_link` attribute input
622+
--> $DIR/malformed-attrs.rs:214:1
623+
|
624+
LL | #[no_link()]
625+
| ^^^^^^^^^--^
626+
| | |
627+
| | didn't expect any arguments here
628+
| help: must be of the form: `#[no_link]`
629+
629630
error[E0539]: malformed `macro_use` attribute input
630631
--> $DIR/malformed-attrs.rs:216:1
631632
|

0 commit comments

Comments
 (0)