Skip to content

Commit

Permalink
pre-expansion gate box_patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 23, 2019
1 parent 1f470ce commit 2aff6b3
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 16 deletions.
16 changes: 2 additions & 14 deletions src/libsyntax/feature_gate/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
visit::walk_expr(self, e)
}

fn visit_arm(&mut self, arm: &'a ast::Arm) {
visit::walk_arm(self, arm)
}

fn visit_pat(&mut self, pattern: &'a ast::Pat) {
match &pattern.kind {
PatKind::Slice(pats) => {
Expand All @@ -533,11 +529,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}
}
PatKind::Box(..) => {
gate_feature_post!(&self, box_patterns,
pattern.span,
"box pattern syntax is experimental");
}
PatKind::Range(_, _, Spanned { node: RangeEnd::Excluded, .. }) => {
gate_feature_post!(&self, exclusive_range_pattern, pattern.span,
"exclusive range pattern syntax is experimental");
Expand All @@ -547,11 +538,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
visit::walk_pat(self, pattern)
}

fn visit_fn(&mut self,
fn_kind: FnKind<'a>,
fn_decl: &'a ast::FnDecl,
span: Span,
_node_id: NodeId) {
fn visit_fn(&mut self, fn_kind: FnKind<'a>, fn_decl: &'a ast::FnDecl, span: Span, _: NodeId) {
if let Some(header) = fn_kind.header() {
// Stability of const fn methods are covered in
// `visit_trait_item` and `visit_impl_item` below; this is
Expand Down Expand Up @@ -827,6 +814,7 @@ pub fn check_crate(krate: &ast::Crate,
gate_all!(crate_visibility_modifier, "`crate` visibility modifier is experimental");
gate_all!(const_generics, "const generics are unstable");
gate_all!(decl_macro, "`macro` is experimental");
gate_all!(box_patterns, "box pattern syntax is experimental");

visit::walk_crate(&mut visitor, krate);
}
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/parse/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ impl<'a> Parser<'a> {
self.parse_pat_ident(BindingMode::ByRef(mutbl))?
} else if self.eat_keyword(kw::Box) {
// Parse `box pat`
PatKind::Box(self.parse_pat_with_range_pat(false, None)?)
let pat = self.parse_pat_with_range_pat(false, None)?;
self.sess.gated_spans.box_patterns.borrow_mut().push(lo.to(self.prev_span));
PatKind::Box(pat)
} else if self.can_be_ident_pat() {
// Parse `ident @ pat`
// This can give false positives and parse nullary enums,
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/sess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ crate struct GatedSpans {
pub const_generics: Lock<Vec<Span>>,
/// Spans collected for gating `decl_macro`, e.g. `macro m() {}`.
pub decl_macro: Lock<Vec<Span>>,
/// Spans collected for gating `box_patterns`, e.g. `box 0`.
pub box_patterns: Lock<Vec<Span>>,
}

/// Info about a parsing session.
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/feature-gates/feature-gate-box_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ fn main() {
let box x = Box::new('c'); //~ ERROR box pattern syntax is experimental
println!("x: {}", x);
}

macro_rules! accept_pat { ($p:pat) => {} }
accept_pat!(box 0); //~ ERROR box pattern syntax is experimental
11 changes: 10 additions & 1 deletion src/test/ui/feature-gates/feature-gate-box_patterns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ LL | let box x = Box::new('c');
= note: for more information, see https://github.com/rust-lang/rust/issues/29641
= help: add `#![feature(box_patterns)]` to the crate attributes to enable

error: aborting due to previous error
error[E0658]: box pattern syntax is experimental
--> $DIR/feature-gate-box_patterns.rs:7:13
|
LL | accept_pat!(box 0);
| ^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/29641
= help: add `#![feature(box_patterns)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
1 change: 1 addition & 0 deletions src/test/ui/or-patterns/or-patterns-syntactic-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// check-pass

#![feature(or_patterns)]
#![feature(box_patterns)]

fn main() {}

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/pattern/rest-pat-syntactic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

// check-pass

#![feature(box_patterns)]

fn main() {}

macro_rules! accept_pat {
Expand Down

0 comments on commit 2aff6b3

Please sign in to comment.