Skip to content

Commit 38fd5a9

Browse files
committed
Account for ADT bodies and struct expressions
1 parent 375f025 commit 38fd5a9

File tree

12 files changed

+142
-2
lines changed

12 files changed

+142
-2
lines changed

compiler/rustc_parse/src/parser/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,7 @@ impl<'a> Parser<'a> {
30393039
/// Parses `ident (COLON expr)?`.
30403040
fn parse_expr_field(&mut self) -> PResult<'a, ExprField> {
30413041
let attrs = self.parse_outer_attributes()?;
3042+
self.recover_diff_marker();
30423043
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
30433044
let lo = this.token.span;
30443045

compiler/rustc_parse/src/parser/item.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,9 @@ impl<'a> Parser<'a> {
13851385
}
13861386

13871387
fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1388+
self.recover_diff_marker();
13881389
let variant_attrs = self.parse_outer_attributes()?;
1390+
self.recover_diff_marker();
13891391
self.collect_tokens_trailing_token(
13901392
variant_attrs,
13911393
ForceCollect::No,
@@ -1579,9 +1581,32 @@ impl<'a> Parser<'a> {
15791581
self.parse_paren_comma_seq(|p| {
15801582
let attrs = p.parse_outer_attributes()?;
15811583
p.collect_tokens_trailing_token(attrs, ForceCollect::No, |p, attrs| {
1584+
let mut snapshot = None;
1585+
if p.is_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
1586+
// Account for `<<<<<<<` diff markers. We can't proactivelly error here because
1587+
// that can be a valid type start, so we snapshot and reparse only we've
1588+
// encountered another parse error.
1589+
snapshot = Some(p.create_snapshot_for_diagnostic());
1590+
}
15821591
let lo = p.token.span;
1583-
let vis = p.parse_visibility(FollowedByType::Yes)?;
1584-
let ty = p.parse_ty()?;
1592+
let vis = match p.parse_visibility(FollowedByType::Yes) {
1593+
Ok(vis) => vis,
1594+
Err(err) => {
1595+
if let Some(ref mut snapshot) = snapshot {
1596+
snapshot.recover_diff_marker();
1597+
}
1598+
return Err(err);
1599+
}
1600+
};
1601+
let ty = match p.parse_ty() {
1602+
Ok(ty) => ty,
1603+
Err(err) => {
1604+
if let Some(ref mut snapshot) = snapshot {
1605+
snapshot.recover_diff_marker();
1606+
}
1607+
return Err(err);
1608+
}
1609+
};
15851610

15861611
Ok((
15871612
FieldDef {
@@ -1602,7 +1627,9 @@ impl<'a> Parser<'a> {
16021627

16031628
/// Parses an element of a struct declaration.
16041629
fn parse_field_def(&mut self, adt_ty: &str) -> PResult<'a, FieldDef> {
1630+
self.recover_diff_marker();
16051631
let attrs = self.parse_outer_attributes()?;
1632+
self.recover_diff_marker();
16061633
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
16071634
let lo = this.token.span;
16081635
let vis = this.parse_visibility(FollowedByType::No)?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
enum E {
2+
Foo {
3+
<<<<<<< HEAD //~ ERROR encountered diff marker
4+
x: u8,
5+
=======
6+
x: i8,
7+
>>>>>>> branch
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: encountered diff marker
2+
--> $DIR/enum-2.rs:3:1
3+
|
4+
LL | <<<<<<< HEAD
5+
| ^^^^^^^ start
6+
LL | x: u8,
7+
LL | =======
8+
| ^^^^^^^ middle
9+
LL | x: i8,
10+
LL | >>>>>>> branch
11+
| ^^^^^^^ end
12+
13+
error: aborting due to previous error
14+
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enum E {
2+
<<<<<<< HEAD //~ ERROR encountered diff marker
3+
Foo(u8),
4+
=======
5+
Bar(i8),
6+
>>>>>>> branch
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: encountered diff marker
2+
--> $DIR/enum.rs:2:1
3+
|
4+
LL | <<<<<<< HEAD
5+
| ^^^^^^^ start
6+
LL | Foo(u8),
7+
LL | =======
8+
| ^^^^^^^ middle
9+
LL | Bar(i8),
10+
LL | >>>>>>> branch
11+
| ^^^^^^^ end
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct S {
2+
x: u8,
3+
}
4+
fn main() {
5+
let _ = S {
6+
<<<<<<< HEAD //~ ERROR encountered diff marker
7+
x: 42,
8+
=======
9+
x: 0,
10+
>>>>>>> branch
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: encountered diff marker
2+
--> $DIR/struct-expr.rs:6:1
3+
|
4+
LL | <<<<<<< HEAD
5+
| ^^^^^^^ start
6+
LL | x: 42,
7+
LL | =======
8+
| ^^^^^^^ middle
9+
LL | x: 0,
10+
LL | >>>>>>> branch
11+
| ^^^^^^^ end
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct S {
2+
<<<<<<< HEAD //~ ERROR encountered diff marker
3+
x: u8,
4+
=======
5+
x: i8,
6+
>>>>>>> branch
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: encountered diff marker
2+
--> $DIR/struct.rs:2:1
3+
|
4+
LL | <<<<<<< HEAD
5+
| ^^^^^^^ start
6+
LL | x: u8,
7+
LL | =======
8+
| ^^^^^^^ middle
9+
LL | x: i8,
10+
LL | >>>>>>> branch
11+
| ^^^^^^^ end
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct S(
2+
<<<<<<< HEAD //~ ERROR encountered diff marker
3+
u8,
4+
=======
5+
i8,
6+
>>>>>>> branch
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: encountered diff marker
2+
--> $DIR/tuple-struct.rs:2:1
3+
|
4+
LL | <<<<<<< HEAD
5+
| ^^^^^^^ start
6+
LL | u8,
7+
LL | =======
8+
| ^^^^^^^ middle
9+
LL | i8,
10+
LL | >>>>>>> branch
11+
| ^^^^^^^ end
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)