Skip to content

Commit

Permalink
Rollup merge of rust-lang#126125 - dev-ardi:conflict-markers, r=estebank
Browse files Browse the repository at this point in the history
Improve conflict marker recovery

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->
closes rust-lang#113826
r? ``@estebank`` since you reviewed rust-lang#115413
cc: ``@rben01`` since you opened up the issue in the first place
  • Loading branch information
matthiaskrgr authored Jun 21, 2024
2 parents af6ecd6 + 9f63712 commit 6c4f668
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 98 deletions.
58 changes: 42 additions & 16 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2965,9 +2965,10 @@ impl<'a> Parser<'a> {

/// This checks if this is a conflict marker, depending of the parameter passed.
///
/// * `>>>>>`
/// * `=====`
/// * `<<<<<`
/// * `<<<<<<<`
/// * `|||||||`
/// * `=======`
/// * `>>>>>>>`
///
pub(super) fn is_vcs_conflict_marker(
&mut self,
Expand Down Expand Up @@ -2997,14 +2998,18 @@ impl<'a> Parser<'a> {
}

pub(crate) fn err_vcs_conflict_marker(&mut self) -> PResult<'a, ()> {
// <<<<<<<
let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
else {
return Ok(());
};
let mut spans = Vec::with_capacity(3);
spans.push(start);
// |||||||
let mut middlediff3 = None;
// =======
let mut middle = None;
// >>>>>>>
let mut end = None;
loop {
if self.token.kind == TokenKind::Eof {
Expand All @@ -3025,29 +3030,50 @@ impl<'a> Parser<'a> {
}
self.bump();
}

let mut err = self.dcx().struct_span_err(spans, "encountered diff marker");
err.span_label(start, "after this is the code before the merge");
if let Some(middle) = middlediff3 {
err.span_label(middle, "");
}
match middlediff3 {
// We're using diff3
Some(middlediff3) => {
err.span_label(
start,
"between this marker and `|||||||` is the code that we're merging into",
);
err.span_label(middlediff3, "between this marker and `=======` is the base code (what the two refs diverged from)");
}
None => {
err.span_label(
start,
"between this marker and `=======` is the code that we're merging into",
);
}
};

if let Some(middle) = middle {
err.span_label(middle, "");
err.span_label(middle, "between this marker and `>>>>>>>` is the incoming code");
}
if let Some(end) = end {
err.span_label(end, "above this are the incoming code changes");
err.span_label(end, "this marker concludes the conflict region");
}
err.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",
err.note(
"conflict markers indicate that a merge was started but could not be completed due \
to merge conflicts\n\
to resolve a conflict, keep only the code you want and then delete the lines \
containing conflict markers",
);
err.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",
"if you're having merge conflicts after pulling new code:\n\
the top section is the code you already had and the bottom section is the remote code\n\
if you're in the middle of a rebase:\n\
the top section is the code being rebased onto and the bottom section is the code \
coming from the current commit being rebased",
);

err.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>",
"for an explanation on these markers from the `git` documentation:\n\
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
);

Err(err)
}

Expand Down
19 changes: 12 additions & 7 deletions tests/ui/parser/diff-markers/enum-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ error: encountered diff marker
--> $DIR/enum-2.rs:3:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `|||||||` is the code that we're merging into
LL | x: u8,
LL | |||||||
| -------
| ------- between this marker and `=======` is the base code (what the two refs diverged from)
LL | z: (),
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | y: i8,
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/enum.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | Foo(u8),
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | Bar(i8),
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/fn-arg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/fn-arg.rs:3:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | x: u8,
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | x: i8,
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/item-with-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/item-with-attr.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | fn foo() {}
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | fn bar() {}
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/item.rs:1:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | fn foo() {}
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | fn bar() {}
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/statement.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/statement.rs:10:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | S::foo();
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | S::bar();
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/struct-expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/struct-expr.rs:6:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | x: 42,
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | x: 0,
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/struct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/struct.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | x: u8,
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | x: i8,
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

17 changes: 11 additions & 6 deletions tests/ui/parser/diff-markers/trait-item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ error: encountered diff marker
--> $DIR/trait-item.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ after this is the code before the merge
| ^^^^^^^ between this marker and `=======` is the code that we're merging into
LL | fn foo() {}
LL | =======
| -------
| ------- between this marker and `>>>>>>>` is the incoming code
LL | fn bar() {}
LL | >>>>>>> branch
| ^^^^^^^ above this are the incoming code changes
| ^^^^^^^ this marker concludes the conflict region
|
= 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
= 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
= 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>
= note: conflict markers indicate that a merge was started but could not be completed due to merge conflicts
to resolve a conflict, keep only the code you want and then delete the lines containing conflict markers
= 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
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
= 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>

error: aborting due to 1 previous error

Loading

0 comments on commit 6c4f668

Please sign in to comment.