From 96fecc3cfcfa91dc797a94f225027a266215d6e5 Mon Sep 17 00:00:00 2001
From: clangenb <37865735+clangenb@users.noreply.github.com>
Date: Sat, 7 Sep 2024 12:15:47 +0200
Subject: [PATCH] Fix occasional `alloc` not found error in
 `format_runtime_string!` (#5632)

The macro hygiene for the `format_runtime_string!` macro was broken
since https://github.com/paritytech/polkadot-sdk/pull/5010, which
resulted in the following build error under certain circumstances:

```console
  error[E0433]: failed to resolve: use of undeclared crate or module `alloc`
      --> /home/clang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/frame-benchmarking-36.0.0/src/v1.rs:1738:2
       |
  1738 | /     sp_runtime::format_runtime_string!(
  1739 | |         "\n* Pallet: {}\n\
  1740 | |         * Benchmark: {}\n\
  1741 | |         * Components: {:?}\n\
  ...    |
  1750 | |         error_message,
  1751 | |     )
       | |_____^ use of undeclared crate or module `alloc`
       |
       = note: this error originates in the macro `sp_runtime::format_runtime_string` (in Nightly builds, run with -Z macro-backtrace for more info)

  For more information about this error, try `rustc --explain E0433`.
```

This bug has been known already, but hasn't been fixed so far, see
https://github.com/paritytech/polkadot-sdk/issues/5213 and
https://substrate.stackexchange.com/questions/11786/use-of-undeclared-crate-or-module-alloc-when-upgrade-to-v1-13-0.

I have made a mini rust crate that can reproduce the bug, and it also
shows that this PR will fix the issue:
https://github.com/clangenb/sp-runtime-string-test.
---
 prdoc/pr_5632.prdoc                                | 13 +++++++++++++
 substrate/primitives/runtime/src/lib.rs            |  4 +---
 substrate/primitives/runtime/src/runtime_string.rs |  2 +-
 3 files changed, 15 insertions(+), 4 deletions(-)
 create mode 100644 prdoc/pr_5632.prdoc

diff --git a/prdoc/pr_5632.prdoc b/prdoc/pr_5632.prdoc
new file mode 100644
index 00000000000..f76428bbc8f
--- /dev/null
+++ b/prdoc/pr_5632.prdoc
@@ -0,0 +1,13 @@
+# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
+# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
+
+title: Fix `alloc` not found error in `format_runtime_string!`
+
+doc:
+  - audience: Runtime Dev
+    description: |
+      Fixes the macro hygiene in the `format_runtime_string!` macro to fix the `alloc` not found build error.
+
+crates:
+  - name: sp-runtime
+    bump: patch
diff --git a/substrate/primitives/runtime/src/lib.rs b/substrate/primitives/runtime/src/lib.rs
index ba1ea376972..260c9a91855 100644
--- a/substrate/primitives/runtime/src/lib.rs
+++ b/substrate/primitives/runtime/src/lib.rs
@@ -49,7 +49,7 @@
 extern crate alloc;
 
 #[doc(hidden)]
-pub use alloc::vec::Vec;
+pub use alloc::{format, vec::Vec};
 #[doc(hidden)]
 pub use codec;
 #[doc(hidden)]
@@ -79,8 +79,6 @@ use sp_core::{
 	sr25519,
 };
 
-#[cfg(all(not(feature = "std"), feature = "serde"))]
-use alloc::format;
 use alloc::vec;
 use codec::{Decode, Encode, MaxEncodedLen};
 use scale_info::TypeInfo;
diff --git a/substrate/primitives/runtime/src/runtime_string.rs b/substrate/primitives/runtime/src/runtime_string.rs
index 71aacf07a76..bb0347badcb 100644
--- a/substrate/primitives/runtime/src/runtime_string.rs
+++ b/substrate/primitives/runtime/src/runtime_string.rs
@@ -50,7 +50,7 @@ macro_rules! format_runtime_string {
 		}
 		#[cfg(not(feature = "std"))]
 		{
-			sp_runtime::RuntimeString::Owned(alloc::format!($($args)*).as_bytes().to_vec())
+			sp_runtime::RuntimeString::Owned($crate::format!($($args)*).as_bytes().to_vec())
 		}
 	}};
 }
-- 
GitLab