Skip to content

Commit f9a4849

Browse files
committed
auto merge of #19984 : japaric/rust/macro-expressions, r=alexcrichton
followed by a semicolon. This allows code like `vec![1i, 2, 3].len();` to work. This breaks code that uses macros as statements without putting semicolons after them, such as: fn main() { ... assert!(a == b) assert!(c == d) println(...); } It also breaks code that uses macros as items without semicolons: local_data_key!(foo) fn main() { println("hello world") } Add semicolons to fix this code. Those two examples can be fixed as follows: fn main() { ... assert!(a == b); assert!(c == d); println(...); } local_data_key!(foo); fn main() { println("hello world") } RFC #378. Closes #18635. [breaking-change] --- Rebased version of #18958 r? @alexcrichton cc @pcwalton
2 parents c4d58ce + ddb2466 commit f9a4849

File tree

222 files changed

+2338
-2047
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

222 files changed

+2338
-2047
lines changed

src/doc/guide-macros.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ macro_rules! early_return(
5858
_ => {}
5959
}
6060
);
61-
)
61+
);
6262
// ...
6363
early_return!(input_1 T::SpecialA);
6464
// ...
@@ -179,8 +179,8 @@ macro_rules! early_return(
179179
)+
180180
_ => {}
181181
}
182-
);
183-
)
182+
)
183+
);
184184
// ...
185185
early_return!(input_1, [T::SpecialA|T::SpecialC|T::SpecialD]);
186186
// ...
@@ -275,17 +275,17 @@ macro_rules! biased_match (
275275
_ => { $err }
276276
};
277277
)
278-
)
278+
);
279279
280280
# enum T1 { Good1(T2, uint), Bad1}
281281
# struct T2 { body: T3 }
282282
# enum T3 { Good2(uint), Bad2}
283283
# fn f(x: T1) -> uint {
284284
biased_match!((x) ~ (T1::Good1(g1, val)) else { return 0 };
285-
binds g1, val )
285+
binds g1, val );
286286
biased_match!((g1.body) ~ (T3::Good2(result) )
287287
else { panic!("Didn't get good_2") };
288-
binds result )
288+
binds result );
289289
// complicated stuff goes here
290290
return result + val;
291291
# }
@@ -303,7 +303,7 @@ pattern we want is clear:
303303
( $( ($e:expr) ~ ($p:pat) else $err:stmt ; )*
304304
binds $( $bind_res:ident ),*
305305
)
306-
# => (0))
306+
# => (0));
307307
~~~~
308308

309309
However, it's not possible to directly expand to nested match statements. But
@@ -323,7 +323,7 @@ input patterns:
323323
# #![feature(macro_rules)]
324324
# macro_rules! b(
325325
( binds $( $bind_res:ident ),* )
326-
# => (0))
326+
# => (0));
327327
# fn main() {}
328328
~~~~
329329

@@ -337,7 +337,7 @@ input patterns:
337337
$( ($e_rest:expr) ~ ($p_rest:pat) else $err_rest:stmt ; )*
338338
binds $( $bind_res:ident ),*
339339
)
340-
# => (0))
340+
# => (0));
341341
~~~~
342342

