diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-01-11 12:36:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-11 12:36:15 +0100 |
| commit | 4eee796679bd13e0935c16f41a5e6e2ab0d5c018 (patch) | |
| tree | fc35ad7fae01337e6873d43c00087dd53c3992db /src/librustc_parse/parser | |
| parent | 50d76d647191bdff875e854ed2ee2282ab274a09 (diff) | |
| parent | 883932c6baf7acd28ab712b80ddeda960f6e37da (diff) | |
Rollup merge of #68120 - Centril:ban-range-to-dotdotdot, r=oli-obk
Ban `...X` pats, harden tests, and improve diagnostics Follow up to https://github.com/rust-lang/rust/pull/67258#issuecomment-565656155 and https://github.com/rust-lang/rust/pull/67258#discussion_r357879932. r? @cramertj @oli-obk
Diffstat (limited to 'src/librustc_parse/parser')
| -rw-r--r-- | src/librustc_parse/parser/expr.rs | 2 | ||||
| -rw-r--r-- | src/librustc_parse/parser/pat.rs | 26 |
2 files changed, 24 insertions, 4 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index b0c78ad7e4b..dd2b6ab9971 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1966,7 +1966,7 @@ impl<'a> Parser<'a> { limits: RangeLimits, ) -> PResult<'a, ExprKind> { if end.is_none() && limits == RangeLimits::Closed { - self.error_inclusive_range_with_no_end(self.token.span); + self.error_inclusive_range_with_no_end(self.prev_span); Ok(ExprKind::Err) } else { Ok(ExprKind::Range(start, end, limits)) diff --git a/src/librustc_parse/parser/pat.rs b/src/librustc_parse/parser/pat.rs index 50756ddec9f..0c2cfc20daf 100644 --- a/src/librustc_parse/parser/pat.rs +++ b/src/librustc_parse/parser/pat.rs @@ -661,14 +661,34 @@ impl<'a> Parser<'a> { pub(super) fn error_inclusive_range_with_no_end(&self, span: Span) { use rustc_error_codes::E0586; struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end") - .help("inclusive ranges must be bounded at the end (`..=b` or `a..=b`)") + .span_suggestion_short( + span, + "use `..` instead", + "..".to_string(), + Applicability::MachineApplicable, + ) + .note("inclusive ranges must be bounded at the end (`..=b` or `a..=b`)") .emit(); } - /// Parse a range-to pattern, e.g. `..X` and `..=X` where `X` remains to be parsed. - fn parse_pat_range_to(&mut self, re: Spanned<RangeEnd>) -> PResult<'a, PatKind> { + /// Parse a range-to pattern, `..X` or `..=X` where `X` remains to be parsed. + /// + /// The form `...X` is prohibited to reduce confusion with the potential + /// expression syntax `...expr` for splatting in expressions. + fn parse_pat_range_to(&mut self, mut re: Spanned<RangeEnd>) -> PResult<'a, PatKind> { let end = self.parse_pat_range_end()?; self.sess.gated_spans.gate(sym::half_open_range_patterns, re.span.to(self.prev_span)); + if let RangeEnd::Included(ref mut syn @ RangeSyntax::DotDotDot) = &mut re.node { + *syn = RangeSyntax::DotDotEq; + self.struct_span_err(re.span, "range-to patterns with `...` are not allowed") + .span_suggestion_short( + re.span, + "use `..=` instead", + "..=".to_string(), + Applicability::MachineApplicable, + ) + .emit(); + } Ok(PatKind::Range(None, Some(end), re)) } |
