Skip to content

Commit 6a855f6

Browse files
committed
librustc: Always parse macro!()/macro![] as expressions if not
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]
1 parent e09d986 commit 6a855f6

File tree

226 files changed

+2301
-2023
lines changed

Some content is hidden

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

226 files changed

+2301
-2023
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
@@ -2073,7 +2073,7 @@ mod tests {
20732073
let bools = vec![true, false, true, true];
20742074
let bitv: Bitv = bools.iter().map(|n| *n).collect();
20752075

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

20782078
let long = Vec::from_fn(10000, |i| i % 2 == 0);
20792079
let bitv: Bitv = long.iter().map(|n| *n).collect();
@@ -2102,8 +2102,8 @@ mod tests {
21022102
for &b in bools.iter() {
21032103
for &l in lengths.iter() {
21042104
let bitset = BitvSet::from_bitv(Bitv::with_capacity(l, b));
2105-
assert_eq!(bitset.contains(&1u), b)
2106-
assert_eq!(bitset.contains(&(l-1u)), b)
2105+
assert_eq!(bitset.contains(&1u), b);
2106+
assert_eq!(bitset.contains(&(l-1u)), b);
21072107
assert!(!bitset.contains(&l))
21082108
}
21092109
}
@@ -2311,12 +2311,12 @@ mod tests {
23112311
assert!(!a.is_disjoint(&d));
23122312
assert!(!d.is_disjoint(&a));
23132313

2314-
assert!(a.is_disjoint(&b))
2315-
assert!(a.is_disjoint(&c))
2316-
assert!(b.is_disjoint(&a))
2317-
assert!(b.is_disjoint(&c))
2318-
assert!(c.is_disjoint(&a))
2319-
assert!(c.is_disjoint(&b))
2314+
assert!(a.is_disjoint(&b));
2315+
assert!(a.is_disjoint(&c));
2316+
assert!(b.is_disjoint(&a));
2317+
assert!(b.is_disjoint(&c));
2318+
assert!(c.is_disjoint(&a));
2319+
assert!(c.is_disjoint(&b));
23202320
}
23212321

23222322
#[test]

src/libcollections/btree/node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ impl<K, V> Node<K, V> {
455455
/// Take all the values from right, separated by the given key and value
456456
fn absorb(&mut self, key: K, val: V, right: Node<K, V>) {
457457
// Just as a sanity check, make sure we can fit this guy in
458-
debug_assert!(self.len() + right.len() <= self.capacity())
458+
debug_assert!(self.len() + right.len() <= self.capacity());
459459

460460
self.keys.push(key);
461461
self.vals.push(val);

src/libcollections/enum_set.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ mod test {
373373

374374
assert!(e1.is_subset(&e2));
375375
assert!(e2.is_superset(&e1));
376-
assert!(!e3.is_superset(&e2))
376+
assert!(!e3.is_superset(&e2));
377377
assert!(!e2.is_superset(&e3))
378378
}
379379

@@ -400,23 +400,23 @@ mod test {
400400
let mut e1: EnumSet<Foo> = EnumSet::new();
401401

402402
let elems: Vec<Foo> = e1.iter().collect();
403-
assert!(elems.is_empty())
403+
assert!(elems.is_empty());
404404

405405
e1.insert(A);
406406
let elems = e1.iter().collect();
407-
assert_eq!(vec![A], elems)
407+
assert_eq!(vec![A], elems);
408408

409409
e1.insert(C);
410410
let elems = e1.iter().collect();
411-
assert_eq!(vec![A,C], elems)
411+
assert_eq!(vec![A,C], elems);
412412

413413
e1.insert(C);
414414
let elems = e1.iter().collect();
415-
assert_eq!(vec![A,C], elems)
415+
assert_eq!(vec![A,C], elems);
416416

417417
e1.insert(B);
418418
let elems = e1.iter().collect();
419-
assert_eq!(vec![A,B,C], elems)
419+
assert_eq!(vec![A,B,C], elems);
420420
}
421421

422422
///////////////////////////////////////////////////////////////////////////
@@ -434,35 +434,35 @@ mod test {
434434

435435
let e_union = e1 | e2;
436436
let elems = e_union.iter().collect();
437-
assert_eq!(vec![A,B,C], elems)
437+
assert_eq!(vec![A,B,C], elems);
438438

439439
let e_intersection = e1 & e2;
440440
let elems = e_intersection.iter().collect();
441-
assert_eq!(vec![C], elems)
441+
assert_eq!(vec![C], elems);
442442

443443
// Another way to express intersection
444444
let e_intersection = e1 - (e1 - e2);
445445
let elems = e_intersection.iter().collect();
446-
assert_eq!(vec![C], elems)
446+
assert_eq!(vec![C], elems);
447447

448448
let e_subtract = e1 - e2;
449449
let elems = e_subtract.iter().collect();
450-
assert_eq!(vec![A], elems)
450+
assert_eq!(vec![A], elems);
451451

452452
// Bitwise XOR of two sets, aka symmetric difference
453453
let e_symmetric_diff = e1 ^ e2;
454454
let elems = e_symmetric_diff.iter().collect();
455-
assert_eq!(vec![A,B], elems)
455+
assert_eq!(vec![A,B], elems);
456456

457457
// Another way to express symmetric difference
458458
let e_symmetric_diff = (e1 - e2) | (e2 - e1);
459459
let elems = e_symmetric_diff.iter().collect();
460-
assert_eq!(vec![A,B], elems)
460+
assert_eq!(vec![A,B], elems);
461461

462462
// Yet another way to express symmetric difference
463463
let e_symmetric_diff = (e1 | e2) - (e1 & e2);
464464
let elems = e_symmetric_diff.iter().collect();
465-
assert_eq!(vec![A,B], elems)
465+
assert_eq!(vec![A,B], elems);
466466
}
467467

468468
#[test]

src/libcollections/hash/mod.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ macro_rules! impl_hash {
113113
}
114114
}
115115

116-
impl_hash!(u8, u8)
117-
impl_hash!(u16, u16)
118-
impl_hash!(u32, u32)
119-
impl_hash!(u64, u64)
120-
impl_hash!(uint, uint)
121-
impl_hash!(i8, u8)
122-
impl_hash!(i16, u16)
123-
impl_hash!(i32, u32)
124-
impl_hash!(i64, u64)
125-
impl_hash!(int, uint)
116+
impl_hash! { u8, u8 }
117+
impl_hash! { u16, u16 }
118+
impl_hash! { u32, u32 }
119+
impl_hash! { u64, u64 }
120+
impl_hash! { uint, uint }
121+
impl_hash! { i8, u8 }
122+
impl_hash! { i16, u16 }
123+
impl_hash! { i32, u32 }
124+
impl_hash! { i64, u64 }
125+
impl_hash! { int, uint }
126126

127127
impl<S: Writer> Hash<S> for bool {
128128
#[inline]
@@ -146,7 +146,7 @@ impl<S: Writer> Hash<S> for str {
146146
}
147147
}
148148

149-
macro_rules! impl_hash_tuple(
149+
macro_rules! impl_hash_tuple {
150150
() => (
151151
impl<S: Writer> Hash<S> for () {
152152
#[inline]
@@ -171,21 +171,21 @@ macro_rules! impl_hash_tuple(
171171
}
172172
}
173173
);
174-
)
175-
176-
impl_hash_tuple!()
177-
impl_hash_tuple!(A)
178-
impl_hash_tuple!(A B)
179-
impl_hash_tuple!(A B C)
180-
impl_hash_tuple!(A B C D)
181-
impl_hash_tuple!(A B C D E)
182-
impl_hash_tuple!(A B C D E F)
183-
impl_hash_tuple!(A B C D E F G)
184-
impl_hash_tuple!(A B C D E F G H)
185-
impl_hash_tuple!(A B C D E F G H I)
186-
impl_hash_tuple!(A B C D E F G H I J)
187-
impl_hash_tuple!(A B C D E F G H I J K)
188-
impl_hash_tuple!(A B C D E F G H I J K L)
174+
}
175+
176+
impl_hash_tuple! {}
177+
impl_hash_tuple! { A }
178+
impl_hash_tuple! { A B }
179+
impl_hash_tuple! { A B C }
180+
impl_hash_tuple! { A B C D }
181+
impl_hash_tuple! { A B C D E }
182+
impl_hash_tuple! { A B C D E F }
183+
impl_hash_tuple! { A B C D E F G }
184+
impl_hash_tuple! { A B C D E F G H }
185+
impl_hash_tuple! { A B C D E F G H I }
186+
impl_hash_tuple! { A B C D E F G H I J }
187+
impl_hash_tuple! { A B C D E F G H I J K }
188+
impl_hash_tuple! { A B C D E F G H I J K L }
189189

190190
impl<S: Writer, T: Hash<S>> Hash<S> for [T] {
191191
#[inline]

src/libcollections/hash/sip.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub struct SipState {
4747
// because they're needed in the following defs;
4848
// this design could be improved.
4949

50-
macro_rules! u8to64_le (
50+
macro_rules! u8to64_le {
5151
($buf:expr, $i:expr) =>
5252
($buf[0+$i] as u64 |
5353
$buf[1+$i] as u64 << 8 |
@@ -67,14 +67,14 @@ macro_rules! u8to64_le (
6767
}
6868
out
6969
});
70-
)
70+
}
7171

72-
macro_rules! rotl (
72+
macro_rules! rotl {
7373
($x:expr, $b:expr) =>
7474
(($x << $b) | ($x >> (64 - $b)))
75-
)
75+
}
7676

77-
macro_rules! compress (
77+
macro_rules! compress {
7878
($v0:expr, $v1:expr, $v2:expr, $v3:expr) =>
7979
({
8080
$v0 += $v1; $v1 = rotl!($v1, 13); $v1 ^= $v0;
@@ -84,7 +84,7 @@ macro_rules! compress (
8484
$v2 += $v1; $v1 = rotl!($v1, 17); $v1 ^= $v2;
8585
$v2 = rotl!($v2, 32);
8686
})
87-
)
87+
}
8888

8989
impl SipState {
9090
/// Creates a `SipState` that is keyed off the provided keys.

0 commit comments

Comments
 (0)