mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-28 11:16:49 +03:00
cleaning up duplicate errors by removing canonicalization from asg
This commit is contained in:
parent
003faed13a
commit
c9345bbcc7
@ -1,5 +1,5 @@
|
||||
[hooks]
|
||||
# pre-commit = "cargo clippy && cargo +nightly fmt --all -- --check"
|
||||
pre-commit = "cargo clippy && cargo fmt --all -- --check"
|
||||
|
||||
[logging]
|
||||
verbose = true
|
||||
|
@ -130,11 +130,7 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
|
||||
if let Some(input) = scope.resolve_input() {
|
||||
input.container
|
||||
} else {
|
||||
return Err(AsgError::illegal_input_variable_reference(
|
||||
"attempted to reference input when none is in scope",
|
||||
&value.span,
|
||||
)
|
||||
.into());
|
||||
return Err(AsgError::illegal_input_variable_reference(&value.span).into());
|
||||
}
|
||||
} else {
|
||||
match scope.resolve_variable(&value.name) {
|
||||
|
@ -97,9 +97,6 @@ impl<'a> Function<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if qualifier != FunctionQualifier::Static && scope.circuit_self.get().is_none() {
|
||||
return Err(AsgError::invalid_self_in_global(&value.span).into());
|
||||
}
|
||||
let function = scope.context.alloc_function(Function {
|
||||
id: scope.context.get_id(),
|
||||
name: RefCell::new(value.identifier.clone()),
|
||||
|
@ -128,8 +128,6 @@ impl<'a> Scope<'a> {
|
||||
pub fn resolve_circuit(&self, name: &str) -> Option<&'a Circuit<'a>> {
|
||||
if let Some(resolved) = self.circuits.borrow().get(name) {
|
||||
Some(*resolved)
|
||||
} else if name == "Self" && self.circuit_self.get().is_some() {
|
||||
self.circuit_self.get()
|
||||
} else if let Some(resolved) = self.parent_scope.get() {
|
||||
resolved.resolve_circuit(name)
|
||||
} else {
|
||||
@ -137,22 +135,6 @@ impl<'a> Scope<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a reference to the current circuit.
|
||||
///
|
||||
/// If the current scope did not have a circuit self present, then the parent scope is checked.
|
||||
/// If there is no parent scope, then `None` is returned.
|
||||
///
|
||||
pub fn resolve_circuit_self(&self) -> Option<&'a Circuit<'a>> {
|
||||
if let Some(resolved) = self.circuit_self.get() {
|
||||
Some(resolved)
|
||||
} else if let Some(resolved) = self.parent_scope.get() {
|
||||
resolved.resolve_circuit_self()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a new scope given a parent scope.
|
||||
///
|
||||
@ -200,14 +182,7 @@ impl<'a> Scope<'a> {
|
||||
.map(|x| self.resolve_ast_type(x, span))
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
),
|
||||
Circuit(name) if name.name.as_ref() == "Self" => Type::Circuit(
|
||||
self.resolve_circuit_self()
|
||||
.ok_or_else(|| AsgError::unresolved_circuit(&name.name, &name.span))?,
|
||||
),
|
||||
SelfType => Type::Circuit(
|
||||
self.resolve_circuit_self()
|
||||
.ok_or_else(|| AsgError::reference_self_outside_circuit(span))?,
|
||||
),
|
||||
SelfType => return Err(AsgError::unexpected_big_self(span).into()),
|
||||
Circuit(name) => Type::Circuit(
|
||||
self.resolve_circuit(&name.name)
|
||||
.ok_or_else(|| AsgError::unresolved_circuit(&name.name, &name.span))?,
|
||||
|
@ -63,11 +63,7 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
|
||||
if let Some(input) = scope.resolve_input() {
|
||||
input.container
|
||||
} else {
|
||||
return Err(AsgError::illegal_input_variable_reference(
|
||||
"attempted to reference input when none is in scope",
|
||||
&statement.span,
|
||||
)
|
||||
.into());
|
||||
return Err(AsgError::illegal_input_variable_reference(&statement.span).into());
|
||||
}
|
||||
} else {
|
||||
scope
|
||||
|
0
errors/ERROR_INDEX.md
Normal file
0
errors/ERROR_INDEX.md
Normal file
0
errors/README.md
Normal file
0
errors/README.md
Normal file
@ -19,10 +19,13 @@ use crate::create_errors;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
create_errors!(
|
||||
/// AsgError enum that represents all the errors for the ASG.
|
||||
AsgError,
|
||||
exit_code_mask: 0i32,
|
||||
error_code_prefix: "ASG",
|
||||
|
||||
/// For when a circuit of the specified type is unresolved.
|
||||
/// Note that the type for a circuit is represented by a name.
|
||||
@formatted
|
||||
unresolved_circuit {
|
||||
args: (name: impl Display),
|
||||
@ -30,6 +33,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a import of the specified name is unresolved.
|
||||
@formatted
|
||||
unresolved_import {
|
||||
args: (name: impl Display),
|
||||
@ -37,6 +41,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a circuit member of the specified name is unresolved.
|
||||
@formatted
|
||||
unresolved_circuit_member {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -47,6 +52,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user is initializing a circuit, and it's missing circuit member.
|
||||
@formatted
|
||||
missing_circuit_member {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -57,6 +63,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user is initializing a circuit, and they declare a cirucit member twice.
|
||||
@formatted
|
||||
overridden_circuit_member {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -67,6 +74,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user is defining a circuit, and they define a circuit member multiple times.
|
||||
@formatted
|
||||
redefined_circuit_member {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -77,6 +85,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user is initializing a circuit, and they add an extra circuit member.
|
||||
@formatted
|
||||
extra_circuit_member {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -87,6 +96,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user attempts to assign to a function.
|
||||
@formatted
|
||||
illegal_function_assign {
|
||||
args: (name: impl Display),
|
||||
@ -94,6 +104,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to call a circuit variable as a function.
|
||||
@formatted
|
||||
circuit_variable_call {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -101,6 +112,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to call an invalid circuit static function.
|
||||
@formatted
|
||||
circuit_static_call_invalid {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -111,6 +123,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to call a mutable circuit member function from immutable context.
|
||||
@formatted
|
||||
circuit_member_mut_call_invalid {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -121,6 +134,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to call a circuit member function from static context.
|
||||
@formatted
|
||||
circuit_member_call_invalid {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
@ -131,16 +145,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
@formatted
|
||||
circuit_function_ref {
|
||||
args: (circuit_name: impl Display, name: impl Display),
|
||||
msg: format!(
|
||||
"cannot reference function member '{}' of circuit '{}' as value",
|
||||
name, circuit_name
|
||||
),
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to index into a non-array type.
|
||||
@formatted
|
||||
index_into_non_array {
|
||||
args: (name: impl Display),
|
||||
@ -148,6 +153,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries index with an invalid integer.
|
||||
@formatted
|
||||
invalid_assign_index {
|
||||
args: (name: impl Display, num: impl Display),
|
||||
@ -155,6 +161,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to index an array range, with a left value greater than right value.
|
||||
@formatted
|
||||
invalid_backwards_assignment {
|
||||
args: (name: impl Display, left: impl Display, right: impl Display),
|
||||
@ -165,6 +172,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to create a constant varaible from non constant values.
|
||||
@formatted
|
||||
invalid_const_assign {
|
||||
args: (name: impl Display),
|
||||
@ -175,6 +183,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user defines function with the same name twice.
|
||||
@formatted
|
||||
duplicate_function_definition {
|
||||
args: (name: impl Display),
|
||||
@ -182,6 +191,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user defines variable with the same name twice.
|
||||
@formatted
|
||||
duplicate_variable_definition {
|
||||
args: (name: impl Display),
|
||||
@ -189,6 +199,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to index into a non-tuple type.
|
||||
@formatted
|
||||
index_into_non_tuple {
|
||||
args: (name: impl Display),
|
||||
@ -196,6 +207,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries access a tuple index out of bounds.
|
||||
@formatted
|
||||
tuple_index_out_of_bounds {
|
||||
args: (index: impl Display),
|
||||
@ -203,6 +215,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries access a array index out of bounds.
|
||||
@formatted
|
||||
array_index_out_of_bounds {
|
||||
args: (index: impl Display),
|
||||
@ -210,6 +223,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries have either side of a ternary return different variable types.
|
||||
@formatted
|
||||
ternary_different_types {
|
||||
args: (left: impl Display, right: impl Display),
|
||||
@ -217,6 +231,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when an array size cannot be inferred.
|
||||
@formatted
|
||||
unknown_array_size {
|
||||
args: (),
|
||||
@ -224,6 +239,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user passes more arguements to a function than expected.
|
||||
@formatted
|
||||
unexpected_call_argument_count {
|
||||
args: (expected: impl Display, got: impl Display),
|
||||
@ -231,6 +247,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For whan a function is unresolved.
|
||||
@formatted
|
||||
unresolved_function {
|
||||
args: (name: impl Display),
|
||||
@ -238,6 +255,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a type cannot be resolved.
|
||||
@formatted
|
||||
unresolved_type {
|
||||
args: (name: impl Display),
|
||||
@ -245,6 +263,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user passes a type, but another was expected.
|
||||
@formatted
|
||||
unexpected_type {
|
||||
args: (expected: impl Display, received: impl Display),
|
||||
@ -256,6 +275,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a constant value was expected, but a non-constant one was received.
|
||||
@formatted
|
||||
unexpected_nonconst {
|
||||
args: (),
|
||||
@ -263,6 +283,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For whan a variable is unresolved.
|
||||
@formatted
|
||||
unresolved_reference {
|
||||
args: (name: impl Display),
|
||||
@ -270,6 +291,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a boolean value cannot be parsed.
|
||||
@formatted
|
||||
invalid_boolean {
|
||||
args: (value: impl Display),
|
||||
@ -277,6 +299,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a char value cannot be parsed.
|
||||
@formatted
|
||||
invalid_char {
|
||||
args: (value: impl Display),
|
||||
@ -284,6 +307,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when an int value cannot be parsed.
|
||||
@formatted
|
||||
invalid_int {
|
||||
args: (value: impl Display),
|
||||
@ -291,6 +315,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to negate an unsigned integer.
|
||||
@formatted
|
||||
unsigned_negation {
|
||||
args: (),
|
||||
@ -298,6 +323,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to assign to an immutable variable.
|
||||
@formatted
|
||||
immutable_assignment {
|
||||
args: (name: impl Display),
|
||||
@ -305,6 +331,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a function is missing a return statement.
|
||||
@formatted
|
||||
function_missing_return {
|
||||
args: (name: impl Display),
|
||||
@ -312,6 +339,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a function fails to resolve the correct return.
|
||||
@formatted
|
||||
function_return_validation {
|
||||
args: (name: impl Display, description: impl Display),
|
||||
@ -319,6 +347,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when the type for an input variable could not be infered.
|
||||
@formatted
|
||||
input_ref_needs_type {
|
||||
args: (category: impl Display, name: impl Display),
|
||||
@ -326,13 +355,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
@formatted
|
||||
invalid_self_in_global {
|
||||
args: (),
|
||||
msg: "cannot have `mut self` or `self` arguments in global functions",
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to call a test function.
|
||||
@formatted
|
||||
call_test_function {
|
||||
args: (),
|
||||
@ -340,6 +363,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to define a circuit function as a test function.
|
||||
@formatted
|
||||
circuit_test_function {
|
||||
args: (),
|
||||
@ -347,6 +371,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// Failed to parse index.
|
||||
@formatted
|
||||
parse_index_error {
|
||||
args: (),
|
||||
@ -354,6 +379,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// Failed to parse array dimensions.
|
||||
@formatted
|
||||
parse_dimension_error {
|
||||
args: (),
|
||||
@ -361,13 +387,7 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
@formatted
|
||||
reference_self_outside_circuit {
|
||||
args: (),
|
||||
msg: "referenced self outside of circuit function",
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when there is an illegal ast structure.
|
||||
@formatted
|
||||
illegal_ast_structure {
|
||||
args: (details: impl Display),
|
||||
@ -375,10 +395,20 @@ create_errors!(
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user tries to reference an input varaible but none is in scope.
|
||||
@formatted
|
||||
illegal_input_variable_reference {
|
||||
args: (details: impl Display),
|
||||
msg: format!("illegal ast structure: {}", details),
|
||||
args: (),
|
||||
msg: "attempted to reference input when none is in scope",
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For the ASG receives an big Self, which should never happen
|
||||
/// as they should be resolved in an earlier compiler phase.
|
||||
@formatted
|
||||
unexpected_big_self {
|
||||
args: (),
|
||||
msg: "received a Self statement, which should never happen",
|
||||
help: None,
|
||||
}
|
||||
);
|
||||
|
@ -17,13 +17,14 @@
|
||||
#[macro_export]
|
||||
macro_rules! create_errors {
|
||||
(@step $_code:expr,) => {};
|
||||
($error_type:ident, exit_code_mask: $exit_code_mask:expr, error_code_prefix: $error_code_prefix:expr, $(@$formatted_or_backtraced_list:ident $names:ident { args: ($($arg_names:ident: $arg_types:ty$(,)?)*), msg: $messages:expr, help: $helps:expr, })*) => {
|
||||
($(#[$error_type_docs:meta])* $error_type:ident, exit_code_mask: $exit_code_mask:expr, error_code_prefix: $error_code_prefix:expr, $($(#[$docs:meta])* @$formatted_or_backtraced_list:ident $names:ident { args: ($($arg_names:ident: $arg_types:ty$(,)?)*), msg: $messages:expr, help: $helps:expr, })*) => {
|
||||
#[allow(unused_imports)]
|
||||
use crate::{BacktracedError, ErrorCode, FormattedError, LeoErrorCode, Span};
|
||||
|
||||
use backtrace::Backtrace;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
$(#[$error_type_docs])*
|
||||
pub enum $error_type {
|
||||
#[error(transparent)]
|
||||
FormattedError(#[from] FormattedError),
|
||||
@ -60,10 +61,11 @@ macro_rules! create_errors {
|
||||
}
|
||||
|
||||
impl $error_type {
|
||||
create_errors!(@step 0i32, $(($formatted_or_backtraced_list, $names($($arg_names: $arg_types,)*), $messages, $helps),)*);
|
||||
create_errors!(@step 0i32, $(($(#[$docs])* $formatted_or_backtraced_list, $names($($arg_names: $arg_types,)*), $messages, $helps),)*);
|
||||
}
|
||||
};
|
||||
(@step $code:expr, (formatted, $error_name:ident($($arg_names:ident: $arg_types:ty,)*), $message:expr, $help:expr), $(($formatted_or_backtraced_tail:ident, $names:ident($($tail_arg_names:ident: $tail_arg_types:ty,)*), $messages:expr, $helps:expr),)*) => {
|
||||
(@step $code:expr, ($(#[$error_func_docs:meta])* formatted, $error_name:ident($($arg_names:ident: $arg_types:ty,)*), $message:expr, $help:expr), $(($(#[$docs:meta])* $formatted_or_backtraced_tail:ident, $names:ident($($tail_arg_names:ident: $tail_arg_types:ty,)*), $messages:expr, $helps:expr),)*) => {
|
||||
$(#[$error_func_docs])*
|
||||
pub fn $error_name($($arg_names: $arg_types,)* span: &Span) -> Self {
|
||||
Self::FormattedError(
|
||||
FormattedError::new_from_span(
|
||||
@ -78,9 +80,10 @@ macro_rules! create_errors {
|
||||
)
|
||||
}
|
||||
|
||||
create_errors!(@step $code + 1i32, $(($formatted_or_backtraced_tail, $names($($tail_arg_names: $tail_arg_types,)*), $messages, $helps),)*);
|
||||
create_errors!(@step $code + 1i32, $(($(#[$docs])* $formatted_or_backtraced_tail, $names($($tail_arg_names: $tail_arg_types,)*), $messages, $helps),)*);
|
||||
};
|
||||
(@step $code:expr, (backtraced, $error_name:ident($($arg_names:ident: $arg_types:ty,)*), $message:expr, $help:expr), $(($formatted_or_backtraced_tail:ident, $names:ident($($tail_arg_names:ident: $tail_arg_types:ty,)*), $messages:expr, $helps:expr),)*) => {
|
||||
(@step $code:expr, ($(#[$error_func_docs:meta])* backtraced, $error_name:ident($($arg_names:ident: $arg_types:ty,)*), $message:expr, $help:expr), $(($(#[$docs:meta])* $formatted_or_backtraced_tail:ident, $names:ident($($tail_arg_names:ident: $tail_arg_types:ty,)*), $messages:expr, $helps:expr),)*) => {
|
||||
$(#[$error_func_docs])*
|
||||
pub fn $error_name($($arg_names: $arg_types,)*) -> Self {
|
||||
Self::BacktracedError(
|
||||
BacktracedError::new_from_backtrace(
|
||||
@ -94,7 +97,7 @@ macro_rules! create_errors {
|
||||
)
|
||||
}
|
||||
|
||||
create_errors!(@step $code + 1i32, $(($formatted_or_backtraced_tail, $names($($tail_arg_names: $tail_arg_types,)*), $messages, $helps),)*);
|
||||
create_errors!(@step $code + 1i32, $(($(#[$docs])* $formatted_or_backtraced_tail, $names($($tail_arg_names: $tail_arg_types,)*), $messages, $helps),)*);
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user