Skip to content

Commit

Permalink
Auto merge of #55373 - estebank:macro-rep, r=<try>
Browse files Browse the repository at this point in the history
Disallow incorrect unseparated repetition

Macro pattern `($a:expr, $b:expr)` is invalid, for good reason.
Correctly reject pattern `($($a:expr)*)`, as it is functionally the
same.

Fix #44975, fix #48220.
  • Loading branch information
bors committed Oct 26, 2018
2 parents 7b0735a + 11b8746 commit 7ae3681
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 131 deletions.
10 changes: 5 additions & 5 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,7 @@ __impl_slice_eq1! { VecDeque<A>, &'b [B] }
__impl_slice_eq1! { VecDeque<A>, &'b mut [B] }

macro_rules! array_impls {
($($N: expr)+) => {
($($N: expr),+) => {
$(
__impl_slice_eq1! { VecDeque<A>, [B; $N] }
__impl_slice_eq1! { VecDeque<A>, &'b [B; $N] }
Expand All @@ -2399,10 +2399,10 @@ macro_rules! array_impls {
}

array_impls! {
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
10 changes: 5 additions & 5 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ __impl_slice_eq1! { Cow<'a, [A]>, &'b mut [B], Clone }
__impl_slice_eq1! { Cow<'a, [A]>, Vec<B>, Clone }

macro_rules! array_impls {
($($N: expr)+) => {
($($N: expr),+) => {
$(
// NOTE: some less important impls are omitted to reduce code bloat
__impl_slice_eq1! { Vec<A>, [B; $N] }
Expand All @@ -2079,10 +2079,10 @@ macro_rules! array_impls {
}

array_impls! {
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32
}

/// Implements comparison of vectors, lexicographically.
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ macro_rules! __impl_slice_eq2 {

// macro for implementing n-element array functions and operations
macro_rules! array_impls {
($($N:expr)+) => {
($($N:expr),+) => {
$(
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for [T; $N] {
Expand Down Expand Up @@ -257,10 +257,10 @@ macro_rules! array_impls {
}

array_impls! {
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32
}

// The Default impls cannot be generated using the array_impls! macro because
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod impls {
use super::Clone;

macro_rules! impl_clone {
($($t:ty)*) => {
($($t:ty),*) => {
$(
#[stable(feature = "rust1", since = "1.0.0")]
impl Clone for $t {
Expand All @@ -172,10 +172,10 @@ mod impls {
}

impl_clone! {
usize u8 u16 u32 u64 u128
isize i8 i16 i32 i64 i128
f32 f64
bool char
usize, u8, u16, u32, u64, u128,
isize, i8, i16, i32, i64, i128,
f32, f64,
bool, char
}

#[unstable(feature = "never_type", issue = "35121")]
Expand Down
16 changes: 8 additions & 8 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ mod impls {
use cmp::Ordering::{self, Less, Greater, Equal};

macro_rules! partial_eq_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for $t {
#[inline]
Expand All @@ -883,20 +883,20 @@ mod impls {
}

partial_eq_impl! {
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64
bool, char, usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64
}

macro_rules! eq_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for $t {}
)*)
}

eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
eq_impl! { (), bool, char, usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128 }

macro_rules! partial_ord_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[inline]
Expand Down Expand Up @@ -936,10 +936,10 @@ mod impls {
}
}

partial_ord_impl! { f32 f64 }
partial_ord_impl! { f32, f64 }

macro_rules! ord_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[inline]
Expand Down Expand Up @@ -982,7 +982,7 @@ mod impls {
}
}

ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
ord_impl! { char, usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128 }

#[unstable(feature = "never_type", issue = "35121")]
impl PartialEq for ! {
Expand Down
26 changes: 13 additions & 13 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ macro_rules! step_identical_methods {
}

macro_rules! step_impl_unsigned {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits",
issue = "42168")]
Expand Down Expand Up @@ -145,7 +145,7 @@ macro_rules! step_impl_signed {
}

macro_rules! step_impl_no_between {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits",
issue = "42168")]
Expand All @@ -165,7 +165,7 @@ macro_rules! step_impl_no_between {
)*)
}

step_impl_unsigned!(usize u8 u16);
step_impl_unsigned!(usize, u8, u16);
#[cfg(not(target_pointer_witdth = "16"))]
step_impl_unsigned!(u32);
#[cfg(target_pointer_witdth = "16")]
Expand All @@ -182,32 +182,32 @@ step_impl_signed!([i64: u64]);
// If the target pointer width is not 64-bits, we
// assume here that it is less than 64-bits.
#[cfg(not(target_pointer_width = "64"))]
step_impl_no_between!(u64 i64);
step_impl_no_between!(u128 i128);
step_impl_no_between!(u64, i64);
step_impl_no_between!(u128, i128);

macro_rules! range_exact_iter_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl ExactSizeIterator for ops::Range<$t> { }
)*)
}

macro_rules! range_incl_exact_iter_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl ExactSizeIterator for ops::RangeInclusive<$t> { }
)*)
}

macro_rules! range_trusted_len_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl TrustedLen for ops::Range<$t> { }
)*)
}

macro_rules! range_incl_trusted_len_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl TrustedLen for ops::RangeInclusive<$t> { }
)*)
Expand Down Expand Up @@ -276,15 +276,15 @@ impl<A: Step> Iterator for ops::Range<A> {
// Range<{u,i}64> and RangeInclusive<{u,i}{32,64,size}> are excluded
// because they cannot guarantee having a length <= usize::MAX, which is
// required by ExactSizeIterator.
range_exact_iter_impl!(usize u8 u16 u32 isize i8 i16 i32);
range_incl_exact_iter_impl!(u8 u16 i8 i16);
range_exact_iter_impl!(usize, u8, u16, u32, isize, i8, i16, i32);
range_incl_exact_iter_impl!(u8, u16, i8, i16);

// These macros generate `TrustedLen` impls.
//
// They need to guarantee that .size_hint() is either exact, or that
// the upper bound is None when it does not fit the type limits.
range_trusted_len_impl!(usize isize u8 i8 u16 i16 u32 i32 i64 u64);
range_incl_trusted_len_impl!(usize isize u8 i8 u16 i16 u32 i32 i64 u64);
range_trusted_len_impl!(usize, isize, u8, i8, u16, i16, u32, i32, i64, u64);
range_incl_trusted_len_impl!(usize, isize, u8, i8, u16, i16, u32, i32, i64, u64);

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: Step> DoubleEndedIterator for ops::Range<A> {
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/iter/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ pub trait Product<A = Self>: Sized {

// NB: explicitly use Add and Mul here to inherit overflow checks
macro_rules! integer_sum_product {
(@impls $zero:expr, $one:expr, #[$attr:meta], $($a:ty)*) => ($(
(@impls $zero:expr, $one:expr, #[$attr:meta], $($a:ty),*) => ($(
#[$attr]
impl Sum for $a {
fn sum<I: Iterator<Item=$a>>(iter: I) -> $a {
Expand Down Expand Up @@ -801,13 +801,13 @@ macro_rules! integer_sum_product {
}
}
)*);
($($a:ty)*) => (
($($a:ty),*) => (
integer_sum_product!(@impls 0, 1,
#[stable(feature = "iter_arith_traits", since = "1.12.0")],
$($a)+);
$($a),+);
integer_sum_product!(@impls Wrapping(0), Wrapping(1),
#[stable(feature = "wrapping_iter_arith", since = "1.14.0")],
$(Wrapping<$a>)+);
$(Wrapping<$a>),+);
);
}

Expand Down Expand Up @@ -843,7 +843,7 @@ macro_rules! float_sum_product {
)*)
}

integer_sum_product! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
integer_sum_product! { i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize }
float_sum_product! { f32 f64 }

/// An iterator adapter that produces output as long as the underlying
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ mod copy_impls {
use super::Copy;

macro_rules! impl_copy {
($($t:ty)*) => {
($($t:ty),*) => {
$(
#[stable(feature = "rust1", since = "1.0.0")]
impl Copy for $t {}
Expand All @@ -673,10 +673,10 @@ mod copy_impls {
}

impl_copy! {
usize u8 u16 u32 u64 u128
isize i8 i16 i32 i64 i128
f32 f64
bool char
usize, u8, u16, u32, u64, u128,
isize, i8, i16, i32, i64, i128,
f32, f64,
bool, char
}

#[unstable(feature = "never_type", issue = "35121")]
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4421,7 +4421,7 @@ pub enum FpCategory {
}

macro_rules! from_str_radix_int_impl {
($($t:ty)*) => {$(
($($t:ty),*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for $t {
type Err = ParseIntError;
Expand All @@ -4431,7 +4431,7 @@ macro_rules! from_str_radix_int_impl {
}
)*}
}
from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
from_str_radix_int_impl! { isize, i8, i16, i32, i64, i128, usize, u8, u16, u32, u64, u128 }

/// The error type returned when a checked integral type conversion fails.
#[unstable(feature = "try_from", issue = "33417")]
Expand Down Expand Up @@ -4662,7 +4662,7 @@ trait FromStrRadixHelper: PartialOrd + Copy {
}

macro_rules! doit {
($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
($($t:ty),*) => ($(impl FromStrRadixHelper for $t {
#[inline]
fn min_value() -> Self { Self::min_value() }
#[inline]
Expand All @@ -4683,7 +4683,7 @@ macro_rules! doit {
}
})*)
}
doit! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
doit! { i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize }

fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, ParseIntError> {
use self::IntErrorKind::*;
Expand Down
16 changes: 8 additions & 8 deletions src/libcore/num/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }

// FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
macro_rules! wrapping_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Add for Wrapping<$t> {
type Output = Wrapping<$t>;
Expand Down Expand Up @@ -323,10 +323,10 @@ macro_rules! wrapping_impl {
)*)
}

wrapping_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
wrapping_impl! { usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128 }

macro_rules! wrapping_int_impl {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
impl Wrapping<$t> {
doc_comment! {
concat!("Returns the smallest value that can be represented by this integer type.
Expand Down Expand Up @@ -685,10 +685,10 @@ assert_eq!(Wrapping(3i8).pow(6), Wrapping(-39));
)*)
}

wrapping_int_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
wrapping_int_impl! { usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128 }

macro_rules! wrapping_int_impl_signed {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
impl Wrapping<$t> {
doc_comment! {
concat!("Returns the number of leading zeros in the binary representation of `self`.
Expand Down Expand Up @@ -814,10 +814,10 @@ assert!(!Wrapping(10", stringify!($t), ").is_negative());
)*)
}

wrapping_int_impl_signed! { isize i8 i16 i32 i64 i128 }
wrapping_int_impl_signed! { isize, i8, i16, i32, i64, i128 }

macro_rules! wrapping_int_impl_unsigned {
($($t:ty)*) => ($(
($($t:ty),*) => ($(
impl Wrapping<$t> {
doc_comment! {
concat!("Returns the number of leading zeros in the binary representation of `self`.
Expand Down Expand Up @@ -891,7 +891,7 @@ assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0));
)*)
}

wrapping_int_impl_unsigned! { usize u8 u16 u32 u64 u128 }
wrapping_int_impl_unsigned! { usize, u8, u16, u32, u64, u128 }

mod shift_max {
#![allow(non_upper_case_globals)]
Expand Down
Loading

0 comments on commit 7ae3681

Please sign in to comment.