span refactor: fix build + simplify &span

This commit is contained in:
Mazdak Farrokhzad 2022-05-12 16:24:17 +02:00
parent 0e5402773a
commit 8d915339a0
18 changed files with 62 additions and 62 deletions

View File

@ -38,7 +38,7 @@ impl TryFrom<(Type, Expression)> for InputValue {
match (type_, value) {
(Type::Address, ValueExpression::Address(value, _)) => Self::Address(value),
(Type::Boolean, ValueExpression::Boolean(value, span)) => {
let bool_value = value.parse::<bool>().map_err(|_| ParserError::unexpected_eof(&span))?; // TODO: change error
let bool_value = value.parse::<bool>().map_err(|_| ParserError::unexpected_eof(span))?; // TODO: change error
Self::Boolean(bool_value)
}
(Type::Char, ValueExpression::Char(value)) => Self::Char(value),
@ -48,18 +48,18 @@ impl TryFrom<(Type, Expression)> for InputValue {
if expected == actual {
Self::Integer(expected, value)
} else {
return Err(InputError::unexpected_type(expected.to_string(), actual, &span).into());
return Err(InputError::unexpected_type(expected.to_string(), actual, span).into());
}
}
(x, y) => {
return Err(InputError::unexpected_type(x, &y, &y.span()).into());
return Err(InputError::unexpected_type(x, &y, y.span()).into());
}
}
}
(type_, Expression::Unary(unary)) if unary.op == UnaryOperation::Negate => {
InputValue::try_from((type_, *unary.inner))?
}
(_type_, expr) => return Err(InputError::illegal_expression(&expr, &expr.span()).into()),
(_type_, expr) => return Err(InputError::illegal_expression(&expr, expr.span()).into()),
})
}
}

View File

@ -35,7 +35,7 @@ impl TryFrom<InputAst> for ProgramInput {
sym::registers => &mut registers,
_ => {
return Err(
InputError::unexpected_section(&["main", "registers"], section.name, &section.span).into(),
InputError::unexpected_section(&["main", "registers"], section.name, section.span).into(),
)
}
};

View File

@ -39,7 +39,7 @@ impl TryFrom<InputAst> for ProgramState {
return Err(InputError::unexpected_section(
&["state", "record", "state_leaf"],
section.name,
&section.span,
section.span,
)
.into());
}

View File

@ -233,7 +233,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
match &console_function_call.function {
ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted),
ConsoleFunction::Log(_) => ConsoleFunction::Log(formatted),
_ => return Err(AstError::impossible_console_assert_call(&args.span).into()),
_ => return Err(AstError::impossible_console_assert_call(args.span).into()),
}
}
};

View File

@ -159,7 +159,7 @@ impl<'a> ParserContext<'a> {
/// Expects an [`Identifier`], or errors.
pub fn expect_ident(&mut self) -> Result<Identifier> {
self.eat_identifier()
.ok_or_else(|| ParserError::unexpected_str(&self.token.token, "ident", &self.token.span).into())
.ok_or_else(|| ParserError::unexpected_str(&self.token.token, "ident", self.token.span).into())
}
/// Returns a reference to the next token if it is a [`GroupCoordinate`], or [None] if
@ -250,7 +250,7 @@ impl<'a> ParserContext<'a> {
/// Returns an unexpected error at the current token.
fn unexpected<T>(&self, expected: impl Display) -> Result<T> {
Err(ParserError::unexpected(&self.token.token, expected, &self.token.span).into())
Err(ParserError::unexpected(&self.token.token, expected, self.token.span).into())
}
/// Eats the expected `token`, or errors.

View File

@ -230,7 +230,7 @@ impl ParserContext<'_> {
loop {
if self.eat(&Token::Dot) {
let curr = &self.token;
return Err(ParserError::unexpected_str(&curr.token, "int or ident", &curr.span).into());
return Err(ParserError::unexpected_str(&curr.token, "int or ident", curr.span).into());
}
if !self.check(&Token::LeftParen) {
@ -261,7 +261,7 @@ impl ParserContext<'_> {
if !trailing && tuple.len() == 1 {
Ok(tuple.remove(0))
} else {
Err(ParserError::unexpected("A tuple expression.", "A valid expression.", &span).into())
Err(ParserError::unexpected("A tuple expression.", "A valid expression.", span).into())
}
}
@ -302,7 +302,7 @@ impl ParserContext<'_> {
let int_ty = Self::token_to_int_type(suffix).expect("unknown int type token");
Expression::Value(ValueExpression::Integer(int_ty, value, full_span))
}
None => return Err(ParserError::implicit_values_not_allowed(value, &span).into()),
None => return Err(ParserError::implicit_values_not_allowed(value, span).into()),
}
}
Token::True => Expression::Value(ValueExpression::Boolean("true".into(), span)),
@ -323,7 +323,7 @@ impl ParserContext<'_> {
span,
}),
token => {
return Err(ParserError::unexpected_str(token, "expression", &span).into());
return Err(ParserError::unexpected_str(token, "expression", span).into());
}
})
}

