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

Fix callable type issue (DO NOT MERGE) #4623

Draft
wants to merge 1 commit into
base: next
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
15 changes: 15 additions & 0 deletions clarity/src/vm/analysis/type_checker/v2_1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,21 @@ fn clarity2_inner_type_check_type<T: CostTracker>(
)?;
}
}
(
TypeSignature::CallableType(CallableSubtype::Principal(_)),
TypeSignature::ListUnionType(types),
) => {
// If all types in the union are callable principals, then the principal
// is compatible with the union (the actual type is just principal).
for subtype in types {
if let CallableSubtype::Principal(_) = subtype {
continue;
}
return Err(
CheckErrors::TypeError(expected_type.clone(), actual_type.clone()).into(),
);
}
}
(TypeSignature::NoType, _) => (),
(_, _) => {
if !expected_type.admits_type(&StacksEpochId::Epoch21, actual_type)? {
Expand Down
58 changes: 58 additions & 0 deletions clarity/src/vm/analysis/type_checker/v2_1/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3658,3 +3658,61 @@ fn test_principal_admits() {
assert!(res.is_err());
}
}

#[test]
fn test_replace_at_callable() {
let contract = "(replace-at?
(list
'SK1655DMX0A8VKB0W7R2EN6B29MS98G1HTZV2XW41.CKPAbAeaxopNIumug
'ST3Z7C6C0Q0VWBM06788X4P386517JNEAYTN1SRB7.euEUmoKftalFLQpCsmeswgNWQQcRelTgmVVSY
'S12R7Q5M2WN8PYVPNH9RXHMHV1GDX1PCT9XQFZ1VF.zQurKADWgvZWXBvpAb
)
u1
'SK1655DMX0A8VKB0W7R2EN6B29MS98G1HTZV2XW41.CKPAbAeaxopNIumug
)";

assert!(mem_type_check(contract).is_ok());
}

#[test]
fn test_replace_at_option_callable() {
let contract = "(replace-at?
(list
(some 'SK1655DMX0A8VKB0W7R2EN6B29MS98G1HTZV2XW41.CKPAbAeaxopNIumug)
none
none
none
(some 'ST3Z7C6C0Q0VWBM06788X4P386517JNEAYTN1SRB7.euEUmoKftalFLQpCsmeswgNWQQcRelTgmVVSY)
(some 'S12R7Q5M2WN8PYVPNH9RXHMHV1GDX1PCT9XQFZ1VF.zQurKADWgvZWXBvpAb)
none
)
u4
(some 'SK1655DMX0A8VKB0W7R2EN6B29MS98G1HTZV2XW41.CKPAbAeaxopNIumug)
)";

assert!(mem_type_check(contract).is_ok());
}

#[test]
fn test_replace_at_callable_workaround() {
let contract = "
(define-private (replace-optional-callable (l (list 8 (optional principal))) (index uint) (element (optional principal)))
(replace-at? l index element)
)
(replace-optional-callable
(list
(some 'SK1655DMX0A8VKB0W7R2EN6B29MS98G1HTZV2XW41.CKPAbAeaxopNIumug)
none
none
none
(some 'ST3Z7C6C0Q0VWBM06788X4P386517JNEAYTN1SRB7.euEUmoKftalFLQpCsmeswgNWQQcRelTgmVVSY)
(some 'S12R7Q5M2WN8PYVPNH9RXHMHV1GDX1PCT9XQFZ1VF.zQurKADWgvZWXBvpAb)
none
)
u4
(some 'SK1655DMX0A8VKB0W7R2EN6B29MS98G1HTZV2XW41.CKPAbAeaxopNIumug)
)
";

assert!(mem_type_check(contract).is_ok());
}