From 9067e4417e52643596ae8dbc4ec6369a0d856b45 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Wed, 28 Dec 2022 18:06:11 +0100 Subject: Rename `Rptr` to `Ref` in AST and HIR The name makes a lot more sense, and `ty::TyKind` calls it `Ref` already as well. --- compiler/rustc_parse/src/parser/diagnostics.rs | 2 +- compiler/rustc_parse/src/parser/ty.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 61fe379c3e9..67e701b00ea 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1229,7 +1229,7 @@ impl<'a> Parser<'a> { let sum_span = ty.span.to(self.prev_token.span); let sub = match &ty.kind { - TyKind::Rptr(lifetime, mut_ty) => { + TyKind::Ref(lifetime, mut_ty) => { let sum_with_parens = pprust::to_string(|s| { s.s.word("&"); s.print_opt_lifetime(lifetime); diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index fc26278909c..a87f6d194ba 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -501,7 +501,7 @@ impl<'a> Parser<'a> { self.bump_with((dyn_tok, dyn_tok_sp)); } let ty = self.parse_ty_no_plus()?; - Ok(TyKind::Rptr(opt_lifetime, MutTy { ty, mutbl })) + Ok(TyKind::Ref(opt_lifetime, MutTy { ty, mutbl })) } // Parses the `typeof(EXPR)`. -- cgit 1.4.1-3-g733a5 From 375f02580522ddacb4777d194d48d82454bb6aa9 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 28 Dec 2022 17:56:22 -0800 Subject: Detect diff markers in the parser Partly address #32059. --- compiler/rustc_parse/src/parser/diagnostics.rs | 54 +++++++++++++++++++++- compiler/rustc_parse/src/parser/item.rs | 11 ++++- compiler/rustc_parse/src/parser/stmt.rs | 10 ++++ src/test/ui/parser/diff-markers/fn-arg.rs | 16 +++++++ src/test/ui/parser/diff-markers/fn-arg.stderr | 14 ++++++ src/test/ui/parser/diff-markers/item-with-attr.rs | 10 ++++ .../ui/parser/diff-markers/item-with-attr.stderr | 14 ++++++ src/test/ui/parser/diff-markers/item.rs | 9 ++++ src/test/ui/parser/diff-markers/item.stderr | 14 ++++++ src/test/ui/parser/diff-markers/statement.rs | 15 ++++++ src/test/ui/parser/diff-markers/statement.stderr | 14 ++++++ src/test/ui/parser/diff-markers/trait-item.rs | 14 ++++++ src/test/ui/parser/diff-markers/trait-item.stderr | 14 ++++++ src/test/ui/parser/diff-markers/use-statement.rs | 9 ++++ .../ui/parser/diff-markers/use-statement.stderr | 14 ++++++ 15 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/parser/diff-markers/fn-arg.rs create mode 100644 src/test/ui/parser/diff-markers/fn-arg.stderr create mode 100644 src/test/ui/parser/diff-markers/item-with-attr.rs create mode 100644 src/test/ui/parser/diff-markers/item-with-attr.stderr create mode 100644 src/test/ui/parser/diff-markers/item.rs create mode 100644 src/test/ui/parser/diff-markers/item.stderr create mode 100644 src/test/ui/parser/diff-markers/statement.rs create mode 100644 src/test/ui/parser/diff-markers/statement.stderr create mode 100644 src/test/ui/parser/diff-markers/trait-item.rs create mode 100644 src/test/ui/parser/diff-markers/trait-item.stderr create mode 100644 src/test/ui/parser/diff-markers/use-statement.rs create mode 100644 src/test/ui/parser/diff-markers/use-statement.stderr (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 61fe379c3e9..ffadb77320a 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -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; @@ -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 { + 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, ()> { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index e1ced2eb965..51de3d40d91 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -98,7 +98,9 @@ impl<'a> Parser<'a> { fn_parse_mode: FnParseMode, force_collect: ForceCollect, ) -> PResult<'a, Option> { + 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) } @@ -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() @@ -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> { @@ -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; diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index bae7f2670cb..4cbf89169b6 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -531,13 +531,23 @@ impl<'a> Parser<'a> { recover: AttemptLocalParseRecovery, ) -> PResult<'a, P> { 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)) diff --git a/src/test/ui/parser/diff-markers/fn-arg.rs b/src/test/ui/parser/diff-markers/fn-arg.rs new file mode 100644 index 00000000000..86c355628ab --- /dev/null +++ b/src/test/ui/parser/diff-markers/fn-arg.rs @@ -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); +} diff --git a/src/test/ui/parser/diff-markers/fn-arg.stderr b/src/test/ui/parser/diff-markers/fn-arg.stderr new file mode 100644 index 00000000000..4a816ff75bc --- /dev/null +++ b/src/test/ui/parser/diff-markers/fn-arg.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/item-with-attr.rs b/src/test/ui/parser/diff-markers/item-with-attr.rs new file mode 100644 index 00000000000..985907c08b2 --- /dev/null +++ b/src/test/ui/parser/diff-markers/item-with-attr.rs @@ -0,0 +1,10 @@ +#[attribute] +<<<<<<< HEAD //~ ERROR encountered diff marker +fn foo() {} +======= +fn bar() {} +>>>>>>> branch + +fn main() { + foo(); +} diff --git a/src/test/ui/parser/diff-markers/item-with-attr.stderr b/src/test/ui/parser/diff-markers/item-with-attr.stderr new file mode 100644 index 00000000000..4fcb782846e --- /dev/null +++ b/src/test/ui/parser/diff-markers/item-with-attr.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/item.rs b/src/test/ui/parser/diff-markers/item.rs new file mode 100644 index 00000000000..4ed36b7b42b --- /dev/null +++ b/src/test/ui/parser/diff-markers/item.rs @@ -0,0 +1,9 @@ +<<<<<<< HEAD //~ ERROR encountered diff marker +fn foo() {} +======= +fn bar() {} +>>>>>>> branch + +fn main() { + foo(); +} diff --git a/src/test/ui/parser/diff-markers/item.stderr b/src/test/ui/parser/diff-markers/item.stderr new file mode 100644 index 00000000000..7c81f0fafa6 --- /dev/null +++ b/src/test/ui/parser/diff-markers/item.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/statement.rs b/src/test/ui/parser/diff-markers/statement.rs new file mode 100644 index 00000000000..e55d16d3bbb --- /dev/null +++ b/src/test/ui/parser/diff-markers/statement.rs @@ -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 +} diff --git a/src/test/ui/parser/diff-markers/statement.stderr b/src/test/ui/parser/diff-markers/statement.stderr new file mode 100644 index 00000000000..4c821c02f0d --- /dev/null +++ b/src/test/ui/parser/diff-markers/statement.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/trait-item.rs b/src/test/ui/parser/diff-markers/trait-item.rs new file mode 100644 index 00000000000..3227c8212c9 --- /dev/null +++ b/src/test/ui/parser/diff-markers/trait-item.rs @@ -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(); +} diff --git a/src/test/ui/parser/diff-markers/trait-item.stderr b/src/test/ui/parser/diff-markers/trait-item.stderr new file mode 100644 index 00000000000..8e18d5db7c7 --- /dev/null +++ b/src/test/ui/parser/diff-markers/trait-item.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/use-statement.rs b/src/test/ui/parser/diff-markers/use-statement.rs new file mode 100644 index 00000000000..6306243a514 --- /dev/null +++ b/src/test/ui/parser/diff-markers/use-statement.rs @@ -0,0 +1,9 @@ +use foo::{ +<<<<<<< HEAD //~ ERROR encountered diff marker + bar, +======= + baz, +>>>>>>> branch +}; + +fn main() {} diff --git a/src/test/ui/parser/diff-markers/use-statement.stderr b/src/test/ui/parser/diff-markers/use-statement.stderr new file mode 100644 index 00000000000..818c90aa59b --- /dev/null +++ b/src/test/ui/parser/diff-markers/use-statement.stderr @@ -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 + -- cgit 1.4.1-3-g733a5 From 38fd5a9acfd4893e572f20591ffea4f3cfd2fc2e Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 28 Dec 2022 18:26:59 -0800 Subject: Account for ADT bodies and struct expressions --- compiler/rustc_parse/src/parser/expr.rs | 1 + compiler/rustc_parse/src/parser/item.rs | 31 ++++++++++++++++++++-- src/test/ui/parser/diff-markers/enum-2.rs | 9 +++++++ src/test/ui/parser/diff-markers/enum-2.stderr | 14 ++++++++++ src/test/ui/parser/diff-markers/enum.rs | 7 +++++ src/test/ui/parser/diff-markers/enum.stderr | 14 ++++++++++ src/test/ui/parser/diff-markers/struct-expr.rs | 12 +++++++++ src/test/ui/parser/diff-markers/struct-expr.stderr | 14 ++++++++++ src/test/ui/parser/diff-markers/struct.rs | 7 +++++ src/test/ui/parser/diff-markers/struct.stderr | 14 ++++++++++ src/test/ui/parser/diff-markers/tuple-struct.rs | 7 +++++ .../ui/parser/diff-markers/tuple-struct.stderr | 14 ++++++++++ 12 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/parser/diff-markers/enum-2.rs create mode 100644 src/test/ui/parser/diff-markers/enum-2.stderr create mode 100644 src/test/ui/parser/diff-markers/enum.rs create mode 100644 src/test/ui/parser/diff-markers/enum.stderr create mode 100644 src/test/ui/parser/diff-markers/struct-expr.rs create mode 100644 src/test/ui/parser/diff-markers/struct-expr.stderr create mode 100644 src/test/ui/parser/diff-markers/struct.rs create mode 100644 src/test/ui/parser/diff-markers/struct.stderr create mode 100644 src/test/ui/parser/diff-markers/tuple-struct.rs create mode 100644 src/test/ui/parser/diff-markers/tuple-struct.stderr (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 6a115088eca..1fc1ffd6cb6 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -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; diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 51de3d40d91..b996ce0a155 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1385,7 +1385,9 @@ impl<'a> Parser<'a> { } fn parse_enum_variant(&mut self) -> PResult<'a, Option> { + self.recover_diff_marker(); let variant_attrs = self.parse_outer_attributes()?; + self.recover_diff_marker(); self.collect_tokens_trailing_token( variant_attrs, ForceCollect::No, @@ -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 { @@ -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)?; diff --git a/src/test/ui/parser/diff-markers/enum-2.rs b/src/test/ui/parser/diff-markers/enum-2.rs new file mode 100644 index 00000000000..d8c527281de --- /dev/null +++ b/src/test/ui/parser/diff-markers/enum-2.rs @@ -0,0 +1,9 @@ +enum E { + Foo { +<<<<<<< HEAD //~ ERROR encountered diff marker + x: u8, +======= + x: i8, +>>>>>>> branch + } +} diff --git a/src/test/ui/parser/diff-markers/enum-2.stderr b/src/test/ui/parser/diff-markers/enum-2.stderr new file mode 100644 index 00000000000..5264a596488 --- /dev/null +++ b/src/test/ui/parser/diff-markers/enum-2.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/enum.rs b/src/test/ui/parser/diff-markers/enum.rs new file mode 100644 index 00000000000..45df6e3251d --- /dev/null +++ b/src/test/ui/parser/diff-markers/enum.rs @@ -0,0 +1,7 @@ +enum E { +<<<<<<< HEAD //~ ERROR encountered diff marker + Foo(u8), +======= + Bar(i8), +>>>>>>> branch +} diff --git a/src/test/ui/parser/diff-markers/enum.stderr b/src/test/ui/parser/diff-markers/enum.stderr new file mode 100644 index 00000000000..6bdc20d3cd3 --- /dev/null +++ b/src/test/ui/parser/diff-markers/enum.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/struct-expr.rs b/src/test/ui/parser/diff-markers/struct-expr.rs new file mode 100644 index 00000000000..99d2fd662c6 --- /dev/null +++ b/src/test/ui/parser/diff-markers/struct-expr.rs @@ -0,0 +1,12 @@ +struct S { + x: u8, +} +fn main() { + let _ = S { +<<<<<<< HEAD //~ ERROR encountered diff marker + x: 42, +======= + x: 0, +>>>>>>> branch + } +} diff --git a/src/test/ui/parser/diff-markers/struct-expr.stderr b/src/test/ui/parser/diff-markers/struct-expr.stderr new file mode 100644 index 00000000000..0ddb1a301e3 --- /dev/null +++ b/src/test/ui/parser/diff-markers/struct-expr.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/struct.rs b/src/test/ui/parser/diff-markers/struct.rs new file mode 100644 index 00000000000..d26464d47bc --- /dev/null +++ b/src/test/ui/parser/diff-markers/struct.rs @@ -0,0 +1,7 @@ +struct S { +<<<<<<< HEAD //~ ERROR encountered diff marker + x: u8, +======= + x: i8, +>>>>>>> branch +} diff --git a/src/test/ui/parser/diff-markers/struct.stderr b/src/test/ui/parser/diff-markers/struct.stderr new file mode 100644 index 00000000000..e47bcfdaedc --- /dev/null +++ b/src/test/ui/parser/diff-markers/struct.stderr @@ -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 + diff --git a/src/test/ui/parser/diff-markers/tuple-struct.rs b/src/test/ui/parser/diff-markers/tuple-struct.rs new file mode 100644 index 00000000000..7eec35c968d --- /dev/null +++ b/src/test/ui/parser/diff-markers/tuple-struct.rs @@ -0,0 +1,7 @@ +struct S( +<<<<<<< HEAD //~ ERROR encountered diff marker + u8, +======= + i8, +>>>>>>> branch +); diff --git a/src/test/ui/parser/diff-markers/tuple-struct.stderr b/src/test/ui/parser/diff-markers/tuple-struct.stderr new file mode 100644 index 00000000000..7f30cbcc1f7 --- /dev/null +++ b/src/test/ui/parser/diff-markers/tuple-struct.stderr @@ -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 + -- cgit 1.4.1-3-g733a5 From 698ebe357f3323a953d0751d5764966b3ffb3cb7 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 28 Dec 2022 18:46:20 -0800 Subject: Tweak wording --- compiler/rustc_parse/src/parser/diagnostics.rs | 19 +++++++++++++++---- compiler/rustc_parse/src/parser/item.rs | 2 +- compiler/rustc_parse/src/parser/stmt.rs | 2 +- src/test/ui/parser/diff-markers/enum-2.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/enum.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/fn-arg.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/item-with-attr.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/item.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/statement.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/struct-expr.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/struct.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/trait-item.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/tuple-struct.stderr | 10 +++++++--- src/test/ui/parser/diff-markers/use-statement.stderr | 10 +++++++--- 14 files changed, 94 insertions(+), 39 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index ffadb77320a..dd7341af4d5 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2586,7 +2586,6 @@ impl<'a> Parser<'a> { 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) { @@ -2597,13 +2596,25 @@ impl<'a> Parser<'a> { self.bump(); } let mut err = self.struct_span_err(spans, "encountered diff marker"); - err.span_label(start, "start"); + err.span_label(start, "after this is the code before the merge"); if let Some(middle) = middle { - err.span_label(middle, "middle"); + err.span_label(middle, ""); } if let Some(end) = end { - err.span_label(end, "end"); + err.span_label(end, "above this are the incoming code changes"); } + 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.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", + ); + err.note( + "for an explanation on these markers from the `git` documentation, visit \ + ", + ); err.emit(); FatalError.raise() } diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index b996ce0a155..c6b6c04de85 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1583,7 +1583,7 @@ impl<'a> Parser<'a> { 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 + // Account for `<<<<<<<` diff markers. We can't proactively 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()); diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 4cbf89169b6..0daae457d30 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -537,7 +537,7 @@ impl<'a> Parser<'a> { break; } if self.is_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) { - // Account for `<<<<<<<` diff markers. We can't proactivelly error here because + // Account for `<<<<<<<` diff markers. We can't proactively 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()); diff --git a/src/test/ui/parser/diff-markers/enum-2.stderr b/src/test/ui/parser/diff-markers/enum-2.stderr index 5264a596488..4e612a5427a 100644 --- a/src/test/ui/parser/diff-markers/enum-2.stderr +++ b/src/test/ui/parser/diff-markers/enum-2.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/enum-2.rs:3:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | x: u8, LL | ======= - | ^^^^^^^ middle + | ------- LL | x: i8, LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/enum.stderr b/src/test/ui/parser/diff-markers/enum.stderr index 6bdc20d3cd3..abbf3fb41e7 100644 --- a/src/test/ui/parser/diff-markers/enum.stderr +++ b/src/test/ui/parser/diff-markers/enum.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/enum.rs:2:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | Foo(u8), LL | ======= - | ^^^^^^^ middle + | ------- LL | Bar(i8), LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/fn-arg.stderr b/src/test/ui/parser/diff-markers/fn-arg.stderr index 4a816ff75bc..933a206410e 100644 --- a/src/test/ui/parser/diff-markers/fn-arg.stderr +++ b/src/test/ui/parser/diff-markers/fn-arg.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/fn-arg.rs:3:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | x: u8, LL | ======= - | ^^^^^^^ middle + | ------- LL | x: i8, LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/item-with-attr.stderr b/src/test/ui/parser/diff-markers/item-with-attr.stderr index 4fcb782846e..850e2368e55 100644 --- a/src/test/ui/parser/diff-markers/item-with-attr.stderr +++ b/src/test/ui/parser/diff-markers/item-with-attr.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/item-with-attr.rs:2:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | fn foo() {} LL | ======= - | ^^^^^^^ middle + | ------- LL | fn bar() {} LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/item.stderr b/src/test/ui/parser/diff-markers/item.stderr index 7c81f0fafa6..9ab3631a60e 100644 --- a/src/test/ui/parser/diff-markers/item.stderr +++ b/src/test/ui/parser/diff-markers/item.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/item.rs:1:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | fn foo() {} LL | ======= - | ^^^^^^^ middle + | ------- LL | fn bar() {} LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/statement.stderr b/src/test/ui/parser/diff-markers/statement.stderr index 4c821c02f0d..7ca2495b829 100644 --- a/src/test/ui/parser/diff-markers/statement.stderr +++ b/src/test/ui/parser/diff-markers/statement.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/statement.rs:10:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | S::foo(); LL | ======= - | ^^^^^^^ middle + | ------- LL | S::bar(); LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/struct-expr.stderr b/src/test/ui/parser/diff-markers/struct-expr.stderr index 0ddb1a301e3..d70476a9833 100644 --- a/src/test/ui/parser/diff-markers/struct-expr.stderr +++ b/src/test/ui/parser/diff-markers/struct-expr.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/struct-expr.rs:6:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | x: 42, LL | ======= - | ^^^^^^^ middle + | ------- LL | x: 0, LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/struct.stderr b/src/test/ui/parser/diff-markers/struct.stderr index e47bcfdaedc..cc0b3da664e 100644 --- a/src/test/ui/parser/diff-markers/struct.stderr +++ b/src/test/ui/parser/diff-markers/struct.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/struct.rs:2:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | x: u8, LL | ======= - | ^^^^^^^ middle + | ------- LL | x: i8, LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/trait-item.stderr b/src/test/ui/parser/diff-markers/trait-item.stderr index 8e18d5db7c7..cdc19f8e076 100644 --- a/src/test/ui/parser/diff-markers/trait-item.stderr +++ b/src/test/ui/parser/diff-markers/trait-item.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/trait-item.rs:2:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | fn foo() {} LL | ======= - | ^^^^^^^ middle + | ------- LL | fn bar() {} LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/tuple-struct.stderr b/src/test/ui/parser/diff-markers/tuple-struct.stderr index 7f30cbcc1f7..d673db89837 100644 --- a/src/test/ui/parser/diff-markers/tuple-struct.stderr +++ b/src/test/ui/parser/diff-markers/tuple-struct.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/tuple-struct.rs:2:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | u8, LL | ======= - | ^^^^^^^ middle + | ------- LL | i8, LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error diff --git a/src/test/ui/parser/diff-markers/use-statement.stderr b/src/test/ui/parser/diff-markers/use-statement.stderr index 818c90aa59b..12e6f57dd50 100644 --- a/src/test/ui/parser/diff-markers/use-statement.stderr +++ b/src/test/ui/parser/diff-markers/use-statement.stderr @@ -2,13 +2,17 @@ error: encountered diff marker --> $DIR/use-statement.rs:2:1 | LL | <<<<<<< HEAD - | ^^^^^^^ start + | ^^^^^^^ after this is the code before the merge LL | bar, LL | ======= - | ^^^^^^^ middle + | ------- LL | baz, LL | >>>>>>> branch - | ^^^^^^^ end + | ^^^^^^^ above this are the incoming code changes + | + = 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 error: aborting due to previous error -- cgit 1.4.1-3-g733a5 From 62c8e3144a8bc7d07d66bc14c8a7cce94a3dbce5 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 28 Dec 2022 20:55:46 -0800 Subject: Add support for diff3 format --- compiler/rustc_parse/src/parser/diagnostics.rs | 7 +++++++ src/test/ui/parser/diff-markers/enum-2.rs | 4 +++- src/test/ui/parser/diff-markers/enum-2.stderr | 5 ++++- 3 files changed, 14 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index dd7341af4d5..7ab924bd39d 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2579,12 +2579,16 @@ impl<'a> Parser<'a> { }; 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 { break; } + if let Some(span) = self.diff_marker(&TokenKind::OrOr, &TokenKind::BinOp(token::Or)) { + middlediff3 = Some(span); + } if let Some(span) = self.diff_marker(&TokenKind::EqEq, &TokenKind::Eq) { middle = Some(span); } @@ -2597,6 +2601,9 @@ impl<'a> Parser<'a> { } let mut err = self.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, ""); + } if let Some(middle) = middle { err.span_label(middle, ""); } diff --git a/src/test/ui/parser/diff-markers/enum-2.rs b/src/test/ui/parser/diff-markers/enum-2.rs index d8c527281de..76ea980fc62 100644 --- a/src/test/ui/parser/diff-markers/enum-2.rs +++ b/src/test/ui/parser/diff-markers/enum-2.rs @@ -2,8 +2,10 @@ enum E { Foo { <<<<<<< HEAD //~ ERROR encountered diff marker x: u8, +||||||| + z: (), ======= - x: i8, + y: i8, >>>>>>> branch } } diff --git a/src/test/ui/parser/diff-markers/enum-2.stderr b/src/test/ui/parser/diff-markers/enum-2.stderr index 4e612a5427a..63da5c2a6e1 100644 --- a/src/test/ui/parser/diff-markers/enum-2.stderr +++ b/src/test/ui/parser/diff-markers/enum-2.stderr @@ -4,9 +4,12 @@ error: encountered diff marker LL | <<<<<<< HEAD | ^^^^^^^ after this is the code before the merge LL | x: u8, +LL | ||||||| + | ------- +LL | z: (), LL | ======= | ------- -LL | x: i8, +LL | y: i8, LL | >>>>>>> branch | ^^^^^^^ above this are the incoming code changes | -- cgit 1.4.1-3-g733a5