Skip to content

Commit b01220c

Browse files
committed
Dollar sign
1 parent 724ba7f commit b01220c

13 files changed

+346
-194
lines changed

compiler/rustc_expand/src/mbe/metavar_expr.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ impl MetaVarExpr {
4343
let mut iter = args.trees();
4444
let rslt = match ident.as_str() {
4545
"count" => parse_count(&mut iter, sess, ident.span)?,
46-
"ignore" => MetaVarExpr::Ignore(parse_ident(&mut iter, sess, ident.span)?),
46+
"ignore" => {
47+
eat_dollar(&mut iter, sess, ident.span)?;
48+
MetaVarExpr::Ignore(parse_ident(&mut iter, sess, ident.span)?)
49+
}
4750
"index" => MetaVarExpr::Index(parse_depth(&mut iter, sess, ident.span)?),
4851
"length" => MetaVarExpr::Length(parse_depth(&mut iter, sess, ident.span)?),
4952
_ => {
@@ -92,6 +95,7 @@ fn parse_count<'sess>(
9295
sess: &'sess ParseSess,
9396
span: Span,
9497
) -> PResult<'sess, MetaVarExpr> {
98+
eat_dollar(iter, sess, span)?;
9599
let ident = parse_ident(iter, sess, span)?;
96100
let depth = if try_eat_comma(iter) {
97101
if iter.look_ahead(0).is_none() {
@@ -166,3 +170,20 @@ fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
166170
}
167171
false
168172
}
173+
174+
/// Expects that the next item is a dollar sign.
175+
fn eat_dollar<'sess>(
176+
iter: &mut RefTokenTreeCursor<'_>,
177+
sess: &'sess ParseSess,
178+
span: Span,
179+
) -> PResult<'sess, ()> {
180+
if let Some(TokenTree::Token(token::Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0)
181+
{
182+
let _ = iter.next();
183+
return Ok(());
184+
}
185+
Err(sess.span_diagnostic.struct_span_err(
186+
span,
187+
"meta-variable expressions must be referenced using a dollar sign ($)",
188+
))
189+
}

library/core/src/tuple.rs

+143
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::marker::{StructuralEq, StructuralPartialEq};
88
//
99
// Also provides implementations for tuples with lesser arity. For example, tuple_impls!(A B C)
1010
// will implement everything for (A, B, C), (A, B) and (A,).
11+
#[cfg(bootstrap)]
1112
macro_rules! tuple_impls {
1213
// Stopping criteria (1-ary tuple)
1314
($T:ident) => {
@@ -145,6 +146,148 @@ macro_rules! tuple_impls {
145146
}
146147
}
147148

149+
// Recursive macro for implementing n-ary tuple functions and operations
150+
//
151+
// Also provides implementations for tuples with lesser arity. For example, tuple_impls!(A B C)
152+
// will implement everything for (A, B, C), (A, B) and (A,).
153+
#[cfg(not(bootstrap))]
154+
macro_rules! tuple_impls {
155+
// Stopping criteria (1-ary tuple)
156+
($T:ident) => {
157+
tuple_impls!(@impl $T);
158+
};
159+
// Running criteria (n-ary tuple, with n >= 2)
160+
($T:ident $( $U:ident )+) => {
161+
tuple_impls!($( $U )+);
162+
tuple_impls!(@impl $T $( $U )+);
163+
};
164+
// "Private" internal implementation
165+
(@impl $( $T:ident )+) => {
166+
maybe_tuple_doc! {
167+
$($T)+ @
168+
#[stable(feature = "rust1", since = "1.0.0")]
169+
impl<$($T: PartialEq),+> PartialEq for ($($T,)+)
170+
where
171+
last_type!($($T,)+): ?Sized
172+
{
173+
#[inline]
174+
fn eq(&self, other: &($($T,)+)) -> bool {
175+
$( ${ignore($T)} self.${index()} == other.${index()} )&&+
176+
}
177+
#[inline]
178+
fn ne(&self, other: &($($T,)+)) -> bool {
179+
$( ${ignore($T)} self.${index()} != other.${index()} )||+
180+
}
181+
}
182+
}
183+
184+
maybe_tuple_doc! {
185+
$($T)+ @
186+
#[stable(feature = "rust1", since = "1.0.0")]
187+
impl<$($T: Eq),+> Eq for ($($T,)+)
188+
where
189+
last_type!($($T,)+): ?Sized
190+
{}
191+
}
192+
193+
maybe_tuple_doc! {
194+
$($T)+ @
195+
#[unstable(feature = "structural_match", issue = "31434")]
196+
impl<$($T: ConstParamTy),+> ConstParamTy for ($($T,)+)
197+
{}
198+
}
199+
200+
maybe_tuple_doc! {
201+
$($T)+ @
202+
#[unstable(feature = "structural_match", issue = "31434")]
203+
impl<$($T),+> StructuralPartialEq for ($($T,)+)
204+
{}
205+
}
206+
207+
maybe_tuple_doc! {
208+
$($T)+ @
209+
#[unstable(feature = "structural_match", issue = "31434")]
210+
impl<$($T),+> StructuralEq for ($($T,)+)
211+
{}
212+
}
213+
214+
maybe_tuple_doc! {
215+
$($T)+ @
216+
#[stable(feature = "rust1", since = "1.0.0")]
217+
impl<$($T: PartialOrd),+> PartialOrd for ($($T,)+)
218+
where
219+
last_type!($($T,)+): ?Sized
220+
{
221+
#[inline]
222+
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
223+
lexical_partial_cmp!($( ${ignore($T)} self.${index()}, other.${index()} ),+)
224+
}
225+
#[inline]
226+
fn lt(&self, other: &($($T,)+)) -> bool {
227+
lexical_ord!(lt, Less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
228+
}
229+
#[inline]
230+
fn le(&self, other: &($($T,)+)) -> bool {
231+
lexical_ord!(le, Less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
232+
}
233+
#[inline]
234+
fn ge(&self, other: &($($T,)+)) -> bool {
235+
lexical_ord!(ge, Greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
236+
}
237+
#[inline]
238+
fn gt(&self, other: &($($T,)+)) -> bool {
239+
lexical_ord!(gt, Greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
240+
}
241+
}
242+
}
243+
244+
maybe_tuple_doc! {
245+
$($T)+ @
246+
#[stable(feature = "rust1", since = "1.0.0")]
247+
impl<$($T: Ord),+> Ord for ($($T,)+)
248+
where
249+
last_type!($($T,)+): ?Sized
250+
{
251+
#[inline]
252+
fn cmp(&self, other: &($($T,)+)) -> Ordering {
253+
lexical_cmp!($( ${ignore($T)} self.${index()}, other.${index()} ),+)
254+
}
255+
}
256+
}
257+
258+
maybe_tuple_doc! {
259+
$($T)+ @
260+
#[stable(feature = "rust1", since = "1.0.0")]
261+
impl<$($T: Default),+> Default for ($($T,)+) {
262+
#[inline]
263+
fn default() -> ($($T,)+) {
264+
($({ let x: $T = Default::default(); x},)+)
265+
}
266+
}
267+
}
268+
269+
#[stable(feature = "array_tuple_conv", since = "1.71.0")]
270+
impl<T> From<[T; ${count($T)}]> for ($(${ignore($T)} T,)+) {
271+
#[inline]
272+
#[allow(non_snake_case)]
273+
fn from(array: [T; ${count($T)}]) -> Self {
274+
let [$($T,)+] = array;
275+
($($T,)+)
276+
}
277+
}
278+
279+
#[stable(feature = "array_tuple_conv", since = "1.71.0")]
280+
impl<T> From<($(${ignore($T)} T,)+)> for [T; ${count($T)}] {
281+
#[inline]
282+
#[allow(non_snake_case)]
283+
fn from(tuple: ($(${ignore($T)} T,)+)) -> Self {
284+
let ($($T,)+) = tuple;
285+
[$($T,)+]
286+
}
287+
}
288+
}
289+
}
290+
148291
// If this is a unary tuple, it adds a doc comment.
149292
// Otherwise, it hides the docs entirely.
150293
macro_rules! maybe_tuple_doc {

tests/ui/macros/rfc-3086-metavar-expr/count-and-length-are-distinct.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ fn main() {
1010
$(
1111
// inner-most repetition
1212
$(
13-
${ignore(l)} ${index()}, ${length()},
13+
${ignore($l)} ${index()}, ${length()},
1414
)*
15-
${count(l)}, ${index()}, ${length()},
15+
${count($l)}, ${index()}, ${length()},
1616
)*
17-
${count(l)},
17+
${count($l)},
1818
]
1919
};
2020
}
@@ -72,30 +72,30 @@ fn main() {
7272
&[
7373
$( $( $(
7474
&[
75-
${ignore(i)} ${count(i, 0)},
75+
${ignore($i)} ${count($i, 0)},
7676
][..],
7777
)* )* )*
7878

7979
$( $(
8080
&[
81-
${ignore(i)} ${count(i, 0)},
82-
${ignore(i)} ${count(i, 1)},
81+
${ignore($i)} ${count($i, 0)},
82+
${ignore($i)} ${count($i, 1)},
8383
][..],
8484
)* )*
8585

8686
$(
8787
&[
88-
${ignore(i)} ${count(i, 0)},
89-
${ignore(i)} ${count(i, 1)},
90-
${ignore(i)} ${count(i, 2)},
88+
${ignore($i)} ${count($i, 0)},
89+
${ignore($i)} ${count($i, 1)},
90+
${ignore($i)} ${count($i, 2)},
9191
][..],
9292
)*
9393

9494
&[
95-
${count(i, 0)},
96-
${count(i, 1)},
97-
${count(i, 2)},
98-
${count(i, 3)},
95+
${count($i, 0)},
96+
${count($i, 1)},
97+
${count($i, 2)},
98+
${count($i, 3)},
9999
][..]
100100
][..]
101101
}
@@ -170,31 +170,31 @@ fn main() {
170170
&[
171171
$( $( $( $(
172172
&[
173-
${ignore(i)} ${length(3)},
174-
${ignore(i)} ${length(2)},
175-
${ignore(i)} ${length(1)},
176-
${ignore(i)} ${length(0)},
173+
${ignore($i)} ${length(3)},
174+
${ignore($i)} ${length(2)},
175+
${ignore($i)} ${length(1)},
176+
${ignore($i)} ${length(0)},
177177
][..],
178178
)* )* )* )*
179179

180180
$( $( $(
181181
&[
182-
${ignore(i)} ${length(2)},
183-
${ignore(i)} ${length(1)},
184-
${ignore(i)} ${length(0)},
182+
${ignore($i)} ${length(2)},
183+
${ignore($i)} ${length(1)},
184+
${ignore($i)} ${length(0)},
185185
][..],
186186
)* )* )*
187187

188188
$( $(
189189
&[
190-
${ignore(i)} ${length(1)},
191-
${ignore(i)} ${length(0)},
190+
${ignore($i)} ${length(1)},
191+
${ignore($i)} ${length(0)},
192192
][..],
193193
)* )*
194194

195195
$(
196196
&[
197-
${ignore(i)} ${length(0)},
197+
${ignore($i)} ${length(0)},
198198
][..],
199199
)*
200200
][..]

tests/ui/macros/rfc-3086-metavar-expr/feature-gate-macro_metavar_expr.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
/// Count the number of idents in a macro repetition.
66
macro_rules! count_idents {
77
( $( $i:ident ),* ) => {
8-
${count(i)}
8+
${count($i)}
99
};
1010
}
1111

1212
/// Count the number of idents in a 2-dimensional macro repetition.
1313
macro_rules! count_idents_2 {
1414
( $( [ $( $i:ident ),* ] ),* ) => {
15-
${count(i)}
15+
${count($i)}
1616
};
1717
}
1818

@@ -21,17 +21,17 @@ macro_rules! count_depth_limits {
2121
( $( { $( [ $( $outer:ident : ( $( $inner:ident )* ) )* ] )* } )* ) => {
2222
(
2323
(
24-
${count(inner)},
25-
${count(inner, 0)},
26-
${count(inner, 1)},
27-
${count(inner, 2)},
28-
${count(inner, 3)},
24+
${count($inner)},
25+
${count($inner, 0)},
26+
${count($inner, 1)},
27+
${count($inner, 2)},
28+
${count($inner, 3)},
2929
),
3030
(
31-
${count(outer)},
32-
${count(outer, 0)},
33-
${count(outer, 1)},
34-
${count(outer, 2)},
31+
${count($outer)},
32+
${count($outer, 0)},
33+
${count($outer, 1)},
34+
${count($outer, 2)},
3535
),
3636
)
3737
};
@@ -43,7 +43,7 @@ macro_rules! count_depth_limits {
4343
/// repetition binding.
4444
macro_rules! enumerate_literals {
4545
( $( ($l:stmt) ),* ) => {
46-
[$( ${ignore(l)} (${index()}, ${length()}) ),*]
46+
[$( ${ignore($l)} (${index()}, ${length()}) ),*]
4747
};
4848
}
4949

@@ -77,7 +77,7 @@ macro_rules! make_count_adders {
7777
$(
7878
macro_rules! $i {
7979
( $$( $$j:ident ),* ) => {
80-
$b + $${count(j)}
80+
$b + $${count($j)}
8181
};
8282
}
8383
)*

tests/ui/macros/rfc-3086-metavar-expr/issue-111904.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(macro_metavar_expr)]
22

33
macro_rules! foo {
4-
( $( $($t:ident),* );* ) => { ${count(t,)} }
4+
( $( $($t:ident),* );* ) => { ${count($t,)} }
55
//~^ ERROR `count` followed by a comma must have an associated
66
//~| ERROR expected expression, found `$`
77
}

tests/ui/macros/rfc-3086-metavar-expr/issue-111904.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: `count` followed by a comma must have an associated index indicating its depth
22
--> $DIR/issue-111904.rs:4:37
33
|
4-
LL | ( $( $($t:ident),* );* ) => { ${count(t,)} }
4+
LL | ( $( $($t:ident),* );* ) => { ${count($t,)} }
55
| ^^^^^
66

77
error: expected expression, found `$`
88
--> $DIR/issue-111904.rs:4:35
99
|
10-
LL | ( $( $($t:ident),* );* ) => { ${count(t,)} }
10+
LL | ( $( $($t:ident),* );* ) => { ${count($t,)} }
1111
| ^ expected expression
1212
...
1313
LL | foo!(a, a; b, b);

0 commit comments

Comments
 (0)