From 52acaa69743be657f7d3003ca2a2abf7f1cd7a2e Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Tue, 3 Dec 2019 10:19:58 +0100 Subject: implement recovery in check_assoc_op --- src/librustc_parse/parser/expr.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/librustc_parse/parser/expr.rs') diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index f20d0aa2236..0792f1b3b7f 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -345,7 +345,31 @@ impl<'a> Parser<'a> { /// /// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively. fn check_assoc_op(&self) -> Option { - AssocOp::from_token(&self.token) + match (AssocOp::from_token(&self.token), &self.token.kind) { + (op @ Some(_), _) => op, + (None, token::Ident(sym::and, false)) => { + self.error_bad_logical_op("and", "&&", "conjunction"); + Some(AssocOp::LAnd) + } + (None, token::Ident(sym::or, false)) => { + self.error_bad_logical_op("or", "||", "disjunction"); + Some(AssocOp::LOr) + } + _ => None, + } + } + + /// Error on `and` and `or` suggesting `&&` and `||` respectively. + fn error_bad_logical_op(&self, bad: &str, good: &str, english: &str) { + self.struct_span_err(self.token.span, &format!("`{}` is not a logical operator", bad)) + .span_suggestion( + self.token.span, + &format!("instead of `{}`, use `{}` to perform logical {}", bad, good, english), + good.to_string(), + Applicability::MachineApplicable, + ) + .note("unlike in e.g., python and PHP, `&&` and `||` are used for logical operators") + .emit(); } /// Checks if this expression is a successfully parsed statement. -- cgit 1.4.1-3-g733a5