Skip to content

Commit 4107396

Browse files
author
Sasha Pourcelot
committed
Group closure-related spans in a structure
1 parent 41ac2c6 commit 4107396

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use super::pat::{RecoverColon, RecoverComma, PARAM_EXPECTED};
22
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
3-
use super::{AttrWrapper, BlockMode, ForceCollect, Parser, PathStyle, Restrictions, TokenType};
3+
use super::{
4+
AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Restrictions, TokenType,
5+
};
46
use super::{SemiColonMode, SeqSep, TokenExpectType, TrailingToken};
57
use crate::maybe_recover_from_interpolated_ty_qpath;
68

@@ -1753,7 +1755,9 @@ impl<'a> Parser<'a> {
17531755
);
17541756

17551757
// Disable recovery for closure body
1756-
self.last_closure_body = Some((clo.span, decl_hi, body_span));
1758+
let spans =
1759+
ClosureSpans { whole_closure: clo.span, closing_pipe: decl_hi, body: body_span };
1760+
self.last_closure_body = Some(spans);
17571761

17581762
Ok(clo)
17591763
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,17 @@ pub struct Parser<'a> {
142142
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
143143
subparser_name: Option<&'static str>,
144144
capture_state: CaptureState,
145-
146145
/// This allows us to recover when the user forget to add braces around
147146
/// multiple statements in the closure body.
148-
pub last_closure_body: Option<(
149-
Span, /* The whole body. */
150-
Span, /* The closing `|` of the closure declarator. */
151-
Span, /* What we parsed as closure body. */
152-
)>,
147+
pub last_closure_body: Option<ClosureSpans>,
148+
}
149+
150+
/// Stores span informations about a closure.
151+
#[derive(Clone)]
152+
pub struct ClosureSpans {
153+
pub whole_closure: Span,
154+
pub closing_pipe: Span,
155+
pub body: Span,
153156
}
154157

155158
/// Indicates a range of tokens that should be replaced by
@@ -783,19 +786,15 @@ impl<'a> Parser<'a> {
783786
let token_str = pprust::token_kind_to_string(t);
784787

785788
match self.last_closure_body.take() {
786-
Some((closure_span, right_pipe_span, expr_span))
787-
if self.token.kind == TokenKind::Semi =>
788-
{
789+
Some(closure_spans) if self.token.kind == TokenKind::Semi => {
789790
// Finding a semicolon instead of a comma
790791
// after a closure body indicates that the
791792
// closure body may be a block but the user
792793
// forgot to put braces around its
793794
// statements.
794795

795796
self.recover_missing_braces_around_closure_body(
796-
closure_span,
797-
right_pipe_span,
798-
expr_span,
797+
closure_spans,
799798
expect_err,
800799
)?;
801800

@@ -876,9 +875,7 @@ impl<'a> Parser<'a> {
876875

877876
fn recover_missing_braces_around_closure_body(
878877
&mut self,
879-
closure_span: Span,
880-
right_pipe_span: Span,
881-
expr_span: Span,
878+
closure_spans: ClosureSpans,
882879
mut expect_err: DiagnosticBuilder<'_>,
883880
) -> PResult<'a, ()> {
884881
let initial_semicolon = self.token.span;
@@ -891,7 +888,7 @@ impl<'a> Parser<'a> {
891888
"closure bodies that contain statements must be surrounded by braces",
892889
);
893890

894-
let preceding_pipe_span = right_pipe_span;
891+
let preceding_pipe_span = closure_spans.closing_pipe;
895892
let following_token_span = self.token.span;
896893

897894
let mut first_note = MultiSpan::from(vec![initial_semicolon]);
@@ -900,13 +897,16 @@ impl<'a> Parser<'a> {
900897
"this `;` turns the preceding expression into a statement".to_string(),
901898
);
902899
first_note.push_span_label(
903-
expr_span,
900+
closure_spans.body,
904901
"this expression is a statement because of the trailing semicolon".to_string(),
905902
);
906903
expect_err.span_note(first_note, "statement found outside of a block");
907904

908-
let mut second_note = MultiSpan::from(vec![closure_span]);
909-
second_note.push_span_label(closure_span, "this is the parsed closure...".to_string());
905+
let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]);
906+
second_note.push_span_label(
907+
closure_spans.whole_closure,
908+
"this is the parsed closure...".to_string(),
909+
);
910910
second_note.push_span_label(
911911
following_token_span,
912912
"...but likely you meant the closure to end here".to_string(),

0 commit comments

Comments
 (0)