Parcourir la source

Fix rust clippies and evm balance length (#1603)

Signed-off-by: Sean Young <sean@mess.org>
Sean Young il y a 2 ans
Parent
commit
f4010693bd

+ 2 - 2
src/bin/languageserver/mod.rs

@@ -1644,7 +1644,7 @@ impl<'a> Builder<'a> {
         }
 
         for (i, constant) in self.ns.constants.iter().enumerate() {
-            let samptb = symtable::Symtable::new();
+            let samptb = symtable::Symtable::default();
             self.contract_variable(constant, &samptb, None, i);
         }
 
@@ -1676,7 +1676,7 @@ impl<'a> Builder<'a> {
             }
 
             for (i, variable) in contract.variables.iter().enumerate() {
-                let symtable = symtable::Symtable::new();
+                let symtable = symtable::Symtable::default();
                 self.contract_variable(variable, &symtable, Some(ci), i);
             }
 

+ 2 - 2
src/codegen/revert.rs

@@ -326,7 +326,7 @@ pub(super) fn revert(
         .collect::<Vec<_>>();
 
     if opt.log_runtime_errors {
-        match (error_no, exprs.get(0)) {
+        match (error_no, exprs.first()) {
             // In the case of Error(string), we can print the reason
             (None, Some(expr)) => {
                 let prefix = b"runtime_error: ";
@@ -369,7 +369,7 @@ pub(super) fn revert(
         }
     }
 
-    let error = match (*error_no, exprs.get(0)) {
+    let error = match (*error_no, exprs.first()) {
         // Having an error number requires a custom error
         (Some(error_no), _) => SolidityError::Custom { error_no, exprs },
         // No error number but an expression requires Error(String)

+ 1 - 1
src/codegen/statements/try_catch.rs

@@ -290,7 +290,7 @@ fn exec_try(
             call_args,
             ..
         } => {
-            let address_res = match try_stmt.returns.get(0) {
+            let address_res = match try_stmt.returns.first() {
                 Some((Some(pos), _)) => *pos,
                 _ => vartab.temp_anonymous(&Type::Contract(*contract_no)),
             };

+ 1 - 1
src/emit/polkadot/storage.rs

@@ -754,7 +754,7 @@ impl StorageSlot for PolkadotTarget {
                 }
             }
             Type::Struct(str_ty) => {
-                for (_, field) in str_ty.definition(ns).fields.iter().enumerate() {
+                for field in &str_ty.definition(ns).fields {
                     self.storage_delete_slot(bin, &field.ty, slot, slot_ptr, function, ns);
 
                     if !field.ty.is_reference_type(ns)

+ 1 - 1
src/sema/ast.rs

@@ -457,7 +457,7 @@ impl Function {
             has_body: false,
             is_override: None,
             body: Vec::new(),
-            symtable: Symtable::new(),
+            symtable: Symtable::default(),
             emits_events: Vec::new(),
             mangled_name,
             annotations: ConstructorAnnotations::default(),

+ 1 - 1
src/sema/contracts.rs

@@ -217,7 +217,7 @@ fn resolve_base_args(contracts: &[ContractDefinition], file_no: usize, ns: &mut
                     .position(|e| e.contract_no == base_no)
                 {
                     if let Some(args) = &base.args {
-                        let mut symtable = Symtable::new();
+                        let mut symtable = Symtable::default();
 
                         // find constructor which matches this
                         if let Ok((Some(constructor_no), args)) = match_constructor_to_args(

+ 1 - 1
src/sema/expression/literals.rs

@@ -826,7 +826,7 @@ fn check_subarrays<'a>(
     flatten: &mut Vec<&'a pt::Expression>,
     diagnostics: &mut Diagnostics,
 ) -> Result<(), ()> {
-    if let Some(pt::Expression::ArrayLiteral(_, first)) = exprs.get(0) {
+    if let Some(pt::Expression::ArrayLiteral(_, first)) = exprs.first() {
         // ensure all elements are array literals of the same length
         check_subarrays(first, dims, flatten, diagnostics)?;
 

+ 1 - 1
src/sema/mod.rs

@@ -159,7 +159,7 @@ fn sema_file(file: &ResolvedFile, resolver: &mut FileResolver, ns: &mut ast::Nam
                     &item.doccomments,
                     None,
                     ns,
-                    &mut Symtable::new(),
+                    &mut Symtable::default(),
                 );
             }
             _ => (),

+ 7 - 7
src/sema/mutability.rs

@@ -235,13 +235,13 @@ fn check_mutability(func: &Function, ns: &Namespace) -> Diagnostics {
             SolanaAccount {
                 loc: Loc::Codegen,
                 is_writer: true,
-                /// With a @payer annotation, the account is created on-chain and needs a signer. The client
-                /// provides an address that does not exist yet, so SystemProgram.CreateAccount is called
-                /// on-chain.
-                ///
-                /// However, if a @seed is also provided, the program can sign for the account
-                /// with the seed using program derived address (pda) when SystemProgram.CreateAccount is called,
-                /// so no signer is required from the client.
+                // With a @payer annotation, the account is created on-chain and needs a signer. The client
+                // provides an address that does not exist yet, so SystemProgram.CreateAccount is called
+                // on-chain.
+                //
+                // However, if a @seed is also provided, the program can sign for the account
+                // with the seed using program derived address (pda) when SystemProgram.CreateAccount is called,
+                // so no signer is required from the client.
                 is_signer: func.has_payer_annotation() && !func.has_seed_annotation(),
                 generated: true,
             },

+ 3 - 3
src/sema/namespace.rs

@@ -35,7 +35,7 @@ impl Namespace {
     /// Create a namespace and populate with the parameters for the target
     pub fn new(target: Target) -> Self {
         let (address_length, value_length) = match target {
-            Target::EVM => (20, 16),
+            Target::EVM => (20, 32),
             Target::Polkadot {
                 address_length,
                 value_length,
@@ -1310,7 +1310,7 @@ impl Namespace {
             }
         }
 
-        if let Some(contract_name) = namespace.get(0) {
+        if let Some(contract_name) = namespace.first() {
             contract_no = match self
                 .variable_symbols
                 .get(&(import_file_no, None, contract_name.name.clone()))
@@ -1524,7 +1524,7 @@ impl Namespace {
         expr: &pt::Expression,
         diagnostics: &mut Diagnostics,
     ) -> Result<ArrayDimension, ()> {
-        let mut symtable = Symtable::new();
+        let mut symtable = Symtable::default();
         let mut context = ExprContext {
             file_no,
             unchecked: true,

+ 1 - 1
src/sema/statements.rs

@@ -38,7 +38,7 @@ pub fn resolve_function_body(
     function_no: usize,
     ns: &mut Namespace,
 ) -> Result<(), ()> {
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut res = Vec::new();
     let mut context = ExprContext {
         file_no,

+ 0 - 9
src/sema/symtable.rs

@@ -93,15 +93,6 @@ pub struct Symtable {
 }
 
 impl Symtable {
-    pub fn new() -> Self {
-        Symtable {
-            vars: IndexMap::new(),
-            arguments: Vec::new(),
-            returns: Vec::new(),
-            scopes: Vec::new(),
-        }
-    }
-
     pub fn add(
         &mut self,
         id: &pt::Identifier,

+ 2 - 2
src/sema/tests/mod.rs

@@ -640,7 +640,7 @@ fn get_import_path() {
 
     let ns = parse_and_resolve(OsStr::new("example.sol"), &mut cache, Target::EVM);
 
-    let file = ns.files.get(0);
+    let file = ns.files.first();
     assert!(file.is_some());
     if let Some(file) = file {
         let import_path = cache.get_import_path(file.import_no.unwrap());
@@ -648,7 +648,7 @@ fn get_import_path() {
     }
 
     let ns = parse_and_resolve(OsStr::new("incrementer.sol"), &mut cache, Target::EVM);
-    let file = ns.files.get(0);
+    let file = ns.files.first();
     assert!(file.is_some());
     if let Some(file) = file {
         let import_path = cache.get_import_path(file.import_no.unwrap());

+ 3 - 3
src/sema/variables.rs

@@ -32,7 +32,7 @@ pub fn contract_variables<'a>(
     file_no: usize,
     ns: &mut Namespace,
 ) -> Vec<DelayedResolveInitializer<'a>> {
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut delayed = Vec::new();
 
     for part in &def.parts {
@@ -447,7 +447,7 @@ pub fn variable_decl<'a>(
 
             // If the variable is an array or mapping, the accessor function takes mapping keys
             // or array indices as arguments, and returns the dereferenced value
-            let mut symtable = Symtable::new();
+            let mut symtable = Symtable::default();
             let mut context = ExprContext::default();
             context.enter_scope();
             let mut params = Vec::new();
@@ -784,7 +784,7 @@ pub fn resolve_initializers(
     file_no: usize,
     ns: &mut Namespace,
 ) {
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut diagnostics = Diagnostics::default();
 
     for DelayedResolveInitializer {

+ 1 - 1
src/sema/yul/functions.rs

@@ -239,7 +239,7 @@ pub(crate) fn resolve_function_definition(
     context: &mut ExprContext,
     ns: &mut Namespace,
 ) -> Result<YulFunction, ()> {
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     context.enter_scope();
 
     let prev_yul_function = context.yul_function;

+ 11 - 11
src/sema/yul/tests/expression.rs

@@ -25,7 +25,7 @@ use std::sync::Arc;
 fn resolve_bool_literal() {
     let mut ctx = ExprContext::default();
     ctx.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
 
     let mut ns = Namespace::new(Target::Solana);
@@ -66,7 +66,7 @@ fn resolve_bool_literal() {
 fn resolve_number_literal() {
     let mut ctx = ExprContext::default();
     ctx.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
 
     let loc = Loc::File(0, 3, 5);
@@ -124,7 +124,7 @@ fn resolve_number_literal() {
 fn resolve_hex_number_literal() {
     let mut ctx = ExprContext::default();
     ctx.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
 
     let mut ns = Namespace::new(Target::EVM);
@@ -170,7 +170,7 @@ fn resolve_hex_number_literal() {
 fn resolve_hex_string_literal() {
     let mut ctx = ExprContext::default();
     ctx.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
 
     let mut ns = Namespace::new(Target::EVM);
@@ -237,7 +237,7 @@ fn resolve_hex_string_literal() {
 fn resolve_string_literal() {
     let mut ctx = ExprContext::default();
     ctx.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
 
     let mut ns = Namespace::new(Target::Solana);
@@ -268,7 +268,7 @@ fn resolve_string_literal() {
 fn resolve_variable_local() {
     let mut context = ExprContext::default();
     context.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
     let mut ns = Namespace::new(Target::EVM);
     let loc = Loc::File(1, 2, 3);
@@ -346,7 +346,7 @@ fn resolve_variable_contract() {
     };
     context.enter_scope();
 
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
     let mut ns = Namespace::new(Target::EVM);
     let loc = Loc::File(0, 2, 3);
@@ -543,7 +543,7 @@ fn resolve_variable_contract() {
 fn function_call() {
     let mut context = ExprContext::default();
     context.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
     function_table.enter_scope();
     let mut ns = Namespace::new(Target::EVM);
@@ -739,7 +739,7 @@ fn function_call() {
 fn check_arguments() {
     let mut context = ExprContext::default();
     context.enter_scope();
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
     function_table.enter_scope();
     let mut ns = Namespace::new(Target::EVM);
@@ -896,7 +896,7 @@ fn test_member_access() {
     };
     context.enter_scope();
 
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     let mut function_table = FunctionsTable::new(0);
     let mut ns = Namespace::new(Target::EVM);
     let loc = Loc::File(0, 2, 3);
@@ -1045,7 +1045,7 @@ fn test_check_types() {
         read: false,
     });
     ns.contracts.push(contract);
-    let mut symtable = Symtable::new();
+    let mut symtable = Symtable::default();
     symtable.add(
         &Identifier {
             loc,

+ 3 - 3
tests/codegen_testcases/yul/evm_builtin.sol

@@ -11,16 +11,16 @@ contract Testing {
             // CHECK: (sext uint256 uint160((load (builtin GetAddress ()))))
             let b := address()
 
-            // CHECK: ty:uint256 %c = (sext uint256 (builtin Balance (address((trunc uint160 (arg #0))))))
+            // CHECK: ty:uint256 %c = uint256((builtin Balance (address((trunc uint160 (arg #0))))))
             let c := balance(arg1)
 
-            // CHECK: ty:uint256 %d = (sext uint256 (builtin Balance ((load (builtin GetAddress ())))))
+            // CHECK: ty:uint256 %d = uint256((builtin Balance ((load (builtin GetAddress ())))))
             let d := selfbalance()
 
             // CHECK: ty:uint256 %e = (sext uint256 uint160((builtin Sender ())))
             let e := caller()
 
-            // CHECK: ty:uint256 %f = (sext uint256 (builtin Value ()))
+            // CHECK: ty:uint256 %f = uint256((builtin Value ()))
             let f := callvalue()
 
             // CHECK: ty:uint256 %g = (zext uint256 (builtin Gasprice ()))

+ 1 - 3
tests/contract_testcases/evm/comment_tests.sol

@@ -677,11 +677,9 @@ function _approve(
     }//
     //
 }/**//**//**//**//**//**//**///
+
 // ---- Expect: diagnostics ----
-// warning: 195:50-56: conversion truncates uint256 to uint128, as value is type uint128 on target EVM
-// warning: 268:17-25: function parameter 'weiValue' is unused
 // warning: 269:23-35: function parameter 'errorMessage' is unused
-// warning: 276:70-78: conversion truncates uint256 to uint128, as value is type uint128 on target EVM
 // warning: 321:9-17: 'internal': visibility for constructors is ignored
 // warning: 386:9-61: storage variable '_isExcluded' has never been used
 // warning: 390:9-51: storage variable 'MAX' has been assigned, but never read

+ 2 - 2
tests/solana.rs

@@ -1125,7 +1125,7 @@ fn create_program_address(program_id: &Account, seeds: &[&[u8]]) -> Pubkey {
 
     let hash = hasher.finalize();
 
-    let new_address: [u8; 32] = hash.try_into().unwrap();
+    let new_address: [u8; 32] = hash.into();
 
     // the real runtime does checks if this address exists on the ed25519 curve
 
@@ -1268,7 +1268,7 @@ fn sol_invoke_signed_c(
 
                     let hash = hasher.finalize();
 
-                    let new_address: [u8; 32] = hash.try_into().unwrap();
+                    let new_address: [u8; 32] = hash.into();
 
                     println!(
                         "creating account {} with space {} owner {}",