Skip to content

Commit 0fd174d

Browse files
committed
Handle binary operators and lifetimes
1 parent 3f7b59c commit 0fd174d

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/macros.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,16 @@ impl MacroArgParser {
669669
if self.buf.is_empty() {
670670
self.lo = lo;
671671
self.start_tok = t.clone();
672-
} else if force_space_before(t) {
673-
self.buf.push(' ');
672+
} else {
673+
let needs_space = match next_space(&self.last_tok) {
674+
SpaceState::Ident => ident_like(t),
675+
SpaceState::Punctuation => !ident_like(t),
676+
SpaceState::Always => true,
677+
SpaceState::Never => false,
678+
};
679+
if force_space_before(t) || needs_space {
680+
self.buf.push(' ');
681+
}
674682
}
675683

676684
self.buf.push_str(&pprust::token_to_string(t));
@@ -888,9 +896,9 @@ fn force_space_before(tok: &Token) -> bool {
888896
| Token::RArrow
889897
| Token::LArrow
890898
| Token::FatArrow
899+
| Token::BinOp(_)
891900
| Token::Pound
892901
| Token::Dollar => true,
893-
Token::BinOp(bot) => bot != BinOpToken::Star,
894902
_ => false,
895903
}
896904
}
@@ -907,6 +915,7 @@ fn next_space(tok: &Token) -> SpaceState {
907915

908916
match *tok {
909917
Token::Not
918+
| Token::BinOp(BinOpToken::And)
910919
| Token::Tilde
911920
| Token::At
912921
| Token::Comma
@@ -916,8 +925,7 @@ fn next_space(tok: &Token) -> SpaceState {
916925
| Token::DotDotEq
917926
| Token::DotEq
918927
| Token::Question
919-
| Token::Underscore
920-
| Token::BinOp(_) => SpaceState::Punctuation,
928+
| Token::Underscore => SpaceState::Punctuation,
921929

922930
Token::ModSep
923931
| Token::Pound

tests/source/macro_rules.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ macro_rules! m {
1010
( $( $ i : ident : $ ty : ty , $def : expr , $stb : expr , $ ( $ dstring : tt ) , + ) ; + $ ( ; ) *
1111
$( $ i : ident : $ ty : ty , $def : expr , $stb : expr , $ ( $ dstring : tt ) , + ) ; + $ ( ; ) *
1212
) => {};
13+
( $foo: tt foo [$ attr : meta] $name: ident ) => {};
14+
( $foo: tt [$ attr: meta] $name: ident ) => {};
15+
( $foo: tt &'a [$attr : meta] $name: ident ) => {};
16+
( $foo: tt foo # [ $attr : meta] $name: ident ) => {};
17+
( $foo: tt # [ $attr : meta] $name: ident) => {};
18+
( $foo: tt &'a # [ $attr : meta] $name: ident ) => {};
19+
( $ x : tt foo bar foo bar foo bar $ y : tt => x*y*z $ z : tt , $ ( $a: tt ) , * ) => {};
1320
}
1421

1522
macro_rules! m {

tests/target/macro_rules.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ macro_rules! m {
1818
$($i: ident: $ty: ty, $def: expr, $stb: expr, $($dstring: tt),+);+ $(;)*
1919
$($i: ident: $ty: ty, $def: expr, $stb: expr, $($dstring: tt),+);+ $(;)*
2020
) => {};
21+
($foo: tt foo[$attr: meta] $name: ident) => {};
22+
($foo: tt[$attr: meta] $name: ident) => {};
23+
($foo: tt &'a[$attr: meta] $name: ident) => {};
24+
($foo: tt foo #[$attr: meta] $name: ident) => {};
25+
($foo: tt #[$attr: meta] $name: ident) => {};
26+
($foo: tt &'a #[$attr: meta] $name: ident) => {};
27+
($x: tt foo bar foo bar foo bar $y: tt => x * y * z $z: tt, $($a: tt),*) => {};
2128
}
2229

2330
macro_rules! m {

0 commit comments

Comments
 (0)