From e7b4d06ec0a9ebe55b1002395ab4db0b79012df8 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 6 Nov 2018 15:51:01 +0100 Subject: [PATCH] Fixed all Rust code warnings --- src/build_spectests.rs | 90 ++++++++++++++++++----------------- src/integrations/mod.rs | 11 ++--- src/macros.rs | 4 +- src/main.rs | 15 +++--- src/sighandler.rs | 6 +-- src/spectests/_common.rs | 5 +- src/webassembly/instance.rs | 16 +++---- src/webassembly/mod.rs | 9 ++-- src/webassembly/module.rs | 25 +++++----- src/webassembly/relocation.rs | 1 - 10 files changed, 86 insertions(+), 96 deletions(-) diff --git a/src/build_spectests.rs b/src/build_spectests.rs index a4090d6f844..21bb80c52ab 100644 --- a/src/build_spectests.rs +++ b/src/build_spectests.rs @@ -4,9 +4,7 @@ use std::collections::HashMap; use std::env; use std::fs; -use std::io::{self, Read}; use std::path::PathBuf; -use std::time::SystemTime; use wabt::script::{Action, Command, CommandKind, ModuleBinary, ScriptParser, Value}; use wabt::wasm2wat; @@ -74,10 +72,10 @@ const TESTS: [&str; 54] = [ fn wabt2rust_type(v: &Value) -> String { match v { - Value::I32(v) => format!("i32"), - Value::I64(v) => format!("i64"), - Value::F32(v) => format!("f32"), - Value::F64(v) => format!("f64"), + Value::I32(_v) => format!("i32"), + Value::I64(_v) => format!("i64"), + Value::F32(_v) => format!("f32"), + Value::F64(_v) => format!("f64"), } } @@ -95,32 +93,30 @@ fn wabt2rust_value(v: &Value) -> String { Value::I32(v) => format!("{:?} as i32", v), Value::I64(v) => format!("{:?} as i64", v), Value::F32(v) => { - match *v { - std::f32::INFINITY => "f32::INFINITY".to_string(), - std::f32::NEG_INFINITY => "f32::NEG_INFINITY".to_string(), - // std::f32::NAN => "f32::NAN".to_string(), - _ => { - if v.is_nan() { - // Support for non-canonical NaNs - format!("f32::from_bits({:?})", v.to_bits()) - } else { - format!("{:?} as f32", v) - } + if v.is_infinite() { + if v.is_sign_negative() { + "f32::NEG_INFINITY".to_string() + } else { + "f32::INFINITY".to_string() } + } else if v.is_nan() { + // Support for non-canonical NaNs + format!("f32::from_bits({:?})", v.to_bits()) + } else { + format!("{:?} as f32", v) } } Value::F64(v) => { - match *v { - std::f64::INFINITY => "f64::INFINITY".to_string(), - std::f64::NEG_INFINITY => "f64::NEG_INFINITY".to_string(), - // std::f64::NAN => "f64::NAN".to_string(), - _ => { - if v.is_nan() { - format!("f64::from_bits({:?})", v.to_bits()) - } else { - format!("{:?} as f64", v) - } + if v.is_infinite() { + if v.is_sign_negative() { + "f64::NEG_INFINITY".to_string() + } else { + "f64::INFINITY".to_string() } + } else if v.is_nan() { + format!("f64::from_bits({:?})", v.to_bits()) + } else { + format!("{:?} as f64", v) } } } @@ -140,9 +136,8 @@ impl WastTestGenerator { fn new(path: &PathBuf) -> Self { let filename = path.file_name().unwrap().to_str().unwrap(); let source = fs::read(&path).unwrap(); - let mut script: ScriptParser = - ScriptParser::from_source_and_name(&source, filename).unwrap(); - let mut buffer = String::new(); + let script: ScriptParser = ScriptParser::from_source_and_name(&source, filename).unwrap(); + let buffer = String::new(); WastTestGenerator { last_module: 0, last_line: 0, @@ -216,7 +211,7 @@ fn test_module_{}() {{ self.module_calls.remove(&module); } - fn visit_module(&mut self, module: &ModuleBinary, name: &Option) { + fn visit_module(&mut self, module: &ModuleBinary, _name: &Option) { let wasm_binary: Vec = module.clone().into_vec(); let wast_string = wasm2wat(wasm_binary).expect("Can't convert back to wasm"); self.flush_module_calls(self.last_module); @@ -280,12 +275,12 @@ fn {}_assert_invalid() {{ fn visit_assert_return_arithmetic_nan(&mut self, action: &Action) { match action { Action::Invoke { - module, + module: _, field, args, } => { - let mut return_type = wabt2rust_type(&args[0]); - let mut func_return = format!(" -> {}", return_type); + let return_type = wabt2rust_type(&args[0]); + let func_return = format!(" -> {}", return_type); let assertion = String::from("assert!(result.is_quiet_nan())"); // We map the arguments provided into the raw Arguments provided @@ -333,16 +328,16 @@ fn {}_assert_invalid() {{ fn visit_assert_return_canonical_nan(&mut self, action: &Action) { match action { Action::Invoke { - module, + module: _, field, args, } => { - let mut return_type = match &field.as_str() { + let return_type = match &field.as_str() { &"f64.promote_f32" => String::from("f64"), &"f32.promote_f64" => String::from("f32"), _ => wabt2rust_type(&args[0]), }; - let mut func_return = format!(" -> {}", return_type); + let func_return = format!(" -> {}", return_type); let assertion = String::from("assert!(result.is_quiet_nan())"); // We map the arguments provided into the raw Arguments provided @@ -409,7 +404,7 @@ fn {}_assert_malformed() {{ fn visit_action(&mut self, action: &Action, expected: Option<&Vec>) -> Option { match action { Action::Invoke { - module, + module: _, field, args, } => { @@ -554,16 +549,25 @@ fn {}() {{ CommandKind::AssertMalformed { module, message: _ } => { self.visit_assert_malformed(module); } - CommandKind::AssertUninstantiable { module, message: _ } => { + CommandKind::AssertUninstantiable { + module: _, + message: _, + } => { // Do nothing for now } - CommandKind::AssertExhaustion { action } => { + CommandKind::AssertExhaustion { action: _ } => { // Do nothing for now } - CommandKind::AssertUnlinkable { module, message: _ } => { + CommandKind::AssertUnlinkable { + module: _, + message: _, + } => { // Do nothing for now } - CommandKind::Register { name, as_name } => { + CommandKind::Register { + name: _, + as_name: _, + } => { // Do nothing for now } CommandKind::PerformAction(action) => { @@ -592,7 +596,7 @@ fn wast_to_rust(wast_filepath: &str) -> (String, i32) { .expect("Can't get wast file metadata") .modified() .expect("Can't get wast file modified date"); - let should_modify = match fs::metadata(&rust_test_filepath) { + let _should_modify = match fs::metadata(&rust_test_filepath) { Ok(m) => { m.modified() .expect("Can't get rust test file modified date") diff --git a/src/integrations/mod.rs b/src/integrations/mod.rs index dea6d42b279..a162ec441ce 100644 --- a/src/integrations/mod.rs +++ b/src/integrations/mod.rs @@ -1,7 +1,5 @@ use crate::webassembly::{ImportObject, VmCtx}; use libc::{printf, putchar}; -use std::io; -use std::io::Write; extern "C" fn _printf(memory_offset: i32, extra: i32, vm_context: &mut VmCtx) -> i32 { // println!("PRINTF"); @@ -23,10 +21,7 @@ pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> { #[cfg(test)] mod tests { use super::generate_libc_env; - use crate::webassembly::{ - instantiate, ErrorKind, Export, ImportObject, Instance, Module, ResultObject, VmCtx, - }; - use libc::putchar; + use crate::webassembly::{instantiate, Export, VmCtx}; #[test] fn test_putchar() { @@ -34,7 +29,7 @@ mod tests { let import_object = generate_libc_env(); let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly"); let module = result_object.module; - let mut instance = result_object.instance; + let instance = result_object.instance; let func_index = match module.info.exports.get("main") { Some(&Export::Function(index)) => index, _ => panic!("Function not found"), @@ -50,7 +45,7 @@ mod tests { let import_object = generate_libc_env(); let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly"); let module = result_object.module; - let mut instance = result_object.instance; + let instance = result_object.instance; let func_index = match module.info.exports.get("main") { Some(&Export::Function(index)) => index, _ => panic!("Function not found"), diff --git a/src/macros.rs b/src/macros.rs index c385cf75d71..c5defe6fea8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -19,8 +19,8 @@ macro_rules! get_instance_function { macro_rules! include_wast2wasm_bytes { ($x:expr) => {{ use wabt::wat2wasm; - const wast_bytes: &[u8] = include_bytes!($x); - wat2wasm(wast_bytes.to_vec()).expect(&format!("Can't convert {} file to wasm", $x)) + const WAST_BYTES: &[u8] = include_bytes!($x); + wat2wasm(WAST_BYTES.to_vec()).expect(&format!("Can't convert {} file to wasm", $x)) }}; } diff --git a/src/main.rs b/src/main.rs index 77a8fcf93be..63c23eadaca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,11 @@ extern crate test; #[macro_use] extern crate error_chain; -#[macro_use] -extern crate structopt; extern crate cranelift_codegen; extern crate cranelift_entity; extern crate cranelift_native; extern crate cranelift_wasm; +extern crate structopt; extern crate wabt; #[macro_use] extern crate target_lexicon; @@ -16,14 +15,14 @@ extern crate nix; extern crate spin; // use std::alloc::System; -use std::time::{Duration, Instant}; +// use std::time::{Duration, Instant}; // #[global_allocator] // static A: System = System; // #[macro_use] extern crate log; -use libc; +// use libc; use std::error::Error; use std::fs::File; use std::io; @@ -78,11 +77,9 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> { } let import_object = integrations::generate_libc_env(); - let webassembly::ResultObject { - module, - mut instance, - } = webassembly::instantiate(wasm_binary, import_object) - .map_err(|err| String::from(err.description()))?; + let webassembly::ResultObject { module, instance } = + webassembly::instantiate(wasm_binary, import_object) + .map_err(|err| String::from(err.description()))?; let func_index = instance .start_func .unwrap_or_else(|| match module.info.exports.get("main") { diff --git a/src/sighandler.rs b/src/sighandler.rs index 24aad078bed..123487e94aa 100644 --- a/src/sighandler.rs +++ b/src/sighandler.rs @@ -18,13 +18,13 @@ pub unsafe fn install_sighandler() { sigaction(SIGILL, &sa).unwrap(); sigaction(SIGSEGV, &sa).unwrap(); sigaction(SIGBUS, &sa).unwrap(); - let result = setjmp((&mut setjmp_buffer[..]).as_mut_ptr() as *mut ::nix::libc::c_void); + let result = setjmp((&mut SETJMP_BUFFER[..]).as_mut_ptr() as *mut ::nix::libc::c_void); if result != 0 { panic!("Signal Error: {}", result); } } -static mut setjmp_buffer: [::nix::libc::c_int; 27] = [0; 27]; +static mut SETJMP_BUFFER: [::nix::libc::c_int; 27] = [0; 27]; extern "C" { fn setjmp(env: *mut ::nix::libc::c_void) -> ::nix::libc::c_int; fn longjmp(env: *mut ::nix::libc::c_void, val: ::nix::libc::c_int); @@ -32,7 +32,7 @@ extern "C" { extern "C" fn signal_trap_handler(_: ::nix::libc::c_int) { unsafe { longjmp( - (&mut setjmp_buffer).as_mut_ptr() as *mut ::nix::libc::c_void, + (&mut SETJMP_BUFFER).as_mut_ptr() as *mut ::nix::libc::c_void, 3, ); } diff --git a/src/spectests/_common.rs b/src/spectests/_common.rs index a9b4510b90d..e20de8b5fed 100644 --- a/src/spectests/_common.rs +++ b/src/spectests/_common.rs @@ -38,7 +38,7 @@ pub trait NaNCheck { impl NaNCheck for f32 { /// The MSB of the mantissa must be set for a NaN to be a quiet NaN. fn is_quiet_nan(&self) -> bool { - let bit_mask = (0b1 << 22); // Used to check if 23rd bit is set, which is MSB of the mantissa + let bit_mask = 0b1 << 22; // Used to check if 23rd bit is set, which is MSB of the mantissa self.is_nan() && (self.to_bits() & bit_mask) == bit_mask } @@ -53,7 +53,7 @@ impl NaNCheck for f32 { impl NaNCheck for f64 { /// The MSB of the mantissa must be set for a NaN to be a quiet NaN. fn is_quiet_nan(&self) -> bool { - let bit_mask = (0b1 << 51); // Used to check if 51st bit is set, which is MSB of the mantissa + let bit_mask = 0b1 << 51; // Used to check if 51st bit is set, which is MSB of the mantissa self.is_nan() && (self.to_bits() & bit_mask) == bit_mask } @@ -65,4 +65,3 @@ impl NaNCheck for f64 { (self.to_bits() & bit_mask) == bit_mask } } - diff --git a/src/webassembly/instance.rs b/src/webassembly/instance.rs index 350c3e577df..62b92040970 100644 --- a/src/webassembly/instance.rs +++ b/src/webassembly/instance.rs @@ -14,8 +14,8 @@ use region; use std::iter::Iterator; use std::marker::PhantomData; use std::ptr::write_unaligned; +use std::slice; use std::sync::Arc; -use std::{mem, slice}; use super::super::common::slice::{BoundedSlice, UncheckedSlice}; use super::errors::ErrorKind; @@ -59,7 +59,7 @@ fn get_function_addr( } // #[derive(Debug)] -#[repr(C, packed)] +#[repr(C)] pub struct VmCtx<'phantom> { pub user_data: UserData, globals: UncheckedSlice, @@ -69,7 +69,7 @@ pub struct VmCtx<'phantom> { } // #[derive(Debug)] -#[repr(C, packed)] +#[repr(C)] pub struct UserData { // pub process: Dispatch, pub instance: Instance, @@ -126,7 +126,7 @@ impl Instance { // We walk through the imported functions and set the relocations // for each of this functions to be an empty vector (as is defined outside of wasm) for (module, field) in module.info.imported_funcs.iter() { - let mut function = import_object + let function = import_object .get(&module.as_str(), &field.as_str()) .ok_or_else(|| { ErrorKind::LinkError(format!( @@ -163,7 +163,7 @@ impl Instance { .compile_and_emit(&*isa, &mut code_buf, &mut reloc_sink, &mut trap_sink) .map_err(|e| ErrorKind::CompileError(e.to_string()))?; // We set this code_buf to be readable & executable - protect_codebuf(&code_buf); + protect_codebuf(&code_buf).unwrap(); let func_offset = code_buf; functions.push(func_offset); @@ -334,7 +334,7 @@ impl Instance { } for init in &module.info.data_initializers { debug_assert!(init.base.is_none(), "globalvar base not supported yet"); - let mut offset = init.offset; + let offset = init.offset; let mem_mut = memories[init.memory_index].as_mut(); let to_init = &mut mem_mut[offset..offset + init.data.len()]; to_init.copy_from_slice(&init.data); @@ -359,7 +359,7 @@ impl Instance { GlobalInit::I64Const(n) => n, GlobalInit::F32Const(f) => f as _, // unsafe { mem::transmute(f as f64) }, GlobalInit::F64Const(f) => f as _, // unsafe { mem::transmute(f) }, - GlobalInit::GlobalRef(global_index) => { + GlobalInit::GlobalRef(_global_index) => { unimplemented!("GlobalInit::GlobalRef is not yet supported") } GlobalInit::Import() => { @@ -484,7 +484,7 @@ impl Clone for Instance { } extern "C" fn grow_memory(size: u32, memory_index: u32, vmctx: &mut VmCtx) -> i32 { - let mut instance = &mut vmctx.user_data.instance; + let instance = &mut vmctx.user_data.instance; instance .memory_mut(memory_index as usize) .grow(size) diff --git a/src/webassembly/mod.rs b/src/webassembly/mod.rs index 9fed5061306..b6951492217 100644 --- a/src/webassembly/mod.rs +++ b/src/webassembly/mod.rs @@ -6,12 +6,9 @@ pub mod module; pub mod relocation; pub mod utils; -use cranelift_native; use std::panic; -use std::ptr; use std::str::FromStr; -use std::time::{Duration, Instant}; -use target_lexicon::{self, Triple}; +use target_lexicon; use wasmparser; pub use self::errors::{Error, ErrorKind}; @@ -58,8 +55,8 @@ pub fn instantiate( /// a WebAssembly module directly from a streamed underlying source. /// This is the most efficient, optimized way to load wasm code. pub fn instantiate_streaming( - buffer_source: Vec, - import_object: ImportObject<&str, &str>, + _buffer_source: Vec, + _import_object: ImportObject<&str, &str>, ) -> Result { unimplemented!(); } diff --git a/src/webassembly/module.rs b/src/webassembly/module.rs index c40f62cf910..cef12a4ac15 100644 --- a/src/webassembly/module.rs +++ b/src/webassembly/module.rs @@ -10,8 +10,7 @@ use cranelift_codegen::cursor::FuncCursor; use cranelift_codegen::ir::immediates::{Imm64, Offset32}; use cranelift_codegen::ir::types::*; use cranelift_codegen::ir::{ - self, AbiParam, ArgumentPurpose, ExtFuncData, ExternalName, FuncRef, Function, InstBuilder, - Signature, + self, AbiParam, ArgumentPurpose, ExtFuncData, ExternalName, FuncRef, InstBuilder, Signature, }; use cranelift_codegen::print_errors::pretty_verifier_error; use cranelift_codegen::settings::CallConv; @@ -244,9 +243,9 @@ impl Module { FuncEnvironment::new(&self.info) //, self.return_mode) } - fn native_pointer(&self) -> ir::Type { - self.func_env().pointer_type() - } + // fn native_pointer(&self) -> ir::Type { + // self.func_env().pointer_type() + // } /// Convert a `DefinedFuncIndex` into a `FuncIndex`. pub fn func_index(&self, defined_func: DefinedFuncIndex) -> FuncIndex { @@ -293,12 +292,12 @@ impl<'environment> FuncEnvironment<'environment> { } } - fn get_real_call_args(func: &Function, call_args: &[ir::Value]) -> Vec { - let mut real_call_args = Vec::with_capacity(call_args.len() + 1); - real_call_args.extend_from_slice(call_args); - real_call_args.push(func.special_param(ArgumentPurpose::VMContext).unwrap()); - real_call_args - } + // fn get_real_call_args(func: &Function, call_args: &[ir::Value]) -> Vec { + // let mut real_call_args = Vec::with_capacity(call_args.len() + 1); + // real_call_args.extend_from_slice(call_args); + // real_call_args.push(func.special_param(ArgumentPurpose::VMContext).unwrap()); + // real_call_args + // } // Create a signature for `sigidx` amended with a `vmctx` argument after the standard wasm // arguments. @@ -562,7 +561,7 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> { &mut self, mut pos: FuncCursor, index: MemoryIndex, - heap: ir::Heap, + _heap: ir::Heap, val: ir::Value, ) -> WasmResult { debug_assert_eq!(index, 0, "non-default memories not supported yet"); @@ -597,7 +596,7 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> { &mut self, mut pos: FuncCursor, index: MemoryIndex, - heap: ir::Heap, + _heap: ir::Heap, ) -> WasmResult { debug_assert_eq!(index, 0, "non-default memories not supported yet"); let cur_mem_func = self.mod_info.current_memory_extfunc.unwrap_or_else(|| { diff --git a/src/webassembly/relocation.rs b/src/webassembly/relocation.rs index 5a9d00d6a12..118204fb19c 100644 --- a/src/webassembly/relocation.rs +++ b/src/webassembly/relocation.rs @@ -6,7 +6,6 @@ use cranelift_codegen::binemit; use cranelift_codegen::ir::{self, ExternalName, LibCall, SourceLoc, TrapCode}; pub use cranelift_codegen::binemit::Reloc; -use cranelift_wasm::FuncIndex; #[derive(Debug, Clone)] pub struct Relocation {