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

Support address.code in sema #1549

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/sema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,7 @@ pub enum StringLocation<T> {

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Builtin {
ContractCode,
GetAddress,
Balance,
PayableSend,
Expand Down
69 changes: 42 additions & 27 deletions src/sema/expression/member_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,37 +347,52 @@ pub(super) fn member_access(
}
_ => {}
},
Type::Address(_) => {
if id.name == "balance" {
if ns.target.is_polkadot() {
let mut is_this = false;

if let Expression::Cast { expr: this, .. } = &expr {
if let Expression::Builtin {
kind: Builtin::GetAddress,
..
} = this.as_ref()
{
is_this = true;
}
Type::Address(_) if id.name == "balance" => {
if ns.target.is_polkadot() {
let mut is_this = false;

if let Expression::Cast { expr: this, .. } = &expr {
if let Expression::Builtin {
kind: Builtin::GetAddress,
..
} = this.as_ref()
{
is_this = true;
}
}

if !is_this {
diagnostics.push(Diagnostic::error(
expr.loc(),
"polkadot can only retrieve balance of this, like 'address(this).balance'".to_string(),
));
return Err(());
}
if !is_this {
diagnostics.push(Diagnostic::error(
expr.loc(),
"polkadot can only retrieve balance of this, like 'address(this).balance'"
xermicus marked this conversation as resolved.
Show resolved Hide resolved
.to_string(),
));
return Err(());
}
used_variable(ns, &expr, symtable);
return Ok(Expression::Builtin {
loc: *loc,
tys: vec![Type::Value],
kind: Builtin::Balance,
args: vec![expr],
});
}
used_variable(ns, &expr, symtable);
return Ok(Expression::Builtin {
loc: *loc,
tys: vec![Type::Value],
kind: Builtin::Balance,
args: vec![expr],
});
}
Type::Address(_) if id.name == "code" => {
if ns.target != Target::EVM {
diagnostics.push(Diagnostic::error(
expr.loc(),
format!("'address.code' is not supported on {}", ns.target),
));
return Err(());
}
used_variable(ns, &expr, symtable);
return Ok(Expression::Builtin {
loc: *loc,
tys: vec![Type::Slice(Type::Uint(8).into())],
xermicus marked this conversation as resolved.
Show resolved Hide resolved
kind: Builtin::ContractCode,
args: vec![expr],
});
}
Type::Contract(ref_contract_no) => {
let mut name_matches = 0;
Expand Down
10 changes: 10 additions & 0 deletions tests/contract_testcases/evm/address_code.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
contract UpgradeableProxy {
function _setImplementation(
address newImplementation
) public pure returns (uint) {
return newImplementation.code;
}
}

// ---- Expect: diagnostics ----
// error: 5:9-38: conversion from uint8 slice to uint256 not possible
10 changes: 10 additions & 0 deletions tests/contract_testcases/polkadot/builtins/address_code_01.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
contract UpgradeableProxy {
function _setImplementation(
address newImplementation
) public pure returns (bytes) {
return newImplementation.code;
}
}

// ---- Expect: diagnostics ----
// error: 5:16-33: 'address.code' is not supported on Polkadot
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract UpgradeableProxy {
function _setImplementation(address newImplementation) public view {
assert(newImplementation.code.length != 0);
}
}

// ---- Expect: diagnostics ----
// error: 3:16-33: 'address.code' is not supported on Polkadot
2 changes: 1 addition & 1 deletion tests/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn ethereum_solidity_tests() {
})
.sum();

assert_eq!(errors, 1012);
assert_eq!(errors, 1010);
}

fn set_file_contents(source: &str, path: &Path) -> (FileResolver, Vec<String>) {
Expand Down