Skip to content

Commit

Permalink
Added failure for bad types in building an executable.
Browse files Browse the repository at this point in the history
commit-id:db822d43
  • Loading branch information
orizi committed Jan 23, 2025
1 parent 8739103 commit b25d7ff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
30 changes: 24 additions & 6 deletions crates/cairo-lang-executable/src/compile_test_data/basic
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CompileExecutableTestRunner(expect_diagnostics: false)
#[executable]
fn main() {}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -57,7 +57,7 @@ fn main(a: felt252, b: felt252) -> felt252 {
a + b
}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -143,7 +143,7 @@ fn fib(a: u128, b: u128, n: u128) -> u128 {
}
}

//! > generated_casm_code
//! > result
# builtins: output, range_check
# header #
[ap + 0] = [fp + -3], ap++;
Expand Down Expand Up @@ -380,7 +380,7 @@ CompileExecutableTestRunner(expect_diagnostics: false)
#[executable_raw]
fn main(mut _input: Span<felt252>, ref _output: Array<felt252>) {}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -417,7 +417,7 @@ fn require_gas() -> felt252 {
}
}

//! > generated_casm_code
//! > result
# builtins: output
# header #
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
Expand Down Expand Up @@ -469,7 +469,7 @@ fn use_dict() {
let _dict: Felt252Dict<u8> = Default::default();
}

//! > generated_casm_code
//! > result
# builtins: output, range_check
# header #
ap += 2;
Expand Down Expand Up @@ -782,3 +782,21 @@ ret;
ret;

//! > expected_diagnostics

//! > ==========================================================================

//! > Test use System implicit.

//! > test_runner_name
CompileExecutableTestRunner(expect_diagnostics: false)

//! > cairo_code
#[executable]
fn use_system() {
core::keccak::compute_keccak_byte_array(@"not allowed without starknet.");
}

//! > result
Emulated builtin `System` not allowed for executable.

//! > expected_diagnostics
5 changes: 3 additions & 2 deletions crates/cairo-lang-executable/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ impl TestFileRunner for CompileExecutableTestRunner {
vec![test_module.crate_id],
DiagnosticsReporter::stderr(),
)
.unwrap();
.map(|compiled| compiled.to_string())
.unwrap_or_else(|e| e.to_string());
let error = verify_diagnostics_expectation(args, &semantic_diagnostics);
TestRunnerResult {
outputs: OrderedHashMap::from([
("generated_casm_code".into(), result.to_string()),
("result".into(), result),
("expected_diagnostics".into(), semantic_diagnostics),
]),
error,
Expand Down
6 changes: 6 additions & 0 deletions crates/cairo-lang-runnable-utils/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub enum BuildError {
SierraCompilationError(#[from] Box<CompilationError>),
#[error(transparent)]
ApChangeError(#[from] ApChangeError),
#[error("Emulated builtin `{0}` not allowed for executable.")]
DisallowedBuiltinForExecutable(GenericTypeId),
}

impl BuildError {
Expand Down Expand Up @@ -369,6 +371,10 @@ pub fn create_entry_code_from_params(
let var = builtin_vars[&name];
casm_build_extend!(ctx, tempvar _builtin = var;);
} else if emulated_builtins.contains(generic_ty) {
if config.outputting_function {
// Emulated builtins are not supported when compiling an executable.
return Err(BuildError::DisallowedBuiltinForExecutable(generic_ty.clone()));
}
casm_build_extend! {ctx,
tempvar system;
hint AllocSegment into {dst: system};
Expand Down

0 comments on commit b25d7ff

Please sign in to comment.