Skip to content

Syntactically permit attributes on 'if' expressions #68658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
10 changes: 10 additions & 0 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ impl<'a> AstValidator<'a> {
}
}
}

fn check_attr_on_if_expr(&self, attrs: &[Attribute]) {
if let [a0, ..] = attrs {
// Just point to the first attribute in there...
self.err_handler()
.struct_span_err(a0.span, "attributes are not yet allowed on `if` expressions")
.emit();
}
}
}

enum GenericPosition {
Expand Down Expand Up @@ -515,6 +524,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
)
.emit();
}
ExprKind::If(..) => self.check_attr_on_if_expr(&*expr.attrs),
_ => {}
}

Expand Down
9 changes: 0 additions & 9 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,20 +676,11 @@ impl<'a> Parser<'a> {
expr.map(|mut expr| {
attrs.extend::<Vec<_>>(expr.attrs.into());
expr.attrs = attrs;
self.error_attr_on_if_expr(&expr);
expr
})
})
}

fn error_attr_on_if_expr(&self, expr: &Expr) {
if let (ExprKind::If(..), [a0, ..]) = (&expr.kind, &*expr.attrs) {
// Just point to the first attribute in there...
self.struct_span_err(a0.span, "attributes are not yet allowed on `if` expressions")
.emit();
}
}

fn parse_dot_or_call_expr_with_(&mut self, mut e: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
loop {
if self.eat(&token::Question) {
Expand Down
28 changes: 28 additions & 0 deletions src/test/pretty/if-attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// pp-exact

#[cfg(FALSE)]
fn simple_attr() {

#[attr]
if true { }

#[allow_warnings]
if true { }
}

#[cfg(FALSE)]
fn if_else_chain() {

#[first_attr]
if true { } else if false { } else { }
}

#[cfg(FALSE)]
fn if_let() {

#[attr]
if let Some(_) = Some(true) { }
}


fn main() { }
10 changes: 2 additions & 8 deletions src/test/ui/parser/attr-stmt-expr-attr-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ fn main() {}
//~^ ERROR an inner attribute is not permitted in this context
#[cfg(FALSE)] fn e() { let _ = #[attr] &mut #![attr] 0; }
//~^ ERROR an inner attribute is not permitted in this context
#[cfg(FALSE)] fn e() { let _ = #[attr] if 0 {}; }
//~^ ERROR attributes are not yet allowed on `if` expressions
#[cfg(FALSE)] fn e() { let _ = if 0 #[attr] {}; }
//~^ ERROR expected `{`, found `#`
#[cfg(FALSE)] fn e() { let _ = if 0 {#![attr]}; }
Expand All @@ -51,14 +49,11 @@ fn main() {}
#[cfg(FALSE)] fn e() { let _ = if 0 {} else {#![attr]}; }
//~^ ERROR an inner attribute is not permitted in this context
#[cfg(FALSE)] fn e() { let _ = if 0 {} else #[attr] if 0 {}; }
//~^ ERROR attributes are not yet allowed on `if` expressions
//~| ERROR expected `{`, found `#`
//~^ ERROR expected `{`, found `#`
#[cfg(FALSE)] fn e() { let _ = if 0 {} else if 0 #[attr] {}; }
//~^ ERROR expected `{`, found `#`
#[cfg(FALSE)] fn e() { let _ = if 0 {} else if 0 {#![attr]}; }
//~^ ERROR an inner attribute is not permitted in this context
#[cfg(FALSE)] fn e() { let _ = #[attr] if let _ = 0 {}; }
//~^ ERROR attributes are not yet allowed on `if` expressions
#[cfg(FALSE)] fn e() { let _ = if let _ = 0 #[attr] {}; }
//~^ ERROR expected `{`, found `#`
#[cfg(FALSE)] fn e() { let _ = if let _ = 0 {#![attr]}; }
Expand All @@ -70,8 +65,7 @@ fn main() {}
#[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else {#![attr]}; }
//~^ ERROR an inner attribute is not permitted in this context
#[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else #[attr] if let _ = 0 {}; }
//~^ ERROR attributes are not yet allowed on `if` expressions
//~| ERROR expected `{`, found `#`
//~^ ERROR expected `{`, found `#`
#[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else if let _ = 0 #[attr] {}; }
//~^ ERROR expected `{`, found `#`
#[cfg(FALSE)] fn e() { let _ = if let _ = 0 {} else if let _ = 0 {#![attr]}; }
Expand Down
Loading