Skip to content

Commit c568879

Browse files
committed
Migrate some range parsing diagnostics
1 parent 4bfab39 commit c568879

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

compiler/rustc_error_messages/locales/en-US/parse.ftl

+11
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ parse_match_arm_body_without_braces = `match` arm body without braces
199199
} with a body
200200
.suggestion_use_comma_not_semicolon = use a comma to end a `match` arm expression
201201
202+
parse_inclusive_range_extra_equals = unexpected `=` after inclusive range
203+
.suggestion_remove_eq = use `..=` instead
204+
.note = inclusive ranges end with a single equals sign (`..=`)
205+
206+
parse_inclusive_range_match_arrow = unexpected `=>` after open range
207+
.suggestion_add_space = add a space between the pattern and `=>`
208+
209+
parse_inclusive_range_no_end = inclusive range with no end
210+
.suggestion_open_range = use `..` instead
211+
.note = inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
212+
202213
parse_struct_literal_not_allowed_here = struct literals are not allowed here
203214
.suggestion = surround the struct literal with parentheses
204215

compiler/rustc_parse/src/errors.rs

+42
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,48 @@ pub(crate) struct MatchArmBodyWithoutBraces {
649649
pub sub: MatchArmBodyWithoutBracesSugg,
650650
}
651651

652+
#[derive(Diagnostic)]
653+
#[diag(parse_inclusive_range_extra_equals)]
654+
#[note]
655+
pub(crate) struct InclusiveRangeExtraEquals {
656+
#[primary_span]
657+
#[suggestion(
658+
suggestion_remove_eq,
659+
style = "short",
660+
code = "..=",
661+
applicability = "maybe-incorrect"
662+
)]
663+
pub span: Span,
664+
}
665+
666+
#[derive(Diagnostic)]
667+
#[diag(parse_inclusive_range_match_arrow)]
668+
pub(crate) struct InclusiveRangeMatchArrow {
669+
#[primary_span]
670+
pub span: Span,
671+
#[suggestion(
672+
suggestion_add_space,
673+
style = "verbose",
674+
code = " ",
675+
applicability = "machine-applicable"
676+
)]
677+
pub after_pat: Span,
678+
}
679+
680+
#[derive(Diagnostic)]
681+
#[diag(parse_inclusive_range_no_end, code = "E0586")]
682+
#[note]
683+
pub(crate) struct InclusiveRangeNoEnd {
684+
#[primary_span]
685+
#[suggestion(
686+
suggestion_open_range,
687+
code = "..",
688+
applicability = "machine-applicable",
689+
style = "short"
690+
)]
691+
pub span: Span,
692+
}
693+
652694
#[derive(Subdiagnostic)]
653695
pub(crate) enum MatchArmBodyWithoutBracesSugg {
654696
#[multipart_suggestion(suggestion_add_braces, applicability = "machine-applicable")]

compiler/rustc_parse/src/parser/pat.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::{ForceCollect, Parser, PathStyle, TrailingToken};
2-
use crate::errors::RemoveLet;
2+
use crate::errors::{
3+
InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, RemoveLet,
4+
};
35
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
46
use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor};
57
use rustc_ast::ptr::P;
@@ -9,7 +11,7 @@ use rustc_ast::{
911
PatField, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
1012
};
1113
use rustc_ast_pretty::pprust;
12-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
14+
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
1315
use rustc_session::errors::ExprParenthesesNeeded;
1416
use rustc_span::source_map::{respan, Span, Spanned};
1517
use rustc_span::symbol::{kw, sym, Ident};
@@ -782,29 +784,16 @@ impl<'a> Parser<'a> {
782784
}
783785

784786
fn error_inclusive_range_with_extra_equals(&self, span: Span) {
785-
self.struct_span_err(span, "unexpected `=` after inclusive range")
786-
.span_suggestion_short(span, "use `..=` instead", "..=", Applicability::MaybeIncorrect)
787-
.note("inclusive ranges end with a single equals sign (`..=`)")
788-
.emit();
787+
self.sess.emit_err(InclusiveRangeExtraEquals { span });
789788
}
790789

791790
fn error_inclusive_range_match_arrow(&self, span: Span) {
792-
let without_eq = span.with_hi(span.hi() - rustc_span::BytePos(1));
793-
self.struct_span_err(span, "unexpected `=>` after open range")
794-
.span_suggestion_verbose(
795-
without_eq.shrink_to_hi(),
796-
"add a space between the pattern and `=>`",
797-
" ",
798-
Applicability::MachineApplicable,
799-
)
800-
.emit();
791+
let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi();
792+
self.sess.emit_err(InclusiveRangeMatchArrow { span, after_pat });
801793
}
802794

803795
fn error_inclusive_range_with_no_end(&self, span: Span) {
804-
struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end")
805-
.span_suggestion_short(span, "use `..` instead", "..", Applicability::MachineApplicable)
806-
.note("inclusive ranges must be bounded at the end (`..=b` or `a..=b`)")
807-
.emit();
796+
self.sess.emit_err(InclusiveRangeNoEnd { span });
808797
}
809798

810799
/// Parse a range-to pattern, `..X` or `..=X` where `X` remains to be parsed.

0 commit comments

Comments
 (0)