From 35f2fe52c2d4c55236446ebdac11ba44cc3704cd Mon Sep 17 00:00:00 2001 From: Aaron Keen Date: Mon, 14 Dec 2015 21:32:16 +0100 Subject: [PATCH 1/4] Corrects issue #28777 by removing, once a binary operator is found, the RESTRICTION_STMT_EXPR restriction to allow subsequent expressions to contain braces. https://github.com/rust-lang/rust/issues/28777 --- src/libsyntax/parse/parser.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7502a8cbc3546..6cbdd9f341118 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2813,16 +2813,25 @@ impl<'a> Parser<'a> { let rhs = try!(match op.fixity() { - Fixity::Right => self.with_res(restrictions, |this|{ - this.parse_assoc_expr_with(op.precedence(), LhsExpr::NotYetParsed) + Fixity::Right => self.with_res( + restrictions & !Restrictions::RESTRICTION_STMT_EXPR, + |this|{ + this.parse_assoc_expr_with(op.precedence(), + LhsExpr::NotYetParsed) }), - Fixity::Left => self.with_res(restrictions, |this|{ - this.parse_assoc_expr_with(op.precedence() + 1, LhsExpr::NotYetParsed) + Fixity::Left => self.with_res( + restrictions & !Restrictions::RESTRICTION_STMT_EXPR, + |this|{ + this.parse_assoc_expr_with(op.precedence() + 1, + LhsExpr::NotYetParsed) }), // We currently have no non-associative operators that are not handled above by // the special cases. The code is here only for future convenience. - Fixity::None => self.with_res(restrictions, |this|{ - this.parse_assoc_expr_with(op.precedence() + 1, LhsExpr::NotYetParsed) + Fixity::None => self.with_res( + restrictions & !Restrictions::RESTRICTION_STMT_EXPR, + |this|{ + this.parse_assoc_expr_with(op.precedence() + 1, + LhsExpr::NotYetParsed) }), }); From 41cc365af92ff3ed5c165fb575a74428e2608a6e Mon Sep 17 00:00:00 2001 From: Aaron Keen Date: Wed, 16 Dec 2015 11:53:36 +0100 Subject: [PATCH 2/4] Corrected formatting mistakes. Changed bit manipulation to use supported - (set difference) instead of explicit '& !'. --- src/libsyntax/parse/parser.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6cbdd9f341118..4f4a78e8760a7 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2814,22 +2814,22 @@ impl<'a> Parser<'a> { let rhs = try!(match op.fixity() { Fixity::Right => self.with_res( - restrictions & !Restrictions::RESTRICTION_STMT_EXPR, - |this|{ + restrictions - Restrictions::RESTRICTION_STMT_EXPR, + |this| { this.parse_assoc_expr_with(op.precedence(), - LhsExpr::NotYetParsed) + LhsExpr::NotYetParsed) }), Fixity::Left => self.with_res( - restrictions & !Restrictions::RESTRICTION_STMT_EXPR, - |this|{ + restrictions - Restrictions::RESTRICTION_STMT_EXPR, + |this| { this.parse_assoc_expr_with(op.precedence() + 1, LhsExpr::NotYetParsed) }), // We currently have no non-associative operators that are not handled above by // the special cases. The code is here only for future convenience. Fixity::None => self.with_res( - restrictions & !Restrictions::RESTRICTION_STMT_EXPR, - |this|{ + restrictions - Restrictions::RESTRICTION_STMT_EXPR, + |this| { this.parse_assoc_expr_with(op.precedence() + 1, LhsExpr::NotYetParsed) }), From ae479725b7eef82c692e30ab2e30dd5cb6104ade Mon Sep 17 00:00:00 2001 From: Aaron Keen Date: Thu, 17 Dec 2015 21:16:55 +0100 Subject: [PATCH 3/4] Removed test case. This now test successfully parses with the modification to parsing of binary operators. This is consistent with the behavior of grammar/parser-lalr. --- src/test/parse-fail/assoc-oddities-3.rs | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 src/test/parse-fail/assoc-oddities-3.rs diff --git a/src/test/parse-fail/assoc-oddities-3.rs b/src/test/parse-fail/assoc-oddities-3.rs deleted file mode 100644 index 0d4f21f0dca26..0000000000000 --- a/src/test/parse-fail/assoc-oddities-3.rs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2015 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// compile-flags: -Z parse-only - -fn that_odd_parse() { - // see assoc-oddities-1 for explanation - x + if c { a } else { b }[n]; //~ ERROR expected one of -} From cedd794d72ee1a9dbf7e55cf66f4a52393066e85 Mon Sep 17 00:00:00 2001 From: Aaron Keen Date: Thu, 17 Dec 2015 22:14:50 +0100 Subject: [PATCH 4/4] Added a functioning version of the assoc-oddities-3 test case to run-pass. Added run-pass/issue-28777 to demonstrate behavior of this parsing modification. --- src/test/run-pass/assoc-oddities-3.rs | 21 +++++++++++++++++++ src/test/run-pass/issue-28777.rs | 30 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/test/run-pass/assoc-oddities-3.rs create mode 100644 src/test/run-pass/issue-28777.rs diff --git a/src/test/run-pass/assoc-oddities-3.rs b/src/test/run-pass/assoc-oddities-3.rs new file mode 100644 index 0000000000000..4913816ffe679 --- /dev/null +++ b/src/test/run-pass/assoc-oddities-3.rs @@ -0,0 +1,21 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn that_odd_parse(c: bool, n: usize) -> u32 { + let x = 2; + let a = [1, 2, 3, 4]; + let b = [5, 6, 7, 7]; + x + if c { a } else { b }[n] +} + +fn main() { + assert_eq!(4, that_odd_parse(true, 1)); + assert_eq!(8, that_odd_parse(false, 1)); +} diff --git a/src/test/run-pass/issue-28777.rs b/src/test/run-pass/issue-28777.rs new file mode 100644 index 0000000000000..ea5d4e4f3c27b --- /dev/null +++ b/src/test/run-pass/issue-28777.rs @@ -0,0 +1,30 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let v1 = { 1 + {2} * {3} }; + let v2 = 1 + {2} * {3} ; + + assert_eq!(7, v1); + assert_eq!(7, v2); + + let v3; + v3 = { 1 + {2} * {3} }; + let v4; + v4 = 1 + {2} * {3}; + assert_eq!(7, v3); + assert_eq!(7, v4); + + let v5 = { 1 + {2} * 3 }; + assert_eq!(7, v5); + + let v9 = { 1 + if 1 > 2 {1} else {2} * {3} }; + assert_eq!(7, v9); +}