-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy patherrors.rs
More file actions
238 lines (219 loc) · 8.78 KB
/
errors.rs
File metadata and controls
238 lines (219 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::collections::BTreeMap;
use acvm::{
acir::circuit::{
ErrorSelector, OpcodeLocation, RawAssertionPayload, ResolvedAssertionPayload,
ResolvedOpcodeLocation,
},
pwg::{ErrorLocation, OpcodeResolutionError},
AcirField, FieldElement,
};
use noirc_abi::{display_abi_error, Abi, AbiErrorType};
use noirc_errors::{
debug_info::DebugInfo, reporter::ReportedErrors, CustomDiagnostic, FileDiagnostic,
};
pub use noirc_errors::Location;
use noirc_frontend::graph::CrateName;
use noirc_printable_type::ForeignCallError;
use thiserror::Error;
/// Errors covering situations where a package cannot be compiled.
#[derive(Debug, Error)]
pub enum CompileError {
#[error("Package `{0}` has type `lib` but only `bin` types can be compiled")]
LibraryCrate(CrateName),
#[error("Package `{0}` is expected to have a `main` function but it does not")]
MissingMainFunction(CrateName),
/// Errors encountered while compiling the Noir program.
/// These errors are already written to stderr.
#[error("Aborting due to {} previous error{}", .0.error_count, if .0.error_count == 1 { "" } else { "s" })]
ReportedErrors(ReportedErrors),
}
impl From<ReportedErrors> for CompileError {
fn from(errors: ReportedErrors) -> Self {
Self::ReportedErrors(errors)
}
}
#[derive(Debug, Error)]
pub enum NargoError<F: AcirField> {
/// Error while compiling Noir into ACIR.
#[error("Failed to compile circuit")]
CompilationError,
/// ACIR circuit execution error
#[error(transparent)]
ExecutionError(#[from] ExecutionError<F>),
/// Oracle handling error
#[error(transparent)]
ForeignCallError(#[from] ForeignCallError),
}
impl<F: AcirField> NargoError<F> {
/// Extracts the user defined failure message from the ExecutionError
/// If one exists.
///
/// We want to extract the user defined error so that we can compare it
/// in tests to expected failure messages
pub fn user_defined_failure_message(
&self,
error_types: &BTreeMap<ErrorSelector, AbiErrorType>,
) -> Option<String> {
match self {
NargoError::ExecutionError(error) => match error {
ExecutionError::AssertionFailed(payload, _) => match payload {
ResolvedAssertionPayload::String(message) => Some(message.to_string()),
ResolvedAssertionPayload::Raw(raw) => {
let abi_type = error_types.get(&raw.selector)?;
let decoded = display_abi_error(&raw.data, abi_type.clone());
Some(decoded.to_string())
}
},
ExecutionError::SolvingError(error, _) => match error {
OpcodeResolutionError::BlackBoxFunctionFailed(_, reason) => {
Some(reason.to_string())
}
_ => None,
},
},
NargoError::ForeignCallError(error) => Some(error.to_string()),
_ => None,
}
}
}
#[derive(Debug, Error)]
pub enum ExecutionError<F: AcirField> {
#[error("Failed assertion")]
AssertionFailed(ResolvedAssertionPayload<F>, Vec<ResolvedOpcodeLocation>),
#[error("Failed to solve program: '{}'", .0)]
SolvingError(OpcodeResolutionError<F>, Option<Vec<ResolvedOpcodeLocation>>),
}
/// Extracts the opcode locations from a nargo error.
fn extract_locations_from_error<F: AcirField>(
error: &ExecutionError<F>,
debug: &[DebugInfo],
) -> Option<Vec<Location>> {
let mut opcode_locations = match error {
ExecutionError::SolvingError(
OpcodeResolutionError::BrilligFunctionFailed { .. },
acir_call_stack,
) => acir_call_stack.clone(),
ExecutionError::AssertionFailed(_, call_stack) => Some(call_stack.clone()),
ExecutionError::SolvingError(
OpcodeResolutionError::IndexOutOfBounds { opcode_location: error_location, .. },
acir_call_stack,
)
| ExecutionError::SolvingError(
OpcodeResolutionError::UnsatisfiedConstrain { opcode_location: error_location, .. },
acir_call_stack,
) => match error_location {
ErrorLocation::Unresolved => {
unreachable!("Cannot resolve index for unsatisfied constraint")
}
ErrorLocation::Resolved(_) => acir_call_stack.clone(),
},
_ => None,
}?;
// Insert the top-level Acir location where the Brillig function failed
for (i, resolved_location) in opcode_locations.iter().enumerate() {
if let ResolvedOpcodeLocation {
acir_function_index,
opcode_location: OpcodeLocation::Brillig { acir_index, .. },
} = resolved_location
{
let acir_location = ResolvedOpcodeLocation {
acir_function_index: *acir_function_index,
opcode_location: OpcodeLocation::Acir(*acir_index),
};
opcode_locations.insert(i, acir_location);
// Go until the first brillig opcode as that means we have the start of a Brillig call stack.
// We have to loop through the opcode locations in case we had ACIR calls
// before the brillig function failure.
break;
}
}
let brillig_function_id = match error {
ExecutionError::SolvingError(
OpcodeResolutionError::BrilligFunctionFailed { function_id, .. },
_,
) => Some(*function_id),
_ => None,
};
Some(
opcode_locations
.iter()
.flat_map(|resolved_location| {
debug[resolved_location.acir_function_index]
.opcode_location(&resolved_location.opcode_location)
.unwrap_or_else(|| {
if let (Some(brillig_function_id), Some(brillig_location)) = (
brillig_function_id,
&resolved_location.opcode_location.to_brillig_location(),
) {
let brillig_locations = debug[resolved_location.acir_function_index]
.brillig_locations
.get(&brillig_function_id);
brillig_locations
.unwrap()
.get(brillig_location)
.cloned()
.unwrap_or_default()
} else {
vec![]
}
})
})
.collect(),
)
}
fn extract_message_from_error(
error_types: &BTreeMap<ErrorSelector, AbiErrorType>,
nargo_err: &NargoError<FieldElement>,
) -> String {
match nargo_err {
NargoError::ExecutionError(ExecutionError::AssertionFailed(
ResolvedAssertionPayload::String(message),
_,
)) => {
format!("Assertion failed: '{message}'")
}
NargoError::ExecutionError(ExecutionError::AssertionFailed(
ResolvedAssertionPayload::Raw(RawAssertionPayload { selector, data }),
..,
)) => {
if let Some(error_type) = error_types.get(selector) {
format!("Assertion failed: {}", display_abi_error(data, error_type.clone()))
} else {
"Assertion failed".to_string()
}
}
NargoError::ExecutionError(ExecutionError::SolvingError(
OpcodeResolutionError::IndexOutOfBounds { index, array_size, .. },
_,
)) => {
format!("Index out of bounds, array has size {array_size:?}, but index was {index:?}")
}
NargoError::ExecutionError(ExecutionError::SolvingError(
OpcodeResolutionError::UnsatisfiedConstrain { .. },
_,
)) => "Failed constraint".into(),
_ => nargo_err.to_string(),
}
}
/// Tries to generate a runtime diagnostic from a nargo error. It will successfully do so if it's a runtime error with a call stack.
pub fn try_to_diagnose_runtime_error(
nargo_err: &NargoError<FieldElement>,
abi: &Abi,
debug: &[DebugInfo],
) -> Option<FileDiagnostic> {
let source_locations = match nargo_err {
NargoError::ExecutionError(execution_error) => {
extract_locations_from_error(execution_error, debug)?
}
_ => return None,
};
// The location of the error itself will be the location at the top
// of the call stack (the last item in the Vec).
let location = source_locations.last()?;
let message = extract_message_from_error(&abi.error_types, nargo_err);
Some(
CustomDiagnostic::simple_error(message, String::new(), location.span)
.in_file(location.file)
.with_call_stack(source_locations),
)
}