Skip to content
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

ast_validation: forbid "nonstandard" literal patterns #43844

Merged
merged 1 commit into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,25 @@ impl<'a> AstValidator<'a> {
}
}

/// matches '-' lit | lit (cf. parser::Parser::parse_pat_literal_maybe_minus)
fn check_expr_within_pat(&self, expr: &Expr) {
/// matches '-' lit | lit (cf. parser::Parser::parse_pat_literal_maybe_minus),
/// or path for ranges.
///
/// FIXME: do we want to allow expr -> pattern conversion to create path expressions?
/// That means making this work:
///
/// ```rust,ignore (FIXME)
/// struct S;
/// macro_rules! m {
/// ($a:expr) => {
/// let $a = S;
/// }
/// }
/// m!(S);
/// ```
fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) {
match expr.node {
ExprKind::Lit(..) | ExprKind::Path(..) => {}
ExprKind::Lit(..) => {}
ExprKind::Path(..) if allow_paths => {}
ExprKind::Unary(UnOp::Neg, ref inner)
if match inner.node { ExprKind::Lit(_) => true, _ => false } => {}
_ => self.err_handler().span_err(expr.span, "arbitrary expressions aren't allowed \
Expand Down Expand Up @@ -332,11 +347,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
fn visit_pat(&mut self, pat: &'a Pat) {
match pat.node {
PatKind::Lit(ref expr) => {
self.check_expr_within_pat(expr);
self.check_expr_within_pat(expr, false);
}
PatKind::Range(ref start, ref end, _) => {
self.check_expr_within_pat(start);
self.check_expr_within_pat(end);
self.check_expr_within_pat(start, true);
self.check_expr_within_pat(end, true);
}
_ => {}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/compile-fail/issue-43250.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

fn main() {
let mut y;
const C: u32 = 0;
macro_rules! m {
($a:expr) => {
let $a = 0;
}
}
m!(y);
//~^ ERROR arbitrary expressions aren't allowed in patterns
m!(C);
//~^ ERROR arbitrary expressions aren't allowed in patterns
}