Clarify that the diff_marker is talking about version control system

conflicts specifically and a few more improvements.
This commit is contained in:
ardi 2024-05-08 14:22:09 +02:00
parent 8dc6a5d145
commit 1f6d271527
5 changed files with 47 additions and 38 deletions

View File

@ -241,7 +241,7 @@ fn unclosed_delim_err(
// we have no way of tracking this in the lexer itself, so we piggyback on the parser // we have no way of tracking this in the lexer itself, so we piggyback on the parser
let mut in_cond = false; let mut in_cond = false;
while parser.token != token::Eof { while parser.token != token::Eof {
if let Err(diff_err) = parser.err_diff_marker() { if let Err(diff_err) = parser.err_vcs_conflict_marker() {
diff_errs.push(diff_err); diff_errs.push(diff_err);
} else if parser.is_keyword_ahead(0, &[kw::If, kw::While]) { } else if parser.is_keyword_ahead(0, &[kw::If, kw::While]) {
in_cond = true; in_cond = true;

View File

@ -2954,13 +2954,23 @@ pub(crate) fn maybe_err_dotdotlt_syntax(&self, maybe_lt: Token, mut err: PErr<'a
err err
} }
pub fn is_diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> bool { /// This checks if this is a conflict marker, depending of the parameter passed.
///
/// * `>>>>>`
/// * `=====`
/// * `<<<<<`
///
pub fn is_vcs_conflict_marker(
&mut self,
long_kind: &TokenKind,
short_kind: &TokenKind,
) -> bool {
(0..3).all(|i| self.look_ahead(i, |tok| tok == long_kind)) (0..3).all(|i| self.look_ahead(i, |tok| tok == long_kind))
&& self.look_ahead(3, |tok| tok == short_kind) && self.look_ahead(3, |tok| tok == short_kind)
} }
fn diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> Option<Span> { fn conflict_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> Option<Span> {
if self.is_diff_marker(long_kind, short_kind) { if self.is_vcs_conflict_marker(long_kind, short_kind) {
let lo = self.token.span; let lo = self.token.span;
for _ in 0..4 { for _ in 0..4 {
self.bump(); self.bump();
@ -2970,15 +2980,16 @@ fn diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> Opti
None None
} }
pub fn recover_diff_marker(&mut self) { pub fn recover_vcs_conflict_marker(&mut self) {
if let Err(err) = self.err_diff_marker() { if let Err(err) = self.err_vcs_conflict_marker() {
err.emit(); err.emit();
FatalError.raise(); FatalError.raise();
} }
} }
pub fn err_diff_marker(&mut self) -> PResult<'a, ()> { pub fn err_vcs_conflict_marker(&mut self) -> PResult<'a, ()> {
let Some(start) = self.diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) else { let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
else {
return Ok(()); return Ok(());
}; };
let mut spans = Vec::with_capacity(3); let mut spans = Vec::with_capacity(3);
@ -2990,13 +3001,15 @@ pub fn err_diff_marker(&mut self) -> PResult<'a, ()> {
if self.token.kind == TokenKind::Eof { if self.token.kind == TokenKind::Eof {
break; break;
} }
if let Some(span) = self.diff_marker(&TokenKind::OrOr, &TokenKind::BinOp(token::Or)) { if let Some(span) = self.conflict_marker(&TokenKind::OrOr, &TokenKind::BinOp(token::Or))
{
middlediff3 = Some(span); middlediff3 = Some(span);
} }
if let Some(span) = self.diff_marker(&TokenKind::EqEq, &TokenKind::Eq) { if let Some(span) = self.conflict_marker(&TokenKind::EqEq, &TokenKind::Eq) {
middle = Some(span); middle = Some(span);
} }
if let Some(span) = self.diff_marker(&TokenKind::BinOp(token::Shr), &TokenKind::Gt) { if let Some(span) = self.conflict_marker(&TokenKind::BinOp(token::Shr), &TokenKind::Gt)
{
spans.push(span); spans.push(span);
end = Some(span); end = Some(span);
break; break;

View File

@ -3734,7 +3734,7 @@ fn recover_ident_into_label(&mut self, ident: Ident) -> Label {
/// Parses `ident (COLON expr)?`. /// Parses `ident (COLON expr)?`.
fn parse_expr_field(&mut self) -> PResult<'a, ExprField> { fn parse_expr_field(&mut self) -> PResult<'a, ExprField> {
let attrs = self.parse_outer_attributes()?; let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let lo = this.token.span; let lo = this.token.span;

View File

@ -49,6 +49,7 @@ fn parse_item_mod(&mut self, attrs: &mut AttrVec) -> PResult<'a, ItemInfo> {
} }
/// Parses the contents of a module (inner attributes followed by module items). /// Parses the contents of a module (inner attributes followed by module items).
/// We exit once we hit `term`
pub fn parse_mod( pub fn parse_mod(
&mut self, &mut self,
term: &TokenKind, term: &TokenKind,
@ -101,9 +102,9 @@ fn parse_item_(
fn_parse_mode: FnParseMode, fn_parse_mode: FnParseMode,
force_collect: ForceCollect, force_collect: ForceCollect,
) -> PResult<'a, Option<Item>> { ) -> PResult<'a, Option<Item>> {
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
let attrs = self.parse_outer_attributes()?; let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
self.parse_item_common(attrs, true, false, fn_parse_mode, force_collect) self.parse_item_common(attrs, true, false, fn_parse_mode, force_collect)
} }
@ -723,7 +724,7 @@ fn parse_item_list<T>(
if self.recover_doc_comment_before_brace() { if self.recover_doc_comment_before_brace() {
continue; continue;
} }
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
match parse_item(self) { match parse_item(self) {
Ok(None) => { Ok(None) => {
let mut is_unnecessary_semicolon = !items.is_empty() let mut is_unnecessary_semicolon = !items.is_empty()
@ -1070,7 +1071,7 @@ fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
/// ``` /// ```
fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> { fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> {
self.parse_delim_comma_seq(Delimiter::Brace, |p| { self.parse_delim_comma_seq(Delimiter::Brace, |p| {
p.recover_diff_marker(); p.recover_vcs_conflict_marker();
Ok((p.parse_use_tree()?, DUMMY_NODE_ID)) Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
}) })
.map(|(r, _)| r) .map(|(r, _)| r)
@ -1497,9 +1498,9 @@ fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
} }
fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> { fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
let variant_attrs = self.parse_outer_attributes()?; let variant_attrs = self.parse_outer_attributes()?;
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
let help = "enum variants can be `Variant`, `Variant = <integer>`, \ let help = "enum variants can be `Variant`, `Variant = <integer>`, \
`Variant(Type, ..., TypeN)` or `Variant { fields: Types }`"; `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
self.collect_tokens_trailing_token( self.collect_tokens_trailing_token(
@ -1688,6 +1689,10 @@ fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
Ok((class_name, ItemKind::Union(vdata, generics))) Ok((class_name, ItemKind::Union(vdata, generics)))
} }
/// This function parses the fields of record structs:
///
/// - `struct S { ... }`
/// - `enum E { Variant { ... } }`
pub(crate) fn parse_record_struct_body( pub(crate) fn parse_record_struct_body(
&mut self, &mut self,
adt_ty: &str, adt_ty: &str,
@ -1714,19 +1719,10 @@ pub(crate) fn parse_record_struct_body(
self.eat(&token::CloseDelim(Delimiter::Brace)); self.eat(&token::CloseDelim(Delimiter::Brace));
} else { } else {
let token_str = super::token_descr(&self.token); let token_str = super::token_descr(&self.token);
let msg = format!( let where_str = if parsed_where { "" } else { "`where`, or " };
"expected {}`{{` after struct name, found {}", let msg = format!("expected {where_str}`{{` after struct name, found {token_str}");
if parsed_where { "" } else { "`where`, or " },
token_str
);
let mut err = self.dcx().struct_span_err(self.token.span, msg); let mut err = self.dcx().struct_span_err(self.token.span, msg);
err.span_label( err.span_label(self.token.span, format!("expected {where_str}`{{` after struct name",));
self.token.span,
format!(
"expected {}`{{` after struct name",
if parsed_where { "" } else { "`where`, or " }
),
);
return Err(err); return Err(err);
} }
@ -1740,7 +1736,7 @@ pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef
let attrs = p.parse_outer_attributes()?; let attrs = p.parse_outer_attributes()?;
p.collect_tokens_trailing_token(attrs, ForceCollect::No, |p, attrs| { p.collect_tokens_trailing_token(attrs, ForceCollect::No, |p, attrs| {
let mut snapshot = None; let mut snapshot = None;
if p.is_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) { if p.is_vcs_conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
// Account for `<<<<<<<` diff markers. We can't proactively 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 // that can be a valid type start, so we snapshot and reparse only we've
// encountered another parse error. // encountered another parse error.
@ -1751,7 +1747,7 @@ pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef
Ok(vis) => vis, Ok(vis) => vis,
Err(err) => { Err(err) => {
if let Some(ref mut snapshot) = snapshot { if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker(); snapshot.recover_vcs_conflict_marker();
} }
return Err(err); return Err(err);
} }
@ -1760,7 +1756,7 @@ pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef
Ok(ty) => ty, Ok(ty) => ty,
Err(err) => { Err(err) => {
if let Some(ref mut snapshot) = snapshot { if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker(); snapshot.recover_vcs_conflict_marker();
} }
return Err(err); return Err(err);
} }
@ -1785,9 +1781,9 @@ pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef
/// Parses an element of a struct declaration. /// Parses an element of a struct declaration.
fn parse_field_def(&mut self, adt_ty: &str) -> PResult<'a, FieldDef> { fn parse_field_def(&mut self, adt_ty: &str) -> PResult<'a, FieldDef> {
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
let attrs = self.parse_outer_attributes()?; let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker(); self.recover_vcs_conflict_marker();
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let lo = this.token.span; let lo = this.token.span;
let vis = this.parse_visibility(FollowedByType::No)?; let vis = this.parse_visibility(FollowedByType::No)?;
@ -2647,7 +2643,7 @@ pub(super) fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, ThinV
} }
let (mut params, _) = self.parse_paren_comma_seq(|p| { let (mut params, _) = self.parse_paren_comma_seq(|p| {
p.recover_diff_marker(); p.recover_vcs_conflict_marker();
let snapshot = p.create_snapshot_for_diagnostic(); let snapshot = p.create_snapshot_for_diagnostic();
let param = p.parse_param_general(req_name, first_param).or_else(|e| { let param = p.parse_param_general(req_name, first_param).or_else(|e| {
let guar = e.emit(); let guar = e.emit();

View File

@ -567,7 +567,7 @@ pub(crate) fn parse_block_tail(
if self.token == token::Eof { if self.token == token::Eof {
break; break;
} }
if self.is_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) { if self.is_vcs_conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
// Account for `<<<<<<<` diff markers. We can't proactively 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 // that can be a valid path start, so we snapshot and reparse only we've
// encountered another parse error. // encountered another parse error.
@ -576,7 +576,7 @@ pub(crate) fn parse_block_tail(
let stmt = match self.parse_full_stmt(recover) { let stmt = match self.parse_full_stmt(recover) {
Err(mut err) if recover.yes() => { Err(mut err) if recover.yes() => {
if let Some(ref mut snapshot) = snapshot { if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker(); snapshot.recover_vcs_conflict_marker();
} }
if self.token == token::Colon { if self.token == token::Colon {
// if a previous and next token of the current one is // if a previous and next token of the current one is