Skip to content

Commit 66470d3

Browse files
committed
recover #[attr] if expr {}
1 parent c9e1f13 commit 66470d3

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

src/librustc_parse/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![feature(bool_to_option)]
44
#![feature(crate_visibility_modifier)]
5+
#![feature(slice_patterns)]
56

67
use syntax::ast;
78
use syntax::print::pprust;

src/librustc_parse/parser/expr.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,19 +668,20 @@ impl<'a> Parser<'a> {
668668
expr.map(|mut expr| {
669669
attrs.extend::<Vec<_>>(expr.attrs.into());
670670
expr.attrs = attrs;
671-
match expr.kind {
672-
ExprKind::If(..) if !expr.attrs.is_empty() => {
673-
// Just point to the first attribute in there...
674-
let span = expr.attrs[0].span;
675-
self.span_err(span, "attributes are not yet allowed on `if` expressions");
676-
}
677-
_ => {}
678-
}
671+
self.error_attr_on_if_expr(&expr);
679672
expr
680673
})
681674
)
682675
}
683676

677+
fn error_attr_on_if_expr(&self, expr: &Expr) {
678+
if let (ExprKind::If(..), [a0, ..]) = (&expr.kind, &*expr.attrs) {
679+
// Just point to the first attribute in there...
680+
self.struct_span_err(a0.span, "attributes are not yet allowed on `if` expressions")
681+
.emit();
682+
}
683+
}
684+
684685
fn parse_dot_or_call_expr_with_(&mut self, e0: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
685686
let mut e = e0;
686687
let mut hi;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
#[attr] if true {};
3+
//~^ ERROR cannot find attribute
4+
//~| ERROR attributes are not yet allowed on `if` expressions
5+
#[attr] if true {};
6+
//~^ ERROR cannot find attribute
7+
//~| ERROR attributes are not yet allowed on `if` expressions
8+
let _recovery_witness: () = 0; //~ ERROR mismatched types
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: attributes are not yet allowed on `if` expressions
2+
--> $DIR/recovery-attr-on-if.rs:2:5
3+
|
4+
LL | #[attr] if true {};
5+
| ^^^^^^^
6+
7+
error: attributes are not yet allowed on `if` expressions
8+
--> $DIR/recovery-attr-on-if.rs:5:5
9+
|
10+
LL | #[attr] if true {};
11+
| ^^^^^^^
12+
13+
error: cannot find attribute `attr` in this scope
14+
--> $DIR/recovery-attr-on-if.rs:5:7
15+
|
16+
LL | #[attr] if true {};
17+
| ^^^^
18+
19+
error: cannot find attribute `attr` in this scope
20+
--> $DIR/recovery-attr-on-if.rs:2:7
21+
|
22+
LL | #[attr] if true {};
23+
| ^^^^
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/recovery-attr-on-if.rs:8:33
27+
|
28+
LL | let _recovery_witness: () = 0;
29+
| -- ^ expected `()`, found integer
30+
| |
31+
| expected due to this
32+
33+
error: aborting due to 5 previous errors
34+
35+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)