Skip to content

Commit

Permalink
Detect diff markers in the parser
Browse files Browse the repository at this point in the history
Partly address rust-lang#32059.
  • Loading branch information
estebank committed Dec 29, 2022
1 parent 92c1937 commit 375f025
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 3 deletions.
54 changes: 53 additions & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use rustc_ast::{
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{
fluent, Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult,
fluent, Applicability, DiagnosticBuilder, DiagnosticMessage, FatalError, Handler, MultiSpan,
PResult,
};
use rustc_errors::{pluralize, Diagnostic, ErrorGuaranteed, IntoDiagnostic};
use rustc_session::errors::ExprParenthesesNeeded;
Expand Down Expand Up @@ -2556,6 +2557,57 @@ impl<'a> Parser<'a> {
Ok(())
}

pub fn is_diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> bool {
(0..3).all(|i| self.look_ahead(i, |tok| tok == long_kind))
&& self.look_ahead(3, |tok| tok == short_kind)
}

fn diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> Option<Span> {
if self.is_diff_marker(long_kind, short_kind) {
let lo = self.token.span;
for _ in 0..4 {
self.bump();
}
return Some(lo.to(self.prev_token.span));
}
None
}

pub fn recover_diff_marker(&mut self) {
let Some(start) = self.diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) else {
return;
};
let mut spans = Vec::with_capacity(3);
spans.push(start);
let mut middle = None;
let mut end = None;
loop {
if self.token.kind == TokenKind::Eof {
break;
}
if let Some(span) = self.diff_marker(&TokenKind::EqEq, &TokenKind::Eq) {
spans.push(span);
middle = Some(span);
}
if let Some(span) = self.diff_marker(&TokenKind::BinOp(token::Shr), &TokenKind::Gt) {
spans.push(span);
end = Some(span);
break;
}
self.bump();
}
let mut err = self.struct_span_err(spans, "encountered diff marker");
err.span_label(start, "start");
if let Some(middle) = middle {
err.span_label(middle, "middle");
}
if let Some(end) = end {
err.span_label(end, "end");
}
err.emit();
FatalError.raise()
}

/// Parse and throw away a parenthesized comma separated
/// sequence of patterns until `)` is reached.
fn skip_pat_list(&mut self) -> PResult<'a, ()> {
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ impl<'a> Parser<'a> {
fn_parse_mode: FnParseMode,
force_collect: ForceCollect,
) -> PResult<'a, Option<Item>> {
self.recover_diff_marker();
let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
self.parse_item_common(attrs, true, false, fn_parse_mode, force_collect)
}

Expand Down Expand Up @@ -704,6 +706,7 @@ impl<'a> Parser<'a> {
if self.recover_doc_comment_before_brace() {
continue;
}
self.recover_diff_marker();
match parse_item(self) {
Ok(None) => {
let mut is_unnecessary_semicolon = !items.is_empty()
Expand Down Expand Up @@ -1039,8 +1042,11 @@ impl<'a> Parser<'a> {
/// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
/// ```
fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
self.parse_delim_comma_seq(Delimiter::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
.map(|(r, _)| r)
self.parse_delim_comma_seq(Delimiter::Brace, |p| {
p.recover_diff_marker();
Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
})
.map(|(r, _)| r)
}

fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
Expand Down Expand Up @@ -2427,6 +2433,7 @@ impl<'a> Parser<'a> {
let mut first_param = true;
// Parse the arguments, starting out with `self` being allowed...
let (mut params, _) = self.parse_paren_comma_seq(|p| {
p.recover_diff_marker();
let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
e.emit();
let lo = p.prev_token.span;
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,23 @@ impl<'a> Parser<'a> {
recover: AttemptLocalParseRecovery,
) -> PResult<'a, P<Block>> {
let mut stmts = vec![];
let mut snapshot = None;
while !self.eat(&token::CloseDelim(Delimiter::Brace)) {
if self.token == token::Eof {
break;
}
if self.is_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
// Account for `<<<<<<<` diff markers. We can't proactivelly error here because
// that can be a valid path start, so we snapshot and reparse only we've
// encountered another parse error.
snapshot = Some(self.create_snapshot_for_diagnostic());
}
let stmt = match self.parse_full_stmt(recover) {
Err(mut err) if recover.yes() => {
self.maybe_annotate_with_ascription(&mut err, false);
if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker();
}
err.emit();
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore);
Some(self.mk_stmt_err(self.token.span))
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/parser/diff-markers/fn-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait T {
fn foo(
<<<<<<< HEAD //~ ERROR encountered diff marker
x: u8,
=======
x: i8,
>>>>>>> branch
) {}
}

struct S;
impl T for S {}

fn main() {
S::foo(42);
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/fn-arg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/fn-arg.rs:3:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | x: u8,
LL | =======
| ^^^^^^^ middle
LL | x: i8,
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

10 changes: 10 additions & 0 deletions src/test/ui/parser/diff-markers/item-with-attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[attribute]
<<<<<<< HEAD //~ ERROR encountered diff marker
fn foo() {}
=======
fn bar() {}
>>>>>>> branch

fn main() {
foo();
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/item-with-attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/item-with-attr.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | fn foo() {}
LL | =======
| ^^^^^^^ middle
LL | fn bar() {}
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

9 changes: 9 additions & 0 deletions src/test/ui/parser/diff-markers/item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<<<<<<< HEAD //~ ERROR encountered diff marker
fn foo() {}
=======
fn bar() {}
>>>>>>> branch

fn main() {
foo();
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/item.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/item.rs:1:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | fn foo() {}
LL | =======
| ^^^^^^^ middle
LL | fn bar() {}
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

15 changes: 15 additions & 0 deletions src/test/ui/parser/diff-markers/statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trait T {
fn foo() {}
fn bar() {}
}

struct S;
impl T for S {}

fn main() {
<<<<<<< HEAD //~ ERROR encountered diff marker
S::foo();
=======
S::bar();
>>>>>>> branch
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/statement.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/statement.rs:10:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | S::foo();
LL | =======
| ^^^^^^^ middle
LL | S::bar();
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/trait-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
trait T {
<<<<<<< HEAD //~ ERROR encountered diff marker
fn foo() {}
=======
fn bar() {}
>>>>>>> branch
}

struct S;
impl T for S {}

fn main() {
S::foo();
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/trait-item.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/trait-item.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | fn foo() {}
LL | =======
| ^^^^^^^ middle
LL | fn bar() {}
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

9 changes: 9 additions & 0 deletions src/test/ui/parser/diff-markers/use-statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use foo::{
<<<<<<< HEAD //~ ERROR encountered diff marker
bar,
=======
baz,
>>>>>>> branch
};

fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/use-statement.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/use-statement.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | bar,
LL | =======
| ^^^^^^^ middle
LL | baz,
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

0 comments on commit 375f025

Please sign in to comment.