Skip to content

Commit f3a3290

Browse files
committed
Account for maybe_whole_expr in range patterns.
1 parent 04b88a9 commit f3a3290

File tree

5 files changed

+140
-1
lines changed

5 files changed

+140
-1
lines changed

src/libsyntax/parse/parser.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3741,6 +3741,7 @@ impl<'a> Parser<'a> {
37413741
self.token.is_path_start() // e.g. `MY_CONST`;
37423742
|| self.token == token::Dot // e.g. `.5` for recovery;
37433743
|| self.token.can_begin_literal_or_bool() // e.g. `42`.
3744+
|| self.token.is_whole_expr()
37443745
}
37453746

37463747
// Helper function to decide whether to parse as ident binding

src/libsyntax/parse/token.rs

+13
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ impl Token {
476476
false
477477
}
478478

479+
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
480+
/// That is, is this a pre-parsed expression dropped into the token stream
481+
/// (which happens while parsing the result ofmacro expansion)?
482+
crate fn is_whole_expr(&self) -> bool {
483+
if let Interpolated(ref nt) = self.kind {
484+
if let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = **nt {
485+
return true;
486+
}
487+
}
488+
489+
false
490+
}
491+
479492
/// Returns `true` if the token is either the `mut` or `const` keyword.
480493
crate fn is_mutability(&self) -> bool {
481494
self.is_keyword(kw::Mut) ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
#![feature(exclusive_range_pattern)]
4+
5+
#![allow(ellipsis_inclusive_range_patterns)]
6+
7+
fn main() {
8+
macro_rules! mac_expr {
9+
($e:expr) => {
10+
if let 2...$e = 3 {}
11+
if let 2..=$e = 3 {}
12+
if let 2..$e = 3 {}
13+
}
14+
}
15+
mac_expr!(4);
16+
}

src/test/ui/parser/recover-range-pats.rs

+28
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,31 @@ fn inclusive2_to() {
121121
//~| ERROR `...` range patterns are deprecated
122122
//~| ERROR mismatched types
123123
}
124+
125+
fn with_macro_expr_var() {
126+
macro_rules! mac2 {
127+
($e1:expr, $e2:expr) => {
128+
let $e1..$e2;
129+
let $e1...$e2;
130+
//~^ ERROR `...` range patterns are deprecated
131+
let $e1..=$e2;
132+
}
133+
}
134+
135+
mac2!(0, 1);
136+
137+
macro_rules! mac {
138+
($e:expr) => {
139+
let ..$e; //~ ERROR `..X` range patterns are not supported
140+
let ...$e; //~ ERROR `...X` range patterns are not supported
141+
//~^ ERROR `...` range patterns are deprecated
142+
let ..=$e; //~ ERROR `..=X` range patterns are not supported
143+
let $e..; //~ ERROR `X..` range patterns are not supported
144+
let $e...; //~ ERROR `X...` range patterns are not supported
145+
//~^ ERROR `...` range patterns are deprecated
146+
let $e..=; //~ ERROR `X..=` range patterns are not supported
147+
}
148+
}
149+
150+
mac!(0);
151+
}

src/test/ui/parser/recover-range-pats.stderr

+82-1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,60 @@ error: `...X` range patterns are not supported
214214
LL | if let ....3 = 0 {}
215215
| ^^^^^ help: try using the minimum value for the type: `MIN...0.3`
216216

217+
error: `..X` range patterns are not supported
218+
--> $DIR/recover-range-pats.rs:139:17
219+
|
220+
LL | let ..$e;
221+
| ^^ help: try using the minimum value for the type: `MIN..0`
222+
...
223+
LL | mac!(0);
224+
| -------- in this macro invocation
225+
226+
error: `...X` range patterns are not supported
227+
--> $DIR/recover-range-pats.rs:140:17
228+
|
229+
LL | let ...$e;
230+
| ^^^ help: try using the minimum value for the type: `MIN...0`
231+
...
232+
LL | mac!(0);
233+
| -------- in this macro invocation
234+
235+
error: `..=X` range patterns are not supported
236+
--> $DIR/recover-range-pats.rs:142:17
237+
|
238+
LL | let ..=$e;
239+
| ^^^ help: try using the minimum value for the type: `MIN..=0`
240+
...
241+
LL | mac!(0);
242+
| -------- in this macro invocation
243+
244+
error: `X..` range patterns are not supported
245+
--> $DIR/recover-range-pats.rs:143:19
246+
|
247+
LL | let $e..;
248+
| ^^ help: try using the maximum value for the type: `0..MAX`
249+
...
250+
LL | mac!(0);
251+
| -------- in this macro invocation
252+
253+
error: `X...` range patterns are not supported
254+
--> $DIR/recover-range-pats.rs:144:19
255+
|
256+
LL | let $e...;
257+
| ^^^ help: try using the maximum value for the type: `0...MAX`
258+
...
259+
LL | mac!(0);
260+
| -------- in this macro invocation
261+
262+
error: `X..=` range patterns are not supported
263+
--> $DIR/recover-range-pats.rs:146:19
264+
|
265+
LL | let $e..=;
266+
| ^^^ help: try using the maximum value for the type: `0..=MAX`
267+
...
268+
LL | mac!(0);
269+
| -------- in this macro invocation
270+
217271
error: `...` range patterns are deprecated
218272
--> $DIR/recover-range-pats.rs:41:13
219273
|
@@ -316,6 +370,33 @@ error: `...` range patterns are deprecated
316370
LL | if let ....3 = 0 {}
317371
| ^^^ help: use `..=` for an inclusive range
318372

373+
error: `...` range patterns are deprecated
374+
--> $DIR/recover-range-pats.rs:129:20
375+
|
376+
LL | let $e1...$e2;
377+
| ^^^ help: use `..=` for an inclusive range
378+
...
379+
LL | mac2!(0, 1);
380+
| ------------ in this macro invocation
381+
382+
error: `...` range patterns are deprecated
383+
--> $DIR/recover-range-pats.rs:140:17
384+
|
385+
LL | let ...$e;
386+
| ^^^ help: use `..=` for an inclusive range
387+
...
388+
LL | mac!(0);
389+
| -------- in this macro invocation
390+
391+
error: `...` range patterns are deprecated
392+
--> $DIR/recover-range-pats.rs:144:19
393+
|
394+
LL | let $e...;
395+
| ^^^ help: use `..=` for an inclusive range
396+
...
397+
LL | mac!(0);
398+
| -------- in this macro invocation
399+
319400
error[E0029]: only char and numeric types are allowed in range patterns
320401
--> $DIR/recover-range-pats.rs:19:12
321402
|
@@ -532,7 +613,7 @@ LL | if let ....3 = 0 {}
532613
= note: expected type `{integer}`
533614
found type `{float}`
534615

535-
error: aborting due to 76 previous errors
616+
error: aborting due to 85 previous errors
536617

537618
Some errors have detailed explanations: E0029, E0308.
538619
For more information about an error, try `rustc --explain E0029`.

0 commit comments

Comments
 (0)