Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add high-level locations to sierra error. #7074

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/cairo-lang-executable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Cairo executable artifact."
anyhow.workspace = true
cairo-lang-casm = { path = "../cairo-lang-casm", version = "~2.9.2", default-features = true, features = ["serde"] }
cairo-lang-compiler = { path = "../cairo-lang-compiler", version = "~2.9.2" }
cairo-lang-debug = { path = "../cairo-lang-debug", version = "~2.9.2" }
cairo-lang-defs = { path = "../cairo-lang-defs", version = "~2.9.2" }
cairo-lang-filesystem = { path = "../cairo-lang-filesystem", version = "~2.9.2" }
cairo-lang-lowering = { path = "../cairo-lang-lowering", version = "~2.9.2" }
Expand Down
21 changes: 19 additions & 2 deletions crates/cairo-lang-executable/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::{Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_compiler::project::setup_project;
use cairo_lang_debug::debug::DebugWithDb;
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
Expand Down Expand Up @@ -141,13 +142,29 @@ pub fn compile_executable_function_in_prepared_db(
mut diagnostics_reporter: DiagnosticsReporter<'_>,
) -> Result<CompiledFunction> {
diagnostics_reporter.ensure(db)?;
let SierraProgramWithDebug { program: sierra_program, debug_info: _ } = Arc::unwrap_or_clone(
let SierraProgramWithDebug { program: sierra_program, debug_info } = Arc::unwrap_or_clone(
db.get_sierra_program_for_functions(vec![executable])
.ok()
.with_context(|| "Compilation failed without any diagnostics.")?,
);

let executable_func = sierra_program.funcs[0].clone();
let builder = RunnableBuilder::new(sierra_program, None)?;
let builder = RunnableBuilder::new(sierra_program, None).map_err(|err| {
let mut locs = vec![];
for stmt_idx in err.stmt_indices() {
if let Some(loc) = debug_info
.statements_locations
.locations
.get(&stmt_idx)
.and_then(|stmt_locs| stmt_locs.first())
{
locs.push(format!("#{stmt_idx} {:?}", loc.diagnostic_location(db).debug(db)))
}
}

anyhow::anyhow!("Failed to create runnable builder: {}\n{}", err, locs.join("\n"))
})?;

let wrapper = builder.create_wrapper_info(&executable_func, EntryCodeConfig::executable())?;
Ok(CompiledFunction { program: builder.casm_program().clone(), wrapper })
}
13 changes: 12 additions & 1 deletion crates/cairo-lang-runnable-utils/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use cairo_lang_sierra::extensions::segment_arena::SegmentArenaType;
use cairo_lang_sierra::extensions::starknet::syscalls::SystemType;
use cairo_lang_sierra::extensions::{ConcreteType, NamedType};
use cairo_lang_sierra::ids::{ConcreteTypeId, GenericTypeId};
use cairo_lang_sierra::program::{ConcreteTypeLongId, Function, Program as SierraProgram};
use cairo_lang_sierra::program::{
ConcreteTypeLongId, Function, Program as SierraProgram, StatementIdx,
};
use cairo_lang_sierra::program_registry::{ProgramRegistry, ProgramRegistryError};
use cairo_lang_sierra_ap_change::ApChangeError;
use cairo_lang_sierra_gas::CostError;
Expand Down Expand Up @@ -50,6 +52,15 @@ pub enum BuildError {
ApChangeError(#[from] ApChangeError),
}

impl BuildError {
pub fn stmt_indices(&self) -> Vec<StatementIdx> {
match self {
BuildError::SierraCompilationError(err) => err.stmt_indices(),
_ => vec![],
}
}
}

/// Builder for creating a runnable CASM program from Sierra code.
pub struct RunnableBuilder {
/// The sierra program.
Expand Down
22 changes: 21 additions & 1 deletion crates/cairo-lang-sierra-to-casm/src/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cairo_lang_sierra::ids::{ConcreteTypeId, FunctionId, VarId};
use cairo_lang_sierra::program::{BranchInfo, Function, StatementIdx};
use cairo_lang_sierra_type_size::TypeSizeMap;
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
use itertools::zip_eq;
use itertools::{chain, zip_eq};
use thiserror::Error;

use crate::environment::ap_tracking::update_ap_tracking;
Expand Down Expand Up @@ -93,6 +93,26 @@ pub enum AnnotationError {
},
}

impl AnnotationError {
pub fn stmt_indices(&self) -> Vec<StatementIdx> {
match self {
AnnotationError::ApChangeError {
source_statement_idx,
destination_statement_idx,
introduction_point,
..
} => chain!(
[source_statement_idx, destination_statement_idx],
&introduction_point.source_statement_idx,
[&introduction_point.destination_statement_idx]
)
.cloned()
.collect(),
_ => vec![],
}
}
}

/// Error representing an inconsistency in the references annotations.
#[derive(Error, Debug, Eq, PartialEq)]
pub enum InconsistentReferenceError {
Expand Down
9 changes: 9 additions & 0 deletions crates/cairo-lang-sierra-to-casm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ pub enum CompilationError {
MetadataNegativeGasVariable,
}

impl CompilationError {
pub fn stmt_indices(&self) -> Vec<StatementIdx> {
match self {
CompilationError::AnnotationError(err) => err.stmt_indices(),
_ => vec![],
}
}
}

/// Configuration for the Sierra to CASM compilation.
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub struct SierraToCasmConfig {
Expand Down
Loading