1
Fork 0
This commit is contained in:
bjorn3 2025-02-08 22:12:13 +00:00
parent 3183b44a1e
commit 1fcae03369
287 changed files with 5888 additions and 4608 deletions

View file

@ -1121,23 +1121,29 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedIdentifier {
let token_descr = TokenDescription::from_token(&self.token);
let mut add_token = true;
let mut diag = Diag::new(dcx, level, match token_descr {
Some(TokenDescription::ReservedIdentifier) => {
fluent::parse_expected_identifier_found_reserved_identifier_str
}
Some(TokenDescription::Keyword) => fluent::parse_expected_identifier_found_keyword_str,
Some(TokenDescription::ReservedKeyword) => {
fluent::parse_expected_identifier_found_reserved_keyword_str
}
Some(TokenDescription::DocComment) => {
fluent::parse_expected_identifier_found_doc_comment_str
}
Some(TokenDescription::MetaVar(_)) => {
add_token = false;
fluent::parse_expected_identifier_found_metavar_str
}
None => fluent::parse_expected_identifier_found_str,
});
let mut diag = Diag::new(
dcx,
level,
match token_descr {
Some(TokenDescription::ReservedIdentifier) => {
fluent::parse_expected_identifier_found_reserved_identifier_str
}
Some(TokenDescription::Keyword) => {
fluent::parse_expected_identifier_found_keyword_str
}
Some(TokenDescription::ReservedKeyword) => {
fluent::parse_expected_identifier_found_reserved_keyword_str
}
Some(TokenDescription::DocComment) => {
fluent::parse_expected_identifier_found_doc_comment_str
}
Some(TokenDescription::MetaVar(_)) => {
add_token = false;
fluent::parse_expected_identifier_found_metavar_str
}
None => fluent::parse_expected_identifier_found_str,
},
);
diag.span(self.span);
if add_token {
diag.arg("token", self.token);
@ -1182,21 +1188,27 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedSemi {
let token_descr = TokenDescription::from_token(&self.token);
let mut add_token = true;
let mut diag = Diag::new(dcx, level, match token_descr {
Some(TokenDescription::ReservedIdentifier) => {
fluent::parse_expected_semi_found_reserved_identifier_str
}
Some(TokenDescription::Keyword) => fluent::parse_expected_semi_found_keyword_str,
Some(TokenDescription::ReservedKeyword) => {
fluent::parse_expected_semi_found_reserved_keyword_str
}
Some(TokenDescription::DocComment) => fluent::parse_expected_semi_found_doc_comment_str,
Some(TokenDescription::MetaVar(_)) => {
add_token = false;
fluent::parse_expected_semi_found_metavar_str
}
None => fluent::parse_expected_semi_found_str,
});
let mut diag = Diag::new(
dcx,
level,
match token_descr {
Some(TokenDescription::ReservedIdentifier) => {
fluent::parse_expected_semi_found_reserved_identifier_str
}
Some(TokenDescription::Keyword) => fluent::parse_expected_semi_found_keyword_str,
Some(TokenDescription::ReservedKeyword) => {
fluent::parse_expected_semi_found_reserved_keyword_str
}
Some(TokenDescription::DocComment) => {
fluent::parse_expected_semi_found_doc_comment_str
}
Some(TokenDescription::MetaVar(_)) => {
add_token = false;
fluent::parse_expected_semi_found_metavar_str
}
None => fluent::parse_expected_semi_found_str,
},
);
diag.span(self.span);
if add_token {
diag.arg("token", self.token);

View file

@ -502,10 +502,10 @@ fn make_attr_token_stream(
for flat_token in iter {
match flat_token {
FlatToken::Token((Token { kind: TokenKind::OpenDelim(delim), span }, spacing)) => {
stack_rest.push(mem::replace(&mut stack_top, FrameData {
open_delim_sp: Some((delim, span, spacing)),
inner: vec![],
}));
stack_rest.push(mem::replace(
&mut stack_top,
FrameData { open_delim_sp: Some((delim, span, spacing)), inner: vec![] },
));
}
FlatToken::Token((Token { kind: TokenKind::CloseDelim(delim), span }, spacing)) => {
let frame_data = mem::replace(&mut stack_top, stack_rest.pop().unwrap());

View file

@ -684,12 +684,15 @@ impl<'a> Parser<'a> {
let span = self.token.span.with_lo(pos).with_hi(pos);
err.span_suggestion_verbose(
span,
format!("add a space before {} to write a regular comment", match (kind, style) {
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
},),
format!(
"add a space before {} to write a regular comment",
match (kind, style) {
(token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
(token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
(token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
},
),
" ".to_string(),
Applicability::MachineApplicable,
);
@ -1894,13 +1897,14 @@ impl<'a> Parser<'a> {
(token::Eof, None) => (self.prev_token.span, self.token.span),
_ => (self.prev_token.span.shrink_to_hi(), self.token.span),
};
let msg = format!("expected `{}`, found {}", token_str, match (
&self.token.kind,
self.subparser_name
) {
(token::Eof, Some(origin)) => format!("end of {origin}"),
_ => this_token_str,
},);
let msg = format!(
"expected `{}`, found {}",
token_str,
match (&self.token.kind, self.subparser_name) {
(token::Eof, Some(origin)) => format!("end of {origin}"),
_ => this_token_str,
},
);
let mut err = self.dcx().struct_span_err(sp, msg);
let label_exp = format!("expected `{token_str}`");
let sm = self.psess.source_map();
@ -2826,25 +2830,27 @@ impl<'a> Parser<'a> {
PatKind::Ident(BindingMode::NONE, ident, None) => {
match &first_pat.kind {
PatKind::Ident(_, old_ident, _) => {
let path = PatKind::Path(None, Path {
span: new_span,
segments: thin_vec![
PathSegment::from_ident(*old_ident),
PathSegment::from_ident(*ident),
],
tokens: None,
});
let path = PatKind::Path(
None,
Path {
span: new_span,
segments: thin_vec![
PathSegment::from_ident(*old_ident),
PathSegment::from_ident(*ident),
],
tokens: None,
},
);
first_pat = self.mk_pat(new_span, path);
show_sugg = true;
}
PatKind::Path(old_qself, old_path) => {
let mut segments = old_path.segments.clone();
segments.push(PathSegment::from_ident(*ident));
let path = PatKind::Path(old_qself.clone(), Path {
span: new_span,
segments,
tokens: None,
});
let path = PatKind::Path(
old_qself.clone(),
Path { span: new_span, segments, tokens: None },
);
first_pat = self.mk_pat(new_span, path);
show_sugg = true;
}

View file

@ -807,17 +807,20 @@ impl<'a> Parser<'a> {
// Check if an illegal postfix operator has been added after the cast.
// If the resulting expression is not a cast, it is an illegal postfix operator.
if !matches!(with_postfix.kind, ExprKind::Cast(_, _)) {
let msg = format!("cast cannot be followed by {}", match with_postfix.kind {
ExprKind::Index(..) => "indexing",
ExprKind::Try(_) => "`?`",
ExprKind::Field(_, _) => "a field access",
ExprKind::MethodCall(_) => "a method call",
ExprKind::Call(_, _) => "a function call",
ExprKind::Await(_, _) => "`.await`",
ExprKind::Match(_, _, MatchKind::Postfix) => "a postfix match",
ExprKind::Err(_) => return Ok(with_postfix),
_ => unreachable!("parse_dot_or_call_expr_with_ shouldn't produce this"),
});
let msg = format!(
"cast cannot be followed by {}",
match with_postfix.kind {
ExprKind::Index(..) => "indexing",
ExprKind::Try(_) => "`?`",
ExprKind::Field(_, _) => "a field access",
ExprKind::MethodCall(_) => "a method call",
ExprKind::Call(_, _) => "a function call",
ExprKind::Await(_, _) => "`.await`",
ExprKind::Match(_, _, MatchKind::Postfix) => "a postfix match",
ExprKind::Err(_) => return Ok(with_postfix),
_ => unreachable!("parse_dot_or_call_expr_with_ shouldn't produce this"),
}
);
let mut err = self.dcx().struct_span_err(span, msg);
let suggest_parens = |err: &mut Diag<'_>| {
@ -2862,13 +2865,10 @@ impl<'a> Parser<'a> {
.emit_err(errors::MissingExpressionInForLoop { span: expr.span.shrink_to_lo() });
let err_expr = self.mk_expr(expr.span, ExprKind::Err(guar));
let block = self.mk_block(thin_vec![], BlockCheckMode::Default, self.prev_token.span);
return Ok(self.mk_expr(lo.to(self.prev_token.span), ExprKind::ForLoop {
pat,
iter: err_expr,
body: block,
label: opt_label,
kind,
}));
return Ok(self.mk_expr(
lo.to(self.prev_token.span),
ExprKind::ForLoop { pat, iter: err_expr, body: block, label: opt_label, kind },
));
}
let (attrs, loop_block) = self.parse_inner_attrs_and_block()?;

View file

@ -1954,10 +1954,10 @@ impl<'a> Parser<'a> {
// Try to recover extra trailing angle brackets
if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind {
if let Some(last_segment) = segments.last() {
let guar = self.check_trailing_angle_brackets(last_segment, &[
exp!(Comma),
exp!(CloseBrace),
]);
let guar = self.check_trailing_angle_brackets(
last_segment,
&[exp!(Comma), exp!(CloseBrace)],
);
if let Some(_guar) = guar {
// Handle a case like `Vec<u8>>,` where we can continue parsing fields
// after the comma

View file

@ -106,11 +106,10 @@ impl<'a> Parser<'a> {
self.parse_path_segments(&mut path.segments, style, None)?;
}
Ok((qself, Path {
segments: path.segments,
span: lo.to(self.prev_token.span),
tokens: None,
}))
Ok((
qself,
Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
))
}
/// Recover from an invalid single colon, when the user likely meant a qualified path.
@ -485,13 +484,16 @@ impl<'a> Parser<'a> {
error.span_suggestion_verbose(
prev_token_before_parsing.span,
format!("consider removing the `::` here to {}", match style {
PathStyle::Expr => "call the expression",
PathStyle::Pat => "turn this into a tuple struct pattern",
_ => {
return;
format!(
"consider removing the `::` here to {}",
match style {
PathStyle::Expr => "call the expression",
PathStyle::Pat => "turn this into a tuple struct pattern",
_ => {
return;
}
}
}),
),
"",
Applicability::MaybeIncorrect,
);

View file

@ -417,14 +417,20 @@ impl<'a> Parser<'a> {
fn check_let_else_init_trailing_brace(&self, init: &ast::Expr) {
if let Some(trailing) = classify::expr_trailing_brace(init) {
let (span, sugg) = match trailing {
TrailingBrace::MacCall(mac) => (mac.span(), errors::WrapInParentheses::MacroArgs {
left: mac.args.dspan.open,
right: mac.args.dspan.close,
}),
TrailingBrace::Expr(expr) => (expr.span, errors::WrapInParentheses::Expression {
left: expr.span.shrink_to_lo(),
right: expr.span.shrink_to_hi(),
}),
TrailingBrace::MacCall(mac) => (
mac.span(),
errors::WrapInParentheses::MacroArgs {
left: mac.args.dspan.open,
right: mac.args.dspan.close,
},
),
TrailingBrace::Expr(expr) => (
expr.span,
errors::WrapInParentheses::Expression {
left: expr.span.shrink_to_lo(),
right: expr.span.shrink_to_hi(),
},
),
};
self.dcx().emit_err(errors::InvalidCurlyInLetElse {
span: span.with_lo(span.hi() - BytePos(1)),