Skip to content

Commit 92e90f9

Browse files
authored
Rollup merge of #74272 - davidtwco:issue-73626-multiline-mixed-comments, r=Mark-Simulacrum
pprust: support multiline comments within lines Fixes #73626. This PR adds support to `rustc_ast_pretty` for multiline comments that start and end within a line of source code. Fun fact: [the commit which added this assert](d12ea39) was from 2011! https://github.com/rust-lang/rust/blob/d12ea3989649616437a7c1434f5c5a6438235eb7/src/comp/pretty/pprust.rs#L1146-L1150
2 parents 7b1247c + 083c2f6 commit 92e90f9

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/librustc_ast_pretty/pprust.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,20 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
450450
fn print_comment(&mut self, cmnt: &comments::Comment) {
451451
match cmnt.style {
452452
comments::Mixed => {
453-
assert_eq!(cmnt.lines.len(), 1);
454453
self.zerobreak();
455-
self.word(cmnt.lines[0].clone());
454+
if let Some((last, lines)) = cmnt.lines.split_last() {
455+
self.ibox(0);
456+
457+
for line in lines {
458+
self.word(line.clone());
459+
self.hardbreak()
460+
}
461+
462+
self.word(last.clone());
463+
self.space();
464+
465+
self.end();
466+
}
456467
self.zerobreak()
457468
}
458469
comments::Isolated => {

src/test/pretty/issue-73626.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fn main(/*
2+
---
3+
*/) {
4+
let x /* this is one line */ = 3;
5+
6+
let x /*
7+
* this
8+
* is
9+
* multiple
10+
* lines
11+
*/ = 3;
12+
13+
let x = /*
14+
* this
15+
* is
16+
* multiple
17+
* lines
18+
* after
19+
* the
20+
* =
21+
*/ 3;
22+
23+
let x /*
24+
* this
25+
* is
26+
* multiple
27+
* lines
28+
* including
29+
* a
30+
31+
* blank
32+
* line
33+
*/ = 3;
34+
}

0 commit comments

Comments
 (0)