Skip to content

Commit 287d9af

Browse files
Port #[export_name] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent bc4376f commit 287d9af

File tree

13 files changed

+84
-46
lines changed

13 files changed

+84
-46
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ pub enum AttributeKind {
231231
/// Represents [`#[doc]`](https://doc.rust-lang.org/stable/rustdoc/write-documentation/the-doc-attribute.html).
232232
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
233233

234+
/// Represents [`#[export_name]`](https://doc.rust-lang.org/reference/abi.html#the-export_name-attribute).
235+
ExportName {
236+
/// The name to export this item with.
237+
/// It may not contain \0 bytes as it will be converted to a null-terminated string.
238+
name: Symbol,
239+
span: Span,
240+
},
241+
234242
/// Represents `#[inline]` and `#[rustc_force_inline]`.
235243
Inline(InlineAttr, Span),
236244

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl AttributeKind {
2222
ConstStabilityIndirect => No,
2323
Deprecation { .. } => Yes,
2424
DocComment { .. } => Yes,
25+
ExportName { .. } => Yes,
2526
Inline(..) => No,
2627
MacroTransparency(..) => Yes,
2728
Repr(..) => No,

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ attr_parsing_naked_functions_incompatible_attribute =
9393
attribute incompatible with `#[unsafe(naked)]`
9494
.label = the `{$attr}` attribute is incompatible with `#[unsafe(naked)]`
9595
.naked_attribute = function marked with `#[unsafe(naked)]` here
96+
9697
attr_parsing_non_ident_feature =
9798
'feature' is not an identifier
9899
100+
attr_parsing_null_on_export = `export_name` may not contain null characters
101+
99102
attr_parsing_repr_ident =
100103
meta item in `repr` must be an identifier
101104

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_span::{Span, Symbol, sym};
66
use super::{AcceptMapping, AttributeOrder, AttributeParser, OnDuplicate, SingleAttributeParser};
77
use crate::context::{AcceptContext, FinalizeContext, Stage};
88
use crate::parser::ArgParser;
9-
use crate::session_diagnostics::NakedFunctionIncompatibleAttribute;
9+
use crate::session_diagnostics::{NakedFunctionIncompatibleAttribute, NullOnExport};
1010

1111
pub(crate) struct OptimizeParser;
1212

@@ -59,6 +59,33 @@ impl<S: Stage> SingleAttributeParser<S> for ColdParser {
5959
}
6060
}
6161

62+
pub(crate) struct ExportNameParser;
63+
64+
impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
65+
const PATH: &[rustc_span::Symbol] = &[sym::export_name];
66+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
67+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
68+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
69+
70+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
71+
let Some(nv) = args.name_value() else {
72+
cx.expected_name_value(cx.attr_span, None);
73+
return None;
74+
};
75+
let Some(name) = nv.value_as_str() else {
76+
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
77+
return None;
78+
};
79+
if name.as_str().contains('\0') {
80+
// `#[export_name = ...]` will be converted to a null-terminated string,
81+
// so it may not contain any null characters.
82+
cx.emit_err(NullOnExport { span: cx.attr_span });
83+
return None;
84+
}
85+
Some(AttributeKind::ExportName { name, span: cx.attr_span })
86+
}
87+
}
88+
6289
#[derive(Default)]
6390
pub(crate) struct NakedParser {
6491
span: Option<Span>,

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1616

1717
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
1818
use crate::attributes::codegen_attrs::{
19-
ColdParser, NakedParser, NoMangleParser, OptimizeParser, TrackCallerParser,
19+
ColdParser, ExportNameParser, NakedParser, NoMangleParser, OptimizeParser, TrackCallerParser,
2020
};
2121
use crate::attributes::confusables::ConfusablesParser;
2222
use crate::attributes::deprecation::DeprecationParser;
@@ -117,6 +117,7 @@ attribute_parsers!(
117117
Single<ConstContinueParser>,
118118
Single<ConstStabilityIndirectParser>,
119119
Single<DeprecationParser>,
120+
Single<ExportNameParser>,
120121
Single<InlineParser>,
121122
Single<LoopMatchParser>,
122123
Single<MayDangleParser>,

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ pub(crate) struct MustUseIllFormedAttributeInput {
445445
pub suggestions: DiagArgValue,
446446
}
447447

448+
#[derive(Diagnostic)]
449+
#[diag(attr_parsing_null_on_export, code = E0648)]
450+
pub(crate) struct NullOnExport {
451+
#[primary_span]
452+
pub span: Span,
453+
}
454+
448455
#[derive(Diagnostic)]
449456
#[diag(attr_parsing_stability_outside_std, code = E0734)]
450457
pub(crate) struct StabilityOutsideStd {

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error}
230230
231231
codegen_ssa_no_saved_object_file = cached cgu {$cgu_name} should have an object file, but doesn't
232232
233-
codegen_ssa_null_on_export = `export_name` may not contain null characters
234-
235233
codegen_ssa_out_of_range_integer = integer value out of range
236234
.label = value must be between `0` and `255`
237235

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
119119
.max();
120120
}
121121
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
122+
AttributeKind::ExportName { name, span: attr_span } => {
123+
codegen_fn_attrs.export_name = Some(*name);
124+
mixed_export_name_no_mangle_lint_state.track_export_name(*attr_span);
125+
}
122126
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
123127
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
124128
AttributeKind::NoMangle(attr_span) => {
@@ -223,17 +227,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
223227
}
224228
}
225229
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
226-
sym::export_name => {
227-
if let Some(s) = attr.value_str() {
228-
if s.as_str().contains('\0') {
229-
// `#[export_name = ...]` will be converted to a null-terminated string,
230-
// so it may not contain any null characters.
231-
tcx.dcx().emit_err(errors::NullOnExport { span: attr.span() });
232-
}
233-
codegen_fn_attrs.export_name = Some(s);
234-
mixed_export_name_no_mangle_lint_state.track_export_name(attr.span());
235-
}
236-
}
237230
sym::target_feature => {
238231
let Some(sig) = tcx.hir_node_by_def_id(did).fn_sig() else {
239232
tcx.dcx().span_delayed_bug(attr.span(), "target_feature applied to non-fn");

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ pub(crate) struct RequiresRustAbi {
140140
pub span: Span,
141141
}
142142

143-
#[derive(Diagnostic)]
144-
#[diag(codegen_ssa_null_on_export, code = E0648)]
145-
pub(crate) struct NullOnExport {
146-
#[primary_span]
147-
pub span: Span,
148-
}
149-
150143
#[derive(Diagnostic)]
151144
#[diag(codegen_ssa_unsupported_instruction_set, code = E0779)]
152145
pub(crate) struct UnsupportedInstructionSet {

compiler/rustc_lint/src/builtin.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -977,9 +977,9 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
977977
};
978978
match it.kind {
979979
hir::ItemKind::Fn { generics, .. } => {
980-
if let Some(attr_span) = attr::find_by_name(attrs, sym::export_name)
981-
.map(|at| at.span())
982-
.or_else(|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span))
980+
if let Some(attr_span) =
981+
find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span)
982+
.or_else(|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span))
983983
{
984984
check_no_mangle_on_generic_fn(attr_span, None, generics, it.span);
985985
}
@@ -1010,9 +1010,11 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
10101010
for it in *items {
10111011
if let hir::AssocItemKind::Fn { .. } = it.kind {
10121012
let attrs = cx.tcx.hir_attrs(it.id.hir_id());
1013-
if let Some(attr_span) = attr::find_by_name(attrs, sym::export_name)
1014-
.map(|at| at.span())
1015-
.or_else(|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span))
1013+
if let Some(attr_span) =
1014+
find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span)
1015+
.or_else(
1016+
|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span),
1017+
)
10161018
{
10171019
check_no_mangle_on_generic_fn(
10181020
attr_span,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
169169
Attribute::Parsed(AttributeKind::Cold(attr_span)) => {
170170
self.check_cold(hir_id, *attr_span, span, target)
171171
}
172+
Attribute::Parsed(AttributeKind::ExportName { span: attr_span, .. }) => {
173+
self.check_export_name(hir_id, *attr_span, span, target)
174+
}
172175
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => {
173176
self.check_align(span, target, *align, *repr_span)
174177
}
@@ -223,7 +226,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
223226
&mut doc_aliases,
224227
),
225228
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
226-
[sym::export_name, ..] => self.check_export_name(hir_id, attr, span, target),
227229
[sym::rustc_layout_scalar_valid_range_start, ..]
228230
| [sym::rustc_layout_scalar_valid_range_end, ..] => {
229231
self.check_rustc_layout_scalar_valid_range(attr, span, target)
@@ -1653,7 +1655,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16531655
}
16541656

16551657
/// Checks if `#[export_name]` is applied to a function or static.
1656-
fn check_export_name(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
1658+
fn check_export_name(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
16571659
match target {
16581660
Target::Static | Target::Fn => {}
16591661
Target::Method(..) if self.is_impl_item(hir_id) => {}
@@ -1662,10 +1664,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16621664
// erroneously allowed it and some crates used it accidentally, to be compatible
16631665
// with crates depending on them, we can't throw an error here.
16641666
Target::Field | Target::Arm | Target::MacroDef => {
1665-
self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "export_name");
1667+
self.inline_attr_str_error_with_macro_def(hir_id, attr_span, "export_name");
16661668
}
16671669
_ => {
1668-
self.dcx().emit_err(errors::ExportName { attr_span: attr.span(), span });
1670+
self.dcx().emit_err(errors::ExportName { attr_span, span });
16691671
}
16701672
}
16711673
}

src/librustdoc/clean/types.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,11 @@ impl Item {
755755
.filter_map(|attr| {
756756
// NoMangle is special cased, as it appears in HTML output, and we want to show it in source form, not HIR printing.
757757
// It is also used by cargo-semver-checks.
758-
if matches!(attr, hir::Attribute::Parsed(AttributeKind::NoMangle(..))) {
758+
if let hir::Attribute::Parsed(AttributeKind::NoMangle(..)) = attr {
759759
Some("#[no_mangle]".to_string())
760+
} else if let hir::Attribute::Parsed(AttributeKind::ExportName { name, .. }) = attr
761+
{
762+
Some(format!("#[export_name = \"{name}\"]"))
760763
} else if is_json {
761764
match attr {
762765
// rustdoc-json stores this in `Item::deprecation`, so we

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:92:1
94-
|
95-
LL | #[export_name = "exported_symbol_name"]
96-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
97-
|
98-
note: attribute also specified here
99-
--> $DIR/unused-attr-duplicate.rs:94:1
100-
|
101-
LL | #[export_name = "exported_symbol_name2"]
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:102:1
10794
|
@@ -277,6 +264,19 @@ note: attribute also specified here
277264
LL | #[track_caller]
278265
| ^^^^^^^^^^^^^^^
279266

267+
error: unused attribute
268+
--> $DIR/unused-attr-duplicate.rs:92:1
269+
|
270+
LL | #[export_name = "exported_symbol_name"]
271+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
272+
|
273+
note: attribute also specified here
274+
--> $DIR/unused-attr-duplicate.rs:94:1
275+
|
276+
LL | #[export_name = "exported_symbol_name2"]
277+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
278+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
279+
280280
error: unused attribute
281281
--> $DIR/unused-attr-duplicate.rs:98:1
282282
|

0 commit comments

Comments
 (0)