Skip to content

Commit 656d4e8

Browse files
authored
Rollup merge of #150072 - Bryntet:parse_no_link, r=JonathanBrouwer
Port #[no_link] to use attribute parser Adds `#[no_link]` to the attribute parser, as well as adds tests making sure to FCW warn on `field`, `arm`, and `macrodef `
2 parents 1a7df03 + b09a9d0 commit 656d4e8

File tree

12 files changed

+190
-175
lines changed

12 files changed

+190
-175
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::Warn;
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;
@@ -241,6 +242,7 @@ attribute_parsers!(
241242
Single<WithoutArgs<MayDangleParser>>,
242243
Single<WithoutArgs<NoCoreParser>>,
243244
Single<WithoutArgs<NoImplicitPreludeParser>>,
245+
Single<WithoutArgs<NoLinkParser>>,
244246
Single<WithoutArgs<NoMangleParser>>,
245247
Single<WithoutArgs<NoStdParser>>,
246248
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_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
@@ -254,6 +254,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
254254
| AttributeKind::LinkSection { .. }
255255
| AttributeKind::MacroUse { .. }
256256
| AttributeKind::MacroEscape( .. )
257+
| AttributeKind::NoLink
257258
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
258259
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
259260
| AttributeKind::RustcScalableVector { .. }
@@ -303,7 +304,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
303304
self.check_diagnostic_on_const(attr.span(), hir_id, target, item)
304305
}
305306
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
306-
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
307307
[sym::rustc_no_implicit_autorefs, ..] => {
308308
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
309309
}
@@ -457,15 +457,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
457457
self.check_mix_no_mangle_export(hir_id, attrs);
458458
}
459459

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

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

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//~ NOTE: not an `extern crate` item
21
// This is testing whether various builtin attributes signals an
32
// error or warning when put in "weird" places.
43
//
@@ -29,7 +28,7 @@
2928
//~| WARN cannot be used on crates
3029
//~| WARN previously accepted
3130
#![no_link]
32-
//~^ ERROR: attribute should be applied to an `extern crate` item
31+
//~^ ERROR: `#[no_link]` attribute cannot be used on crates
3332
#![export_name = "2200"]
3433
//~^ ERROR: attribute cannot be used on
3534
//~| NOTE takes precedence
@@ -60,29 +59,40 @@ mod inline {
6059
}
6160

6261
#[no_link]
63-
//~^ ERROR attribute should be applied to an `extern crate` item
62+
//~^ ERROR `#[no_link]` attribute cannot be used on modules
6463
mod no_link {
65-
//~^ NOTE not an `extern crate` item
66-
6764
mod inner { #![no_link] }
68-
//~^ ERROR attribute should be applied to an `extern crate` item
69-
//~| NOTE not an `extern crate` item
70-
71-
#[no_link] fn f() { }
72-
//~^ ERROR attribute should be applied to an `extern crate` item
73-
//~| NOTE not an `extern crate` item
65+
//~^ ERROR `#[no_link]` attribute cannot be used on modules
66+
67+
#[no_link] fn f() {
68+
//~^ ERROR `#[no_link]` attribute cannot be used on functions
69+
match () {
70+
#[no_link]
71+
//~^ WARN `#[no_link]` attribute cannot be used on match arms [unused_attributes]
72+
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
73+
_ => ()
74+
}
75+
}
7476

75-
#[no_link] struct S;
76-
//~^ ERROR attribute should be applied to an `extern crate` item
77-
//~| NOTE not an `extern crate` item
77+
#[no_link]
78+
//~^ ERROR `#[no_link]` attribute cannot be used on structs
79+
struct S {
80+
#[no_link]
81+
//~^ WARN `#[no_link]` attribute cannot be used on struct fields [unused_attributes]
82+
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
83+
field: ()
84+
}
7885

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

8389
#[no_link] impl S { }
84-
//~^ ERROR attribute should be applied to an `extern crate` item
85-
//~| NOTE not an `extern crate` item
90+
//~^ ERROR `#[no_link]` attribute cannot be used on inherent impl blocks
91+
92+
#[no_link]
93+
//~^ WARN `#[no_link]` attribute cannot be used on macro defs
94+
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
95+
macro_rules! m{() => {}}
8696
}
8797

8898
#[export_name = "2200"]

0 commit comments

Comments
 (0)