diff options
| author | Daniel Grunwald <daniel@danielgrunwald.de> | 2015-01-08 01:44:01 +0100 | 
|---|---|---|
| committer | Daniel Grunwald <daniel@danielgrunwald.de> | 2015-01-08 01:44:01 +0100 | 
| commit | 1cc69c484eadec5e54f178a9fcfaff21b5d25cf4 (patch) | |
| tree | 1be1f1d2b71077fd4d26822431d00475bd9c57a9 /src/libsyntax/parse/parser.rs | |
| parent | 9e4e524e0eb17c8f463e731f23b544003e8709c6 (diff) | |
| download | rust-1cc69c484eadec5e54f178a9fcfaff21b5d25cf4.tar.gz rust-1cc69c484eadec5e54f178a9fcfaff21b5d25cf4.zip | |
RFC 558: Require parentheses for chained comparisons
Fixes #20724.
Diffstat (limited to 'src/libsyntax/parse/parser.rs')
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 24 | 
1 files changed, 23 insertions, 1 deletions
| diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 92e0395eca4..6624f3cd9a8 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -16,7 +16,7 @@ use ast::{AssociatedType, BareFnTy}; use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier}; use ast::{ProvidedMethod, Public, Unsafety}; use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue}; -use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, Block}; +use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, BiLt, BiGt, Block}; use ast::{BlockCheckMode, CaptureByRef, CaptureByValue, CaptureClause}; use ast::{Crate, CrateConfig, Decl, DeclItem}; use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf}; @@ -2906,6 +2906,9 @@ impl<'a> Parser<'a> { let cur_opt = self.token.to_binop(); match cur_opt { Some(cur_op) => { + if ast_util::is_comparison_binop(cur_op) { + self.check_no_chained_comparison(&*lhs, cur_op) + } let cur_prec = operator_prec(cur_op); if cur_prec > min_prec { self.bump(); @@ -2934,6 +2937,25 @@ impl<'a> Parser<'a> { } } + /// Produce an error if comparison operators are chained (RFC #558). + /// We only need to check lhs, not rhs, because all comparison ops + /// have same precedence and are left-associative + fn check_no_chained_comparison(&mut self, lhs: &Expr, outer_op: ast::BinOp) { + debug_assert!(ast_util::is_comparison_binop(outer_op)); + match lhs.node { + ExprBinary(op, _, _) if ast_util::is_comparison_binop(op) => { + let op_span = self.span; + self.span_err(op_span, + "Chained comparison operators require parentheses"); + if op == BiLt && outer_op == BiGt { + self.span_help(op_span, + "Use ::< instead of < if you meant to specify type arguments."); + } + } + _ => {} + } + } + /// Parse an assignment expression.... /// actually, this seems to be the main entry point for /// parsing an arbitrary expression. | 
