Skip to content

Commit d7a4d14

Browse files
committed
Fix pretty-printing of !.
This fixes several FIXMEs in `tests/ui/macros/stringify.rs`.
1 parent ebcd405 commit d7a4d14

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,7 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
173173
// ANYTHING + `,`: `foo,`
174174
// ANYTHING + `;`: `x = 3;`, `[T; 3]`
175175
// ANYTHING + `.`: `x.y`, `tup.0`
176-
// ANYTHING + `!`: `foo! { ... }`
177-
//
178-
// FIXME: Incorrect cases:
179-
// - Logical not: `x =! y`, `if! x { f(); }`
180-
// - Never type: `Fn() ->!`
181-
(_, Tok(Token { kind: Comma | Semi | Dot | Not, .. }, _)) => false,
176+
(_, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) => false,
182177

183178
// IDENT + `(`: `f(3)`
184179
//
@@ -189,6 +184,16 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
189184
// `#` + `[`: `#[attr]`
190185
(Tok(Token { kind: Pound, .. }, _), Del(_, Bracket, _)) => false,
191186

187+
// `#` + `!`: `#![attr]`
188+
(Tok(Token { kind: Pound, .. }, _), Tok(Token { kind: Not, .. }, _)) => false,
189+
190+
// IDENT + `!`: `println!()`, but `if !x { ... }` needs a space after the `if`
191+
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: Not, .. }, _))
192+
if !Ident::new(*sym, *span).is_reserved() || *is_raw =>
193+
{
194+
false
195+
}
196+
192197
_ => true,
193198
}
194199
}

tests/ui/macros/stringify.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ fn test_expr() {
103103
c1!(expr, [ true || false ], "true || false");
104104
c1!(expr, [ true || false && false ], "true || false && false");
105105
c1!(expr, [ a < 1 && 2 < b && c > 3 && 4 > d ], "a < 1 && 2 < b && c > 3 && 4 > d");
106-
c2!(expr, [ a & b & !c ], "a & b & !c", "a & b &! c"); // FIXME
106+
c2!(expr, [ a & b & !c ], "a & b & !c", "a & b & ! c");
107107
c2!(expr,
108108
[ a + b * c - d + -1 * -2 - -3],
109109
"a + b * c - d + -1 * -2 - -3",
110110
"a + b * c - d + - 1 * - 2 - - 3"
111111
);
112-
c2!(expr, [ x = !y ], "x = !y", "x =! y"); // FIXME
112+
c2!(expr, [ x = !y ], "x = !y", "x = ! y");
113113

114114
// ExprKind::Unary
115115
c2!(expr, [ *expr ], "*expr", "* expr");
@@ -132,7 +132,7 @@ fn test_expr() {
132132

133133
// ExprKind::If
134134
c1!(expr, [ if true {} ], "if true {}");
135-
c2!(expr, [ if !true {} ], "if !true {}", "if! true {}"); // FIXME
135+
c2!(expr, [ if !true {} ], "if !true {}", "if ! true {}");
136136
c2!(expr,
137137
[ if ::std::blah() { } else { } ],
138138
"if ::std::blah() {} else {}",
@@ -725,8 +725,8 @@ fn test_ty() {
725725
c2!(ty, [ Ref<'a> ], "Ref<'a>", "Ref < 'a >");
726726
c2!(ty, [ PhantomData<T> ], "PhantomData<T>", "PhantomData < T >");
727727
c2!(ty, [ PhantomData::<T> ], "PhantomData<T>", "PhantomData :: < T >");
728-
c2!(ty, [ Fn() -> ! ], "Fn() -> !", "Fn() ->!");
729-
c2!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !", "Fn(u8) ->!"); // FIXME
728+
c1!(ty, [ Fn() -> ! ], "Fn() -> !");
729+
c1!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !");
730730
c2!(ty, [ <Struct as Trait>::Type ], "<Struct as Trait>::Type", "< Struct as Trait > :: Type");
731731

732732
// TyKind::TraitObject

0 commit comments

Comments
 (0)