Skip to content

Commit 287d9af

Browse files
Port #[export_name] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
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,

0 commit comments

Comments
 (0)