Skip to content

Commit

Permalink
Clarify that the diff_marker is talking about git specifically and a few
Browse files Browse the repository at this point in the history
more improvements.
  • Loading branch information
dev-ardi committed May 14, 2024
1 parent ad84b32 commit 9db789a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
// we have no way of tracking this in the lexer itself, so we piggyback on the parser
let mut in_cond = false;
while parser.token != token::Eof {
if let Err(diff_err) = parser.err_diff_marker() {
if let Err(diff_err) = parser.err_git_diff_marker() {
diff_errs.push(diff_err);
} else if parser.is_keyword_ahead(0, &[kw::If, kw::While]) {
in_cond = true;
Expand Down
27 changes: 17 additions & 10 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2954,13 +2954,17 @@ impl<'a> Parser<'a> {
err
}

pub fn is_diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> bool {
/// This checks if this is a git diff marker, one of
/// >>>>>
/// =====
/// <<<<<
pub fn is_git_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) {
fn git_diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> Option<Span> {
if self.is_git_diff_marker(long_kind, short_kind) {
let lo = self.token.span;
for _ in 0..4 {
self.bump();
Expand All @@ -2970,15 +2974,16 @@ impl<'a> Parser<'a> {
None
}

pub fn recover_diff_marker(&mut self) {
if let Err(err) = self.err_diff_marker() {
pub fn recover_git_diff_marker(&mut self) {
if let Err(err) = self.err_git_diff_marker() {
err.emit();
FatalError.raise();
}
}

pub fn err_diff_marker(&mut self) -> PResult<'a, ()> {
let Some(start) = self.diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) else {
pub fn err_git_diff_marker(&mut self) -> PResult<'a, ()> {
let Some(start) = self.git_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
else {
return Ok(());
};
let mut spans = Vec::with_capacity(3);
Expand All @@ -2990,13 +2995,15 @@ impl<'a> Parser<'a> {
if self.token.kind == TokenKind::Eof {
break;
}
if let Some(span) = self.diff_marker(&TokenKind::OrOr, &TokenKind::BinOp(token::Or)) {
if let Some(span) = self.git_diff_marker(&TokenKind::OrOr, &TokenKind::BinOp(token::Or))
{
middlediff3 = Some(span);
}
if let Some(span) = self.diff_marker(&TokenKind::EqEq, &TokenKind::Eq) {
if let Some(span) = self.git_diff_marker(&TokenKind::EqEq, &TokenKind::Eq) {
middle = Some(span);
}
if let Some(span) = self.diff_marker(&TokenKind::BinOp(token::Shr), &TokenKind::Gt) {
if let Some(span) = self.git_diff_marker(&TokenKind::BinOp(token::Shr), &TokenKind::Gt)
{
spans.push(span);
end = Some(span);
break;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3734,7 +3734,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.recover_git_diff_marker();
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
let lo = this.token.span;

Expand Down
43 changes: 19 additions & 24 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl<'a> Parser<'a> {
}

/// Parses the contents of a module (inner attributes followed by module items).
/// We exit once we hit `term`
pub fn parse_mod(
&mut self,
term: &TokenKind,
Expand Down Expand Up @@ -101,9 +102,9 @@ impl<'a> Parser<'a> {
fn_parse_mode: FnParseMode,
force_collect: ForceCollect,
) -> PResult<'a, Option<Item>> {
self.recover_diff_marker();
self.recover_git_diff_marker();
let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
self.recover_git_diff_marker();
self.parse_item_common(attrs, true, false, fn_parse_mode, force_collect)
}

Expand Down Expand Up @@ -723,7 +724,7 @@ impl<'a> Parser<'a> {
if self.recover_doc_comment_before_brace() {
continue;
}
self.recover_diff_marker();
self.recover_git_diff_marker();
match parse_item(self) {
Ok(None) => {
let mut is_unnecessary_semicolon = !items.is_empty()
Expand Down Expand Up @@ -1070,7 +1071,7 @@ impl<'a> Parser<'a> {
/// ```
fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> {
self.parse_delim_comma_seq(Delimiter::Brace, |p| {
p.recover_diff_marker();
p.recover_git_diff_marker();
Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
})
.map(|(r, _)| r)
Expand Down Expand Up @@ -1497,9 +1498,9 @@ impl<'a> Parser<'a> {
}

fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
self.recover_diff_marker();
self.recover_git_diff_marker();
let variant_attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
self.recover_git_diff_marker();
let help = "enum variants can be `Variant`, `Variant = <integer>`, \
`Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
self.collect_tokens_trailing_token(
Expand Down Expand Up @@ -1688,6 +1689,9 @@ impl<'a> Parser<'a> {
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(
&mut self,
adt_ty: &str,
Expand All @@ -1714,19 +1718,10 @@ impl<'a> Parser<'a> {
self.eat(&token::CloseDelim(Delimiter::Brace));
} else {
let token_str = super::token_descr(&self.token);
let msg = format!(
"expected {}`{{` after struct name, found {}",
if parsed_where { "" } else { "`where`, or " },
token_str
);
let where_str = if parsed_where { "" } else { "`where`, or " };
let msg = format!("expected {where_str}`{{` after struct name, found {token_str}");
let mut err = self.dcx().struct_span_err(self.token.span, msg);
err.span_label(
self.token.span,
format!(
"expected {}`{{` after struct name",
if parsed_where { "" } else { "`where`, or " }
),
);
err.span_label(self.token.span, format!("expected {where_str}`{{` after struct name",));
return Err(err);
}

Expand All @@ -1740,7 +1735,7 @@ impl<'a> Parser<'a> {
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) {
if p.is_git_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
// 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.
Expand All @@ -1751,7 +1746,7 @@ impl<'a> Parser<'a> {
Ok(vis) => vis,
Err(err) => {
if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker();
snapshot.recover_git_diff_marker();
}
return Err(err);
}
Expand All @@ -1760,7 +1755,7 @@ impl<'a> Parser<'a> {
Ok(ty) => ty,
Err(err) => {
if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker();
snapshot.recover_git_diff_marker();
}
return Err(err);
}
Expand All @@ -1785,9 +1780,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();
self.recover_git_diff_marker();
let attrs = self.parse_outer_attributes()?;
self.recover_diff_marker();
self.recover_git_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 Expand Up @@ -2647,7 +2642,7 @@ impl<'a> Parser<'a> {
}

let (mut params, _) = self.parse_paren_comma_seq(|p| {
p.recover_diff_marker();
p.recover_git_diff_marker();
let snapshot = p.create_snapshot_for_diagnostic();
let param = p.parse_param_general(req_name, first_param).or_else(|e| {
let guar = e.emit();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ impl<'a> Parser<'a> {
if self.token == token::Eof {
break;
}
if self.is_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
if self.is_git_diff_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) {
// 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.
Expand All @@ -576,7 +576,7 @@ impl<'a> Parser<'a> {
let stmt = match self.parse_full_stmt(recover) {
Err(mut err) if recover.yes() => {
if let Some(ref mut snapshot) = snapshot {
snapshot.recover_diff_marker();
snapshot.recover_git_diff_marker();
}
if self.token == token::Colon {
// if a previous and next token of the current one is
Expand Down

0 comments on commit 9db789a

Please sign in to comment.