Skip to content

Commit 1b7008d

Browse files
committed
refactor: change to use peekable
1 parent 4a0f8d5 commit 1b7008d

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -557,39 +557,36 @@ impl<'a> StringReader<'a> {
557557
);
558558
let mut nested_block_comment_open_idxs = vec![];
559559
let mut last_nested_block_comment_idxs = None;
560-
let mut content_chars = self.str_from(start).char_indices();
560+
let mut content_chars = self.str_from(start).char_indices().peekable();
561561

562-
if let Some((_, mut last_char)) = content_chars.next() {
563-
while let Some((idx, c)) = content_chars.next() {
564-
match c {
565-
'*' if last_char == '/' => {
566-
nested_block_comment_open_idxs.push(idx);
567-
}
568-
'/' if last_char == '*' => {
569-
last_nested_block_comment_idxs =
570-
nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
571-
}
572-
_ => {}
573-
};
574-
last_char = c;
575-
}
562+
while let Some((idx, current_char)) = content_chars.next() {
563+
match content_chars.peek() {
564+
Some((_, '*')) if current_char == '/' => {
565+
nested_block_comment_open_idxs.push(idx);
566+
}
567+
Some((_, '/')) if current_char == '*' => {
568+
last_nested_block_comment_idxs =
569+
nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
570+
}
571+
_ => {}
572+
};
576573
}
577574

578575
if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs {
579576
err.span_label(self.mk_sp(start, start + BytePos(2)), msg)
580577
.span_label(
581578
self.mk_sp(
582-
start + BytePos(nested_open_idx as u32 - 1),
583-
start + BytePos(nested_open_idx as u32 + 1),
579+
start + BytePos(nested_open_idx as u32),
580+
start + BytePos(nested_open_idx as u32 + 2),
584581
),
585582
"...as last nested comment starts here, maybe you want to close this instead?",
586583
)
587584
.span_label(
588585
self.mk_sp(
589-
start + BytePos(nested_close_idx as u32 - 1),
590-
start + BytePos(nested_close_idx as u32 + 1),
586+
start + BytePos(nested_close_idx as u32),
587+
start + BytePos(nested_close_idx as u32 + 2),
591588
),
592-
"...and last nested comment terminates here",
589+
"...and last nested comment terminates here.",
593590
);
594591
}
595592

src/test/ui/unterminated-nested-comment.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | | /*
1414
LL | | */
1515
| |_--^
1616
| |
17-
| ...and last nested comment terminates here
17+
| ...and last nested comment terminates here.
1818

1919
error: aborting due to previous error
2020

0 commit comments

Comments
 (0)