Skip to content

Commit a06740f

Browse files
Add high-level locations to sierra error.
1 parent ca1fe0c commit a06740f

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

crates/cairo-lang-executable/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ description = "Cairo executable artifact."
1010
anyhow.workspace = true
1111
cairo-lang-casm = { path = "../cairo-lang-casm", version = "~2.9.2", default-features = true, features = ["serde"] }
1212
cairo-lang-compiler = { path = "../cairo-lang-compiler", version = "~2.9.2" }
13+
cairo-lang-debug = { path = "../cairo-lang-debug", version = "~2.9.2" }
1314
cairo-lang-defs = { path = "../cairo-lang-defs", version = "~2.9.2" }
1415
cairo-lang-filesystem = { path = "../cairo-lang-filesystem", version = "~2.9.2" }
1516
cairo-lang-lowering = { path = "../cairo-lang-lowering", version = "~2.9.2" }

crates/cairo-lang-executable/src/compile.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use anyhow::{Context, Result};
55
use cairo_lang_compiler::db::RootDatabase;
66
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
77
use cairo_lang_compiler::project::setup_project;
8+
use cairo_lang_debug::debug::DebugWithDb;
89
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
910
use cairo_lang_filesystem::ids::CrateId;
1011
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
@@ -141,13 +142,30 @@ pub fn compile_executable_function_in_prepared_db(
141142
mut diagnostics_reporter: DiagnosticsReporter<'_>,
142143
) -> Result<CompiledFunction> {
143144
diagnostics_reporter.ensure(db)?;
144-
let SierraProgramWithDebug { program: sierra_program, debug_info: _ } = Arc::unwrap_or_clone(
145+
let SierraProgramWithDebug { program: sierra_program, debug_info } = Arc::unwrap_or_clone(
145146
db.get_sierra_program_for_functions(vec![executable])
146147
.ok()
147148
.with_context(|| "Compilation failed without any diagnostics.")?,
148149
);
150+
149151
let executable_func = sierra_program.funcs[0].clone();
150-
let builder = RunnableBuilder::new(sierra_program, None)?;
152+
let builder = RunnableBuilder::new(sierra_program, None).map_err(|err| {
153+
let mut locs = vec![];
154+
for stmt_idx in err.stmt_indices() {
155+
if let Some(loc) = debug_info
156+
.statements_locations
157+
.locations
158+
.get(&stmt_idx)
159+
.and_then(|stmt_locs| stmt_locs.first())
160+
{
161+
locs.push(format!("#{stmt_idx} {:?}", loc.diagnostic_location(db).debug(db)))
162+
}
163+
}
164+
165+
anyhow::anyhow!("Failed to create runnable builder: {}\n{}", err, locs.join("\n"))
166+
}
167+
)?;
168+
151169
let wrapper = builder.create_wrapper_info(&executable_func, EntryCodeConfig::executable())?;
152170
Ok(CompiledFunction { program: builder.casm_program().clone(), wrapper })
153171
}

crates/cairo-lang-runnable-utils/src/builder.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use cairo_lang_sierra::extensions::segment_arena::SegmentArenaType;
1616
use cairo_lang_sierra::extensions::starknet::syscalls::SystemType;
1717
use cairo_lang_sierra::extensions::{ConcreteType, NamedType};
1818
use cairo_lang_sierra::ids::{ConcreteTypeId, GenericTypeId};
19-
use cairo_lang_sierra::program::{ConcreteTypeLongId, Function, Program as SierraProgram};
19+
use cairo_lang_sierra::program::{
20+
ConcreteTypeLongId, Function, Program as SierraProgram, StatementIdx,
21+
};
2022
use cairo_lang_sierra::program_registry::{ProgramRegistry, ProgramRegistryError};
2123
use cairo_lang_sierra_ap_change::ApChangeError;
2224
use cairo_lang_sierra_gas::CostError;
@@ -50,6 +52,15 @@ pub enum BuildError {
5052
ApChangeError(#[from] ApChangeError),
5153
}
5254

55+
impl BuildError {
56+
pub fn stmt_indices(&self) -> Vec<StatementIdx> {
57+
match self {
58+
BuildError::SierraCompilationError(err) => err.stmt_indices(),
59+
_ => vec![],
60+
}
61+
}
62+
}
63+
5364
/// Builder for creating a runnable CASM program from Sierra code.
5465
pub struct RunnableBuilder {
5566
/// The sierra program.

crates/cairo-lang-sierra-to-casm/src/annotations.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cairo_lang_sierra::ids::{ConcreteTypeId, FunctionId, VarId};
66
use cairo_lang_sierra::program::{BranchInfo, Function, StatementIdx};
77
use cairo_lang_sierra_type_size::TypeSizeMap;
88
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
9-
use itertools::zip_eq;
9+
use itertools::{chain, zip_eq};
1010
use thiserror::Error;
1111

1212
use crate::environment::ap_tracking::update_ap_tracking;
@@ -93,6 +93,26 @@ pub enum AnnotationError {
9393
},
9494
}
9595

96+
impl AnnotationError {
97+
pub fn stmt_indices(&self) -> Vec<StatementIdx> {
98+
match self {
99+
AnnotationError::ApChangeError {
100+
source_statement_idx,
101+
destination_statement_idx,
102+
introduction_point,
103+
..
104+
} => chain!(
105+
[source_statement_idx, destination_statement_idx],
106+
&introduction_point.source_statement_idx,
107+
[&introduction_point.destination_statement_idx]
108+
)
109+
.cloned()
110+
.collect(),
111+
_ => vec![],
112+
}
113+
}
114+
}
115+
96116
/// Error representing an inconsistency in the references annotations.
97117
#[derive(Error, Debug, Eq, PartialEq)]
98118
pub enum InconsistentReferenceError {

crates/cairo-lang-sierra-to-casm/src/compiler.rs

+9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ pub enum CompilationError {
8585
MetadataNegativeGasVariable,
8686
}
8787

88+
impl CompilationError {
89+
pub fn stmt_indices(&self) -> Vec<StatementIdx> {
90+
match self {
91+
CompilationError::AnnotationError(err) => err.stmt_indices(),
92+
_ => vec![],
93+
}
94+
}
95+
}
96+
8897
/// Configuration for the Sierra to CASM compilation.
8998
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
9099
pub struct SierraToCasmConfig {

0 commit comments

Comments
 (0)