From 26e35d096735b7651d018ec7f9f8cfaf947d26b6 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Tue, 21 May 2024 11:07:13 +0200 Subject: [PATCH] Macro hygiene of `chain!` If someone has a module named `core` and uses `itertools::chain!` then there is a conflict! I improve our macro hygiene test by adding (empty) modules with all the names we might be tempted to use. Without changing `core` in `chain!`, the test failed! --- src/lib.rs | 8 ++++---- tests/macros_hygiene.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f4de79c50..1a847d42b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -393,16 +393,16 @@ macro_rules! izip { /// ``` macro_rules! chain { () => { - core::iter::empty() + $crate::__std_iter::empty() }; ($first:expr $(, $rest:expr )* $(,)?) => { { - let iter = core::iter::IntoIterator::into_iter($first); + let iter = $crate::__std_iter::IntoIterator::into_iter($first); $( let iter = - core::iter::Iterator::chain( + $crate::__std_iter::Iterator::chain( iter, - core::iter::IntoIterator::into_iter($rest)); + $crate::__std_iter::IntoIterator::into_iter($rest)); )* iter } diff --git a/tests/macros_hygiene.rs b/tests/macros_hygiene.rs index 20b59fba8..e6e895555 100644 --- a/tests/macros_hygiene.rs +++ b/tests/macros_hygiene.rs @@ -1,3 +1,8 @@ +mod alloc {} +mod core {} +mod either {} +mod std {} + #[test] fn iproduct_hygiene() { let _ = itertools::iproduct!(); @@ -12,3 +17,11 @@ fn izip_hygiene() { let _ = itertools::izip!(0..6, 0..9); let _ = itertools::izip!(0..6, 0..9, 0..12); } + +#[test] +fn chain_hygiene() { + let _: ::std::iter::Empty = itertools::chain!(); + let _ = itertools::chain!(0..6); + let _ = itertools::chain!(0..6, 0..9); + let _ = itertools::chain!(0..6, 0..9, 0..12); +}