Skip to content

Commit

Permalink
Account for ADT bodies and struct expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Dec 29, 2022
1 parent 375f025 commit 38fd5a9
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3039,6 +3039,7 @@ impl<'a> Parser<'a> {
/// Parses `ident (COLON expr)?`.
fn parse_expr_field(&mut self) -> PResult<'a, ExprField> {
let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let lo = this.token.span;

Expand Down
31 changes: 29 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,9 @@ impl<'a> Parser<'a> {
}

fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
self.recover_diff_marker();
let variant_attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
self.collect_tokens_trailing_token(
variant_attrs,
ForceCollect::No,
Expand Down Expand Up @@ -1579,9 +1581,32 @@ impl<'a> Parser<'a> {
self.parse_paren_comma_seq(|p| {
let attrs = p.parse_outer_attributes()?;
p.collect_tokens_trailing_token(attrs, ForceCollect::No, |p, attrs| {
let mut snapshot = None;
if p.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 type start, so we snapshot and reparse only we've
// encountered another parse error.
snapshot = Some(p.create_snapshot_for_diagnostic());
}
let lo = p.token.span;
let vis = p.parse_visibility(FollowedByType::Yes)?;
let ty = p.parse_ty()?;
let vis = match p.parse_visibility(FollowedByType::Yes) {
Ok(vis) => vis,
Err(err) => {
if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker();
}
return Err(err);
}
};
let ty = match p.parse_ty() {
Ok(ty) => ty,
Err(err) => {
if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker();
}
return Err(err);
}
};

Ok((
FieldDef {
Expand All @@ -1602,7 +1627,9 @@ impl<'a> Parser<'a> {

/// Parses an element of a struct declaration.
fn parse_field_def(&mut self, adt_ty: &str) -> PResult<'a, FieldDef> {
self.recover_diff_marker();
let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let lo = this.token.span;
let vis = this.parse_visibility(FollowedByType::No)?;
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/parser/diff-markers/enum-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum E {
Foo {
<<<<<<< HEAD //~ ERROR encountered diff marker
x: u8,
=======
x: i8,
>>>>>>> branch
}
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/enum-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/enum-2.rs:3:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | x: u8,
LL | =======
| ^^^^^^^ middle
LL | x: i8,
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

7 changes: 7 additions & 0 deletions src/test/ui/parser/diff-markers/enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum E {
<<<<<<< HEAD //~ ERROR encountered diff marker
Foo(u8),
=======
Bar(i8),
>>>>>>> branch
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/enum.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/enum.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | Foo(u8),
LL | =======
| ^^^^^^^ middle
LL | Bar(i8),
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

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

error: aborting due to previous error

7 changes: 7 additions & 0 deletions src/test/ui/parser/diff-markers/struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct S {
<<<<<<< HEAD //~ ERROR encountered diff marker
x: u8,
=======
x: i8,
>>>>>>> branch
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/struct.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/struct.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | x: u8,
LL | =======
| ^^^^^^^ middle
LL | x: i8,
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

7 changes: 7 additions & 0 deletions src/test/ui/parser/diff-markers/tuple-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct S(
<<<<<<< HEAD //~ ERROR encountered diff marker
u8,
=======
i8,
>>>>>>> branch
);
14 changes: 14 additions & 0 deletions src/test/ui/parser/diff-markers/tuple-struct.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: encountered diff marker
--> $DIR/tuple-struct.rs:2:1
|
LL | <<<<<<< HEAD
| ^^^^^^^ start
LL | u8,
LL | =======
| ^^^^^^^ middle
LL | i8,
LL | >>>>>>> branch
| ^^^^^^^ end

error: aborting due to previous error

0 comments on commit 38fd5a9

Please sign in to comment.