about summary refs log tree commit diff
path: root/compiler/rustc_parse/src
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-06-20 16:41:46 +0900
committerGitHub <noreply@github.com>2022-06-20 16:41:46 +0900
commitb6fb582cb7804da8ebe4f13027b14e7d4a248bff (patch)
treeaa3f3527b7f4013d4a7c3b79b1a4c2a8d94d1e12 /compiler/rustc_parse/src
parent9a0b7749665d925d8f533756149deba74f2db88b (diff)
parentfe9635f7c0751aaa23f0b0d2309882a1a4641f9b (diff)
downloadrust-b6fb582cb7804da8ebe4f13027b14e7d4a248bff.tar.gz
rust-b6fb582cb7804da8ebe4f13027b14e7d4a248bff.zip
Rollup merge of #98183 - dtolnay:emptybound, r=lcnr
Fix pretty printing of empty bound lists in where-clause

Repro:

```rust
macro_rules! assert_item_stringify {
    ($item:item $expected:literal) => {
        assert_eq!(stringify!($item), $expected);
    };
}

fn main() {
    assert_item_stringify! {
        fn f<'a, T>() where 'a:, T: {}
        "fn f<'a, T>() where 'a:, T: {}"
    }
}
```

Previously this assertion would fail because rustc renders the where-clause as `where 'a, T` which is invalid syntax.

This PR makes the above assertion pass.

This bug also affects `-Zunpretty=expanded`. The intention is for that to emit syntactically valid code, but the buggy output is not valid Rust syntax.

```console
$ rustc <(echo "fn f<'a, T>() where 'a:, T: {}") -Zunpretty=expanded
#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
fn f<'a, T>() where 'a, T {}
```

```console
$ rustc <(echo "fn f<'a, T>() where 'a:, T: {}") -Zunpretty=expanded | rustc -
error: expected `:`, found `,`
 --> <anon>:7:23
  |
7 | fn f<'a, T>() where 'a, T {}
  |                       ^ expected `:`
```
Diffstat (limited to 'compiler/rustc_parse/src')
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs5
1 files changed, 4 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index acf892cec53..ba325d70422 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -1355,7 +1355,10 @@ impl<'a> Parser<'a> {
                     s.print_mutability(mut_ty.mutbl, false);
                     s.popen();
                     s.print_type(&mut_ty.ty);
-                    s.print_type_bounds(" +", &bounds);
+                    if !bounds.is_empty() {
+                        s.word(" + ");
+                        s.print_type_bounds(&bounds);
+                    }
                     s.pclose()
                 });