Skip to content

Commit d0ea88e

Browse files
committed
error on empty export_name
1 parent ec2d669 commit d0ea88e

4 files changed

Lines changed: 71 additions & 2 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_span::edition::Edition::Edition2024;
55
use super::prelude::*;
66
use crate::attributes::AttributeSafety;
77
use crate::session_diagnostics::{
8-
NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector,
9-
ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
8+
EmptyExportName, NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass,
9+
NullOnObjcSelector, ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
1010
};
1111
use crate::target_checking::Policy::AllowSilent;
1212

@@ -133,6 +133,12 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
133133
cx.emit_err(NullOnExport { span: cx.attr_span });
134134
return None;
135135
}
136+
if name.is_empty() {
137+
// LLVM will make up a name if the empty string is given, but that name will be
138+
// inconsistent between compilation units, causing linker errors.
139+
cx.emit_err(EmptyExportName { span: cx.attr_span });
140+
return None;
141+
}
136142
Some(AttributeKind::ExportName { name, span: cx.attr_span })
137143
}
138144
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ pub(crate) struct UnusedMultiple {
396396
pub name: Symbol,
397397
}
398398

399+
#[derive(Diagnostic)]
400+
#[diag("`export_name` may not be empty")]
401+
pub(crate) struct EmptyExportName {
402+
#[primary_span]
403+
pub span: Span,
404+
}
405+
399406
#[derive(Diagnostic)]
400407
#[diag("`export_name` may not contain null characters", code = E0648)]
401408
pub(crate) struct NullOnExport {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![crate_type = "lib"]
2+
3+
#[export_name = "\0foo"]
4+
//~^ ERROR `export_name` may not contain null characters
5+
fn has_null_byte() {}
6+
7+
#[export_name = "foo\0"]
8+
//~^ ERROR `export_name` may not contain null characters
9+
fn null_terminated() {}
10+
11+
#[export_name = "\0"]
12+
//~^ ERROR `export_name` may not contain null characters
13+
fn empty_null() {}
14+
15+
#[export_name = ""]
16+
//~^ ERROR `export_name` may not be empty
17+
fn empty() {}
18+
19+
#[export_name = "\
20+
"]
21+
//~^^ ERROR `export_name` may not be empty
22+
fn empty_newline() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error[E0648]: `export_name` may not contain null characters
2+
--> $DIR/invalid-export-name.rs:3:1
3+
|
4+
LL | #[export_name = "\0foo"]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0648]: `export_name` may not contain null characters
8+
--> $DIR/invalid-export-name.rs:7:1
9+
|
10+
LL | #[export_name = "foo\0"]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0648]: `export_name` may not contain null characters
14+
--> $DIR/invalid-export-name.rs:11:1
15+
|
16+
LL | #[export_name = "\0"]
17+
| ^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: `export_name` may not be empty
20+
--> $DIR/invalid-export-name.rs:15:1
21+
|
22+
LL | #[export_name = ""]
23+
| ^^^^^^^^^^^^^^^^^^^
24+
25+
error: `export_name` may not be empty
26+
--> $DIR/invalid-export-name.rs:19:1
27+
|
28+
LL | / #[export_name = "\
29+
LL | | "]
30+
| |__^
31+
32+
error: aborting due to 5 previous errors
33+
34+
For more information about this error, try `rustc --explain E0648`.

0 commit comments

Comments
 (0)