From e07e77cf71fc43cc7181cdddd9b91d5489f40cf0 Mon Sep 17 00:00:00 2001 From: Edgar Date: Thu, 16 May 2024 14:24:54 +0100 Subject: [PATCH 1/4] fix signed bug (#603) --- Cargo.lock | 4 ++-- src/libfuncs/cast.rs | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8ef8d4f9..8bbcb31d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1676,7 +1676,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.50", + "syn 2.0.61", ] [[package]] @@ -2973,7 +2973,7 @@ dependencies = [ "regex", "relative-path", "rustc_version", - "syn 2.0.50", + "syn 2.0.61", "unicode-ident", ] diff --git a/src/libfuncs/cast.rs b/src/libfuncs/cast.rs index 8e71d2a47..381376d13 100644 --- a/src/libfuncs/cast.rs +++ b/src/libfuncs/cast.rs @@ -282,8 +282,6 @@ pub fn build_upcast<'ctx, 'this>( let is_signed = src_ty.is_integer_signed().ok_or_else(|| { Error::SierraAssert("casts always happen between numerical types".to_string()) - })? || dst_ty.is_integer_signed().ok_or_else(|| { - Error::SierraAssert("casts always happen between numerical types".to_string()) })?; let is_felt = matches!(dst_ty, CoreTypeConcrete::Felt252(_)); @@ -292,7 +290,7 @@ pub fn build_upcast<'ctx, 'this>( let result = if src_width == dst_width { block.argument(0)?.into() - } else if is_signed { + } else if is_signed || is_felt { if is_felt { let result = block.append_op_result(arith::extsi( block.argument(0)?.into(), From c8cdc898521e858cabac5d1e3f6a6b53342a8efc Mon Sep 17 00:00:00 2001 From: Edgar Date: Thu, 16 May 2024 14:39:21 +0100 Subject: [PATCH 2/4] enable print tests (#598) --- cairo-tests/src/print_test.cairo | 3 --- 1 file changed, 3 deletions(-) diff --git a/cairo-tests/src/print_test.cairo b/cairo-tests/src/print_test.cairo index 161d10a8e..884b5d2f2 100644 --- a/cairo-tests/src/print_test.cairo +++ b/cairo-tests/src/print_test.cairo @@ -1,7 +1,6 @@ use core::byte_array::BYTE_ARRAY_MAGIC; use core::debug::{PrintTrait, print_byte_array_as_string}; -#[ignore] #[test] fn test_prints() { // Valid short string. @@ -102,7 +101,6 @@ fn test_prints() { x.print(); } -#[ignore] #[test] fn test_print_macro() { // With a ByteArray. @@ -116,7 +114,6 @@ fn test_print_macro() { print!("{}", 97_usize); } -#[ignore] #[test] fn test_println_macro() { // With a ByteArray. From b98c1ec12d7784916a9b9ab570a0b22653528e8a Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 16 May 2024 16:05:14 +0200 Subject: [PATCH 3/4] increase test coverage of OptLevel (#592) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: IƱaki Garay --- src/ffi.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 056811d9a..4d6167a3d 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -1,3 +1,4 @@ +use crate::error::Error as CompileError; use llvm_sys::{ core::{ LLVMContextCreate, LLVMContextDispose, LLVMDisposeMemoryBuffer, LLVMDisposeMessage, @@ -30,8 +31,6 @@ use std::{ }; use tempfile::NamedTempFile; -use crate::error::Error as CompileError; - extern "C" { fn LLVMStructType_getFieldTypeAt(ty_ptr: *const c_void, index: u32) -> *const c_void; @@ -327,3 +326,43 @@ pub fn get_data_layout_rep() -> Result { Ok(data_layout_str.to_string_lossy().into_owned()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_opt_level_default() { + // Asserts that the default implementation of `OptLevel` returns `OptLevel::Default`. + assert_eq!(OptLevel::default(), OptLevel::Default); + + // Asserts that converting from usize value 2 returns `OptLevel::Default`. + assert_eq!(OptLevel::from(2usize), OptLevel::Default); + + // Asserts that converting from u8 value 2 returns `OptLevel::Default`. + assert_eq!(OptLevel::from(2u8), OptLevel::Default); + } + + #[test] + fn test_opt_level_conversion() { + // Test conversion from usize to OptLevel + assert_eq!(OptLevel::from(0usize), OptLevel::None); + assert_eq!(OptLevel::from(1usize), OptLevel::Less); + assert_eq!(OptLevel::from(2usize), OptLevel::Default); + assert_eq!(OptLevel::from(3usize), OptLevel::Aggressive); + assert_eq!(OptLevel::from(30usize), OptLevel::Aggressive); + + // Test conversion from OptLevel to usize + assert_eq!(usize::from(OptLevel::None), 0usize); + assert_eq!(usize::from(OptLevel::Less), 1usize); + assert_eq!(usize::from(OptLevel::Default), 2usize); + assert_eq!(usize::from(OptLevel::Aggressive), 3usize); + + // Test conversion from u8 to OptLevel + assert_eq!(OptLevel::from(0u8), OptLevel::None); + assert_eq!(OptLevel::from(1u8), OptLevel::Less); + assert_eq!(OptLevel::from(2u8), OptLevel::Default); + assert_eq!(OptLevel::from(3u8), OptLevel::Aggressive); + assert_eq!(OptLevel::from(30u8), OptLevel::Aggressive); + } +} From d2d9cd1cc11267e136c1309674092315a6c10b4d Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Thu, 16 May 2024 19:39:54 +0200 Subject: [PATCH 4/4] test: debug info module (#587) * refactor + add test * refactor with load_cairo * remove macro --- Cargo.lock | 96 ++++++++++++++++++------------------ Cargo.toml | 6 +-- src/debug_info.rs | 121 +++++++++++++++++++++++++--------------------- 3 files changed, 118 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8bbcb31d7..64775c2f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,7 +306,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.61", + "syn 2.0.63", "which", ] @@ -329,7 +329,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.61", + "syn 2.0.63", "which", ] @@ -577,7 +577,7 @@ checksum = "67f9da66325ce7ed6c002360f26106fe79deb9f8a2fca30abdbb8d388da7bb46" dependencies = [ "cairo-lang-debug", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -1097,7 +1097,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -1414,9 +1414,9 @@ dependencies = [ [[package]] name = "deunicode" -version = "1.4.4" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322ef0094744e63628e6f0eb2295517f79276a5b342a4c2ff3042566ca181d4e" +checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00" [[package]] name = "diff" @@ -1486,7 +1486,7 @@ dependencies = [ "enum-ordinalize", "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -1547,7 +1547,7 @@ checksum = "0d28318a75d4aead5c4db25382e8ef717932d0346600cacae6357eb5941bc5ff" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -1558,9 +1558,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", "windows-sys 0.52.0", @@ -1676,7 +1676,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -1734,7 +1734,7 @@ checksum = "d4cf186fea4af17825116f72932fe52cce9a13bae39ff63b4dc0cfdb3fb4bde1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -2230,7 +2230,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.61", + "syn 2.0.63", "tblgen", "unindent", ] @@ -2708,7 +2708,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -2973,7 +2973,7 @@ dependencies = [ "regex", "relative-path", "rustc_version", - "syn 2.0.61", + "syn 2.0.63", "unicode-ident", ] @@ -3007,9 +3007,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "092474d1a01ea8278f69e6a358998405fae5b8b963ddaeb2b0b04a128bf1dfb0" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" [[package]] name = "rusty-fork" @@ -3089,7 +3089,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -3120,33 +3120,33 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.201" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c" +checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.201" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865" +checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] name = "serde_derive_internals" -version = "0.29.0" +version = "0.29.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "330f01ce65a3a5fe59a60c82f3c9a024b573b8a6e875bd233fe5f934e71d54e3" +checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -3162,9 +3162,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" dependencies = [ "serde", ] @@ -3254,9 +3254,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "smol_str" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6845563ada680337a52d43bb0b29f396f2d911616f6573012645b9e3d048a49" +checksum = "dd538fb6910ac1099850255cf94a94df6551fbdd602454387d0adb2d1ca6dead" dependencies = [ "serde", ] @@ -3316,7 +3316,7 @@ checksum = "bbc159a1934c7be9761c237333a57febe060ace2bc9e3b337a59a37af206d19f" dependencies = [ "starknet-curve", "starknet-ff", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -3397,9 +3397,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.61" +version = "2.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9" +checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704" dependencies = [ "proc-macro2", "quote", @@ -3498,7 +3498,7 @@ dependencies = [ "cfg-if", "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -3509,7 +3509,7 @@ checksum = "5c89e72a01ed4c579669add59014b9a524d609c0c88c6a585ce37485879f6ffb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", "test-case-core", ] @@ -3530,7 +3530,7 @@ checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -3615,21 +3615,21 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.12" +version = "0.8.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +checksum = "a4e43f8cc456c9704c851ae29c67e17ef65d2c30017c17a9765b89c382dc8bba" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.12", + "toml_edit 0.22.13", ] [[package]] name = "toml_datetime" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" dependencies = [ "serde", ] @@ -3647,9 +3647,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.12" +version = "0.22.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef" +checksum = "c127785850e8c20836d49732ae6abfa47616e60bf9d9f57c43c250361a9db96c" dependencies = [ "indexmap 2.2.6", "serde", @@ -3677,7 +3677,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -3846,7 +3846,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", "wasm-bindgen-shared", ] @@ -3868,7 +3868,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4151,7 +4151,7 @@ checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] [[package]] @@ -4171,5 +4171,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.63", ] diff --git a/Cargo.toml b/Cargo.toml index 21236bcdf..2a55a739e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,8 +75,8 @@ tracing-subscriber = { version = "0.3", features = [ ], optional = true } serde = { version = "1.0", features = ["derive"], optional = true } anyhow = { version = "1.0", optional = true } -cairo-lang-test-plugin = { version = "2.6.3", optional = true} -cairo-lang-runner = { version = "2.6.3", optional = true} +cairo-lang-test-plugin = { version = "2.6.3", optional = true } +cairo-lang-runner = { version = "2.6.3", optional = true } colored = { version = "2.1.0", optional = true } # needed to interface with cairo-lang-* cairo-felt = { version = "0.9.1", optional = true } @@ -88,6 +88,7 @@ sec1 = { version = "0.7.3", optional = true } [dev-dependencies] cairo-felt = "0.9.1" cairo-lang-runner = "2.6.3" +cairo-lang-semantic = { version = "2.6.3", features = ["testing"] } criterion = { version = "0.5.1", features = ["html_reports"] } lambdaworks-math = "0.7" pretty_assertions_sorted = "1.2.3" @@ -96,7 +97,6 @@ rstest = "0.19.0" test-case = "3.3" walkdir = "2" serde_json = { version = "1.0" } -cairo-lang-semantic = { version = "2.5.4", features = ["testing"] } [build-dependencies] cc = "1.0.95" diff --git a/src/debug_info.rs b/src/debug_info.rs index 4ccd0d377..7c0e6259f 100644 --- a/src/debug_info.rs +++ b/src/debug_info.rs @@ -160,18 +160,13 @@ fn extract_location_from_stable_loc<'c>( #[cfg(test)] mod test { use super::*; + use crate::{context::NativeContext, utils::test::load_cairo}; use cairo_lang_semantic::test_utils::setup_test_function; - use cairo_lang_sierra::program::ConcreteLibfuncLongId; - use cairo_lang_sierra::program::ConcreteTypeLongId; - use cairo_lang_sierra::program::FunctionSignature; - use cairo_lang_sierra::program::GenFunction; - use cairo_lang_sierra::program::LibfuncDeclaration; - use cairo_lang_sierra::program::StatementIdx; - use cairo_lang_sierra::program::TypeDeclaration; use cairo_lang_sierra_generator::db::SierraGenGroup; + use rstest::*; - #[test] - fn test_extract_debug_locations() { + #[fixture] + fn db() -> RootDatabase { // Build the root database with corelib detection let db = RootDatabase::builder().detect_corelib().build().unwrap(); @@ -183,60 +178,78 @@ mod test { ); let _ = db.function_with_body_sierra(function_id); + db + } + + #[fixture] + fn program() -> Program { // Define a dummy program for testing - let program = Program { - type_declarations: vec![TypeDeclaration { - id: "test_id_type_declarations".into(), - long_id: ConcreteTypeLongId { - generic_id: "u128".into(), - generic_args: vec![], - }, - declared_type_info: None, - }], - libfunc_declarations: vec![LibfuncDeclaration { - id: "test_id_libfunc_declarations".into(), - long_id: ConcreteLibfuncLongId { - generic_id: "u128_sqrt".into(), - generic_args: vec![], - }, - }], - statements: vec![], - funcs: vec![GenFunction { - id: FunctionId { - id: 0, - debug_name: Some("some_name".into()), - }, - signature: FunctionSignature { - ret_types: vec![], - param_types: vec![], - }, - params: vec![], - entry_point: StatementIdx(0), - }], + let (_, program) = load_cairo! { + fn run_test() -> u128 { + let a: u128 = 1; + u128_sqrt(a).into() + } }; + program + } + #[fixture] + fn debug_info(db: RootDatabase, program: Program) -> DebugInfo { // Extract debug information from the program - let res = DebugInfo::extract(&db, &program).unwrap(); + DebugInfo::extract(&db, &program).unwrap() + } - // Assertions to test the extracted debug information - assert!(res.type_declarations.len() == 1); - assert!(res + #[rstest] + fn test_extract_debug_info(debug_info: DebugInfo) { + // Assert the debug information contains u128 + assert!(debug_info .type_declarations - .contains_key(&ConcreteTypeId::from_string("test_id_type_declarations"))); + .iter() + .any(|(k, _)| k.debug_name == Some("u128".into()))); - assert!(res.libfunc_declarations.len() == 1); - assert!(res + // Assert the debug information contains u128_sqrt + assert!(debug_info .libfunc_declarations - .contains_key(&ConcreteLibfuncId::from_string( - "test_id_libfunc_declarations" - ))); + .iter() + .any(|(k, _)| k.debug_name == Some("u128_sqrt".into()))); + + assert!(debug_info.statements.is_empty()); + + // Assert the debug information contains the run_test function + assert!(debug_info.funcs.iter().any(|(k, _)| k + .debug_name + .clone() + .unwrap() + .contains("run_test"))); + } + + #[rstest] + fn test_extract_debug_locations(db: RootDatabase, debug_info: DebugInfo) { + // Get the native context + let native_context = NativeContext::new(); + + // Extract debug locations from the debug information + let debug_locations = DebugLocations::extract(native_context.context(), &db, &debug_info); + + // Assert the debug locations contain u128 + assert!(debug_locations + .type_declarations + .iter() + .any(|(k, _)| k.debug_name == Some("u128".into()))); + + // Assert the debug locations contain u128_sqrt + assert!(debug_locations + .libfunc_declarations + .iter() + .any(|(k, _)| k.debug_name == Some("u128_sqrt".into()))); - assert!(res.statements.is_empty()); + assert!(debug_locations.statements.is_empty()); - assert!(res.funcs.len() == 1); - assert!(res.funcs.contains_key(&FunctionId { - id: 0, - debug_name: Some("some_name".into()), - })); + // Assert the debug locations contain the run_test function + assert!(debug_locations.funcs.iter().any(|(k, _)| k + .debug_name + .clone() + .unwrap() + .contains("run_test"))); } }