View File

@ -26,7 +26,7 @@ impl ParserContext<'_> {
while self.has_next() {
match &self.token.token {
Token::Ident(sym::test) => return Err(ParserError::test_function(&self.token.span).into()),
Token::Ident(sym::test) => return Err(ParserError::test_function(self.token.span).into()),
// Const functions share the first token with the global Const.
Token::Const if self.peek_is_function() => {
let (id, function) = self.parse_function_declaration()?;
@ -54,7 +54,7 @@ impl ParserContext<'_> {
.map(|x| format!("'{}'", x))
.collect::<Vec<_>>()
.join(", "),
&token.span,
token.span,
)
}
@ -64,7 +64,7 @@ impl ParserContext<'_> {
let constant = self.eat(&Token::Constant).then(|| self.prev_token.span);
let const_ = self.eat(&Token::Const).then(|| self.prev_token.span);
if let Some(span) = &const_ {
if let Some(span) = const_ {
self.emit_warning(ParserWarning::const_parameter_or_input(span));
}
@ -74,10 +74,10 @@ impl ParserContext<'_> {
(None, None, None) => Ok(ParamMode::Private),
(Some(_), None, None) => Ok(ParamMode::Public),
(Some(m1), Some(m2), None) | (Some(m1), None, Some(m2)) | (None, Some(m1), Some(m2)) => {
Err(ParserError::inputs_multiple_variable_types_specified(&(m1 + m2)).into())
Err(ParserError::inputs_multiple_variable_types_specified(m1 + m2).into())
}
(Some(m1), Some(m2), Some(m3)) => {
Err(ParserError::inputs_multiple_variable_types_specified(&(m1 + m2 + m3)).into())
Err(ParserError::inputs_multiple_variable_types_specified(m1 + m2 + m3).into())
}
}
}
@ -90,7 +90,7 @@ impl ParserContext<'_> {
let name = self.expect_ident()?;
if let Some(mutable) = &mutable {
self.emit_err(ParserError::mut_function_input(&(mutable.span + name.span)));
self.emit_err(ParserError::mut_function_input(mutable.span + name.span));
}
self.expect(&Token::Colon)?;

View File

@ -27,7 +27,7 @@ impl ParserContext<'_> {
if self.check(&Token::LeftSquare) {
sections.push(self.parse_section()?);
} else {
return Err(ParserError::unexpected_token(self.token.token.clone(), &self.token.span).into());
return Err(ParserError::unexpected_token(self.token.token.clone(), self.token.span).into());
}
}

View File

