@@ -142,14 +142,17 @@ pub struct Parser<'a> {
142
142
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
143
143
subparser_name : Option < & ' static str > ,
144
144
capture_state : CaptureState ,
145
-
146
145
/// This allows us to recover when the user forget to add braces around
147
146
/// 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 ,
153
156
}
154
157
155
158
/// Indicates a range of tokens that should be replaced by
@@ -783,19 +786,15 @@ impl<'a> Parser<'a> {
783
786
let token_str = pprust:: token_kind_to_string ( t) ;
784
787
785
788
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 => {
789
790
// Finding a semicolon instead of a comma
790
791
// after a closure body indicates that the
791
792
// closure body may be a block but the user
792
793
// forgot to put braces around its
793
794
// statements.
794
795
795
796
self . recover_missing_braces_around_closure_body (
796
- closure_span,
797
- right_pipe_span,
798
- expr_span,
797
+ closure_spans,
799
798
expect_err,
800
799
) ?;
801
800
@@ -876,9 +875,7 @@ impl<'a> Parser<'a> {
876
875
877
876
fn recover_missing_braces_around_closure_body (
878
877
& mut self ,
879
- closure_span : Span ,
880
- right_pipe_span : Span ,
881
- expr_span : Span ,
878
+ closure_spans : ClosureSpans ,
882
879
mut expect_err : DiagnosticBuilder < ' _ > ,
883
880
) -> PResult < ' a , ( ) > {
884
881
let initial_semicolon = self . token . span ;
@@ -891,7 +888,7 @@ impl<'a> Parser<'a> {
891
888
"closure bodies that contain statements must be surrounded by braces" ,
892
889
) ;
893
890
894
- let preceding_pipe_span = right_pipe_span ;
891
+ let preceding_pipe_span = closure_spans . closing_pipe ;
895
892
let following_token_span = self . token . span ;
896
893
897
894
let mut first_note = MultiSpan :: from ( vec ! [ initial_semicolon] ) ;
@@ -900,13 +897,16 @@ impl<'a> Parser<'a> {
900
897
"this `;` turns the preceding expression into a statement" . to_string ( ) ,
901
898
) ;
902
899
first_note. push_span_label (
903
- expr_span ,
900
+ closure_spans . body ,
904
901
"this expression is a statement because of the trailing semicolon" . to_string ( ) ,
905
902
) ;
906
903
expect_err. span_note ( first_note, "statement found outside of a block" ) ;
907
904
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
+ ) ;
910
910
second_note. push_span_label (
911
911
following_token_span,
912
912
"...but likely you meant the closure to end here" . to_string ( ) ,
0 commit comments