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

Workaround for index-of #541

Merged
merged 4 commits into from
Oct 22, 2024
Merged
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
32 changes: 20 additions & 12 deletions clar2wasm/src/words/equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,19 @@ 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)?;
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)
.ok_or(GeneratorError::TypeError(
"index_of element must be typed".to_owned(),
))?
{
generator.set_expr_type(elem_expr, ltd.get_list_item_type().clone())?;
}

// Traverse the sequence, leaving its offset and size on the stack.
generator.traverse_expr(builder, seq)?;
// STACK: [offset, size]

Expand Down Expand Up @@ -1254,6 +1265,14 @@ mod tests {
crosscheck(snippet, Ok(Some(clarity::vm::Value::Bool(true))));
}

#[test]
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())),
);
}

//
// Module with tests that should only be executed
// when running Clarity::V2 or Clarity::v3.
Expand All @@ -1264,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();
Expand Down