@ -42,7 +42,7 @@ pub mod type_;
pub(crate) fn assert_no_whitespace(left_span: Span, right_span: Span, left: &str, right: &str) -> Result<()> {
if left_span.hi != right_span.lo {
let error_span = Span::new(left_span.hi, right_span.lo); // The span between them.
return Err(ParserError::unexpected_whitespace(left, right, &error_span).into());
return Err(ParserError::unexpected_whitespace(left, right, error_span).into());
}
Ok(())

View File

@ -27,7 +27,7 @@ impl ParserContext<'_> {
pub fn construct_assignee_access(expr: Expression, _accesses: &mut [AssigneeAccess]) -> Result<Identifier> {
match expr {
Expression::Identifier(id) => Ok(id),
_ => Err(ParserError::invalid_assignment_target(&expr.span()).into()),
_ => Err(ParserError::invalid_assignment_target(expr.span()).into()),
}
}
@ -114,7 +114,7 @@ impl ParserContext<'_> {
let next = if self.eat(&Token::Else) {
let s = self.parse_statement()?;
if !matches!(s, Statement::Block(_) | Statement::Conditional(_)) {
self.emit_err(ParserError::unexpected_statement(&s, "Block or Conditional", &s.span()));
self.emit_err(ParserError::unexpected_statement(&s, "Block or Conditional", s.span()));
}
Some(Box::new(s))
} else {
@ -168,7 +168,7 @@ impl ParserContext<'_> {
string = Some(match token {
Token::StringLit(chars) => chars,
_ => {
p.emit_err(ParserError::unexpected_str(token, "formatted string", &span));
p.emit_err(ParserError::unexpected_str(token, "formatted string", span));
Vec::new()
}
});
@ -204,7 +204,7 @@ impl ParserContext<'_> {
self.emit_err(ParserError::unexpected_ident(
x,
&["assert", "error", "log"],
&function.span,
function.span,
));
ConsoleFunction::Log(self.parse_console_args()?)
}
@ -219,9 +219,9 @@ impl ParserContext<'_> {
/// Returns a [`VariableName`] AST node if the next tokens represent a variable name with
/// valid keywords.
pub fn parse_variable_name(&mut self, decl_ty: Declare, span: &Span) -> Result<VariableName> {
pub fn parse_variable_name(&mut self, decl_ty: Declare, span: Span) -> Result<VariableName> {
if self.eat(&Token::Mut) {
self.emit_err(ParserError::let_mut_statement(&(&self.prev_token.span + span)));
self.emit_err(ParserError::let_mut_statement(self.prev_token.span + span));
}
let name = self.expect_ident()?;
@ -244,16 +244,16 @@ impl ParserContext<'_> {
// Parse variable names.
let variable_names = if self.peek_is_left_par() {
let vars = self
.parse_paren_comma_list(|p| p.parse_variable_name(decl_type, &decl_span).map(Some))
.parse_paren_comma_list(|p| p.parse_variable_name(decl_type, decl_span).map(Some))
.map(|(vars, ..)| vars)?;
if vars.len() == 1 {
self.emit_err(ParserError::invalid_parens_around_single_variable(&vars[0].span()));
self.emit_err(ParserError::invalid_parens_around_single_variable(vars[0].span()));
}
vars
} else {
vec![self.parse_variable_name(decl_type, &decl_span)?]
vec![self.parse_variable_name(decl_type, decl_span)?]
};
self.expect(&Token::Colon)?;

View File

@ -58,7 +58,7 @@ pub(crate) fn tokenize_iter(input: &str, mut lo: BytePos) -> impl '_ + Iterator<
match token {
Token::WhiteSpace => continue,
Token::AddressLit(address) if !check_address(&address) => {
return Some(Err(ParserError::invalid_address_lit(address, &span).into()));
return Some(Err(ParserError::invalid_address_lit(address, span).into()));
}
_ => return Some(Ok(SpannedToken { token, span })),
}

View File

@ -37,7 +37,7 @@ pub struct SymbolTable<'a> {
impl<'a> SymbolTable<'a> {
pub fn check_shadowing(&self, symbol: &Symbol) -> Result<()> {
if let Some(function) = self.functions.get(symbol) {
Err(AstError::shadowed_function(symbol, &function.span).into())
Err(AstError::shadowed_function(symbol, function.span).into())
} else {
self.variables.check_shadowing(symbol)?;
Ok(())

View File

@ -37,7 +37,7 @@ pub struct VariableScope<'a> {
impl<'a> VariableScope<'a> {
pub fn check_shadowing(&self, symbol: &Symbol) -> Result<()> {
if let Some(var) = self.variables.get(symbol) {
Err(AstError::shadowed_variable(symbol, &var.span).into())
Err(AstError::shadowed_variable(symbol, var.span).into())
} else if let Some(parent) = &self.parent {
parent.check_shadowing(symbol)
} else {

View File

@ -46,7 +46,7 @@ impl<'a> TypeChecker<'a> {
Some(self.assert_type(var.type_.clone(), expected, span))
} else {
self.handler
.emit_err(TypeCheckerError::unknown_sym("variable", ident.name, &span).into());
.emit_err(TypeCheckerError::unknown_sym("variable", ident.name, span).into());
None
}
}
@ -67,7 +67,7 @@ impl<'a> TypeChecker<'a> {
if int.parse::<i8>().is_err() {
self.handler
.emit_err(TypeCheckerError::invalid_int_value(int, "i8", &value.span()).into());
.emit_err(TypeCheckerError::invalid_int_value(int, "i8", value.span()).into());
}
}
IntegerType::I16 => {
@ -80,7 +80,7 @@ impl<'a> TypeChecker<'a> {
if int.parse::<i16>().is_err() {
self.handler
.emit_err(TypeCheckerError::invalid_int_value(int, "i16", &value.span()).into());
.emit_err(TypeCheckerError::invalid_int_value(int, "i16", value.span()).into());
}
}
IntegerType::I32 => {
@ -93,7 +93,7 @@ impl<'a> TypeChecker<'a> {
if int.parse::<i32>().is_err() {
self.handler
.emit_err(TypeCheckerError::invalid_int_value(int, "i32", &value.span()).into());
.emit_err(TypeCheckerError::invalid_int_value(int, "i32", value.span()).into());
}
}
IntegerType::I64 => {
@ -106,7 +106,7 @@ impl<'a> TypeChecker<'a> {
if int.parse::<i64>().is_err() {
self.handler
.emit_err(TypeCheckerError::invalid_int_value(int, "i64", &value.span()).into());
.emit_err(TypeCheckerError::invalid_int_value(int, "i64", value.span()).into());
}
}
IntegerType::I128 => {
@ -119,25 +119,25 @@ impl<'a> TypeChecker<'a> {
if int.parse::<i128>().is_err() {
self.handler
.emit_err(TypeCheckerError::invalid_int_value(int, "i128", &value.span()).into());
.emit_err(TypeCheckerError::invalid_int_value(int, "i128", value.span()).into());
}
}
IntegerType::U8 if str_content.parse::<u8>().is_err() => self
.handler
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u8", &value.span()).into()),
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u8", value.span()).into()),
IntegerType::U16 if str_content.parse::<u16>().is_err() => self
.handler
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u16", &value.span()).into()),
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u16", value.span()).into()),
IntegerType::U32 if str_content.parse::<u32>().is_err() => self
.handler
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u32", &value.span()).into()),
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u32", value.span()).into()),
IntegerType::U64 if str_content.parse::<u64>().is_err() => self
.handler
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u64", &value.span()).into()),
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u64", value.span()).into()),
IntegerType::U128 if str_content.parse::<u128>().is_err() => self
.handler
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u128", &value.span()).into()),
.emit_err(TypeCheckerError::invalid_int_value(str_content, "u128", value.span()).into()),
_ => {}
}
Some(self.assert_type(Type::IntegerType(*type_), expected, value.span()))
@ -195,7 +195,7 @@ impl<'a> TypeChecker<'a> {
// But Type B was not a unsigned int.
(Some(Type::IntegerType(_)), Some(t)) => {
self.handler.emit_err(
TypeCheckerError::incorrect_pow_exponent_type("unsigned int", t, &binary.right.span())
TypeCheckerError::incorrect_pow_exponent_type("unsigned int", t, binary.right.span())
.into(),
);
}
@ -208,13 +208,13 @@ impl<'a> TypeChecker<'a> {
// But Type B was not an int.
(Some(Type::Field), Some(t)) => {
self.handler.emit_err(
TypeCheckerError::incorrect_pow_exponent_type("int", t, &binary.right.span()).into(),
TypeCheckerError::incorrect_pow_exponent_type("int", t, binary.right.span()).into(),
);
}
// The base is some type thats not an int or field.
(Some(t), _) => {
self.handler
.emit_err(TypeCheckerError::incorrect_pow_base_type(t, &binary.left.span()).into());
.emit_err(TypeCheckerError::incorrect_pow_base_type(t, binary.left.span()).into());
}
_ => {}
}
@ -261,7 +261,7 @@ impl<'a> TypeChecker<'a> {
) => self.negate = !self.negate,
Some(t) => self
.handler
.emit_err(TypeCheckerError::type_is_not_negatable(t, &unary.inner.span()).into()),
.emit_err(TypeCheckerError::type_is_not_negatable(t, unary.inner.span()).into()),
_ => {}
};
self.compare_expr_type(&unary.inner, expected, unary.inner.span())
@ -283,7 +283,7 @@ impl<'a> TypeChecker<'a> {
TypeCheckerError::incorrect_num_args_to_call(
func.input.len(),
call.arguments.len(),
&call.span(),
call.span(),
)
.into(),
);
@ -303,7 +303,7 @@ impl<'a> TypeChecker<'a> {
Some(ret)
} else {
self.handler
.emit_err(TypeCheckerError::unknown_sym("function", &ident.name, &ident.span()).into());
.emit_err(TypeCheckerError::unknown_sym("function", &ident.name, ident.span()).into());
None
}
}

View File

@ -63,17 +63,17 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> {
match &var.declaration {
Declaration::Const => self
.handler
.emit_err(TypeCheckerError::cannont_assign_to_const_var(var_name, &var.span).into()),
.emit_err(TypeCheckerError::cannont_assign_to_const_var(var_name, var.span).into()),
Declaration::Input(ParamMode::Constant) => self
.handler
.emit_err(TypeCheckerError::cannont_assign_to_const_input(var_name, &var.span).into()),
.emit_err(TypeCheckerError::cannont_assign_to_const_input(var_name, var.span).into()),
_ => {}
}
Some(var.type_.clone())
} else {
self.handler.emit_err(
TypeCheckerError::unknown_sym("variable", &input.assignee.identifier.name, &input.assignee.span).into(),
TypeCheckerError::unknown_sym("variable", &input.assignee.identifier.name, input.assignee.span).into(),
);
None

View File

@ -83,7 +83,7 @@ impl<'a> TypeChecker<'a> {
if let Some(expected) = expected {
if type_ != expected {
self.handler
.emit_err(TypeCheckerError::type_should_be(type_.clone(), expected, &span).into());
.emit_err(TypeCheckerError::type_should_be(type_.clone(), expected, span).into());
}
}
@ -102,7 +102,7 @@ impl<'a> TypeChecker<'a> {
TypeCheckerError::expected_one_type_of(
expected.iter().map(|t| t.to_string() + ",").collect::<String>(),
type_,
&span,
span,
)
.into(),
);

View File

@ -96,7 +96,7 @@ macro_rules! create_messages {
// Formatted errors always takes a span.
$(#[$error_func_docs])*
// Expands additional arguments for the error defining function.
pub fn $name($($arg_names: $arg_types,)* span: &leo_span::Span) -> Self {
pub fn $name($($arg_names: $arg_types,)* span: leo_span::Span) -> Self {
Self::Formatted(
Formatted::new_from_span(
$message,
@ -105,7 +105,7 @@ macro_rules! create_messages {
Self::code_identifier(),
Self::message_type(),
Self::is_error(),
*span,
span,
// Each function always generates its own backtrace for backtrace clarity to originate from the error function.
Backtrace::new(),
)

View File

@ -282,19 +282,19 @@ mod tests {
let res: Result<(), _> = Handler::with(|h| {
let s = Span::default();
assert_eq!(h.err_count(), 0);
h.emit_err(ParserError::invalid_import_list(&s).into());
h.emit_err(ParserError::invalid_import_list(s).into());
assert_eq!(h.err_count(), 1);
h.emit_err(ParserError::unexpected_eof(&s).into());
h.emit_err(ParserError::unexpected_eof(s).into());
assert_eq!(h.err_count(), 2);
Err(ParserError::spread_in_array_init(&s).into())
Err(ParserError::spread_in_array_init(s).into())
});
assert_eq!(count_err(res.unwrap_err().to_string()), 3);
let res: Result<(), _> = Handler::with(|h| {
let s = Span::default();
h.emit_err(ParserError::invalid_import_list(&s).into());
h.emit_err(ParserError::unexpected_eof(&s).into());
h.emit_err(ParserError::invalid_import_list(s).into());
h.emit_err(ParserError::unexpected_eof(s).into());
Ok(())
});
assert_eq!(count_err(res.unwrap_err().to_string()), 2);