Skip to content

Commit 1961bf9

Browse files
committed
fix: Include frontmatter in -Zunpretty output
In the implementation (#140035), this was left as an open question for the tracking issue (#136889). My assumption is that this should be carried over. Thankfully, either way, `-Zunpretty` is unstable and we can always change it even if we stabilize frontmatter.
1 parent 425cd0f commit 1961bf9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment>
120120
pos += shebang_len;
121121
}
122122

123-
for token in rustc_lexer::tokenize(&text[pos..]) {
123+
for token in rustc_lexer::tokenize_document(&text[pos..]) {
124124
let token_text = &text[pos..pos + token.len as usize];
125125
match token.kind {
126126
rustc_lexer::TokenKind::Whitespace => {
@@ -171,6 +171,14 @@ fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment>
171171
})
172172
}
173173
}
174+
rustc_lexer::TokenKind::Frontmatter { .. } => {
175+
code_to_the_left = false;
176+
comments.push(Comment {
177+
style: CommentStyle::Isolated,
178+
lines: vec![token_text.to_string()],
179+
pos: start_bpos + BytePos(pos as u32),
180+
});
181+
}
174182
_ => {
175183
code_to_the_left = true;
176184
}

compiler/rustc_lexer/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,17 @@ pub fn tokenize(input: &str) -> impl Iterator<Item = Token> {
311311
})
312312
}
313313

314+
/// Creates an iterator that produces tokens from the input string.
315+
///
316+
/// Be sure to call [`strip_shebang`] first
317+
pub fn tokenize_document(input: &str) -> impl Iterator<Item = Token> {
318+
let mut cursor = Cursor::new(input, FrontmatterAllowed::Yes);
319+
std::iter::from_fn(move || {
320+
let token = cursor.advance_token();
321+
if token.kind != TokenKind::Eof { Some(token) } else { None }
322+
})
323+
}
324+
314325
/// True if `c` is considered a whitespace according to Rust language definition.
315326
/// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
316327
/// for definitions of these classes.

tests/ui/unpretty/frontmatter.stdout

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
---
2+
---
13

24
//@ compile-flags: -Zunpretty=normal
35
//@ check-pass

0 commit comments

Comments
 (0)