error on empty export_name

This commit is contained in:
Folkert de Vries
2026-04-19 14:27:10 +02:00
parent ec2d669db8
commit d0ea88eeb7
4 changed files with 71 additions and 2 deletions
@@ -5,8 +5,8 @@
use super::prelude::*;
use crate::attributes::AttributeSafety;
use crate::session_diagnostics::{
NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector,
ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
EmptyExportName, NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass,
NullOnObjcSelector, ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
};
use crate::target_checking::Policy::AllowSilent;
@@ -133,6 +133,12 @@ fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<Attrib
cx.emit_err(NullOnExport { span: cx.attr_span });
return None;
}
if name.is_empty() {
// LLVM will make up a name if the empty string is given, but that name will be
// inconsistent between compilation units, causing linker errors.
cx.emit_err(EmptyExportName { span: cx.attr_span });
return None;
}
Some(AttributeKind::ExportName { name, span: cx.attr_span })
}
}
@@ -396,6 +396,13 @@ pub(crate) struct UnusedMultiple {
pub name: Symbol,
}
#[derive(Diagnostic)]
#[diag("`export_name` may not be empty")]
pub(crate) struct EmptyExportName {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("`export_name` may not contain null characters", code = E0648)]
pub(crate) struct NullOnExport {
@@ -0,0 +1,22 @@
#![crate_type = "lib"]
#[export_name = "\0foo"]
//~^ ERROR `export_name` may not contain null characters
fn has_null_byte() {}
#[export_name = "foo\0"]
//~^ ERROR `export_name` may not contain null characters
fn null_terminated() {}
#[export_name = "\0"]
//~^ ERROR `export_name` may not contain null characters
fn empty_null() {}
#[export_name = ""]
//~^ ERROR `export_name` may not be empty
fn empty() {}
#[export_name = "\
"]
//~^^ ERROR `export_name` may not be empty
fn empty_newline() {}
@@ -0,0 +1,34 @@
error[E0648]: `export_name` may not contain null characters
--> $DIR/invalid-export-name.rs:3:1
|
LL | #[export_name = "\0foo"]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0648]: `export_name` may not contain null characters
--> $DIR/invalid-export-name.rs:7:1
|
LL | #[export_name = "foo\0"]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0648]: `export_name` may not contain null characters
--> $DIR/invalid-export-name.rs:11:1
|
LL | #[export_name = "\0"]
| ^^^^^^^^^^^^^^^^^^^^^
error: `export_name` may not be empty
--> $DIR/invalid-export-name.rs:15:1
|
LL | #[export_name = ""]
| ^^^^^^^^^^^^^^^^^^^
error: `export_name` may not be empty
--> $DIR/invalid-export-name.rs:19:1
|
LL | / #[export_name = "\
LL | | "]
| |__^
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0648`.