Skip to content

Commit 5fc48ff

Browse files
authored
Rollup merge of #143138 - JonathanBrouwer:link_name_parser, r=jdonszelmann
Port `#[link_name]` to the new attribute parsing infrastructure Ports `link_name` to the new attribute parsing infrastructure for #131229 (comment) r? `@jdonszelmann`
2 parents 170fd43 + 1249c14 commit 5fc48ff

File tree

14 files changed

+96
-70
lines changed

14 files changed

+96
-70
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ pub enum AttributeKind {
253253
/// Represents `#[inline]` and `#[rustc_force_inline]`.
254254
Inline(InlineAttr, Span),
255255

256+
/// Represents `#[link_name]`.
257+
LinkName { name: Symbol, span: Span },
258+
256259
/// Represents `#[loop_match]`.
257260
LoopMatch(Span),
258261

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl AttributeKind {
2929
Stability { .. } => Yes,
3030
Cold(..) => No,
3131
ConstContinue(..) => No,
32+
LinkName { .. } => Yes,
3233
LoopMatch(..) => No,
3334
MayDangle(..) => No,
3435
MustUse { .. } => Yes,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_attr_data_structures::AttributeKind::LinkName;
3+
use rustc_feature::{AttributeTemplate, template};
4+
use rustc_span::{Symbol, sym};
5+
6+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7+
use crate::context::{AcceptContext, Stage};
8+
use crate::parser::ArgParser;
9+
10+
pub(crate) struct LinkNameParser;
11+
12+
impl<S: Stage> SingleAttributeParser<S> for LinkNameParser {
13+
const PATH: &[Symbol] = &[sym::link_name];
14+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
15+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
16+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
17+
18+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
19+
let Some(nv) = args.name_value() else {
20+
cx.expected_name_value(cx.attr_span, None);
21+
return None;
22+
};
23+
let Some(name) = nv.value_as_str() else {
24+
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
25+
return None;
26+
};
27+
28+
Some(LinkName { name, span: cx.attr_span })
29+
}
30+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(crate) mod codegen_attrs;
3131
pub(crate) mod confusables;
3232
pub(crate) mod deprecation;
3333
pub(crate) mod inline;
34+
pub(crate) mod link_attrs;
3435
pub(crate) mod lint_helpers;
3536
pub(crate) mod loop_match;
3637
pub(crate) mod must_use;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::attributes::codegen_attrs::{
2222
use crate::attributes::confusables::ConfusablesParser;
2323
use crate::attributes::deprecation::DeprecationParser;
2424
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
25+
use crate::attributes::link_attrs::LinkNameParser;
2526
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2627
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2728
use crate::attributes::must_use::MustUseParser;
@@ -121,6 +122,7 @@ attribute_parsers!(
121122
Single<DeprecationParser>,
122123
Single<ExportNameParser>,
123124
Single<InlineParser>,
125+
Single<LinkNameParser>,
124126
Single<LoopMatchParser>,
125127
Single<MayDangleParser>,
126128
Single<MustUseParser>,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
123123
}
124124
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
125125
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
126+
AttributeKind::LinkName { name, .. } => codegen_fn_attrs.link_name = Some(*name),
126127
AttributeKind::NoMangle(attr_span) => {
127128
if tcx.opt_item_name(did.to_def_id()).is_some() {
128129
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
@@ -262,7 +263,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
262263
}
263264
}
264265
}
265-
sym::link_name => codegen_fn_attrs.link_name = attr.value_str(),
266266
sym::link_ordinal => {
267267
link_ordinal_span = Some(attr.span());
268268
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {

compiler/rustc_lint/src/foreign_modules.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use rustc_abi::FIRST_VARIANT;
2+
use rustc_attr_data_structures::{AttributeKind, find_attr};
23
use rustc_data_structures::stack::ensure_sufficient_stack;
34
use rustc_data_structures::unord::{UnordMap, UnordSet};
45
use rustc_hir as hir;
56
use rustc_hir::def::DefKind;
67
use rustc_middle::query::Providers;
78
use rustc_middle::ty::{self, AdtDef, Instance, Ty, TyCtxt};
89
use rustc_session::declare_lint;
9-
use rustc_span::{Span, Symbol, sym};
10+
use rustc_span::{Span, Symbol};
1011
use tracing::{debug, instrument};
1112

1213
use crate::lints::{BuiltinClashingExtern, BuiltinClashingExternSub};
@@ -182,7 +183,11 @@ fn name_of_extern_decl(tcx: TyCtxt<'_>, fi: hir::OwnerId) -> SymbolName {
182183
// information, we could have codegen_fn_attrs also give span information back for
183184
// where the attribute was defined. However, until this is found to be a
184185
// bottleneck, this does just fine.
185-
(overridden_link_name, tcx.get_attr(fi, sym::link_name).unwrap().span())
186+
(
187+
overridden_link_name,
188+
find_attr!(tcx.get_all_attrs(fi), AttributeKind::LinkName {span, ..} => *span)
189+
.unwrap(),
190+
)
186191
})
187192
{
188193
SymbolName::Link(overridden_link_name, overridden_link_name_span)

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ fn emit_malformed_attribute(
302302
| sym::no_mangle
303303
| sym::must_use
304304
| sym::track_caller
305+
| sym::link_name
305306
) {
306307
return;
307308
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
191191
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => {
192192
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
193193
}
194+
Attribute::Parsed(AttributeKind::LinkName { span: attr_span, name }) => {
195+
self.check_link_name(hir_id, *attr_span, *name, span, target)
196+
}
194197
Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
195198
self.check_may_dangle(hir_id, *attr_span)
196199
}
@@ -283,7 +286,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
283286
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
284287
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
285288
[sym::link, ..] => self.check_link(hir_id, attr, span, target),
286-
[sym::link_name, ..] => self.check_link_name(hir_id, attr, span, target),
287289
[sym::link_section, ..] => self.check_link_section(hir_id, attr, span, target),
288290
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
289291
self.check_macro_use(hir_id, attr, target)
@@ -1604,35 +1606,33 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16041606
}
16051607

16061608
/// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
1607-
fn check_link_name(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
1609+
fn check_link_name(
1610+
&self,
1611+
hir_id: HirId,
1612+
attr_span: Span,
1613+
name: Symbol,
1614+
span: Span,
1615+
target: Target,
1616+
) {
16081617
match target {
16091618
Target::ForeignFn | Target::ForeignStatic => {}
16101619
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
16111620
// `#[link_name]` attribute with just a lint, because we previously
16121621
// erroneously allowed it and some crates used it accidentally, to be compatible
16131622
// with crates depending on them, we can't throw an error here.
16141623
Target::Field | Target::Arm | Target::MacroDef => {
1615-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "link_name");
1624+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "link_name");
16161625
}
16171626
_ => {
1618-
// FIXME: #[cold] was previously allowed on non-functions/statics and some crates
1627+
// FIXME: #[link_name] was previously allowed on non-functions/statics and some crates
16191628
// used this, so only emit a warning.
1620-
let attr_span = matches!(target, Target::ForeignMod).then_some(attr.span());
1621-
if let Some(s) = attr.value_str() {
1622-
self.tcx.emit_node_span_lint(
1623-
UNUSED_ATTRIBUTES,
1624-
hir_id,
1625-
attr.span(),
1626-
errors::LinkName { span, attr_span, value: s.as_str() },
1627-
);
1628-
} else {
1629-
self.tcx.emit_node_span_lint(
1630-
UNUSED_ATTRIBUTES,
1631-
hir_id,
1632-
attr.span(),
1633-
errors::LinkName { span, attr_span, value: "..." },
1634-
);
1635-
};
1629+
let help_span = matches!(target, Target::ForeignMod).then_some(attr_span);
1630+
self.tcx.emit_node_span_lint(
1631+
UNUSED_ATTRIBUTES,
1632+
hir_id,
1633+
attr_span,
1634+
errors::LinkName { span, help_span, value: name.as_str() },
1635+
);
16361636
}
16371637
}
16381638
}

compiler/rustc_passes/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ pub(crate) struct Link {
502502
#[warning]
503503
pub(crate) struct LinkName<'a> {
504504
#[help]
505-
pub attr_span: Option<Span>,
505+
pub help_span: Option<Span>,
506506
#[label]
507507
pub span: Span,
508508
pub value: &'a str,

tests/ui/extern/issue-47725.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ extern "C" {
1717
#[link_name]
1818
//~^ ERROR malformed `link_name` attribute input
1919
//~| HELP must be of the form
20-
//~| WARN attribute should be applied to a foreign function or static [unused_attributes]
21-
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22-
//~| HELP try `#[link(name = "...")]` instead
20+
//~| NOTE expected this to be of the form `link_name = "..."
2321
extern "C" {
2422
fn bar() -> u32;
2523
}
26-
//~^^^ NOTE not a foreign function or static
2724

2825
fn main() {}

tests/ui/extern/issue-47725.stderr

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: malformed `link_name` attribute input
1+
error[E0539]: malformed `link_name` attribute input
22
--> $DIR/issue-47725.rs:17:1
33
|
44
LL | #[link_name]
5-
| ^^^^^^^^^^^^ help: must be of the form: `#[link_name = "name"]`
5+
| ^^^^^^^^^^^^
6+
| |
7+
| expected this to be of the form `link_name = "..."`
8+
| help: must be of the form: `#[link_name = "name"]`
69

710
warning: attribute should be applied to a foreign function or static
811
--> $DIR/issue-47725.rs:3:1
@@ -38,23 +41,6 @@ help: try `#[link(name = "foobar")]` instead
3841
LL | #[link_name = "foobar"]
3942
| ^^^^^^^^^^^^^^^^^^^^^^^
4043

41-
warning: attribute should be applied to a foreign function or static
42-
--> $DIR/issue-47725.rs:17:1
43-
|
44-
LL | #[link_name]
45-
| ^^^^^^^^^^^^
46-
...
47-
LL | / extern "C" {
48-
LL | | fn bar() -> u32;
49-
LL | | }
50-
| |_- not a foreign function or static
51-
|
52-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
53-
help: try `#[link(name = "...")]` instead
54-
--> $DIR/issue-47725.rs:17:1
55-
|
56-
LL | #[link_name]
57-
| ^^^^^^^^^^^^
58-
59-
error: aborting due to 1 previous error; 3 warnings emitted
44+
error: aborting due to 1 previous error; 2 warnings emitted
6045

46+
For more information about this error, try `rustc --explain E0539`.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,6 @@ LL | #![link()]
387387
|
388388
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
389389

390-
warning: attribute should be applied to a foreign function or static
391-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:66:1
392-
|
393-
LL | #![link_name = "1900"]
394-
| ^^^^^^^^^^^^^^^^^^^^^^ not a foreign function or static
395-
|
396-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
397-
398390
warning: attribute should be applied to a function or static
399391
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:69:1
400392
|
@@ -411,6 +403,14 @@ LL | #![cold]
411403
|
412404
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
413405

406+
warning: attribute should be applied to a foreign function or static
407+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:66:1
408+
|
409+
LL | #![link_name = "1900"]
410+
| ^^^^^^^^^^^^^^^^^^^^^^ not a foreign function or static
411+
|
412+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
413+
414414
warning: `#[must_use]` has no effect when applied to a module
415415
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:72:1
416416
|

tests/ui/lint/unused/unused-attr-duplicate.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,6 @@ note: attribute also specified here
8989
LL | #[automatically_derived]
9090
| ^^^^^^^^^^^^^^^^^^^^^^^^
9191

92-
error: unused attribute
93-
--> $DIR/unused-attr-duplicate.rs:86:5
94-
|
95-
LL | #[link_name = "this_does_not_exist"]
96-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
97-
|
98-
note: attribute also specified here
99-
--> $DIR/unused-attr-duplicate.rs:88:5
100-
|
101-
LL | #[link_name = "rust_dbg_extern_identity_u32"]
102-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
104-
10592
error: unused attribute
10693
--> $DIR/unused-attr-duplicate.rs:14:1
10794
|
@@ -252,6 +239,19 @@ note: attribute also specified here
252239
LL | #[track_caller]
253240
| ^^^^^^^^^^^^^^^
254241

242+
error: unused attribute
243+
--> $DIR/unused-attr-duplicate.rs:86:5
244+
|
245+
LL | #[link_name = "this_does_not_exist"]
246+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
247+
|
248+
note: attribute also specified here
249+
--> $DIR/unused-attr-duplicate.rs:88:5
250+
|
251+
LL | #[link_name = "rust_dbg_extern_identity_u32"]
252+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
253+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
254+
255255
error: unused attribute
256256
--> $DIR/unused-attr-duplicate.rs:92:1
257257
|

0 commit comments

Comments
 (0)