Fix deprecation warning for increment/decrement statements

This commit is contained in:
Pranav Gaddamadugu 2023-04-03 10:04:34 -07:00
parent 7446c63018
commit 3b20befc97
6 changed files with 43 additions and 69 deletions

View File

@ -119,7 +119,7 @@ impl<'a> ParserContext<'a> {
}
/// Emit the warning `warning`.
pub(super) fn _emit_warning(&self, warning: ParserWarning) {
pub(super) fn emit_warning(&self, warning: ParserWarning) {
self.handler.emit_warning(warning.into());
}

View File

@ -16,7 +16,7 @@
use super::*;
use leo_errors::{ParserError, Result};
use leo_errors::{ParserError, ParserWarning, Result};
use leo_span::sym;
const ASSIGN_TOKENS: &[Token] = &[
@ -41,8 +41,6 @@ impl ParserContext<'_> {
pub(crate) fn parse_statement(&mut self) -> Result<Statement> {
match &self.token.token {
Token::Return => Ok(Statement::Return(self.parse_return_statement()?)),
Token::Increment => Ok(self.parse_increment_statement()?),
Token::Decrement => Ok(self.parse_decrement_statement()?),
Token::If => Ok(Statement::Conditional(self.parse_conditional_statement()?)),
Token::For => Ok(Statement::Iteration(Box::new(self.parse_loop_statement()?))),
Token::Assert | Token::AssertEq | Token::AssertNeq => Ok(self.parse_assert_statement()?),
@ -89,6 +87,7 @@ impl ParserContext<'_> {
/// Returns a [`AssignStatement`] AST node if the next tokens represent a assign, otherwise expects an expression statement.
fn parse_assign_statement(&mut self) -> Result<Statement> {
// Look ahead and
let place = self.parse_expression()?;
if self.eat_any(ASSIGN_TOKENS) {
@ -131,6 +130,27 @@ impl ParserContext<'_> {
Ok(Statement::Assign(Box::new(AssignStatement { span, place, value })))
} else {
// Check for `increment` and `decrement` statements. If found, emit a deprecation warning.
if let Expression::Call(call_expression) = &place {
match *call_expression.function {
Expression::Identifier(Identifier { name: sym::decrement, .. }) => {
self.emit_warning(ParserWarning::deprecated(
"decrement",
"Use `Mapping::{get, get_or, put}` for manipulating on-chain mappings.",
place.span(),
));
}
Expression::Identifier(Identifier { name: sym::increment, .. }) => {
self.emit_warning(ParserWarning::deprecated(
"increment",
"Use `Mapping::{get, get_or, put}` for manipulating on-chain mappings.",
place.span(),
));
}
_ => (),
}
}
// Parse the expression as a statement.
let end = self.expect(&Token::Semicolon)?;
Ok(Statement::Expression(ExpressionStatement { span: place.span() + end, expression: place }))
@ -174,48 +194,6 @@ impl ParserContext<'_> {
Ok(ReturnStatement { span, expression, finalize_arguments: finalize_args })
}
/// Returns a dummy [`Statement`] AST node and emits and error if the next tokens represent a decrement statement.
fn parse_decrement_statement(&mut self) -> Result<Statement> {
// Parse the decrement token.
let span = self.expect(&Token::Decrement)?;
// Emit a deprecation error.
self.handler.emit_err(ParserError::deprecated(
"decrement",
"Consider using `Mapping::{get, get_or, set}` instead",
span,
));
// Skip tokens until after next semi-colon.
while self.has_next() {
if self.check(&Token::Semicolon) {
self.bump();
break;
}
self.bump();
}
Ok(Statement::dummy(Default::default()))
}
/// Returns a dummy [`Statement`] AST node and emits an error if the next tokens represent an increment statement.
fn parse_increment_statement(&mut self) -> Result<Statement> {
// Parse the increment token.
let span = self.expect(&Token::Increment)?;
// Emit a deprecation error.
self.handler.emit_err(ParserError::deprecated(
"increment",
"Consider using `Mapping::{get, get_or, set}` instead",
span,
));
// Skip tokens until after the next semi-colon.
while self.has_next() {
if self.check(&Token::Semicolon) {
self.bump();
break;
}
self.bump();
}
Ok(Statement::dummy(Default::default()))
}
/// Returns a [`ConditionalStatement`] AST node if the next tokens represent a conditional statement.
fn parse_conditional_statement(&mut self) -> Result<ConditionalStatement> {
let start = self.expect(&Token::If)?;

View File

@ -382,7 +382,6 @@ impl Token {
"bool" => Token::Bool,
"console" => Token::Console,
"constant" => Token::Constant,
"decrement" => Token::Decrement,
"else" => Token::Else,
"false" => Token::False,
"field" => Token::Field,
@ -398,7 +397,6 @@ impl Token {
"if" => Token::If,
"import" => Token::Import,
"in" => Token::In,
"increment" => Token::Increment,
"inline" => Token::Inline,
"let" => Token::Let,
"leo" => Token::Leo,

View File

@ -113,7 +113,6 @@ pub enum Token {
AssertNeq,
Console,
Constant,
Decrement,
Else,
Finalize,
For,
@ -121,7 +120,6 @@ pub enum Token {
If,
Import,
In,
Increment,
Inline,
Let,
Mapping,
@ -153,7 +151,6 @@ pub const KEYWORD_TOKENS: &[Token] = &[
Token::Bool,
Token::Console,
Token::Constant,
Token::Decrement,
Token::Else,
Token::False,
Token::Field,
@ -169,7 +166,6 @@ pub const KEYWORD_TOKENS: &[Token] = &[
Token::If,
Token::Import,
Token::In,
Token::Increment,
Token::Inline,
Token::Let,
Token::Mapping,
@ -208,7 +204,6 @@ impl Token {
Token::Bool => sym::bool,
Token::Console => sym::console,
Token::Constant => sym::constant,
Token::Decrement => sym::decrement,
Token::Else => sym::Else,
Token::False => sym::False,
Token::Field => sym::field,
@ -224,7 +219,6 @@ impl Token {
Token::If => sym::If,
Token::Import => sym::import,
Token::In => sym::In,
Token::Increment => sym::increment,
Token::Inline => sym::inline,
Token::Let => sym::Let,
Token::Leo => sym::leo,
@ -340,7 +334,6 @@ impl fmt::Display for Token {
AssertNeq => write!(f, "assert_neq"),
Console => write!(f, "console"),
Constant => write!(f, "constant"),
Decrement => write!(f, "decrement"),
Else => write!(f, "else"),
Finalize => write!(f, "finalize"),
For => write!(f, "for"),
@ -348,7 +341,6 @@ impl fmt::Display for Token {
If => write!(f, "if"),
Import => write!(f, "import"),
In => write!(f, "in"),
Increment => write!(f, "increment"),
Inline => write!(f, "inline"),
Let => write!(f, "let"),
Mapping => write!(f, "mapping"),

View File

@ -283,11 +283,4 @@ create_messages!(
msg: format!("`console` statements are not yet supported."),
help: Some("Consider using `assert`, `assert_eq`, or `assert_neq` instead.".to_string()),
}
@formatted
deprecated {
args: (keyword: impl Display, help: impl Display),
msg: format!("The keyword `{keyword}` is deprecated."),
help: Some(help.to_string()),
}
);

View File

@ -16,6 +16,8 @@
use crate::create_messages;
use std::fmt::Display;
create_messages!(
/// ParserWarning enum that represents all the warnings for the `leo-parser` crate.
ParserWarning,
@ -23,10 +25,21 @@ create_messages!(
code_prefix: "PAR",
/// For when a user used const on a parameter or input instead of constant.
@formatted
const_parameter_or_input {
args: (),
msg: "`constant` is preferred over `const` for function parameters to indicate a R1CS constant.",
help: None,
}
@formatted
const_parameter_or_input {
args: (),
msg: "`constant` is preferred over `const` for function parameters to indicate a R1CS constant.",
help: None,
}
/// For when a keyword is deprecated but could be used as a valid identifier.
@formatted
deprecated {
args: (keyword: impl Display, help: impl Display),
msg: format!("The keyword `{keyword}` is deprecated."),
help: Some(help.to_string()),
}
);