From 06721685027bf71289ae464e5f6b9b3191c03e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Str=C3=B6m?= Date: Tue, 22 Oct 2024 10:26:57 +0000 Subject: [PATCH 1/4] Workaround for index-of --- clar2wasm/src/words/equal.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/clar2wasm/src/words/equal.rs b/clar2wasm/src/words/equal.rs index 64003952..22df20e6 100644 --- a/clar2wasm/src/words/equal.rs +++ b/clar2wasm/src/words/equal.rs @@ -113,8 +113,16 @@ impl ComplexWord for IndexOf { _expr: &SymbolicExpression, args: &[SymbolicExpression], ) -> Result<(), GeneratorError> { - // Traverse the sequence, leaving its offset and size on the stack. let seq = args.get_expr(0)?; + // workaround to fix types in the case of elements that are themself Sequences + if let TypeSignature::SequenceType(SequenceSubtype::ListType(ltd)) = + generator.get_expr_type(seq).cloned().unwrap() + { + let (elem_ty, _) = ltd.destruct(); + generator.set_expr_type(&args[1], elem_ty)?; + } + + // Traverse the sequence, leaving its offset and size on the stack. generator.traverse_expr(builder, seq)?; // STACK: [offset, size] @@ -993,10 +1001,11 @@ fn wasm_equal_list( #[cfg(test)] mod tests { + use crate::{ClarityVersion, StacksEpochId}; use clarity::vm::types::{ListData, ListTypeData, SequenceData}; use clarity::vm::Value; - use crate::tools::{crosscheck, TestEnvironment}; + use crate::tools::{crosscheck, evaluate_at, TestEnvironment}; #[test] fn index_of_list_not_present() { @@ -1254,6 +1263,17 @@ mod tests { crosscheck(snippet, Ok(Some(clarity::vm::Value::Bool(true)))); } + #[test] + fn index_of_complex_type_versions() { + let snippet = "(index-of + (list (list (err 7)) + (list (ok 3))) + (list (err 7)))"; + let v1 = evaluate_at(snippet, StacksEpochId::latest(), ClarityVersion::Clarity1); + let v2 = evaluate_at(snippet, StacksEpochId::latest(), ClarityVersion::Clarity2); + assert_eq!(v1, v2); + } + // // Module with tests that should only be executed // when running Clarity::V2 or Clarity::v3. From 12fbbbba68a2baaa0d16403cf9d9328a53b0a8ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Str=C3=B6m?= Date: Tue, 22 Oct 2024 11:36:30 +0000 Subject: [PATCH 2/4] Better error handling --- clar2wasm/src/words/equal.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/clar2wasm/src/words/equal.rs b/clar2wasm/src/words/equal.rs index 22df20e6..b434ad74 100644 --- a/clar2wasm/src/words/equal.rs +++ b/clar2wasm/src/words/equal.rs @@ -114,12 +114,16 @@ impl ComplexWord for IndexOf { args: &[SymbolicExpression], ) -> Result<(), GeneratorError> { let seq = args.get_expr(0)?; + let elem_expr = args.get_expr(1)?; // workaround to fix types in the case of elements that are themself Sequences - if let TypeSignature::SequenceType(SequenceSubtype::ListType(ltd)) = - generator.get_expr_type(seq).cloned().unwrap() + if let TypeSignature::SequenceType(SequenceSubtype::ListType(ltd)) = generator + .get_expr_type(seq) + .ok_or(GeneratorError::TypeError( + "index_of element must be typed".to_owned(), + ))? { - let (elem_ty, _) = ltd.destruct(); - generator.set_expr_type(&args[1], elem_ty)?; + let elem_ty = ltd.get_list_item_type().clone(); + generator.set_expr_type(&elem_expr, elem_ty)?; } // Traverse the sequence, leaving its offset and size on the stack. From fb8687525215ec61902ec25b8df3cc06106469a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Str=C3=B6m?= Date: Tue, 22 Oct 2024 11:46:07 +0000 Subject: [PATCH 3/4] chore: clippy/formating --- clar2wasm/src/words/equal.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clar2wasm/src/words/equal.rs b/clar2wasm/src/words/equal.rs index b434ad74..8ba18ffd 100644 --- a/clar2wasm/src/words/equal.rs +++ b/clar2wasm/src/words/equal.rs @@ -122,8 +122,7 @@ impl ComplexWord for IndexOf { "index_of element must be typed".to_owned(), ))? { - let elem_ty = ltd.get_list_item_type().clone(); - generator.set_expr_type(&elem_expr, elem_ty)?; + generator.set_expr_type(elem_expr, ltd.get_list_item_type().clone())?; } // Traverse the sequence, leaving its offset and size on the stack. @@ -1005,11 +1004,11 @@ fn wasm_equal_list( #[cfg(test)] mod tests { - use crate::{ClarityVersion, StacksEpochId}; use clarity::vm::types::{ListData, ListTypeData, SequenceData}; use clarity::vm::Value; use crate::tools::{crosscheck, evaluate_at, TestEnvironment}; + use crate::{ClarityVersion, StacksEpochId}; #[test] fn index_of_list_not_present() { From e5788ce13c13535093db91559e815a5facbd6e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Str=C3=B6m?= Date: Tue, 22 Oct 2024 13:52:13 +0000 Subject: [PATCH 4/4] Remove cross-version test, re-enable test for v1 --- clar2wasm/src/words/equal.rs | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/clar2wasm/src/words/equal.rs b/clar2wasm/src/words/equal.rs index 8ba18ffd..4bf8bcdd 100644 --- a/clar2wasm/src/words/equal.rs +++ b/clar2wasm/src/words/equal.rs @@ -1007,8 +1007,7 @@ mod tests { use clarity::vm::types::{ListData, ListTypeData, SequenceData}; use clarity::vm::Value; - use crate::tools::{crosscheck, evaluate_at, TestEnvironment}; - use crate::{ClarityVersion, StacksEpochId}; + use crate::tools::{crosscheck, TestEnvironment}; #[test] fn index_of_list_not_present() { @@ -1267,14 +1266,11 @@ mod tests { } #[test] - fn index_of_complex_type_versions() { - let snippet = "(index-of - (list (list (err 7)) - (list (ok 3))) - (list (err 7)))"; - let v1 = evaluate_at(snippet, StacksEpochId::latest(), ClarityVersion::Clarity1); - let v2 = evaluate_at(snippet, StacksEpochId::latest(), ClarityVersion::Clarity2); - assert_eq!(v1, v2); + fn index_of_complex_type() { + crosscheck( + "(index-of (list (list (ok 2) (err 5)) (list (ok 42)) (list (err 7))) (list (err 7)))", + Ok(Some(Value::some(Value::UInt(2)).unwrap())), + ); } // @@ -1287,17 +1283,6 @@ mod tests { use super::*; use crate::tools::crosscheck; - #[test] - // TODO: see issue #496. - // The test below should pass when running it in ClarityV1. - // It should be removed from this module when the issue is fixed. - fn index_of_complex_type() { - crosscheck( - "(index-of (list (list (ok 2) (err 5)) (list (ok 42)) (list (err 7))) (list (err 7)))", - Ok(Some(Value::some(Value::UInt(2)).unwrap())), - ); - } - #[test] fn index_of_alias_list_zero_len() { let mut env = TestEnvironment::default();