343343
The resulting macro looks like this. Note that the separation into
@@ -366,7 +366,7 @@ macro_rules! biased_match_rec (
366366
);
367367
// Produce the requested values
368368
( binds $( $bind_res:ident ),* ) => ( ($( $bind_res ),*) )
369-
)
369+
);
370370
371371
// Wrap the whole thing in a `let`.
372372
macro_rules! biased_match (
@@ -388,7 +388,7 @@ macro_rules! biased_match (
388388
binds $( $bind_res ),*
389389
);
390390
)
391-
)
391+
);
392392
393393
394394
# enum T1 { Good1(T2, uint), Bad1}
@@ -398,7 +398,7 @@ macro_rules! biased_match (
398398
biased_match!(
399399
(x) ~ (T1::Good1(g1, val)) else { return 0 };
400400
(g1.body) ~ (T3::Good2(result) ) else { panic!("Didn't get Good2") };
401-
binds val, result )
401+
binds val, result );
402402
// complicated stuff goes here
403403
return result + val;
404404
# }
@@ -444,7 +444,7 @@ macro_rules! loop_x (
444444
$e
445445
}
446446
);
447-
)
447+
);
448448
449449
fn main() {
450450
'x: loop {
@@ -482,30 +482,30 @@ An example:
482482

483483
```rust
484484
# #![feature(macro_rules)]
485-
macro_rules! m1 (() => (()))
485+
macro_rules! m1 (() => (()));
486486

487487
// visible here: m1
488488

489489
mod foo {
490490
// visible here: m1
491491

492492
#[macro_export]
493-
macro_rules! m2 (() => (()))
493+
macro_rules! m2 (() => (()));
494494

495495
// visible here: m1, m2
496496
}
497497

498498
// visible here: m1
499499

500-
macro_rules! m3 (() => (()))
500+
macro_rules! m3 (() => (()));
501501

502502
// visible here: m1, m3
503503

504504
#[macro_escape]
505505
mod bar {
506506
// visible here: m1, m3
507507

508-
macro_rules! m4 (() => (()))
508+
macro_rules! m4 (() => (()));
509509

510510
// visible here: m1, m3, m4
511511
}

src/etc/regex-match-tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def read_tests(f):
6363
def test_tostr(t):
6464
lineno, pat, text, groups = t
6565
options = map(group_tostr, groups)
66-
return 'mat!(match_%s, r"%s", r"%s", %s)' \
66+
return 'mat!{match_%s, r"%s", r"%s", %s}' \
6767
% (lineno, pat, '' if text == "NULL" else text, ', '.join(options))
6868

6969

src/libcollections/bit.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ mod tests {
20832083
let bools = vec![true, false, true, true];
20842084
let bitv: Bitv = bools.iter().map(|n| *n).collect();
20852085

2086-
assert_eq!(bitv.iter().collect::<Vec<bool>>(), bools)
2086+
assert_eq!(bitv.iter().collect::<Vec<bool>>(), bools);
20872087

20882088
let long = Vec::from_fn(10000, |i| i % 2 == 0);
20892089
let bitv: Bitv = long.iter().map(|n| *n).collect();
@@ -2112,8 +2112,8 @@ mod tests {
21122112
for &b in bools.iter() {
21132113
for &l in lengths.iter() {
21142114
let bitset = BitvSet::from_bitv(Bitv::with_capacity(l, b));
2115-
assert_eq!(bitset.contains(&1u), b)
2116-
assert_eq!(bitset.contains(&(l-1u)), b)
2115+
assert_eq!(bitset.contains(&1u), b);
2116+
assert_eq!(bitset.contains(&(l-1u)), b);
21172117
assert!(!bitset.contains(&l))
21182118
}
21192119
}
@@ -2321,12 +2321,12 @@ mod tests {
23212321
assert!(!a.is_disjoint(&d));
23222322
assert!(!d.is_disjoint(&a));
23232323

2324-
assert!(a.is_disjoint(&b))
2325-
assert!(a.is_disjoint(&c))
2326-
assert!(b.is_disjoint(&a))
2327-
assert!(b.is_disjoint(&c))
2328-
assert!(c.is_disjoint(&a))
2329-
assert!(c.is_disjoint(&b))
2324+
assert!(a.is_disjoint(&b));
2325+
assert!(a.is_disjoint(&c));
2326+
assert!(b.is_disjoint(&a));
2327+
assert!(b.is_disjoint(&c));
2328+
assert!(c.is_disjoint(&a));
2329+
assert!(c.is_disjoint(&b));
23302330
}
23312331

23322332
#[test]

src/libcollections/enum_set.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ mod test {
411411

412412
assert!(e1.is_subset(&e2));
413413
assert!(e2.is_superset(&e1));
414-
assert!(!e3.is_superset(&e2))
414+
assert!(!e3.is_superset(&e2));
415415
assert!(!e2.is_superset(&e3))
416416
}
417417

@@ -438,23 +438,23 @@ mod test {
438438
let mut e1: EnumSet<Foo> = EnumSet::new();
439439

440440
let elems: ::vec::Vec<Foo> = e1.iter().collect();
441-
assert!(elems.is_empty())
441+
assert!(elems.is_empty());
442442

443443
e1.insert(A);
444444
let elems: ::vec::Vec<_> = e1.iter().collect();
445-
assert_eq!(vec![A], elems)
445+
assert_eq!(vec![A], elems);
446446

447447
e1.insert(C);
448448
let elems: ::vec::Vec<_> = e1.iter().collect();
449-
assert_eq!(vec![A,C], elems)
449+
assert_eq!(vec![A,C], elems);
450450

451451
e1.insert(C);
452452
let elems: ::vec::Vec<_> = e1.iter().collect();
453-
assert_eq!(vec![A,C], elems)
453+
assert_eq!(vec![A,C], elems);
454454

455455
e1.insert(B);
456456
let elems: ::vec::Vec<_> = e1.iter().collect();
457-
assert_eq!(vec![A,B,C], elems)
457+
assert_eq!(vec![A,B,C], elems);
458458
}
459459

460460
///////////////////////////////////////////////////////////////////////////
@@ -472,35 +472,35 @@ mod test {
472472

473473
let e_union = e1 | e2;
474474
let elems: ::vec::Vec<_> = e_union.iter().collect();
475-
assert_eq!(vec![A,B,C], elems)
475+
assert_eq!(vec![A,B,C], elems);
476476

477477
let e_intersection = e1 & e2;
478478
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
479-
assert_eq!(vec![C], elems)
479+
assert_eq!(vec![C], elems);
480480

481481
// Another way to express intersection
482482
let e_intersection = e1 - (e1 - e2);
483483
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
484-
assert_eq!(vec![C], elems)
484+
assert_eq!(vec![C], elems);
485485

486486
let e_subtract = e1 - e2;
487487
let elems: ::vec::Vec<_> = e_subtract.iter().collect();
488-
assert_eq!(vec![A], elems)
488+
assert_eq!(vec![A], elems);
489489

490490
// Bitwise XOR of two sets, aka symmetric difference
491491
let e_symmetric_diff = e1 ^ e2;
492492
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
493-
assert_eq!(vec![A,B], elems)
493+
assert_eq!(vec![A,B], elems);
494494

495495
// Another way to express symmetric difference
496496
let e_symmetric_diff = (e1 - e2) | (e2 - e1);
497497
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
498-
assert_eq!(vec![A,B], elems)
498+
assert_eq!(vec![A,B], elems);
499499

500500
// Yet another way to express symmetric difference
501501
let e_symmetric_diff = (e1 | e2) - (e1 & e2);
502502
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
503-
assert_eq!(vec![A,B], elems)
503+
assert_eq!(vec![A,B], elems);
504504
}
505505

506506
#[test]

src/libcollections/macros.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#![macro_escape]
1212

1313
/// Creates a `std::vec::Vec` containing the arguments.
14-
macro_rules! vec(
14+
macro_rules! vec {
1515
($($e:expr),*) => ({
1616
// leading _ to allow empty construction without a warning.
1717
let mut _temp = ::vec::Vec::new();
1818
$(_temp.push($e);)*
1919
_temp
2020
});
2121
($($e:expr),+,) => (vec!($($e),+))
22-
)
22+
}
23+

src/libcollections/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,7 @@ mod tests {
25152515
assert_eq!(format!("{}", x), x_str);
25162516
assert_eq!(format!("{}", x.as_slice()), x_str);
25172517
})
2518-
)
2518+
);
25192519
let empty: Vec<int> = vec![];
25202520
test_show_vec!(empty, "[]");
25212521
test_show_vec!(vec![1i], "[1]");

src/libcollections/str.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,14 @@ Section: Misc
415415
// Return the initial codepoint accumulator for the first byte.
416416
// The first byte is special, only want bottom 5 bits for width 2, 4 bits
417417
// for width 3, and 3 bits for width 4
418-
macro_rules! utf8_first_byte(
418+
macro_rules! utf8_first_byte {
419419
($byte:expr, $width:expr) => (($byte & (0x7F >> $width)) as u32)
420-
)
420+
}
421421

422422
// return the value of $ch updated with continuation byte $byte
423-
macro_rules! utf8_acc_cont_byte(
423+
macro_rules! utf8_acc_cont_byte {
424424
($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63u8) as u32)
425-
)
425+
}
426426

427427
/*
428428
Section: MaybeOwned

src/libcollections/string.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl String {
167167
subseqidx = i;
168168
res.as_mut_vec().push_all(REPLACEMENT);
169169
}
170-
}))
170+
}));
171171

172172
if byte < 128u8 {
173173
// subseqidx handles this
@@ -788,8 +788,8 @@ macro_rules! impl_eq {
788788
}
789789
}
790790

791-
impl_eq!(String, &'a str)
792-
impl_eq!(CowString<'a>, String)
791+
impl_eq! { String, &'a str }
792+
impl_eq! { CowString<'a>, String }
793793

794794
impl<'a, 'b> PartialEq<&'b str> for CowString<'a> {
795795
#[inline]

src/libcollections/tree/map.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ macro_rules! define_iterator {
900900
) => {
901901
// private methods on the forward iterator (item!() for the
902902
// addr_mut in the next_ return value)
903-
item!(impl<'a, K, V> $name<'a, K, V> {
903+
item! { impl<'a, K, V> $name<'a, K, V> {
904904
#[inline(always)]
905905
fn next_(&mut self, forward: bool) -> Option<(&'a K, &'a $($addr_mut)* V)> {
906906
while !self.stack.is_empty() || !self.node.is_null() {
@@ -968,10 +968,10 @@ macro_rules! define_iterator {
968968
self.node = ptr::RawPtr::null();
969969
}
970970
}
971-
})
971+
} }
972972

973973
// the forward Iterator impl.
974-
item!(impl<'a, K, V> Iterator<(&'a K, &'a $($addr_mut)* V)> for $name<'a, K, V> {
974+
item! { impl<'a, K, V> Iterator<(&'a K, &'a $($addr_mut)* V)> for $name<'a, K, V> {
975975
/// Advances the iterator to the next node (in order) and return a
976976
/// tuple with a reference to the key and value. If there are no
977977
/// more nodes, return `None`.
@@ -983,10 +983,10 @@ macro_rules! define_iterator {
983983
fn size_hint(&self) -> (uint, Option<uint>) {
984984
(self.remaining_min, Some(self.remaining_max))
985985
}
986-
})
986+
} }
987987

988988
// the reverse Iterator impl.
989-
item!(impl<'a, K, V> Iterator<(&'a K, &'a $($addr_mut)* V)> for $rev_name<'a, K, V> {
989+
item! { impl<'a, K, V> Iterator<(&'a K, &'a $($addr_mut)* V)> for $rev_name<'a, K, V> {
990990
fn next(&mut self) -> Option<(&'a K, &'a $($addr_mut)* V)> {
991991
self.iter.next_(false)
992992
}
@@ -995,7 +995,7 @@ macro_rules! define_iterator {
995995
fn size_hint(&self) -> (uint, Option<uint>) {
996996
self.iter.size_hint()
997997
}
998-
})
998+
} }
999999
}
10001000
} // end of define_iterator
10011001

src/libcollections/trie/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ macro_rules! iterator_impl {
11411141
}
11421142
}
11431143

1144-
item!(impl<'a, T> Iterator<(uint, &'a $($mut_)* T)> for $name<'a, T> {
1144+
item! { impl<'a, T> Iterator<(uint, &'a $($mut_)* T)> for $name<'a, T> {
11451145
// you might wonder why we're not even trying to act within the
11461146
// rules, and are just manipulating raw pointers like there's no
11471147
// such thing as invalid pointers and memory unsafety. The
@@ -1213,7 +1213,7 @@ macro_rules! iterator_impl {
12131213
fn size_hint(&self) -> (uint, Option<uint>) {
12141214
(self.remaining_min, Some(self.remaining_max))
12151215
}
1216-
})
1216+
} }
12171217
}
12181218
}
12191219

0 commit comments

Comments
 (0)