Przeglądaj źródła

Pass by value instead of refernce (#1246)

Signed-off-by: Lucas Steuernagel <lucas.tnagel@gmail.com>
Lucas Steuernagel 2 lat temu
rodzic
commit
e66d8a9b4c

+ 3 - 3
src/abi/substrate.rs

@@ -236,7 +236,7 @@ fn resolve_ast(ty: &ast::Type, ns: &ast::Namespace, registry: &mut PortableRegis
 /// Recursively build the storage layout after all types are registered
 fn type_to_storage_layout(
     key: u32,
-    root: &LayoutKey,
+    root: LayoutKey,
     registry: &PortableRegistryBuilder,
 ) -> Layout<PortableForm> {
     let ty = registry.get(key).unwrap();
@@ -250,7 +250,7 @@ fn type_to_storage_layout(
                 )
             }),
         )),
-        _ => Layout::Leaf(LeafLayout::new(*root, key.into())),
+        _ => Layout::Leaf(LeafLayout::new(root, key.into())),
     }
 }
 
@@ -271,7 +271,7 @@ pub fn gen_project(contract_no: usize, ns: &ast::Namespace) -> InkProject {
                 let layout_key = LayoutKey::new(slot);
                 let root = RootLayout::new(
                     layout_key,
-                    type_to_storage_layout(ty, &layout_key, &registry),
+                    type_to_storage_layout(ty, layout_key, &registry),
                 );
                 Some(FieldLayout::new(var.name.clone(), root))
             } else {

+ 4 - 4
src/codegen/array_boundary.rs

@@ -16,7 +16,7 @@ pub(crate) fn handle_array_assign(
     right: Expression,
     cfg: &mut ControlFlowGraph,
     vartab: &mut Vartable,
-    pos: &usize,
+    pos: usize,
 ) -> Expression {
     if let Expression::AllocDynamicBytes(loc, ty @ Type::Array(..), size, option) = right {
         // If we re-allocate the pointer, create a new temp variable to hold the new array length
@@ -31,7 +31,7 @@ pub(crate) fn handle_array_assign(
             },
         );
 
-        cfg.array_lengths_temps.insert(*pos, temp_res);
+        cfg.array_lengths_temps.insert(pos, temp_res);
 
         Expression::AllocDynamicBytes(
             loc,
@@ -45,10 +45,10 @@ pub(crate) fn handle_array_assign(
             if cfg.array_lengths_temps.contains_key(right_res) {
                 let to_update = cfg.array_lengths_temps[right_res];
 
-                cfg.array_lengths_temps.insert(*pos, to_update);
+                cfg.array_lengths_temps.insert(pos, to_update);
             } else {
                 // If the right hand side doesn't have a temp, it must be a function parameter or a struct member.
-                cfg.array_lengths_temps.remove(pos);
+                cfg.array_lengths_temps.remove(&pos);
             }
         }
 

+ 5 - 5
src/codegen/constructor.rs

@@ -16,7 +16,7 @@ use super::encoding::abi_encode;
 /// call the constructor of a contract.
 pub(super) fn call_constructor(
     loc: &Loc,
-    contract_no: &usize,
+    contract_no: usize,
     callee_contract_no: usize,
     constructor_no: &Option<usize>,
     constructor_args: &[ast::Expression],
@@ -59,13 +59,13 @@ pub(super) fn call_constructor(
         .collect::<Vec<Expression>>();
 
     let selector = match constructor_no {
-        Some(func_no) => ns.functions[*func_no].selector(ns, contract_no),
-        None => ns.contracts[*contract_no]
+        Some(func_no) => ns.functions[*func_no].selector(ns, &contract_no),
+        None => ns.contracts[contract_no]
             .default_constructor
             .as_ref()
             .unwrap()
             .0
-            .selector(ns, contract_no),
+            .selector(ns, &contract_no),
     };
 
     let mut args = vec![Expression::BytesLiteral(
@@ -83,7 +83,7 @@ pub(super) fn call_constructor(
         Instr::Constructor {
             success,
             res: address_res,
-            contract_no: *contract_no,
+            contract_no,
             encoded_args,
             encoded_args_len,
             value,

+ 27 - 27
src/codegen/expression.rs

@@ -65,7 +65,7 @@ pub fn expression(
         } => add(
             loc,
             ty,
-            unchecked,
+            *unchecked,
             left,
             cfg,
             contract_no,
@@ -84,7 +84,7 @@ pub fn expression(
         } => subtract(
             loc,
             ty,
-            unchecked,
+            *unchecked,
             left,
             cfg,
             contract_no,
@@ -353,7 +353,7 @@ pub fn expression(
             } = &**left
             {
                 // If cfg_right is an AllocDynamicArray(_,_,size,_), update it such that it becomes AllocDynamicArray(_,_,temp_var,_) to avoid repetitive expressions in the cfg.
-                cfg_right = handle_array_assign(cfg_right, cfg, vartab, var_no);
+                cfg_right = handle_array_assign(cfg_right, cfg, vartab, *var_no);
             }
 
             assign_single(left, cfg_right, cfg, contract_no, func, ns, vartab, opt)
@@ -379,7 +379,7 @@ pub fn expression(
             ns,
             loc,
             expr,
-            unchecked,
+            *unchecked,
             opt,
         ),
         ast::Expression::PostDecrement {
@@ -403,7 +403,7 @@ pub fn expression(
             ns,
             loc,
             expr,
-            unchecked,
+            *unchecked,
             opt,
         ),
         ast::Expression::Constructor {
@@ -417,7 +417,7 @@ pub fn expression(
 
             call_constructor(
                 loc,
-                constructor_contract,
+                *constructor_contract,
                 contract_no,
                 constructor_no,
                 args,
@@ -903,7 +903,7 @@ pub fn expression(
         ast::Expression::Builtin {
             loc,
             tys,
-            kind: builtin,
+            kind,
             args,
         } => expr_builtin(
             args,
@@ -914,7 +914,7 @@ pub fn expression(
             vartab,
             loc,
             tys,
-            builtin,
+            *kind,
             opt,
         ),
         ast::Expression::FormatString { loc, format: args } => {
@@ -945,7 +945,7 @@ pub fn expression(
             right,
             opt,
         ),
-        ast::Expression::InterfaceId { loc, contract_no } => interfaceid(ns, contract_no, loc),
+        ast::Expression::InterfaceId { loc, contract_no } => interfaceid(ns, *contract_no, loc),
         ast::Expression::BoolLiteral { loc, value } => Expression::BoolLiteral(*loc, *value),
         ast::Expression::BytesLiteral { loc, ty, value } => {
             Expression::BytesLiteral(*loc, ty.clone(), value.clone())
@@ -1050,7 +1050,7 @@ fn post_incdec(
     ns: &Namespace,
     loc: &pt::Loc,
     expr: &ast::Expression,
-    unchecked: &bool,
+    unchecked: bool,
     opt: &Options,
 ) -> Expression {
     let res = vartab.temp_anonymous(ty);
@@ -1073,14 +1073,14 @@ fn post_incdec(
         ast::Expression::PostDecrement { .. } => Expression::Subtract(
             *loc,
             ty.clone(),
-            *unchecked,
+            unchecked,
             Box::new(Expression::Variable(*loc, ty.clone(), res)),
             one,
         ),
         ast::Expression::PostIncrement { .. } => Expression::Add(
             *loc,
             ty.clone(),
-            *unchecked,
+            unchecked,
             Box::new(Expression::Variable(*loc, ty.clone(), res)),
             one,
         ),
@@ -1146,7 +1146,7 @@ fn pre_incdec(
     ns: &Namespace,
     loc: &pt::Loc,
     expr: &ast::Expression,
-    unchecked: &bool,
+    unchecked: bool,
     opt: &Options,
 ) -> Expression {
     let res = vartab.temp_anonymous(ty);
@@ -1159,10 +1159,10 @@ fn pre_incdec(
     let one = Box::new(Expression::NumberLiteral(*loc, ty.clone(), BigInt::one()));
     let expr = match expr {
         ast::Expression::PreDecrement { .. } => {
-            Expression::Subtract(*loc, ty.clone(), *unchecked, Box::new(v), one)
+            Expression::Subtract(*loc, ty.clone(), unchecked, Box::new(v), one)
         }
         ast::Expression::PreIncrement { .. } => {
-            Expression::Add(*loc, ty.clone(), *unchecked, Box::new(v), one)
+            Expression::Add(*loc, ty.clone(), unchecked, Box::new(v), one)
         }
         _ => unreachable!(),
     };
@@ -1779,7 +1779,7 @@ fn expr_builtin(
     vartab: &mut Vartable,
     loc: &pt::Loc,
     tys: &[Type],
-    builtin: &ast::Builtin,
+    builtin: ast::Builtin,
     opt: &Options,
 ) -> Expression {
     match builtin {
@@ -1983,7 +1983,7 @@ fn expr_builtin(
 
             cfg.set_basic_block(in_bounds);
 
-            Expression::Builtin(*loc, tys.to_vec(), builtin.into(), vec![buf, offset])
+            Expression::Builtin(*loc, tys.to_vec(), (&builtin).into(), vec![buf, offset])
         }
         ast::Builtin::AddMod | ast::Builtin::MulMod => {
             let arguments: Vec<Expression> = args
@@ -2017,7 +2017,7 @@ fn expr_builtin(
                 Instr::Set {
                     loc: *loc,
                     res: temp,
-                    expr: Expression::Builtin(*loc, tys.to_vec(), builtin.into(), arguments),
+                    expr: Expression::Builtin(*loc, tys.to_vec(), (&builtin).into(), arguments),
                 },
             );
             cfg.add(vartab, Instr::Branch { block: end_if });
@@ -2043,7 +2043,7 @@ fn expr_builtin(
                 .map(|v| expression(v, cfg, contract_no, func, ns, vartab, opt))
                 .collect();
 
-            if !arguments.is_empty() && builtin == &ast::Builtin::ArrayLength {
+            if !arguments.is_empty() && builtin == ast::Builtin::ArrayLength {
                 // If an array length instruction is called
                 // Get the variable it is assigned with
                 if let Expression::Variable(_, _, num) = &arguments[0] {
@@ -2054,7 +2054,7 @@ fn expr_builtin(
                     }
                 }
             }
-            Expression::Builtin(*loc, tys.to_vec(), builtin.into(), arguments)
+            Expression::Builtin(*loc, tys.to_vec(), (&builtin).into(), arguments)
         }
     }
 }
@@ -2078,7 +2078,7 @@ fn alloc_dynamic_array(
 fn add(
     loc: &pt::Loc,
     ty: &Type,
-    unchecked: &bool,
+    unchecked: bool,
     left: &ast::Expression,
     cfg: &mut ControlFlowGraph,
     contract_no: usize,
@@ -2091,7 +2091,7 @@ fn add(
     Expression::Add(
         *loc,
         ty.clone(),
-        *unchecked,
+        unchecked,
         Box::new(expression(left, cfg, contract_no, func, ns, vartab, opt)),
         Box::new(expression(right, cfg, contract_no, func, ns, vartab, opt)),
     )
@@ -2100,7 +2100,7 @@ fn add(
 fn subtract(
     loc: &pt::Loc,
     ty: &Type,
-    unchecked: &bool,
+    unchecked: bool,
     left: &ast::Expression,
     cfg: &mut ControlFlowGraph,
     contract_no: usize,
@@ -2113,7 +2113,7 @@ fn subtract(
     Expression::Subtract(
         *loc,
         ty.clone(),
-        *unchecked,
+        unchecked,
         Box::new(expression(left, cfg, contract_no, func, ns, vartab, opt)),
         Box::new(expression(right, cfg, contract_no, func, ns, vartab, opt)),
     )
@@ -2293,14 +2293,14 @@ fn conditional_operator(
     Expression::Variable(*loc, ty.clone(), pos)
 }
 
-fn interfaceid(ns: &Namespace, contract_no: &usize, loc: &pt::Loc) -> Expression {
+fn interfaceid(ns: &Namespace, contract_no: usize, loc: &pt::Loc) -> Expression {
     let selector_len = ns.target.selector_length();
     let mut id = vec![0u8; selector_len as usize];
-    for func_no in &ns.contracts[*contract_no].functions {
+    for func_no in &ns.contracts[contract_no].functions {
         let func = &ns.functions[*func_no];
 
         if func.ty == pt::FunctionTy::Function {
-            let selector = func.selector(ns, contract_no);
+            let selector = func.selector(ns, &contract_no);
             debug_assert_eq!(id.len(), selector.len());
 
             for (i, e) in id.iter_mut().enumerate() {

+ 4 - 4
src/codegen/statements.rs

@@ -58,7 +58,7 @@ pub(crate) fn statement(
             }
         }
         Statement::VariableDecl(loc, pos, _, Some(init)) => {
-            if should_remove_variable(pos, func, opt) {
+            if should_remove_variable(*pos, func, opt) {
                 let mut params = SideEffectsCheckParameters {
                     cfg,
                     contract_no,
@@ -119,7 +119,7 @@ pub(crate) fn statement(
             );
         }
         Statement::VariableDecl(loc, pos, param, None) => {
-            if should_remove_variable(pos, func, opt) {
+            if should_remove_variable(*pos, func, opt) {
                 return;
             }
 
@@ -920,7 +920,7 @@ fn destructure(
             DestructureField::VariableDecl(res, param) => {
                 let expr = try_load_and_cast(&param.loc, &right, &param.ty, ns, cfg, vartab);
 
-                if should_remove_variable(res, func, opt) {
+                if should_remove_variable(*res, func, opt) {
                     continue;
                 }
 
@@ -1142,7 +1142,7 @@ fn try_catch(
 
             call_constructor(
                 loc,
-                contract_no,
+                *contract_no,
                 callee_contract_no,
                 constructor_no,
                 args,

+ 6 - 6
src/codegen/strength_reduce/expression_values.rs

@@ -17,7 +17,7 @@ pub(super) fn expression_values(
 ) -> HashSet<Value> {
     match expr {
         Expression::NumberLiteral(_, ty, v) => number_literal_values(ty, v, ns),
-        Expression::BoolLiteral(_, v) => bool_literal_values(v),
+        Expression::BoolLiteral(_, v) => bool_literal_values(*v),
         Expression::ZeroExt(_, ty, expr) => zero_ext_values(ty, expr, vars, ns),
         Expression::SignExt(_, ty, expr) => sign_ext_values(ty, expr, vars, ns),
         Expression::Trunc(_, ty, expr) => trunc_values(ty, expr, vars, ns),
@@ -59,7 +59,7 @@ pub(super) fn expression_values(
         }
         Expression::Not(_, expr) => not_values(expr, vars, ns),
         Expression::Complement(_, _, expr) => complement_values(expr, vars, ns),
-        Expression::Variable(_, _, var_no) => variable_values(var_no, vars),
+        Expression::Variable(_, _, var_no) => variable_values(*var_no, vars),
         Expression::InternalFunctionCfg(_) => {
             // reference to a function; ignore
             HashSet::new()
@@ -110,11 +110,11 @@ fn number_literal_values(ty: &Type, v: &BigInt, ns: &Namespace) -> HashSet<Value
     set
 }
 
-fn bool_literal_values(v: &bool) -> HashSet<Value> {
+fn bool_literal_values(v: bool) -> HashSet<Value> {
     let mut set = HashSet::new();
 
     let mut value = BitArray::new([0u8; 32]);
-    value.set(0, *v);
+    value.set(0, v);
     let mut known_bits = BitArray::new([0u8; 32]);
     known_bits.set(0, true);
 
@@ -802,8 +802,8 @@ fn complement_values(expr: &Expression, vars: &Variables, ns: &Namespace) -> Has
         .collect()
 }
 
-fn variable_values(var_no: &usize, vars: &Variables) -> HashSet<Value> {
-    if let Some(v) = vars.get(var_no) {
+fn variable_values(var_no: usize, vars: &Variables) -> HashSet<Value> {
+    if let Some(v) = vars.get(&var_no) {
         v.clone()
     } else {
         HashSet::new()

+ 7 - 7
src/codegen/strength_reduce/reaching_values.rs

@@ -25,7 +25,7 @@ pub(super) fn reaching_values(
         let mut changes = false;
 
         for (var_no, set) in vars.iter() {
-            changes |= update_map(var_no, set, map);
+            changes |= update_map(*var_no, set, map);
         }
 
         if !changes {
@@ -97,8 +97,8 @@ pub(super) fn reaching_values(
 /// Update the Variable's map based on the incoming set of values. Returns true if there was any
 /// changes in the set.
 /// There is a discussion to improve this function: https://github.com/hyperledger/solang/issues/934
-fn update_map(var_no: &usize, set: &HashSet<Value>, map: &mut Variables) -> bool {
-    return if let Some(existing) = map.get_mut(var_no) {
+fn update_map(var_no: usize, set: &HashSet<Value>, map: &mut Variables) -> bool {
+    return if let Some(existing) = map.get_mut(&var_no) {
         if existing.iter().next().map_or(false, |v| v.all_unknown()) {
             // If we already think it is unknown, nothing can improve on that
             false
@@ -108,7 +108,7 @@ fn update_map(var_no: &usize, set: &HashSet<Value>, map: &mut Variables) -> bool
 
             set.insert(v.clone());
 
-            map.insert(*var_no, set);
+            map.insert(var_no, set);
             true
         } else {
             let mut changes = false;
@@ -127,7 +127,7 @@ fn update_map(var_no: &usize, set: &HashSet<Value>, map: &mut Variables) -> bool
                 set.insert(Value::unknown(bits));
 
                 changes = true;
-                map.insert(*var_no, set);
+                map.insert(var_no, set);
             }
             changes
         }
@@ -141,9 +141,9 @@ fn update_map(var_no: &usize, set: &HashSet<Value>, map: &mut Variables) -> bool
 
             set.insert(Value::unknown(bits));
 
-            map.insert(*var_no, set);
+            map.insert(var_no, set);
         } else {
-            map.insert(*var_no, set.clone());
+            map.insert(var_no, set.clone());
         }
 
         true

+ 11 - 11
src/codegen/subexpression_elimination/available_expression_set.rs

@@ -52,7 +52,7 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
     /// Checks if an expression is available on both sets
     fn check_intersection(
         key: &ExpressionType,
-        value: &NodeId,
+        value: NodeId,
         set_2: &AvailableExpressionSet,
     ) -> bool {
         // Basic case: the expression is available only available on one set
@@ -62,7 +62,7 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
 
         // If the expression is a variable, we must ensure that it points to the same node
         if matches!(key, ExpressionType::Variable(_)) {
-            return *value == set_2.expr_map[key];
+            return value == set_2.expr_map[key];
         }
 
         true
@@ -75,7 +75,7 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
         cst: &CommonSubExpressionTracker,
     ) {
         self.expr_map
-            .retain(|key, value| AvailableExpressionSet::check_intersection(key, value, set_2));
+            .retain(|key, value| AvailableExpressionSet::check_intersection(key, *value, set_2));
 
         let mut to_maintain: HashSet<usize> = HashSet::new();
 
@@ -191,8 +191,8 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
     }
 
     /// Add expressions to the common subexpression tracker.
-    fn add_to_cst(&self, exp: &Expression, id: &NodeId, cst: &mut CommonSubExpressionTracker) {
-        let node = &*self.expression_memory.get(id).unwrap().borrow();
+    fn add_to_cst(&self, exp: &Expression, id: NodeId, cst: &mut CommonSubExpressionTracker) {
+        let node = &*self.expression_memory.get(&id).unwrap().borrow();
         cst.add_expression(exp, &node.expr_type, node);
     }
 
@@ -249,7 +249,7 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
     ) -> Option<NodeId> {
         if let Some(id) = self.find_expression(exp) {
             if let Some(tracker) = cst.as_mut() {
-                self.add_to_cst(exp, &id, tracker);
+                self.add_to_cst(exp, id, tracker);
             }
             return Some(id);
         }
@@ -312,20 +312,20 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
     }
 
     /// Remove from the set all children from a node
-    fn kill_child(&mut self, child_node: &Rc<RefCell<BasicExpression>>, parent_id: &NodeId) {
+    fn kill_child(&mut self, child_node: &Rc<RefCell<BasicExpression>>, parent_id: NodeId) {
         self.kill_recursive(&child_node.borrow(), parent_id);
         child_node.borrow_mut().children.clear();
     }
 
     /// Recursively remove from the set all the children of a node
-    fn kill_recursive(&mut self, basic_exp: &BasicExpression, parent_id: &NodeId) {
+    fn kill_recursive(&mut self, basic_exp: &BasicExpression, parent_id: NodeId) {
         for (child_id, node) in &basic_exp.children {
-            self.kill_child(node, &basic_exp.expression_id);
+            self.kill_child(node, basic_exp.expression_id);
             self.expression_memory.remove(child_id);
         }
 
         if let ExpressionType::BinaryOperation(left, right, _) = &basic_exp.expr_type {
-            let other_parent = if *left == *parent_id { right } else { left };
+            let other_parent = if *left == parent_id { right } else { left };
             // If the graph has a cycle, we may have already borrowed or deleted a parent.
             if let Some(parent_ref) = self.expression_memory.get_mut(other_parent) {
                 if let Ok(mut parent) = parent_ref.try_borrow_mut() {
@@ -366,7 +366,7 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
         let var_id = self.expr_map[&key];
         let var_node = self.expression_memory[&var_id].clone();
         for (child_id, node) in &var_node.borrow_mut().children {
-            self.kill_child(node, &var_id);
+            self.kill_child(node, var_id);
             self.expression_memory.remove(child_id);
         }
         self.expression_memory.remove(&var_id);

+ 2 - 2
src/codegen/subexpression_elimination/common_subexpression_tracker.rs

@@ -92,8 +92,8 @@ impl<'a> CommonSubExpressionTracker<'a> {
     }
 
     /// Invalidate a mapped variable
-    pub fn invalidate_mapped_variable(&mut self, var_no: &usize) {
-        if let Some(expr_id) = self.mapped_variables.remove(var_no) {
+    pub fn invalidate_mapped_variable(&mut self, var_no: usize) {
+        if let Some(expr_id) = self.mapped_variables.remove(&var_no) {
             self.common_subexpressions[expr_id].var_loc = None;
             self.common_subexpressions[expr_id].in_cfg = false;
             self.common_subexpressions[expr_id].var_no = None;

+ 1 - 1
src/codegen/subexpression_elimination/instruction.rs

@@ -56,7 +56,7 @@ impl<'a, 'b: 'a> AvailableExpressionSet<'a> {
                     // x = x + y -> make x+y available, than make kill x, which also kills x+y
                     // -- x + y is not available here, because x has a new definition
                     self.kill(*res);
-                    tracker.invalidate_mapped_variable(res);
+                    tracker.invalidate_mapped_variable(*res);
                 }
             }
 

+ 10 - 15
src/codegen/undefined_variable.rs

@@ -101,7 +101,7 @@ pub fn find_undefined_variables_in_expression(
                             && !*modified
                             && !matches!(var.ty, Type::Array(..))
                         {
-                            add_diagnostic(var, pos, &exp.loc(), ctx.diagnostics);
+                            add_diagnostic(var, *pos, &exp.loc(), ctx.diagnostics);
                         }
                     }
                 }
@@ -120,7 +120,7 @@ pub fn find_undefined_variables_in_expression(
 /// error messages in Diagnotics
 fn add_diagnostic(
     var: &symtable::Variable,
-    var_no: &usize,
+    var_no: usize,
     expr_loc: &Loc,
     diagnostics: &mut HashMap<usize, Diagnostic>,
 ) {
@@ -130,20 +130,15 @@ fn add_diagnostic(
         return;
     }
 
-    if !diagnostics.contains_key(var_no) {
-        diagnostics.insert(
-            *var_no,
-            Diagnostic {
-                level: Level::Error,
-                ty: ErrorType::TypeError,
-                loc: var.id.loc,
-                message: format!("Variable '{}' is undefined", var.id.name),
-                notes: vec![],
-            },
-        );
-    }
+    diagnostics.entry(var_no).or_insert(Diagnostic {
+        level: Level::Error,
+        ty: ErrorType::TypeError,
+        loc: var.id.loc,
+        message: format!("Variable '{}' is undefined", var.id.name),
+        notes: vec![],
+    });
 
-    let diag = diagnostics.get_mut(var_no).unwrap();
+    let diag = diagnostics.get_mut(&var_no).unwrap();
     diag.notes.push(Note {
         loc: *expr_loc,
         message: "Variable read before being defined".to_string(),

+ 3 - 3
src/codegen/unused_variable.rs

@@ -38,7 +38,7 @@ pub fn should_remove_assignment(
             !var.read
         }
 
-        Expression::Variable { var_no, .. } => should_remove_variable(var_no, func, opt),
+        Expression::Variable { var_no, .. } => should_remove_variable(*var_no, func, opt),
 
         Expression::StructMember { expr, .. } => should_remove_assignment(ns, expr, func, opt),
 
@@ -55,12 +55,12 @@ pub fn should_remove_assignment(
 }
 
 /// Checks if we should remove a variable
-pub fn should_remove_variable(pos: &usize, func: &Function, opt: &Options) -> bool {
+pub fn should_remove_variable(pos: usize, func: &Function, opt: &Options) -> bool {
     if opt.opt_level == OptimizationLevel::None {
         return false;
     }
 
-    let var = &func.symtable.vars[pos];
+    let var = &func.symtable.vars[&pos];
 
     //If the variable has never been read nor assigned, we can remove it right away.
     if !var.read && !var.assigned {

+ 3 - 3
src/codegen/yul/builtin.rs

@@ -36,7 +36,7 @@ impl Expression {
 /// Transfrom YUL builtin functions into CFG instructions
 pub(crate) fn process_builtin(
     loc: &pt::Loc,
-    builtin_ty: &YulBuiltInFunction,
+    builtin_ty: YulBuiltInFunction,
     args: &[ast::YulExpression],
     contract_no: usize,
     ns: &Namespace,
@@ -214,7 +214,7 @@ pub(crate) fn process_builtin(
 /// Process arithmetic operations
 fn process_arithmetic(
     loc: &pt::Loc,
-    builtin_ty: &YulBuiltInFunction,
+    builtin_ty: YulBuiltInFunction,
     args: &[ast::YulExpression],
     contract_no: usize,
     ns: &Namespace,
@@ -319,7 +319,7 @@ fn process_arithmetic(
         YulBuiltInFunction::AddMod | YulBuiltInFunction::MulMod => {
             let modulo_operand = expression(&args[2], contract_no, ns, vartab, cfg, opt);
             let (_, equalized_modulo) = equalize_types(left.clone(), modulo_operand.clone(), ns);
-            let builtin = if builtin_ty == &YulBuiltInFunction::AddMod {
+            let builtin = if builtin_ty == YulBuiltInFunction::AddMod {
                 Builtin::AddMod
             } else {
                 Builtin::MulMod

+ 1 - 1
src/codegen/yul/expression.rs

@@ -86,7 +86,7 @@ pub(crate) fn expression(
         }
 
         ast::YulExpression::BuiltInCall(loc, builtin_ty, args) => {
-            process_builtin(loc, builtin_ty, args, contract_no, ns, vartab, cfg, opt)
+            process_builtin(loc, *builtin_ty, args, contract_no, ns, vartab, cfg, opt)
         }
     }
 }

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

@@ -37,7 +37,7 @@ pub(crate) fn statement(
         }
 
         YulStatement::BuiltInCall(loc, _, builtin_ty, args) => {
-            let expr = process_builtin(loc, builtin_ty, args, contract_no, ns, vartab, cfg, opt);
+            let expr = process_builtin(loc, *builtin_ty, args, contract_no, ns, vartab, cfg, opt);
             assert_eq!(expr, Expression::Poison);
         }
 

+ 17 - 17
src/sema/dotgraphviz.rs

@@ -612,7 +612,7 @@ impl Dot {
                 contract_no,
                 var_no,
             } => {
-                self.add_constant_variable(loc, ty, contract_no, var_no, parent, parent_rel, ns);
+                self.add_constant_variable(loc, ty, contract_no, *var_no, parent, parent_rel, ns);
             }
             Expression::Variable { loc, ty, var_no } => {
                 let labels = vec![
@@ -633,7 +633,7 @@ impl Dot {
                 contract_no,
                 var_no,
             } => {
-                self.add_storage_variable(loc, ty, contract_no, var_no, parent, parent_rel, ns);
+                self.add_storage_variable(loc, ty, *contract_no, *var_no, parent, parent_rel, ns);
             }
             Expression::Load { loc, ty, expr } => {
                 let node = self.add_node(
@@ -1904,16 +1904,16 @@ impl Dot {
                 );
             }
             YulExpression::ConstantVariable(loc, ty, contract, var_no) => {
-                self.add_constant_variable(loc, ty, contract, var_no, parent, parent_rel, ns);
+                self.add_constant_variable(loc, ty, contract, *var_no, parent, parent_rel, ns);
             }
             YulExpression::StorageVariable(loc, ty, contract, var_no) => {
-                self.add_storage_variable(loc, ty, contract, var_no, parent, parent_rel, ns);
+                self.add_storage_variable(loc, ty, *contract, *var_no, parent, parent_rel, ns);
             }
             YulExpression::BuiltInCall(loc, builtin_ty, args) => {
-                self.add_yul_builtin_call(loc, builtin_ty, args, parent, parent_rel, symtable, ns);
+                self.add_yul_builtin_call(loc, *builtin_ty, args, parent, parent_rel, symtable, ns);
             }
             YulExpression::FunctionCall(loc, func_no, args, _) => {
-                self.add_yul_function_call(loc, func_no, args, parent, parent_rel, symtable, ns);
+                self.add_yul_function_call(loc, *func_no, args, parent, parent_rel, symtable, ns);
             }
             YulExpression::SuffixAccess(loc, member, suffix) => {
                 let labels = vec![
@@ -1937,7 +1937,7 @@ impl Dot {
         loc: &Loc,
         ty: &Type,
         contract: &Option<usize>,
-        var_no: &usize,
+        var_no: usize,
         parent: usize,
         parent_rel: String,
         ns: &Namespace,
@@ -1953,11 +1953,11 @@ impl Dot {
                 1,
                 format!(
                     "{}.{}",
-                    ns.contracts[*contract].name, ns.contracts[*contract].variables[*var_no].name
+                    ns.contracts[*contract].name, ns.contracts[*contract].variables[var_no].name
                 ),
             );
         } else {
-            labels.insert(1, ns.constants[*var_no].name.to_string());
+            labels.insert(1, ns.constants[var_no].name.to_string());
         }
 
         self.add_node(
@@ -1971,8 +1971,8 @@ impl Dot {
         &mut self,
         loc: &Loc,
         ty: &Type,
-        contract: &usize,
-        var_no: &usize,
+        contract: usize,
+        var_no: usize,
         parent: usize,
         parent_rel: String,
         ns: &Namespace,
@@ -1981,7 +1981,7 @@ impl Dot {
             String::from("storage variable"),
             format!(
                 "{}.{}",
-                ns.contracts[*contract].name, ns.contracts[*contract].variables[*var_no].name
+                ns.contracts[contract].name, ns.contracts[contract].variables[var_no].name
             ),
             ty.to_string(ns),
             ns.loc_to_string(true, loc),
@@ -2004,10 +2004,10 @@ impl Dot {
     ) -> usize {
         match statement {
             YulStatement::FunctionCall(loc, _, func_no, args) => {
-                self.add_yul_function_call(loc, func_no, args, parent, parent_rel, symtable, ns)
+                self.add_yul_function_call(loc, *func_no, args, parent, parent_rel, symtable, ns)
             }
             YulStatement::BuiltInCall(loc, _, builtin_ty, args) => {
-                self.add_yul_builtin_call(loc, builtin_ty, args, parent, parent_rel, symtable, ns)
+                self.add_yul_builtin_call(loc, *builtin_ty, args, parent, parent_rel, symtable, ns)
             }
             YulStatement::Block(block) => {
                 self.add_yul_block(block, parent, parent_rel, symtable, ns)
@@ -2217,7 +2217,7 @@ impl Dot {
     fn add_yul_function_call(
         &mut self,
         loc: &Loc,
-        func_no: &usize,
+        func_no: usize,
         args: &[YulExpression],
         parent: usize,
         parent_rel: String,
@@ -2225,7 +2225,7 @@ impl Dot {
         ns: &Namespace,
     ) -> usize {
         let labels = vec![
-            format!("yul function call '{}'", ns.yul_functions[*func_no].name),
+            format!("yul function call '{}'", ns.yul_functions[func_no].name),
             ns.loc_to_string(true, loc),
         ];
 
@@ -2245,7 +2245,7 @@ impl Dot {
     fn add_yul_builtin_call(
         &mut self,
         loc: &Loc,
-        builtin_ty: &YulBuiltInFunction,
+        builtin_ty: YulBuiltInFunction,
         args: &[YulExpression],
         parent: usize,
         parent_rel: String,

+ 2 - 2
src/sema/yul/builtin.rs

@@ -208,7 +208,7 @@ impl YulBuiltInFunction {
         &YUL_BUILTIN[index]
     }
 
-    pub(crate) fn modify_state(&self) -> bool {
+    pub(crate) fn modify_state(self) -> bool {
         matches!(
             self,
             YulBuiltInFunction::SStore
@@ -226,7 +226,7 @@ impl YulBuiltInFunction {
         )
     }
 
-    pub(crate) fn read_state(&self) -> bool {
+    pub(crate) fn read_state(self) -> bool {
         matches!(
             self,
             YulBuiltInFunction::Address