Skip to content

Commit

Permalink
Fix occasional alloc not found error in format_runtime_string! (#…
Browse files Browse the repository at this point in the history
…5632)

The macro hygiene for the `format_runtime_string!` macro was broken
since #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
#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.

(cherry picked from commit 96fecc3)
  • Loading branch information
clangenb authored and github-actions[bot] committed Sep 7, 2024
1 parent 61fc831 commit e1aea1f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
13 changes: 13 additions & 0 deletions prdoc/pr_5632.prdoc
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions substrate/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/runtime/src/runtime_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}};
}
Expand Down

0 comments on commit e1aea1f

Please sign in to comment.