@@ -57,40 +57,45 @@ impl<'a> Parser<'a> {
57
57
/// to subpatterns within such).
58
58
pub ( super ) fn parse_top_level_pat ( & mut self ) -> PResult < ' a , P < Pat > > {
59
59
let pat = self . parse_pat ( None ) ?;
60
- if self . token == token:: Comma {
61
- // An unexpected comma after a top-level pattern is a clue that the
62
- // user (perhaps more accustomed to some other language) forgot the
63
- // parentheses in what should have been a tuple pattern; return a
64
- // suggestion-enhanced error here rather than choking on the comma
65
- // later.
66
- let comma_span = self . token . span ;
67
- self . bump ( ) ;
68
- if let Err ( mut err) = self . skip_pat_list ( ) {
69
- // We didn't expect this to work anyway; we just wanted
70
- // to advance to the end of the comma-sequence so we know
71
- // the span to suggest parenthesizing
72
- err. cancel ( ) ;
73
- }
74
- let seq_span = pat. span . to ( self . prev_span ) ;
75
- let mut err = self . struct_span_err ( comma_span, "unexpected `,` in pattern" ) ;
76
- if let Ok ( seq_snippet) = self . span_to_snippet ( seq_span) {
77
- err. span_suggestion (
78
- seq_span,
79
- "try adding parentheses to match on a tuple.." ,
80
- format ! ( "({})" , seq_snippet) ,
81
- Applicability :: MachineApplicable
82
- ) . span_suggestion (
83
- seq_span,
84
- "..or a vertical bar to match on multiple alternatives" ,
85
- format ! ( "{}" , seq_snippet. replace( "," , " |" ) ) ,
86
- Applicability :: MachineApplicable
87
- ) ;
88
- }
89
- return Err ( err) ;
90
- }
60
+ self . maybe_recover_unexpected_comma ( pat. span ) ?;
91
61
Ok ( pat)
92
62
}
93
63
64
+ fn maybe_recover_unexpected_comma ( & mut self , lo : Span ) -> PResult < ' a , ( ) > {
65
+ if self . token != token:: Comma {
66
+ return Ok ( ( ) ) ;
67
+ }
68
+
69
+ // An unexpected comma after a top-level pattern is a clue that the
70
+ // user (perhaps more accustomed to some other language) forgot the
71
+ // parentheses in what should have been a tuple pattern; return a
72
+ // suggestion-enhanced error here rather than choking on the comma later.
73
+ let comma_span = self . token . span ;
74
+ self . bump ( ) ;
75
+ if let Err ( mut err) = self . skip_pat_list ( ) {
76
+ // We didn't expect this to work anyway; we just wanted to advance to the
77
+ // end of the comma-sequence so we know the span to suggest parenthesizing.
78
+ err. cancel ( ) ;
79
+ }
80
+ let seq_span = lo. to ( self . prev_span ) ;
81
+ let mut err = self . struct_span_err ( comma_span, "unexpected `,` in pattern" ) ;
82
+ if let Ok ( seq_snippet) = self . span_to_snippet ( seq_span) {
83
+ err. span_suggestion (
84
+ seq_span,
85
+ "try adding parentheses to match on a tuple.." ,
86
+ format ! ( "({})" , seq_snippet) ,
87
+ Applicability :: MachineApplicable
88
+ )
89
+ . span_suggestion (
90
+ seq_span,
91
+ "..or a vertical bar to match on multiple alternatives" ,
92
+ format ! ( "{}" , seq_snippet. replace( "," , " |" ) ) ,
93
+ Applicability :: MachineApplicable
94
+ ) ;
95
+ }
96
+ Err ( err)
97
+ }
98
+
94
99
/// Parse and throw away a parentesized comma separated
95
100
/// sequence of patterns until `)` is reached.
96
101
fn skip_pat_list ( & mut self ) -> PResult < ' a , ( ) > {
0 commit comments