From 356869dfb16a112c68def3718fc707654217df0c Mon Sep 17 00:00:00 2001 From: Ravi Chamarthy Date: Thu, 3 Sep 2026 18:33:28 -0700 Subject: [PATCH] fix: `vec!` macro resolution via alloc::vec self-import `use alloc::vec::{self, Vec};` relies on the `self` import also bringing the `vec!` macro into scope alongside the `vec` module. On some rustc versions this only resolves the module (type namespace), so unqualified `vec![...]` calls in this file fail with "cannot find macro `vec` in this scope". Import `Drain` by name instead of the `vec` module, and fully qualify the two `vec!` invocations (`alloc::vec!` / `$crate::alloc::vec!`) so macro resolution no longer depends on the `self`-import quirk. Signed-off-by: Ravi Chamarthy --- .github/workflows/rust.yml | 2 +- Cargo.toml | 4 ++++ src/tinyvec.rs | 6 +++--- tests/no_std_macro.rs | 11 +++++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 tests/no_std_macro.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 44af3b1..bc1acaa 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -34,7 +34,7 @@ jobs: - name: Check no_std with alloc if: matrix.rust != '1.60.0' run: | - cargo check --no-default-features --features=alloc,grab_spare_slice,latest_stable_rust + cargo check --tests --no-default-features --features=alloc,grab_spare_slice,latest_stable_rust - name: Test non nightly if: matrix.rust != '1.60.0' && matrix.rust != 'nightly' run: | diff --git a/Cargo.toml b/Cargo.toml index 28e8d68..651084f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -113,6 +113,10 @@ debugger_test_parser = "0.1" name = "tinyvec" required-features = ["alloc", "std"] +[[test]] +name = "no_std_macro" +required-features = ["alloc"] + [[bench]] name = "macros" harness = false diff --git a/src/tinyvec.rs b/src/tinyvec.rs index 48ef36b..0781fc7 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -1,6 +1,6 @@ use super::*; -use alloc::vec::{self, Vec}; +use alloc::vec::{Drain, Vec}; use core::convert::TryFrom; use tinyvec_macros::impl_mirrored; @@ -46,7 +46,7 @@ macro_rules! tiny_vec { f($crate::array_vec!($array_type => $($elem),*)) } $crate::TinyVecConstructor::Heap(f) => { - f(vec![$($elem),*]) + f($crate::alloc::vec![$($elem),*]) } } } @@ -1308,7 +1308,7 @@ pub enum TinyVecDrain<'p, A: Array> { #[allow(missing_docs)] Inline(ArrayVecDrain<'p, A::Item>), #[allow(missing_docs)] - Heap(vec::Drain<'p, A::Item>), + Heap(Drain<'p, A::Item>), } impl<'p, A: Array> Iterator for TinyVecDrain<'p, A> { diff --git a/tests/no_std_macro.rs b/tests/no_std_macro.rs new file mode 100644 index 0000000..9478e6e --- /dev/null +++ b/tests/no_std_macro.rs @@ -0,0 +1,11 @@ +//! Regression test for the `tiny_vec!` macro's heap arm expanding cleanly +//! in a downstream `#![no_std]` crate, where there is no `std` prelude +//! `vec!` to mask a macro-hygiene resolution bug. +#![no_std] + +use tinyvec::{tiny_vec, TinyVec}; + +#[allow(dead_code)] +fn expands_to_heap() -> TinyVec<[u8; 1]> { + tiny_vec!([u8; 1] => 1, 2) +}