about summary refs log tree commit diff
path: root/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
diff options
context:
space:
mode:
authorJonathan Brouwer <jonathantbrouwer@gmail.com>2025-06-24 22:33:44 +0200
committerJonathan Brouwer <jonathantbrouwer@gmail.com>2025-06-26 08:50:42 +0200
commit287d9afce729dd81fa9abfa860af31656d9c5e16 (patch)
tree39bb92113ee1aab40e55fe7f6279f33b65f6db1e /compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
parentbc4376fa73b636eb6f2c7d48b1f731d70f022c4b (diff)
downloadrust-287d9afce729dd81fa9abfa860af31656d9c5e16.tar.gz
rust-287d9afce729dd81fa9abfa860af31656d9c5e16.zip
Port `#[export_name]` to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
Diffstat (limited to 'compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs')
-rw-r--r--compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
index eadf8657a0f..5a849e79cc3 100644
--- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
+++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
@@ -6,7 +6,7 @@ use rustc_span::{Span, Symbol, sym};
 use super::{AcceptMapping, AttributeOrder, AttributeParser, OnDuplicate, SingleAttributeParser};
 use crate::context::{AcceptContext, FinalizeContext, Stage};
 use crate::parser::ArgParser;
-use crate::session_diagnostics::NakedFunctionIncompatibleAttribute;
+use crate::session_diagnostics::{NakedFunctionIncompatibleAttribute, NullOnExport};
 
 pub(crate) struct OptimizeParser;
 
@@ -59,6 +59,33 @@ impl<S: Stage> SingleAttributeParser<S> for ColdParser {
     }
 }
 
+pub(crate) struct ExportNameParser;
+
+impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
+    const PATH: &[rustc_span::Symbol] = &[sym::export_name];
+    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
+    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
+    const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
+
+    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
+        let Some(nv) = args.name_value() else {
+            cx.expected_name_value(cx.attr_span, None);
+            return None;
+        };
+        let Some(name) = nv.value_as_str() else {
+            cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
+            return None;
+        };
+        if name.as_str().contains('\0') {
+            // `#[export_name = ...]` will be converted to a null-terminated string,
+            // so it may not contain any null characters.
+            cx.emit_err(NullOnExport { span: cx.attr_span });
+            return None;
+        }
+        Some(AttributeKind::ExportName { name, span: cx.attr_span })
+    }
+}
+
 #[derive(Default)]
 pub(crate) struct NakedParser {
     span: Option<Span>,