Use no_std when testing (fixes #4681)
What changed, and why it matters
This commit changes how two Rust Bitcoin library crates are configured during testing. It makes the code always use 'no_std' mode (a leaner environment without the standard library) even when running tests, and adjusts test imports to use allocator-provided collections instead of the standard library. It also replaces a 'HashSet' alias with a direct 'BTreeSet' import for clarity. There is no security-relevant change to runtime behavior or public API.
No security action required. Treat as a normal build/test consistency improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch switches #![cfg_attr(all(not(test), not(feature = "std")), no_std)] to unconditional #![no_std] in addresses/src/lib.rs and primitives/src/lib.rs. Tests are updated to import alloc::{format, vec, collections::BTreeSet} under #[cfg(feature = "alloc")] and to gate tests that require allocation or standard output. A HashSet alias in opcodes.rs tests is replaced with BTreeSet. These are build/test hygiene changes only.
Changed components
addresses/src/lib.rsprimitives/src/lib.rsprimitives/src/block.rsprimitives/src/opcodes.rsprimitives/src/pow.rsprimitives/src/script/borrowed.rsprimitives/src/script/mod.rsprimitives/src/script/owned.rsprimitives/src/transaction.rsprimitives/src/witness.rsInspect captured patch +37 / −5
diff --git a/addresses/src/lib.rs b/addresses/src/lib.rs
index 06c99f4e..437b9a0a 100644
--- a/addresses/src/lib.rs
+++ b/addresses/src/lib.rs
@@ -11,7 +11,7 @@
// NB: This crate is empty if `alloc` is not enabled.
#![cfg(feature = "alloc")]
-#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
+#![no_std]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc(test(attr(warn(unused))))]
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index 17c79af5..476dec4a 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -393,6 +393,9 @@ impl<'a> Arbitrary<'a> for Version {
mod tests {
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{format, vec};
+
fn dummy_header() -> Header {
Header {
version: Version::ONE,
@@ -550,6 +553,7 @@ mod tests {
}
#[test]
+ #[cfg(feature = "alloc")]
fn header_debug() {
let header = dummy_header();
let expected = format!(
@@ -567,6 +571,7 @@ mod tests {
#[test]
#[cfg(feature = "hex")]
+ #[cfg(feature = "alloc")]
fn header_display() {
let seconds: u32 = 1_653_195_600; // Arbitrary timestamp: May 22nd, 5am UTC.
diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs
index c2e1072f..71b51643 100644
--- a/primitives/src/lib.rs
+++ b/primitives/src/lib.rs
@@ -9,7 +9,7 @@
//!
//! [`rust-bitcoin`]: <https://github.com/rust-bitcoin>
-#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
+#![no_std]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
// Coding conventions.
diff --git a/primitives/src/opcodes.rs b/primitives/src/opcodes.rs
index 933629e5..090d64db 100644
--- a/primitives/src/opcodes.rs
+++ b/primitives/src/opcodes.rs
@@ -545,10 +545,12 @@ impl Ordinary {
#[cfg(test)]
mod tests {
- use std::collections::HashSet;
-
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{collections::BTreeSet, format};
+
+ #[cfg(feature = "alloc")]
macro_rules! roundtrip {
($unique:expr, $op:ident) => {
assert_eq!($op, Opcode::from($op.to_u8()));
@@ -562,6 +564,7 @@ mod tests {
}
#[test]
+ #[cfg(feature = "alloc")]
fn formatting_works() {
let op = all::OP_NOP;
let s = format!("{:>10}", op);
@@ -642,8 +645,9 @@ mod tests {
#[test]
#[allow(clippy::too_many_lines)] // This is fine, we never need to read it.
+ #[cfg(feature = "alloc")]
fn str_roundtrip() {
- let mut unique = HashSet::new();
+ let mut unique = BTreeSet::new();
roundtrip!(unique, OP_PUSHBYTES_0);
roundtrip!(unique, OP_PUSHBYTES_1);
roundtrip!(unique, OP_PUSHBYTES_2);
diff --git a/primitives/src/pow.rs b/primitives/src/pow.rs
index abae9c6d..00ec8629 100644
--- a/primitives/src/pow.rs
+++ b/primitives/src/pow.rs
@@ -52,6 +52,9 @@ impl fmt::UpperHex for CompactTarget {
mod tests {
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{format};
+
#[test]
fn compact_target_ordering() {
let lower = CompactTarget::from_consensus(0x1d00_fffe);
@@ -63,6 +66,7 @@ mod tests {
}
#[test]
+ #[cfg(feature = "alloc")]
fn compact_target_formatting() {
let compact_target = CompactTarget::from_consensus(0x1d00_ffff);
assert_eq!(format!("{:x}", compact_target), "1d00ffff");
diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs
index 47065e06..349cfddc 100644
--- a/primitives/src/script/borrowed.rs
+++ b/primitives/src/script/borrowed.rs
@@ -190,6 +190,9 @@ delegate_index!(
mod tests {
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{vec};
+
#[test]
fn script_from_bytes() {
let script = Script::from_bytes(&[1, 2, 3]);
diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs
index 0e656fcc..28305054 100644
--- a/primitives/src/script/mod.rs
+++ b/primitives/src/script/mod.rs
@@ -613,6 +613,9 @@ impl<'de> serde::Deserialize<'de> for ScriptBuf {
mod tests {
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{format, vec};
+
#[test]
fn scriptbuf_from_vec_u8() {
let vec = vec![0x51, 0x52, 0x53];
diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs
index c350b02f..2490b989 100644
--- a/primitives/src/script/owned.rs
+++ b/primitives/src/script/owned.rs
@@ -150,6 +150,9 @@ impl<'a> Arbitrary<'a> for ScriptBuf {
mod tests {
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{vec};
+
#[test]
fn script_buf_from_bytes() {
let bytes = vec![1, 2, 3];
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index 26dbfcdb..b32623fc 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -664,6 +664,9 @@ impl<'a> Arbitrary<'a> for Wtxid {
mod tests {
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{format, vec};
+
#[test]
fn sanity_check() {
let version = Version(123);
diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs
index 02f9c7a7..530d6f5f 100644
--- a/primitives/src/witness.rs
+++ b/primitives/src/witness.rs
@@ -575,6 +575,12 @@ impl<'a> Arbitrary<'a> for Witness {
mod test {
use super::*;
+ #[cfg(feature = "alloc")]
+ use alloc::{vec};
+
+ #[cfg(feature = "std")]
+ use std::println;
+
// Appends all the indices onto the end of a list of elements.
fn append_u32_vec(elements: &[u8], indices: &[u32]) -> Vec<u8> {
let mut v = elements.to_vec();
@@ -588,6 +594,7 @@ mod test {
fn single_empty_element() -> Witness { Witness::from([[0u8; 0]]) }
#[test]
+ #[cfg(feature = "std")]
fn witness_debug_can_display_empty_element() {
let witness = single_empty_element();
println!("{:?}", witness);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.