From 1cc0e38fdc4d2ec8c9c130d7ffc9c30d100885a2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 21 May 2025 13:28:08 +1000 Subject: [PATCH] Avoid creating an empty identifer in `Symbol::to_ident_string`. Because that causes an assertion failure in debug builds. Fixes #140884. --- compiler/rustc_span/src/symbol.rs | 3 ++- tests/crashes/140884.rs | 6 ------ .../ui/extern/extern-empty-string-issue-140884.rs | 3 +++ .../extern-empty-string-issue-140884.stderr | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 7 deletions(-) delete mode 100644 tests/crashes/140884.rs create mode 100644 tests/ui/extern/extern-empty-string-issue-140884.rs create mode 100644 tests/ui/extern/extern-empty-string-issue-140884.stderr diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index efae6250b0720..fbe3b4ca6f5f6 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2592,7 +2592,8 @@ impl Symbol { /// (`token_to_string`, `Ident::to_string`), except that symbols don't keep the rawness flag /// or edition, so we have to guess the rawness using the global edition. pub fn to_ident_string(self) -> String { - Ident::with_dummy_span(self).to_string() + // Avoid creating an empty identifier, because that asserts in debug builds. + if self == kw::Empty { String::new() } else { Ident::with_dummy_span(self).to_string() } } } diff --git a/tests/crashes/140884.rs b/tests/crashes/140884.rs deleted file mode 100644 index 6840760933a39..0000000000000 --- a/tests/crashes/140884.rs +++ /dev/null @@ -1,6 +0,0 @@ -//@ known-bug: #140884 -//@ needs-rustc-debug-assertions - -fn a() { - extern "" {} -} diff --git a/tests/ui/extern/extern-empty-string-issue-140884.rs b/tests/ui/extern/extern-empty-string-issue-140884.rs new file mode 100644 index 0000000000000..10291513d34d2 --- /dev/null +++ b/tests/ui/extern/extern-empty-string-issue-140884.rs @@ -0,0 +1,3 @@ +extern "" {} //~ ERROR invalid ABI: found `` + +fn main() {} diff --git a/tests/ui/extern/extern-empty-string-issue-140884.stderr b/tests/ui/extern/extern-empty-string-issue-140884.stderr new file mode 100644 index 0000000000000..accae0c0f7c73 --- /dev/null +++ b/tests/ui/extern/extern-empty-string-issue-140884.stderr @@ -0,0 +1,15 @@ +error[E0703]: invalid ABI: found `` + --> $DIR/extern-empty-string-issue-140884.rs:1:8 + | +LL | extern "" {} + | ^^ invalid ABI + | + = note: invoke `rustc --print=calling-conventions` for a full list of supported calling conventions +help: there's a similarly named valid ABI `C` + | +LL | extern "C" {} + | + + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0703`.