-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
syntax: enable attributes and cfg on struct fields #38814
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,6 +427,7 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) { | |
PatKind::Struct(ref path, ref fields, _) => { | ||
visitor.visit_path(path, pattern.id); | ||
for field in fields { | ||
walk_list!(visitor, visit_attribute, field.node.attrs.iter()); | ||
visitor.visit_ident(field.span, field.node.ident); | ||
visitor.visit_pat(&field.node.pat) | ||
} | ||
|
@@ -659,6 +660,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { | |
ExprKind::Struct(ref path, ref fields, ref optional_base) => { | ||
visitor.visit_path(path, expression.id); | ||
for field in fields { | ||
walk_list!(visitor, visit_attribute, field.attrs.iter()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It cannot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right -- |
||
visitor.visit_ident(field.ident.span, field.ident.node); | ||
visitor.visit_expr(&field.expr) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
struct Foo { | ||
present: (), | ||
} | ||
|
||
fn main() { | ||
let foo = Foo { #[cfg(all())] present: () }; | ||
//~^ ERROR attributes on struct pattern or literal fields are unstable | ||
let Foo { #[cfg(all())] present: () } = foo; | ||
//~^ ERROR attributes on struct pattern or literal fields are unstable | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(struct_field_attributes)] | ||
|
||
struct Foo { | ||
present: (), | ||
} | ||
|
||
fn main() { | ||
let foo = Foo { #[cfg(all())] present: () }; | ||
let _ = Foo { #[cfg(any())] present: () }; | ||
//~^ ERROR missing field `present` in initializer of `Foo` | ||
let _ = Foo { present: (), #[cfg(any())] absent: () }; | ||
let _ = Foo { present: (), #[cfg(all())] absent: () }; | ||
//~^ ERROR struct `Foo` has no field named `absent` | ||
let Foo { #[cfg(all())] present: () } = foo; | ||
let Foo { #[cfg(any())] present: () } = foo; | ||
//~^ ERROR pattern does not mention field `present` | ||
let Foo { present: (), #[cfg(any())] absent: () } = foo; | ||
let Foo { present: (), #[cfg(all())] absent: () } = foo; | ||
//~^ ERROR struct `Foo` does not have a field named `absent` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we could refactor out this gated feature check (and the one below) into
self.visit_struct_field_attrs()
(c.f.self.visit_expr_attrs()
) and then refactor awayself.configure_struct_expr_field()
andself.configure_struct_pat_field()
?