Skip to content

Commit d51b446

Browse files
committed
Improve conflict marker recovery
1 parent dd104ef commit d51b446

15 files changed

+189
-95
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+38-13
Original file line numberDiff line numberDiff line change
@@ -2990,14 +2990,18 @@ impl<'a> Parser<'a> {
29902990
}
29912991

29922992
pub(crate) fn err_vcs_conflict_marker(&mut self) -> PResult<'a, ()> {
2993+
// <<<<<<<
29932994
let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
29942995
else {
29952996
return Ok(());
29962997
};
29972998
let mut spans = Vec::with_capacity(3);
29982999
spans.push(start);
3000+
// |||||||
29993001
let mut middlediff3 = None;
3002+
// =======
30003003
let mut middle = None;
3004+
// >>>>>>>
30013005
let mut end = None;
30023006
loop {
30033007
if self.token.kind == TokenKind::Eof {
@@ -3018,29 +3022,50 @@ impl<'a> Parser<'a> {
30183022
}
30193023
self.bump();
30203024
}
3025+
30213026
let mut err = self.dcx().struct_span_err(spans, "encountered diff marker");
3022-
err.span_label(start, "after this is the code before the merge");
3023-
if let Some(middle) = middlediff3 {
3024-
err.span_label(middle, "");
3025-
}
3027+
match middlediff3 {
3028+
// We're using diff3
3029+
Some(middlediff3) => {
3030+
err.span_label(
3031+
start,
3032+
"between this marker and `|||||||` is the code that we're merging into",
3033+
);
3034+
err.span_label(middlediff3, "between this marker and `=======` is the base code (what the two refs diverged from)");
3035+
}
3036+
None => {
3037+
err.span_label(
3038+
start,
3039+
"between this marker and `=======` is the code that we're merging into",
3040+
);
3041+
}
3042+
};
3043+
30263044
if let Some(middle) = middle {
3027-
err.span_label(middle, "");
3045+
err.span_label(middle, "between this marker and `>>>>>>>` is the incoming code");
30283046
}
30293047
if let Some(end) = end {
3030-
err.span_label(end, "above this are the incoming code changes");
3048+
err.span_label(end, "this marker concludes the conflict region");
30313049
}
3032-
err.help(
3033-
"if you're having merge conflicts after pulling new code, the top section is the code \
3034-
you already had and the bottom section is the remote code",
3050+
err.note(
3051+
"conflict markers indicate that a merge was started but could not be completed due \
3052+
to merge conflicts\n\
3053+
to resolve a conflict, keep only the code you want and then delete the lines \
3054+
containing conflict markers",
30353055
);
30363056
err.help(
3037-
"if you're in the middle of a rebase, the top section is the code being rebased onto \
3038-
and the bottom section is the code coming from the current commit being rebased",
3057+
"if you're having merge conflicts after pulling new code:\n\
3058+
the top section is the code you already had and the bottom section is the remote code\n\
3059+
if you're in the middle of a rebase:\n\
3060+
the top section is the code being rebased onto and the bottom section is the code \
3061+
coming from the current commit being rebased",
30393062
);
3063+
30403064
err.note(
3041-
"for an explanation on these markers from the `git` documentation, visit \
3042-
<https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
3065+
"for an explanation on these markers from the `git` documentation:\n\
3066+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
30433067
);
3068+
30443069
Err(err)
30453070
}
30463071

tests/ui/parser/diff-markers/enum-2.stderr

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ error: encountered diff marker
22
--> $DIR/enum-2.rs:3:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `|||||||` is the code that we're merging into
66
LL | x: u8,
77
LL | |||||||
8-
| -------
8+
| ------- between this marker and `=======` is the base code (what the two refs diverged from)
99
LL | z: (),
1010
LL | =======
11-
| -------
11+
| ------- between this marker and `>>>>>>>` is the incoming code
1212
LL | y: i8,
1313
LL | >>>>>>> branch
14-
| ^^^^^^^ above this are the incoming code changes
14+
| ^^^^^^^ this marker concludes the conflict region
1515
|
16-
= 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
17-
= 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
18-
= 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+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
17+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
18+
= help: if you're having merge conflicts after pulling new code:
19+
the top section is the code you already had and the bottom section is the remote code
20+
if you're in the middle of a rebase:
21+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
22+
= note: for an explanation on these markers from the `git` documentation:
23+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1924

2025
error: aborting due to 1 previous error
2126

tests/ui/parser/diff-markers/enum.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/enum.rs:2:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | Foo(u8),
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | Bar(i8),
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

tests/ui/parser/diff-markers/fn-arg.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/fn-arg.rs:3:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | x: u8,
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | x: i8,
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

tests/ui/parser/diff-markers/item-with-attr.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/item-with-attr.rs:2:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | fn foo() {}
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | fn bar() {}
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

tests/ui/parser/diff-markers/item.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/item.rs:1:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | fn foo() {}
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | fn bar() {}
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

tests/ui/parser/diff-markers/statement.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/statement.rs:10:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | S::foo();
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | S::bar();
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

tests/ui/parser/diff-markers/struct-expr.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/struct-expr.rs:6:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | x: 42,
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | x: 0,
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

tests/ui/parser/diff-markers/struct.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/struct.rs:2:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | x: u8,
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | x: i8,
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

tests/ui/parser/diff-markers/trait-item.stderr

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ error: encountered diff marker
22
--> $DIR/trait-item.rs:2:1
33
|
44
LL | <<<<<<< HEAD
5-
| ^^^^^^^ after this is the code before the merge
5+
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
66
LL | fn foo() {}
77
LL | =======
8-
| -------
8+
| ------- between this marker and `>>>>>>>` is the incoming code
99
LL | fn bar() {}
1010
LL | >>>>>>> branch
11-
| ^^^^^^^ above this are the incoming code changes
11+
| ^^^^^^^ this marker concludes the conflict region
1212
|
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>
13+
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
14+
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
15+
= help: if you're having merge conflicts after pulling new code:
16+
the top section is the code you already had and the bottom section is the remote code
17+
if you're in the middle of a rebase:
18+
the top section is the code being rebased onto and the bottom section is the code coming from the current commit being rebased
19+
= note: for an explanation on these markers from the `git` documentation:
20+
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>
1621

1722
error: aborting due to 1 previous error
1823

0 commit comments

Comments
 (0)