Skip to content

Commit 075cb61

Browse files
committed
When encountering unclosed delimiters during parsing, check for diff markers
Fix #116252.
1 parent 9d6d5d4 commit 075cb61

File tree

7 files changed

+104
-23
lines changed

7 files changed

+104
-23
lines changed

Diff for: compiler/rustc_parse/src/lexer/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ pub(crate) fn parse_token_trees<'a>(
6464
override_span,
6565
nbsp_is_whitespace: false,
6666
};
67-
let (token_trees, unmatched_delims) =
67+
let (stream, res, unmatched_delims) =
6868
tokentrees::TokenTreesReader::parse_all_token_trees(string_reader);
69-
match token_trees {
70-
Ok(stream) if unmatched_delims.is_empty() => Ok(stream),
69+
match res {
70+
Ok(()) if unmatched_delims.is_empty() => Ok(stream),
7171
_ => {
7272
// Return error if there are unmatched delimiters or unclosed delimiters.
7373
// We emit delimiter mismatch errors first, then emit the unclosing delimiter mismatch
@@ -79,7 +79,7 @@ pub(crate) fn parse_token_trees<'a>(
7979
err.buffer(&mut buffer);
8080
}
8181
}
82-
if let Err(err) = token_trees {
82+
if let Err(err) = res {
8383
// Add unclosing delimiter error
8484
err.buffer(&mut buffer);
8585
}

Diff for: compiler/rustc_parse/src/lexer/tokentrees.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,39 @@ pub(super) struct TokenTreesReader<'a> {
1818
impl<'a> TokenTreesReader<'a> {
1919
pub(super) fn parse_all_token_trees(
2020
string_reader: StringReader<'a>,
21-
) -> (PResult<'a, TokenStream>, Vec<UnmatchedDelim>) {
21+
) -> (TokenStream, PResult<'a, ()>, Vec<UnmatchedDelim>) {
2222
let mut tt_reader = TokenTreesReader {
2323
string_reader,
2424
token: Token::dummy(),
2525
diag_info: TokenTreeDiagInfo::default(),
2626
};
27-
let res = tt_reader.parse_token_trees(/* is_delimited */ false);
28-
(res, tt_reader.diag_info.unmatched_delims)
27+
let (stream, res) = tt_reader.parse_token_trees(/* is_delimited */ false);
28+
(stream, res, tt_reader.diag_info.unmatched_delims)
2929
}
3030

3131
// Parse a stream of tokens into a list of `TokenTree`s.
32-
fn parse_token_trees(&mut self, is_delimited: bool) -> PResult<'a, TokenStream> {
32+
fn parse_token_trees(&mut self, is_delimited: bool) -> (TokenStream, PResult<'a, ()>) {
3333
self.token = self.string_reader.next_token().0;
3434
let mut buf = Vec::new();
3535
loop {
3636
match self.token.kind {
37-
token::OpenDelim(delim) => buf.push(self.parse_token_tree_open_delim(delim)?),
37+
token::OpenDelim(delim) => {
38+
buf.push(match self.parse_token_tree_open_delim(delim) {
39+
Ok(val) => val,
40+
Err(err) => return (TokenStream::new(buf), Err(err)),
41+
})
42+
}
3843
token::CloseDelim(delim) => {
39-
return if is_delimited {
40-
Ok(TokenStream::new(buf))
41-
} else {
42-
Err(self.close_delim_err(delim))
43-
};
44+
return (
45+
TokenStream::new(buf),
46+
if is_delimited { Ok(()) } else { Err(self.close_delim_err(delim)) },
47+
);
4448
}
4549
token::Eof => {
46-
return if is_delimited {
47-
Err(self.eof_err())
48-
} else {
49-
Ok(TokenStream::new(buf))
50-
};
50+
return (
51+
TokenStream::new(buf),
52+
if is_delimited { Err(self.eof_err()) } else { Ok(()) },
53+
);
5154
}
5255
_ => {
5356
// Get the next normal token. This might require getting multiple adjacent
@@ -106,7 +109,20 @@ impl<'a> TokenTreesReader<'a> {
106109
// Parse the token trees within the delimiters.
107110
// We stop at any delimiter so we can try to recover if the user
108111
// uses an incorrect delimiter.
109-
let tts = self.parse_token_trees(/* is_delimited */ true)?;
112+
let (tts, res) = self.parse_token_trees(/* is_delimited */ true);
113+
if let Err(mut err) = res {
114+
// If there are unclosed delims, see if there are diff markers and if so, point them
115+
// out instead of complaining about the unclosed delims.
116+
let mut parser = crate::stream_to_parser(self.string_reader.sess, tts, None);
117+
while parser.token != token::Eof {
118+
if let Err(mut diff_err) = parser.err_diff_marker() {
119+
diff_err.emit();
120+
err.delay_as_bug();
121+
}
122+
parser.bump();
123+
}
124+
return Err(err);
125+
}
110126

111127
// Expand to cover the entire delimited token tree
112128
let delim_span = DelimSpan::from_pair(pre_span, self.token.span);

Diff for: compiler/rustc_parse/src/parser/diagnostics.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2808,8 +2808,15 @@ impl<'a> Parser<'a> {
28082808
}
28092809

28102810
pub fn recover_diff_marker(&mut self) {
2811+
if let Err(mut err) = self.err_diff_marker() {
2812+
err.emit();
2813+
FatalError.raise();
2814+
}
2815+
}
2816+
2817+
pub fn err_diff_marker(&mut self) -> PResult<'a, ()> {
28112818
let Some(start) = self.diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) else {
2812-
return;
2819+
return Ok(());
28132820
};
28142821
let mut spans = Vec::with_capacity(3);
28152822
spans.push(start);
@@ -2856,8 +2863,7 @@ impl<'a> Parser<'a> {
28562863
"for an explanation on these markers from the `git` documentation, visit \
28572864
<https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
28582865
);
2859-
err.emit();
2860-
FatalError.raise()
2866+
Err(err)
28612867
}
28622868

28632869
/// Parse and throw away a parenthesized comma separated
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
macro_rules! foo {
2+
<<<<<<< HEAD
3+
//~^ ERROR encountered diff marker
4+
() {
5+
=======
6+
() { //
7+
>>>>>>> 7a4f13c blah blah blah
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: encountered diff marker
2+
--> $DIR/unclosed-delims-in-macro.rs:2:1
3+
|
4+
LL | <<<<<<< HEAD
5+
| ^^^^^^^ after this is the code before the merge
6+
...
7+
LL | =======
8+
| -------
9+
LL | () { //
10+
LL | >>>>>>> 7a4f13c blah blah blah
11+
| ^^^^^^^ above this are the incoming code changes
12+
|
13+
= help: if you're having merge conflicts after pulling new code, the top section is the code you already had and the bottom section is the remote code
14+
= help: if you're in the middle of a rebase, the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
15+
= note: for an explanation on these markers from the `git` documentation, visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
16+
17+
error: aborting due to previous error
18+

Diff for: tests/ui/parser/diff-markers/unclosed-delims.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
mod tests {
2+
#[test]
3+
<<<<<<< HEAD
4+
//~^ ERROR encountered diff marker
5+
//~| NOTE after this is the code before the merge
6+
//~| NOTE for an explanation on these markers
7+
fn test1() {
8+
=======
9+
//~^ NOTE
10+
fn test2() {
11+
>>>>>>> 7a4f13c blah blah blah
12+
//~^ NOTE above this are the incoming code changes
13+
}
14+
}

Diff for: tests/ui/parser/diff-markers/unclosed-delims.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: encountered diff marker
2+
--> $DIR/unclosed-delims.rs:3:1
3+
|
4+
LL | <<<<<<< HEAD
5+
| ^^^^^^^ after this is the code before the merge
6+
...
7+
LL | =======
8+
| -------
9+
...
10+
LL | >>>>>>> 7a4f13c blah blah blah
11+
| ^^^^^^^ above this are the incoming code changes
12+
|
13+
= help: if you're having merge conflicts after pulling new code, the top section is the code you already had and the bottom section is the remote code
14+
= help: if you're in the middle of a rebase, the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
15+
= note: for an explanation on these markers from the `git` documentation, visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)