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 contract caller types #2778

Merged
merged 7 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
38 changes: 22 additions & 16 deletions sway-core/src/type_system/type_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,41 +391,47 @@ impl TypeEngine {
}

(
TypeInfo::ContractCaller {
abi_name: ref abi_name_a,
address,
ref r @ TypeInfo::ContractCaller {
abi_name: ref abi_name_received,
address: ref received_address,
},
ref e @ TypeInfo::ContractCaller {
abi_name: ref abi_name_b,
TypeInfo::ContractCaller {
abi_name: ref abi_name_expected,
..
},
) if (abi_name_a == abi_name_b && address.is_none())
|| matches!(abi_name_a, AbiName::Deferred) =>
) if (abi_name_received == abi_name_expected && received_address.is_none())
|| matches!(abi_name_received, AbiName::Deferred) =>
{
// if one address is empty, coerce to the other one
match self.slab.replace(expected, e, look_up_type_id(expected)) {
match self.slab.replace(received, r, look_up_type_id(expected)) {
None => (vec![], vec![]),
Some(_) => self.unify(received, expected, span, help_text),
}
}
(
ref r @ TypeInfo::ContractCaller {
abi_name: ref abi_name_a,
TypeInfo::ContractCaller {
abi_name: ref abi_name_received,
..
},
TypeInfo::ContractCaller {
abi_name: ref abi_name_b,
address,
ref e @ TypeInfo::ContractCaller {
abi_name: ref abi_name_expected,
ref address,
},
) if (abi_name_a == abi_name_b && address.is_none())
|| matches!(abi_name_b, AbiName::Deferred) =>
) if (abi_name_received == abi_name_expected && address.is_none())
|| matches!(abi_name_expected, AbiName::Deferred) =>
{
// if one address is empty, coerce to the other one
match self.slab.replace(received, r, look_up_type_id(expected)) {
match self.slab.replace(expected, e, look_up_type_id(received)) {
None => (vec![], vec![]),
Some(_) => self.unify(received, expected, span, help_text),
}
}
(ref r @ TypeInfo::ContractCaller { .. }, ref e @ TypeInfo::ContractCaller { .. })
if r == e =>
{
// if they are the same, then it's ok
(vec![], vec![])
}
// When unifying complex types, we must check their sub-types. This
// can be trivially implemented for tuples, sum types, etc.
// (List(a_item), List(b_item)) => self.unify(a_item, b_item),
Expand Down
11 changes: 9 additions & 2 deletions sway-core/src/type_system/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,15 @@ impl fmt::Display for TypeInfo {
name.as_str().to_string(),
type_parameters.iter().map(|x| x.type_id),
),
ContractCaller { abi_name, .. } => {
format!("contract caller {}", abi_name)
ContractCaller { abi_name, address } => {
format!(
"contract caller {} ( {} )",
abi_name,
address
.as_ref()
.map(|address| address.span.as_str().to_string())
.unwrap_or_else(|| "None".into())
)
}
Array(elem_ty, count, _) => format!("[{}; {}]", elem_ty, count),
Storage { .. } => "contract storage".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ category = "fail"
# check: let caller = abi(OtherAbi, ADDRESS);
# nextln: caller
# nextln: $()Mismatched types.
# nextln: $()expected: contract caller SomeAbi
# nextln: $()found: contract caller OtherAbi.
# nextln: $()expected: contract caller SomeAbi ( None )
# nextln: $()found: contract caller OtherAbi ( ADDRESS ).
# nextln: $()help: Implicit return must match up with block's type.

# check: let caller = abi(OtherAbi, ADDRESS);
# nextln: caller
# nextln: $()Mismatched types.
# nextln: $()expected: contract caller SomeAbi
# nextln: $()found: contract caller OtherAbi.
# nextln: $()expected: contract caller SomeAbi ( None )
# nextln: $()found: contract caller OtherAbi ( ADDRESS ).
# nextln: $()help: Function body's return type does not match up with its return type annotation.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[package]]
name = 'contract_caller_dynamic_address'
source = 'root'
dependencies = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "contract_caller_dynamic_address"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

script;


fn main(addr: b256) -> u64 {
let caller: ContractCaller<SomeAbi> = abi(SomeAbi, addr);
// this should revert since we don't have the script data being passed in to the harness
let _ = caller.baz();
return 42;
}

abi SomeAbi {
fn baz() -> u32;
fn quux() -> u64;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "revert", value = 0 }
validate_abi = false