From 23910f78e77ede70a815feb40cd443bd97377aab Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Tue, 11 Sep 2018 15:05:33 -0400 Subject: [PATCH] Accept slices to function calls Instead of allocated types, per [0]. [0] https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#ptr_arg --- parser/src/parser.rs | 6 +++--- vm/src/compile.rs | 4 ++-- vm/src/eval.rs | 2 +- vm/src/import.rs | 8 ++++---- vm/src/obj/objsequence.rs | 8 ++++---- vm/src/obj/objstr.rs | 6 +++--- vm/src/pyobject.rs | 6 +++--- vm/src/stdlib/ast.rs | 2 +- vm/src/vm.rs | 12 ++++++------ 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 607c6e7a4..e47bfc06b 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -40,7 +40,7 @@ pub fn parse(filename: &Path) -> Result { } } -pub fn parse_program(source: &String) -> Result { +pub fn parse_program(source: &str) -> Result { let lxr = lexer::Lexer::new(&source); match python::ProgramParser::new().parse(lxr) { Err(lalrpop_util::ParseError::UnrecognizedToken { @@ -52,7 +52,7 @@ pub fn parse_program(source: &String) -> Result { } } -pub fn parse_statement(source: &String) -> Result { +pub fn parse_statement(source: &str) -> Result { let lxr = lexer::Lexer::new(&source); match python::StatementParser::new().parse(lxr) { Err(why) => Err(String::from(format!("{:?}", why))), @@ -60,7 +60,7 @@ pub fn parse_statement(source: &String) -> Result } } -pub fn parse_expression(source: &String) -> Result { +pub fn parse_expression(source: &str) -> Result { let lxr = lexer::Lexer::new(&source); match python::ExpressionParser::new().parse(lxr) { Err(why) => Err(String::from(format!("{:?}", why))), diff --git a/vm/src/compile.rs b/vm/src/compile.rs index 702057fb9..c37824615 100644 --- a/vm/src/compile.rs +++ b/vm/src/compile.rs @@ -18,7 +18,7 @@ struct Compiler { pub fn compile( vm: &mut VirtualMachine, - source: &String, + source: &str, mode: Mode, source_path: Option, ) -> Result { @@ -115,7 +115,7 @@ impl Compiler { self.emit(Instruction::ReturnValue); } - fn compile_statements(&mut self, statements: &Vec) { + fn compile_statements(&mut self, statements: &[ast::LocatedStatement]) { for statement in statements { self.compile_statement(statement) } diff --git a/vm/src/eval.rs b/vm/src/eval.rs index dc7dce297..1d0ee3087 100644 --- a/vm/src/eval.rs +++ b/vm/src/eval.rs @@ -4,7 +4,7 @@ use super::compile; use super::pyobject::{PyObjectRef, PyResult}; use super::vm::VirtualMachine; -pub fn eval(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> PyResult { +pub fn eval(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> PyResult { match compile::compile(vm, source, compile::Mode::Eval, None) { Ok(bytecode) => { debug!("Code object: {:?}", bytecode); diff --git a/vm/src/import.rs b/vm/src/import.rs index 3bf0d8e2b..534959fe3 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -13,7 +13,7 @@ use super::compile; use super::pyobject::{DictProtocol, PyObjectKind, PyResult}; use super::vm::VirtualMachine; -fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult { +fn import_uncached_module(vm: &mut VirtualMachine, module: &str) -> PyResult { // Check for Rust-native modules if let Some(module) = vm.stdlib_inits.get(module) { return Ok(module(&vm.ctx).clone()); @@ -53,7 +53,7 @@ fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult Ok(vm.ctx.new_module(module, scope)) } -fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult { +fn import_module(vm: &mut VirtualMachine, module_name: &str) -> PyResult { // First, see if we already loaded the module: let sys_modules = vm.sys_module.get_item("modules").unwrap(); if let Some(module) = sys_modules.get_item(module_name) { @@ -64,7 +64,7 @@ fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult { Ok(module) } -pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option) -> PyResult { +pub fn import(vm: &mut VirtualMachine, module_name: &str, symbol: &Option) -> PyResult { let module = import_module(vm, module_name)?; // If we're importing a symbol, look it up and use it, otherwise construct a module and return // that @@ -75,7 +75,7 @@ pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option io::Result { +fn find_source(vm: &VirtualMachine, name: &str) -> io::Result { let sys_path = vm.sys_module.get_item("path").unwrap(); let mut paths: Vec = match sys_path.borrow().kind { PyObjectKind::List { ref elements } => elements diff --git a/vm/src/obj/objsequence.rs b/vm/src/obj/objsequence.rs index dc7e39fb1..d76432f1f 100644 --- a/vm/src/obj/objsequence.rs +++ b/vm/src/obj/objsequence.rs @@ -63,12 +63,12 @@ impl PySliceableSequence for Vec { pub fn get_item( vm: &mut VirtualMachine, sequence: &PyObjectRef, - elements: &Vec, + elements: &[PyObjectRef], subscript: PyObjectRef, ) -> PyResult { match &(subscript.borrow()).kind { PyObjectKind::Integer { value } => { - let pos_index = elements.get_pos(*value); + let pos_index = elements.to_vec().get_pos(*value); if pos_index < elements.len() { let obj = elements[pos_index].clone(); Ok(obj) @@ -84,10 +84,10 @@ pub fn get_item( } => Ok(PyObject::new( match &(sequence.borrow()).kind { PyObjectKind::Tuple { elements: _ } => PyObjectKind::Tuple { - elements: elements.get_slice_items(&subscript), + elements: elements.to_vec().get_slice_items(&subscript), }, PyObjectKind::List { elements: _ } => PyObjectKind::List { - elements: elements.get_slice_items(&subscript), + elements: elements.to_vec().get_slice_items(&subscript), }, ref kind => panic!("sequence get_item called for non-sequence: {:?}", kind), }, diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 38e66025c..b45c0ae47 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -147,18 +147,18 @@ impl PySliceableSequence for String { } } -pub fn subscript(vm: &mut VirtualMachine, value: &String, b: PyObjectRef) -> PyResult { +pub fn subscript(vm: &mut VirtualMachine, value: &str, b: PyObjectRef) -> PyResult { // let value = a match &(*b.borrow()).kind { &PyObjectKind::Integer { value: ref pos } => { - let idx = value.get_pos(*pos); + let idx = value.to_string().get_pos(*pos); Ok(vm.new_str(value[idx..idx + 1].to_string())) } &PyObjectKind::Slice { start: _, stop: _, step: _, - } => Ok(vm.new_str(value.get_slice_items(&b))), + } => Ok(vm.new_str(value.to_string().get_slice_items(&b).to_string())), _ => panic!( "TypeError: indexing type {:?} with index {:?} is not supported (yet?)", value, b diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 5a66135ce..9cd7215e2 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -273,7 +273,7 @@ impl PyContext { ) } - pub fn new_class(&self, name: &String, base: PyObjectRef) -> PyObjectRef { + pub fn new_class(&self, name: &str, base: PyObjectRef) -> PyObjectRef { objtype::new(self.type_type(), name, vec![base], self.new_dict()).unwrap() } @@ -289,10 +289,10 @@ impl PyContext { }.into_ref() } - pub fn new_module(&self, name: &String, scope: PyObjectRef) -> PyObjectRef { + pub fn new_module(&self, name: &str, scope: PyObjectRef) -> PyObjectRef { PyObject::new( PyObjectKind::Module { - name: name.clone(), + name: name.to_string(), dict: scope.clone(), }, self.module_type.clone(), diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 46a4317c8..299ea27e3 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -51,7 +51,7 @@ fn create_node(ctx: &PyContext, _name: &str) -> PyObjectRef { node } -fn statements_to_ast(ctx: &PyContext, statements: &Vec) -> PyObjectRef { +fn statements_to_ast(ctx: &PyContext, statements: &[ast::LocatedStatement]) -> PyObjectRef { let mut py_statements = vec![]; for statement in statements { py_statements.push(statement_to_ast(ctx, statement)); diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 1286f2d03..6ab5ca91a 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -219,13 +219,13 @@ impl VirtualMachine { self.current_frame().last_value() } - fn store_name(&mut self, name: &String) -> Option { + fn store_name(&mut self, name: &str) -> Option { let obj = self.pop_value(); self.current_frame_mut().locals.set_item(name, obj); None } - fn load_name(&mut self, name: &String) -> Option { + fn load_name(&mut self, name: &str) -> Option { // Lookup name in scope and put it onto the stack! let mut scope = self.current_frame().locals.clone(); loop { @@ -628,8 +628,8 @@ impl VirtualMachine { } } - fn import(&mut self, module: &String, symbol: &Option) -> Option { - let obj = match import(self, module, symbol) { + fn import(&mut self, module: &str, symbol: &Option) -> Option { + let obj = match import(self, &module.to_string(), symbol) { Ok(value) => value, Err(value) => return Some(Err(value)), }; @@ -643,7 +643,7 @@ impl VirtualMachine { objtype::get_attribute(self, obj.clone(), attr_name) } - fn load_attr(&mut self, attr_name: &String) -> Option { + fn load_attr(&mut self, attr_name: &str) -> Option { let parent = self.pop_value(); match self.get_attribute(parent, attr_name) { Ok(obj) => { @@ -654,7 +654,7 @@ impl VirtualMachine { } } - fn store_attr(&mut self, attr_name: &String) -> Option { + fn store_attr(&mut self, attr_name: &str) -> Option { let parent = self.pop_value(); let value = self.pop_value(); parent.set_attr(attr_name, value);