From 2e888d0341e81de1744b257c25b012c2c148f0ba Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 13 Jan 2015 14:24:37 +1100 Subject: Add the span of the operator itself to ast::BinOp. --- src/libsyntax/parse/parser.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index e59dbe52b76..15254988ce0 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2840,6 +2840,7 @@ impl<'a> Parser<'a> { self.expected_tokens.push(TokenType::Operator); + let cur_op_span = self.span; let cur_opt = self.token.to_binop(); match cur_opt { Some(cur_op) => { @@ -2853,7 +2854,7 @@ impl<'a> Parser<'a> { let rhs = self.parse_more_binops(expr, cur_prec + 1); let lhs_span = lhs.span; let rhs_span = rhs.span; - let binary = self.mk_binary(cur_op, lhs, rhs); + let binary = self.mk_binary(codemap::respan(cur_op_span, cur_op), lhs, rhs); let bin = self.mk_expr(lhs_span.lo, rhs_span.hi, binary); self.parse_more_binops(bin, min_prec) } else { @@ -2877,14 +2878,14 @@ 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) { + 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) => { + ExprBinary(op, _, _) if ast_util::is_comparison_binop(op.node) => { let op_span = self.span; self.span_err(op_span, "Chained comparison operators require parentheses"); - if op == BiLt && outer_op == BiGt { + if op.node == BiLt && outer_op == BiGt { self.span_help(op_span, "use ::< instead of < if you meant to specify type arguments"); } @@ -2919,6 +2920,7 @@ impl<'a> Parser<'a> { pub fn parse_assign_expr_with(&mut self, lhs: P) -> P { let restrictions = self.restrictions & RESTRICTION_NO_STRUCT_LITERAL; + let op_span = self.span; match self.token { token::Eq => { self.bump(); @@ -2942,7 +2944,7 @@ impl<'a> Parser<'a> { }; let rhs_span = rhs.span; let span = lhs.span; - let assign_op = self.mk_assign_op(aop, lhs, rhs); + let assign_op = self.mk_assign_op(codemap::respan(op_span, aop), lhs, rhs); self.mk_expr(span.lo, rhs_span.hi, assign_op) } // A range expression, either `expr..expr` or `expr..`. -- cgit 1.4.1-3-g733a5 From ec790d6fcc426e13431cbb7510c62864515a3ef8 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 13 Jan 2015 15:18:55 +1100 Subject: Tweak chained comparison errors. Lower case and give a more precise span: from operator to operator, not just the last one. --- src/libsyntax/parse/parser.rs | 7 ++++--- src/test/compile-fail/require-parens-for-chained-comparison.rs | 8 ++++---- src/test/compile-fail/unsized2.rs | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 15254988ce0..759e5e8229a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2882,12 +2882,13 @@ impl<'a> Parser<'a> { debug_assert!(ast_util::is_comparison_binop(outer_op)); match lhs.node { ExprBinary(op, _, _) if ast_util::is_comparison_binop(op.node) => { - let op_span = self.span; + // respan to include both operators + let op_span = mk_sp(op.span.lo, self.span.hi); self.span_err(op_span, - "Chained comparison operators require parentheses"); + "chained comparison operators require parentheses"); if op.node == BiLt && outer_op == BiGt { self.span_help(op_span, - "use ::< instead of < if you meant to specify type arguments"); + "use `::<...>` instead of `<...>` if you meant to specify type arguments"); } } _ => {} diff --git a/src/test/compile-fail/require-parens-for-chained-comparison.rs b/src/test/compile-fail/require-parens-for-chained-comparison.rs index f5d8c574814..f2705f58331 100644 --- a/src/test/compile-fail/require-parens-for-chained-comparison.rs +++ b/src/test/compile-fail/require-parens-for-chained-comparison.rs @@ -12,12 +12,12 @@ fn f() {} fn main() { false == false == false; - //~^ ERROR: Chained comparison operators require parentheses + //~^ ERROR: chained comparison operators require parentheses false == 0 < 2; - //~^ ERROR: Chained comparison operators require parentheses + //~^ ERROR: chained comparison operators require parentheses f(); - //~^ ERROR: Chained comparison operators require parentheses - //~^^ HELP: use ::< instead of < if you meant to specify type arguments + //~^ ERROR: chained comparison operators require parentheses + //~^^ HELP: use `::<...>` instead of `<...>` } diff --git a/src/test/compile-fail/unsized2.rs b/src/test/compile-fail/unsized2.rs index a47d81e38cc..b2eb2064aeb 100644 --- a/src/test/compile-fail/unsized2.rs +++ b/src/test/compile-fail/unsized2.rs @@ -15,6 +15,6 @@ fn f() {} pub fn main() { f(); //~^ ERROR expected identifier, found keyword `type` - //~^^ ERROR: Chained comparison operators require parentheses - //~^^^ HELP: use ::< instead of < if you meant to specify type arguments + //~^^ ERROR: chained comparison + //~^^^ HELP: use `::< } -- cgit 1.4.1-3-g733a5