Skip to content

Commit 6399d16

Browse files
committed
Disallow &a..=b and box a..=b in pattern.
They are disallowed because they have different precedence than expressions. I assume parenthesis in pattern will be soon stabilized and thus write that as suggestion directly.
1 parent a4d8033 commit 6399d16

File tree

5 files changed

+131
-2
lines changed

5 files changed

+131
-2
lines changed

src/librustc/hir/print.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1810,15 +1810,35 @@ impl<'a> State<'a> {
18101810
self.pclose()?;
18111811
}
18121812
PatKind::Box(ref inner) => {
1813+
let is_range_inner = match inner.node {
1814+
PatKind::Range(..) => true,
1815+
_ => false,
1816+
};
18131817
self.s.word("box ")?;
1818+
if is_range_inner {
1819+
self.popen()?;
1820+
}
18141821
self.print_pat(&inner)?;
1822+
if is_range_inner {
1823+
self.pclose()?;
1824+
}
18151825
}
18161826
PatKind::Ref(ref inner, mutbl) => {
1827+
let is_range_inner = match inner.node {
1828+
PatKind::Range(..) => true,
1829+
_ => false,
1830+
};
18171831
self.s.word("&")?;
18181832
if mutbl == hir::MutMutable {
18191833
self.s.word("mut ")?;
18201834
}
1835+
if is_range_inner {
1836+
self.popen()?;
1837+
}
18211838
self.print_pat(&inner)?;
1839+
if is_range_inner {
1840+
self.pclose()?;
1841+
}
18221842
}
18231843
PatKind::Lit(ref e) => self.print_expr(&e)?,
18241844
PatKind::Range(ref begin, ref end, ref end_kind) => {

src/libsyntax/parse/parser.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -3804,6 +3804,12 @@ impl<'a> Parser<'a> {
38043804

38053805
/// Parse a pattern.
38063806
pub fn parse_pat(&mut self) -> PResult<'a, P<Pat>> {
3807+
self.parse_pat_with_range_pat(true)
3808+
}
3809+
3810+
/// Parse a pattern, with a setting whether modern range patterns e.g. `a..=b`, `a..b` are
3811+
/// allowed.
3812+
fn parse_pat_with_range_pat(&mut self, allow_range_pat: bool) -> PResult<'a, P<Pat>> {
38073813
maybe_whole!(self, NtPat, |x| x);
38083814

38093815
let lo = self.span;
@@ -3824,7 +3830,7 @@ impl<'a> Parser<'a> {
38243830
err.span_label(self.span, "unexpected lifetime");
38253831
return Err(err);
38263832
}
3827-
let subpat = self.parse_pat()?;
3833+
let subpat = self.parse_pat_with_range_pat(false)?;
38283834
pat = PatKind::Ref(subpat, mutbl);
38293835
}
38303836
token::OpenDelim(token::Paren) => {
@@ -3863,7 +3869,7 @@ impl<'a> Parser<'a> {
38633869
pat = self.parse_pat_ident(BindingMode::ByRef(mutbl))?;
38643870
} else if self.eat_keyword(keywords::Box) {
38653871
// Parse box pat
3866-
let subpat = self.parse_pat()?;
3872+
let subpat = self.parse_pat_with_range_pat(false)?;
38673873
pat = PatKind::Box(subpat);
38683874
} else if self.token.is_ident() && !self.token.is_reserved_ident() &&
38693875
self.parse_as_ident() {
@@ -3968,6 +3974,25 @@ impl<'a> Parser<'a> {
39683974
let pat = Pat { node: pat, span: lo.to(self.prev_span), id: ast::DUMMY_NODE_ID };
39693975
let pat = self.maybe_recover_from_bad_qpath(pat, true)?;
39703976

3977+
if !allow_range_pat {
3978+
match pat.node {
3979+
PatKind::Range(_, _, RangeEnd::Included(RangeSyntax::DotDotDot)) => {}
3980+
PatKind::Range(..) => {
3981+
let mut err = self.struct_span_err(
3982+
pat.span,
3983+
"the range pattern here has ambiguous interpretation",
3984+
);
3985+
err.span_suggestion(
3986+
pat.span,
3987+
"add parentheses to clarify the precedence",
3988+
format!("({})", pprust::pat_to_string(&pat)),
3989+
);
3990+
return Err(err);
3991+
}
3992+
_ => {}
3993+
}
3994+
}
3995+
39713996
Ok(P(pat))
39723997
}
39733998

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(box_patterns, pattern_parentheses)]
12+
13+
const VALUE: usize = 21;
14+
15+
pub fn main() {
16+
match &18 {
17+
&(18..=18) => {}
18+
_ => { unreachable!(); }
19+
}
20+
match &21 {
21+
&(VALUE..=VALUE) => {}
22+
_ => { unreachable!(); }
23+
}
24+
match Box::new(18) {
25+
box (18..=18) => {}
26+
_ => { unreachable!(); }
27+
}
28+
match Box::new(21) {
29+
box (VALUE..=VALUE) => {}
30+
_ => { unreachable!(); }
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// In expression, `&a..=b` is treated as `(&a)..=(b)` and `box a..=b` is
12+
// `(box a)..=(b)`. In a pattern, however, `&a..=b` means `&(a..=b)`. This may
13+
// lead to confusion.
14+
//
15+
// We are going to disallow `&a..=b` and `box a..=b` in a pattern. However, the
16+
// older ... syntax is still allowed as a stability guarantee.
17+
18+
#![feature(box_patterns)]
19+
20+
pub fn main() {
21+
match &12 {
22+
&0...9 => {}
23+
&10..=15 => {}
24+
//~^ ERROR the range pattern here has ambiguous interpretation
25+
//~^^ HELP add parentheses to clarify the precedence
26+
&(16..=20) => {}
27+
_ => {}
28+
}
29+
30+
match Box::new(12) {
31+
box 0...9 => {}
32+
box 10..=15 => {}
33+
//~^ ERROR the range pattern here has ambiguous interpretation
34+
//~^^ HELP add parentheses to clarify the precedence
35+
box (16..=20) => {}
36+
_ => {}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the range pattern here has ambiguous interpretation
2+
--> $DIR/range-inclusive-pattern-precedence.rs:23:10
3+
|
4+
LL | &10..=15 => {}
5+
| ^^^^^^^ help: add parentheses to clarify the precedence: `(10 ..=15)`
6+
7+
error: the range pattern here has ambiguous interpretation
8+
--> $DIR/range-inclusive-pattern-precedence.rs:32:13
9+
|
10+
LL | box 10..=15 => {}
11+
| ^^^^^^^ help: add parentheses to clarify the precedence: `(10 ..=15)`
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)