diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index bd8effaa..9f577fb2 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -25,6 +25,23 @@ jobs: - run: RUSTFLAGS="-D warnings" cargo check --all-targets --features trace - run: RUSTFLAGS="-D warnings" cargo check --all-targets --features _fuzz - run: RUSTFLAGS="-D warnings" cargo check --all-targets --no-default-features + clippy: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - uses: dsherret/rust-toolchain-file@v1 + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-clippy-${{ hashFiles('**/Cargo.lock') }} + - run: RUSTFLAGS="-D warnings" cargo clippy --all-targets + - run: RUSTFLAGS="-D warnings" cargo clippy --all-targets --features trace + - run: RUSTFLAGS="-D warnings" cargo clippy --all-targets --features _fuzz + - run: RUSTFLAGS="-D warnings" cargo clippy --all-targets --no-default-features test: runs-on: ubuntu-latest timeout-minutes: 10 diff --git a/src/ast.rs b/src/ast.rs index 2f45e723..52efcf4e 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -149,16 +149,16 @@ pub const MAX_ADT_FIELDS: usize = MAX_ARITY - 1; impl Net { pub fn trees(&self) -> impl Iterator { - iter::once(&self.root).chain(self.redexes.iter().map(|(x, y)| [x, y]).flatten()) + iter::once(&self.root).chain(self.redexes.iter().flat_map(|(x, y)| [x, y])) } pub fn trees_mut(&mut self) -> impl Iterator { - iter::once(&mut self.root).chain(self.redexes.iter_mut().map(|(x, y)| [x, y]).flatten()) + iter::once(&mut self.root).chain(self.redexes.iter_mut().flat_map(|(x, y)| [x, y])) } } impl Tree { #[inline(always)] - pub fn children(&self) -> impl Iterator + ExactSizeIterator + DoubleEndedIterator { + pub fn children(&self) -> impl ExactSizeIterator + DoubleEndedIterator { ArrayVec::<_, MAX_ARITY>::into_iter(match self { Tree::Era | Tree::Num { .. } | Tree::Ref { .. } | Tree::Var { .. } => array_vec::from_array([]), Tree::Ctr { ports, .. } => array_vec::from_iter(ports), @@ -169,7 +169,7 @@ impl Tree { } #[inline(always)] - pub fn children_mut(&mut self) -> impl Iterator + ExactSizeIterator + DoubleEndedIterator { + pub fn children_mut(&mut self) -> impl ExactSizeIterator + DoubleEndedIterator { ArrayVec::<_, MAX_ARITY>::into_iter(match self { Tree::Era | Tree::Num { .. } | Tree::Ref { .. } | Tree::Var { .. } => array_vec::from_array([]), Tree::Ctr { ports, .. } => array_vec::from_iter(ports), @@ -470,17 +470,14 @@ impl Clone for Tree { fn clone(&self) -> Tree { maybe_grow(|| match self { Tree::Era => Tree::Era, - Tree::Num { val } => Tree::Num { val: val.clone() }, + Tree::Num { val } => Tree::Num { val: *val }, Tree::Ref { nam } => Tree::Ref { nam: nam.clone() }, - Tree::Ctr { lab, ports } => Tree::Ctr { lab: lab.clone(), ports: ports.clone() }, - Tree::Op { op, rhs, out } => Tree::Op { op: op.clone(), rhs: rhs.clone(), out: out.clone() }, + Tree::Ctr { lab, ports } => Tree::Ctr { lab: *lab, ports: ports.clone() }, + Tree::Op { op, rhs, out } => Tree::Op { op: *op, rhs: rhs.clone(), out: out.clone() }, Tree::Mat { zero, succ, out } => Tree::Mat { zero: zero.clone(), succ: succ.clone(), out: out.clone() }, - Tree::Adt { lab, variant_index, variant_count, fields } => Tree::Adt { - lab: lab.clone(), - variant_index: variant_index.clone(), - variant_count: variant_count.clone(), - fields: fields.clone(), - }, + Tree::Adt { lab, variant_index, variant_count, fields } => { + Tree::Adt { lab: *lab, variant_index: *variant_index, variant_count: *variant_count, fields: fields.clone() } + } Tree::Var { nam } => Tree::Var { nam: nam.clone() }, }) } diff --git a/src/compile.rs b/src/compile.rs index bb83d24c..0ed92fc9 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -7,8 +7,8 @@ use crate::{ run::{Instruction, InterpretedDef, LabSet, Port, Tag}, stdlib::HostedDef, }; -use core::hash::Hasher; -use std::{fmt::Write, hash::DefaultHasher}; +use core::{fmt::Write, hash::Hasher}; +use std::hash::DefaultHasher; /// Compiles a [`Host`] to Rust, returning a file to replace `gen.rs`. pub fn compile_host(host: &Host) -> String { diff --git a/src/fuzz.rs b/src/fuzz.rs index ec3c519a..a945c39b 100644 --- a/src/fuzz.rs +++ b/src/fuzz.rs @@ -72,6 +72,7 @@ use crate::prelude::*; +use alloc::sync::Arc; use core::{ any::Any, cell::{OnceCell, RefCell}, @@ -81,7 +82,7 @@ use core::{ sync::atomic, }; use std::{ - sync::{Arc, Condvar, Mutex}, + sync::{Condvar, Mutex}, thread::{self, Scope, ThreadId}, thread_local, }; @@ -460,7 +461,7 @@ impl<'s, 'p: 's, 'e: 's> FuzzScope<'s, 'p, 'e> { } }); while !ready.load(atomic::Ordering::Relaxed) { - std::hint::spin_loop() + hint::spin_loop() } } } diff --git a/src/host.rs b/src/host.rs index 479d3397..7ea67829 100644 --- a/src/host.rs +++ b/src/host.rs @@ -77,7 +77,7 @@ impl Host { for (name, labs) in calculate_label_sets(book, |nam| match self.defs.get(nam) { Some(x) => x.labs.clone(), None => { - self.insert_def(&nam, default_def(nam)); + self.insert_def(nam, default_def(nam)); self.defs[nam].labs.clone() } }) diff --git a/src/lib.rs b/src/lib.rs index 6fc67f4f..f1213e9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,19 @@ #![feature(const_type_id, extern_types, inline_const, generic_const_exprs, new_uninit)] #![cfg_attr(feature = "trace", feature(const_type_name))] -#![allow(non_snake_case, incomplete_features)] #![cfg_attr(not(feature = "std"), no_std)] +#![allow( + non_snake_case, + incomplete_features, + clippy::field_reassign_with_default, + clippy::missing_safety_doc, + clippy::new_ret_no_self +)] +#![warn( + clippy::alloc_instead_of_core, + clippy::std_instead_of_core, + clippy::std_instead_of_alloc, + clippy::absolute_paths +)] extern crate alloc; diff --git a/src/prelude.rs b/src/prelude.rs index 43511de3..e6f23d19 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -6,7 +6,7 @@ pub use alloc::{ vec, vec::Vec, }; -pub use core::{fmt, iter, mem, ptr}; +pub use core::{fmt, hint, iter, mem, ptr}; #[cfg(feature = "std")] pub use std::collections::{hash_map::Entry, HashMap as Map, HashSet as Set}; diff --git a/src/run/allocator.rs b/src/run/allocator.rs index ab6867f4..525aea02 100644 --- a/src/run/allocator.rs +++ b/src/run/allocator.rs @@ -1,5 +1,8 @@ use super::*; +// Really, rust? +use alloc::alloc::alloc; + /// The memory behind a two-word allocation. /// /// This must be aligned to 16 bytes so that the left word's address always ends @@ -41,7 +44,7 @@ impl Heap { return None; } unsafe { - let ptr = alloc::alloc::alloc(Layout::array::(nodes).unwrap()) as *mut Node; + let ptr = alloc(Layout::array::(nodes).unwrap()) as *mut Node; if ptr.is_null() { return None; } @@ -96,7 +99,7 @@ impl<'h> Allocator<'h> { pub fn alloc(&mut self) -> Addr { trace!(self.tracer, self.head); let addr = if self.head != Addr::NULL { - let addr = self.head.clone(); + let addr = self.head; let next = Addr(self.head.val().load(Relaxed) as usize); trace!(self.tracer, next); self.head = next; diff --git a/src/run/def.rs b/src/run/def.rs index a72e508d..7ee9b801 100644 --- a/src/run/def.rs +++ b/src/run/def.rs @@ -145,9 +145,9 @@ impl Def { } #[inline(always)] pub unsafe fn call(slf: *const Def, net: &mut Net, port: Port) { - match net.match_laziness_mut() { - Ok(net) => ((*slf).call_lazy)(slf as *const _, net, port), - Err(net) => ((*slf).call_strict)(slf as *const _, net, port), + match net.as_dyn_mut() { + DynNetMut::Strict(net) => ((*slf).call_strict)(slf as *const _, net, port), + DynNetMut::Lazy(net) => ((*slf).call_lazy)(slf as *const _, net, port), } } } @@ -171,9 +171,9 @@ impl, Port) + Send + Sync + 'static, G: Fn(&mut Net for (F, G) { unsafe fn call(slf: *const Def, net: &mut Net, port: Port) { - match net.match_laziness_mut() { - Ok(net) => ((*slf).data.1)(net, port), - Err(net) => ((*slf).data.0)(net, port), + match net.as_dyn_mut() { + DynNetMut::Strict(net) => ((*slf).data.0)(net, port), + DynNetMut::Lazy(net) => ((*slf).data.1)(net, port), } } } diff --git a/src/run/dyn_net.rs b/src/run/dyn_net.rs index 37a58fd8..ec80a0b5 100644 --- a/src/run/dyn_net.rs +++ b/src/run/dyn_net.rs @@ -1,5 +1,7 @@ use super::{Heap, Lazy, Mode, Net, Strict}; +use crate::prelude::*; + /// A [`Net`] whose mode is determined dynamically, at runtime. /// /// Use [`dispatch_dyn_net!`] to wrap operations on the inner net. @@ -24,12 +26,28 @@ impl<'h> DynNet<'h> { impl<'r, 'h, M: Mode> From<&'r mut Net<'h, M>> for DynNetMut<'r, 'h> { fn from(value: &'r mut Net<'h, M>) -> Self { - match value.match_laziness_mut() { - Ok(net) => DynNetMut::Lazy(net), - Err(net) => DynNetMut::Strict(net), + value.as_dyn_mut() + } +} + +impl<'h, M: Mode> Net<'h, M> { + pub fn as_dyn_mut(&mut self) -> DynNetMut<'_, 'h> { + if M::LAZY { + DynNetMut::Lazy(unsafe { mem::transmute(self) }) + } else { + DynNetMut::Strict(unsafe { mem::transmute(self) }) + } + } + + pub fn into_dyn(self) -> DynNet<'h> { + if M::LAZY { + DynNet::Lazy(unsafe { mem::transmute(self) }) + } else { + DynNet::Strict(unsafe { mem::transmute(self) }) } } } + #[macro_export] macro_rules! dispatch_dyn_net { ($pat:pat = $expr:expr => $body:expr) => { diff --git a/src/run/instruction.rs b/src/run/instruction.rs index 4390eb11..c9794d97 100644 --- a/src/run/instruction.rs +++ b/src/run/instruction.rs @@ -112,6 +112,7 @@ impl<'a, M: Mode> Net<'a, M> { #[inline(always)] pub(crate) fn do_ctr(&mut self, lab: Lab, trg: Trg) -> (Trg, Trg) { let port = trg.target(); + #[allow(clippy::overly_complex_bool_expr)] if !M::LAZY && port.tag() == Ctr && port.lab() == lab { trace!(self.tracer, "fast"); self.free_trg(trg); @@ -119,7 +120,7 @@ impl<'a, M: Mode> Net<'a, M> { self.rwts.anni += 1; (Trg::wire(node.p1), Trg::wire(node.p2)) // TODO: fast copy? - } else if false && !M::LAZY && port.tag() == Num || port.tag() == Ref && lab >= port.lab() { + } else if false && !M::LAZY && (port.tag() == Num || port.tag() == Ref && lab >= port.lab()) { self.rwts.comm += 1; self.free_trg(trg); (Trg::port(port.clone()), Trg::port(port)) @@ -203,12 +204,7 @@ impl<'a, M: Mode> Net<'a, M> { pub(crate) fn do_wires(&mut self) -> (Trg, Trg, Trg, Trg) { let a = self.alloc(); let b = a.other_half(); - ( - Trg::port(Port::new_var(a.clone())), - Trg::wire(Wire::new(a)), - Trg::port(Port::new_var(b.clone())), - Trg::wire(Wire::new(b)), - ) + (Trg::port(Port::new_var(a)), Trg::wire(Wire::new(a)), Trg::port(Port::new_var(b)), Trg::wire(Wire::new(b))) } /// `trg ~ ?<(x (y z)) out>` diff --git a/src/run/linker.rs b/src/run/linker.rs index 82b1091a..a92da3f3 100644 --- a/src/run/linker.rs +++ b/src/run/linker.rs @@ -111,10 +111,8 @@ impl<'h, M: Mode> Linker<'h, M> { trace!(self, a_port, b_port); if a_port.is(Tag::Var) { a_port.wire().set_target(b_port); - } else { - if M::LAZY { - self.set_header(a_port, b_port); - } + } else if M::LAZY { + self.set_header(a_port, b_port); } } @@ -284,11 +282,13 @@ impl Trg { } #[inline(always)] + #[allow(clippy::wrong_self_convention)] pub(super) fn as_wire(self) -> Wire { Wire(self.0.0 as _) } #[inline(always)] + #[allow(clippy::wrong_self_convention)] pub(super) fn as_port(self) -> Port { self.0 } diff --git a/src/run/net.rs b/src/run/net.rs index 89a1a1e6..b5934aaa 100644 --- a/src/run/net.rs +++ b/src/run/net.rs @@ -29,13 +29,6 @@ impl<'h, M: Mode> Net<'h, M> { pub fn boot(&mut self, def: &Def) { self.call(Port::new_ref(def), self.root.as_var()); } - - pub fn match_laziness_mut(&mut self) -> Result<&mut Net<'h, Lazy>, &mut Net<'h, Strict>> { - if M::LAZY { Ok(unsafe { mem::transmute(self) }) } else { Err(unsafe { mem::transmute(self) }) } - } - pub fn match_laziness(self) -> Result, Net<'h, Strict>> { - if M::LAZY { Ok(unsafe { mem::transmute(self) }) } else { Err(unsafe { mem::transmute(self) }) } - } } impl<'a, M: Mode> Net<'a, M> { @@ -99,7 +92,7 @@ impl<'a, M: Mode> Net<'a, M> { prev = main.this.clone(); } - return self.get_target_full(prev); + self.get_target_full(prev) } pub fn normal_from(&mut self, root: Wire) { diff --git a/src/run/node.rs b/src/run/node.rs index fb0116d2..7fdec7e3 100644 --- a/src/run/node.rs +++ b/src/run/node.rs @@ -35,11 +35,7 @@ impl<'a, M: Mode> Net<'a, M> { #[inline(always)] pub fn create_node(&mut self, tag: Tag, lab: Lab) -> CreatedNode { let addr = self.alloc(); - CreatedNode { - p0: Port::new(tag, lab, addr.clone()), - p1: Port::new_var(addr.clone()), - p2: Port::new_var(addr.other_half()), - } + CreatedNode { p0: Port::new(tag, lab, addr), p1: Port::new_var(addr), p2: Port::new_var(addr.other_half()) } } /// Creates a wire an aux port pair. @@ -47,7 +43,7 @@ impl<'a, M: Mode> Net<'a, M> { pub fn create_wire(&mut self) -> (Wire, Port) { let addr = self.alloc(); self.half_free(addr.other_half()); - (Wire::new(addr.clone()), Port::new_var(addr)) + (Wire::new(addr), Port::new_var(addr)) } /// Creates a wire pointing to a given port; sometimes necessary to avoid diff --git a/src/run/parallel.rs b/src/run/parallel.rs index b1369ff2..5c7babb8 100644 --- a/src/run/parallel.rs +++ b/src/run/parallel.rs @@ -18,7 +18,7 @@ impl<'h, M: Mode> Net<'h, M> { (0 .. tids).map(move |tid| { let heap_size = (heap.0.len() / tids) & !63; // round down to needed alignment let heap_start = heap_size * tid; - let area = unsafe { std::mem::transmute(&heap.0[heap_start .. heap_start + heap_size]) }; + let area = unsafe { mem::transmute(&heap.0[heap_start .. heap_start + heap_size]) }; let mut net = Net::new_with_root(area, root.clone()); net.next = next.saturating_sub(heap_start); net.head = if tid == 0 { net.head } else { Addr::NULL }; @@ -54,7 +54,7 @@ impl<'h, M: Mode> Net<'h, M> { } // Initialize global objects - let cores = std::thread::available_parallelism().unwrap().get() as usize; + let cores = thread::available_parallelism().unwrap().get(); let tlog2 = cores.ilog2() as usize; let tids = 1 << tlog2; let delta = AtomicRewrites::default(); // delta rewrite counter @@ -64,7 +64,7 @@ impl<'h, M: Mode> Net<'h, M> { let barry = Arc::new(Barrier::new(tids)); // global barrier // Perform parallel reductions - std::thread::scope(|s| { + thread::scope(|s| { for net in self.fork(tids) { let mut ctx = ThreadContext { tid: net.tid, @@ -133,11 +133,11 @@ impl<'h, M: Mode> Net<'h, M> { let b_len = ctx.rlens[b_tid].load(Relaxed); let send = if a_len > b_len { (a_len - b_len) / 2 } else { 0 }; let recv = if b_len > a_len { (b_len - a_len) / 2 } else { 0 }; - let send = std::cmp::min(send, SHARE_LIMIT); - let recv = std::cmp::min(recv, SHARE_LIMIT); + let send = usize::min(send, SHARE_LIMIT); + let recv = usize::min(recv, SHARE_LIMIT); for i in 0 .. send { let init = a_len - send * 2; - let rdx0 = ctx.net.redexes.slow[init + i * 2 + 0].clone(); + let rdx0 = ctx.net.redexes.slow[init + i * 2].clone(); let rdx1 = ctx.net.redexes.slow[init + i * 2 + 1].clone(); //let init = 0; //let ref0 = ctx.net.redexes.get_unchecked_mut(init + i * 2 + 0); diff --git a/src/stdlib.rs b/src/stdlib.rs index 4c861fb4..f271a20f 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -60,6 +60,7 @@ impl AsHostedDef for LogDef { /// Create a `Host` from a `Book`, including `hvm-core`'s built-in definitions #[cfg(feature = "std")] +#[allow(clippy::absolute_paths)] pub fn create_host(book: &crate::ast::Book) -> Arc> { let host = Arc::new(Mutex::new(Host::default())); host.lock().insert_def("HVM.log", unsafe { @@ -70,7 +71,7 @@ pub fn create_host(book: &crate::ast::Book) -> Arc> { }) }); host.lock().insert_def("HVM.black_box", DefRef::Static(unsafe { &*IDENTITY })); - host.lock().insert_book(&book); + host.lock().insert_book(book); host } @@ -84,11 +85,8 @@ impl BoxDef { /// SAFETY: if port is a ref, it must be a valid pointer. pub unsafe fn try_downcast_box(port: Port) -> Option>> { if port.is(Tag::Ref) { - if let Some(port) = unsafe { Def::downcast_ptr::(port.addr().0 as *const _) } { - Some(unsafe { Box::from_raw(port as *mut Def) }) - } else { - None - } + unsafe { Def::downcast_ptr::(port.addr().0 as *const _) } + .map(|port| unsafe { Box::from_raw(port as *mut Def) }) } else { None } @@ -121,11 +119,8 @@ impl ArcDef { /// SAFETY: if port is a ref, it must be a valid pointer. pub unsafe fn try_downcast_arc(port: Port) -> Option>> { if port.is(Tag::Ref) && port != Port::ERA { - if let Some(port) = unsafe { Def::downcast_ptr::(port.addr().0 as *const _) } { - Some(unsafe { Arc::from_raw(port as *mut Def) }) - } else { - None - } + unsafe { Def::downcast_ptr::(port.addr().0 as *const _) } + .map(|port| unsafe { Arc::from_raw(port as *mut Def) }) } else { None } @@ -182,7 +177,7 @@ pub struct ReadbackDef { } impl ReadbackDef { - fn maybe_finish<'a, 'b>(net: DynNetMut<'a, 'b>, root: Arc) { + fn maybe_finish(net: DynNetMut<'_, '_>, root: Arc) { let Some(root) = Arc::into_inner(root) else { return }; (root)(net) } @@ -266,7 +261,7 @@ pub fn readback( from: Trg, f: impl FnOnce(DynNetMut, Tree) + Send + Sync + 'static, ) { - let root = UniqueTreePtr(Box::leak(Box::new(Tree::default()))); + let root = UniqueTreePtr(Box::leak(Box::default())); if M::LAZY { let from = net.wire_to_trg(from); diff --git a/src/trace.rs b/src/trace.rs index 466e3901..b0fba3b5 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -99,7 +99,7 @@ macro_rules! trace { impl $crate::trace::TraceSourceBearer for __ { const SOURCE: $crate::trace::TraceSource = $crate::trace::TraceSource { func: { - #[cfg(feature = "trace")] { std::any::type_name::<__>() } + #[cfg(feature = "trace")] { core::any::type_name::<__>() } #[cfg(not(feature = "trace"))] { "" } }, file: file!(), @@ -179,6 +179,7 @@ impl TraceData { } } +#[allow(clippy::vec_box)] // the address of `TraceLock` needs to remain stable static ACTIVE_TRACERS: Mutex>> = Mutex::new(Vec::new()); struct TraceWriter { @@ -207,7 +208,7 @@ impl TraceWriter { } fn acquire(&self, cb: impl FnOnce(&mut TraceData)) { while self.lock.locked.compare_exchange_weak(false, true, Ordering::Relaxed, Ordering::Relaxed).is_err() { - core::hint::spin_loop(); + hint::spin_loop(); } cb(unsafe { &mut *self.lock.data.get() }); self.lock.locked.store(false, Ordering::Release); @@ -299,7 +300,7 @@ pub fn _read_traces(limit: usize) { .enumerate() .map(|(i, t)| { while t.locked.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() { - core::hint::spin_loop(); + hint::spin_loop(); } TraceReader::new(unsafe { &*t.data.get() }, i) }) @@ -354,7 +355,7 @@ impl TraceArg for Wire { impl TraceArg for Trg { fn to_word(&self) -> u64 { - self.0.0 as u64 + self.0.0 } fn from_word(word: u64) -> impl Debug { Trg(Port(word)) @@ -465,8 +466,9 @@ pub fn set_hook() { if cfg!(feature = "trace") { #[cfg(feature = "std")] ONCE.call_once(|| { - let hook = std::panic::take_hook(); - std::panic::set_hook(Box::new(move |info| { + use std::panic; + let hook = panic::take_hook(); + panic::set_hook(Box::new(move |info| { hook(info); _read_traces(usize::MAX); })); diff --git a/src/transform.rs b/src/transform.rs index 2bd2c95b..3cfe021c 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -24,7 +24,7 @@ impl Book { } if passes.pre_reduce { if passes.eta_reduce { - for (_, def) in &mut self.nets { + for def in self.nets.values_mut() { def.eta_reduce(); } } @@ -34,7 +34,7 @@ impl Book { opts.pre_reduce_rewrites, ); } - for (_, def) in &mut self.nets { + for def in &mut self.nets.values_mut() { if passes.eta_reduce { def.eta_reduce(); } diff --git a/src/transform/coalesce_ctrs.rs b/src/transform/coalesce_ctrs.rs index aeb0cf3a..2bf7b770 100644 --- a/src/transform/coalesce_ctrs.rs +++ b/src/transform/coalesce_ctrs.rs @@ -16,7 +16,7 @@ impl Tree { Some(Tree::Ctr { lab: inner_lab, ports: inner_ports }) if inner_lab == lab && ports.len() + inner_ports.len() < MAX_ARITY => { - ports.extend(inner_ports.drain(..)); + ports.append(inner_ports); } Some(other) => ports.push(mem::take(other)), None => (), diff --git a/src/transform/eta_reduce.rs b/src/transform/eta_reduce.rs index 57c39c6f..89c09fc3 100644 --- a/src/transform/eta_reduce.rs +++ b/src/transform/eta_reduce.rs @@ -101,8 +101,7 @@ impl<'a> Phase1<'a> { } } Tree::Var { nam } => { - let nam: &str = &*nam; - if let Some(i) = self.vars.get(nam) { + if let Some(i) = self.vars.get(&**nam) { let j = self.nodes.len() as isize; self.nodes.push(NodeType::Var(*i as isize - j)); self.nodes[*i] = NodeType::Var(j - *i as isize); diff --git a/src/transform/pre_reduce.rs b/src/transform/pre_reduce.rs index 84855611..8eb86acc 100644 --- a/src/transform/pre_reduce.rs +++ b/src/transform/pre_reduce.rs @@ -59,7 +59,7 @@ impl Book { }; for nam in self.nets.keys() { - state.pre_reduce(&nam) + state.pre_reduce(nam) } let State { seen, rewrites, .. } = state; @@ -149,7 +149,7 @@ impl<'a> State<'a> { // Move interactions with inert defs back into the net redexes array self.captured_redexes.lock().drain(..).for_each(|r| rt.redux(r.0, r.1)); - let net = self.host.readback(&mut rt); + let net = self.host.readback(&rt); // Mutate the host in-place with the pre-reduced net. let instr = self.host.encode_def(&net); diff --git a/src/util/parse_abbrev_number.rs b/src/util/parse_abbrev_number.rs index edbbf013..32f31957 100644 --- a/src/util/parse_abbrev_number.rs +++ b/src/util/parse_abbrev_number.rs @@ -15,5 +15,5 @@ where Some(_) => (arg, 1), }; let base = base.parse::().map_err(|e| e.to_string())?; - Ok((base * scale).try_into().map_err(|e| format!("{:?}", e))?) + (base * scale).try_into().map_err(|e| format!("{:?}", e)) } diff --git a/src/util/stats.rs b/src/util/stats.rs index fa88c598..6b155556 100644 --- a/src/util/stats.rs +++ b/src/util/stats.rs @@ -1,8 +1,7 @@ use crate::prelude::*; -use core::time::Duration; - use crate::run::Rewrites; +use core::{str, time::Duration}; pub fn show_rewrites(rwts: &Rewrites) -> String { format!( @@ -25,12 +24,13 @@ pub fn show_stats(rwts: &Rewrites, elapsed: Duration) -> String { ) } +#[rustfmt::skip] // utterly unreadable on one line fn pretty_num(n: u64) -> String { n.to_string() .as_bytes() .rchunks(3) .rev() - .map(|x| core::str::from_utf8(x).unwrap()) + .map(|x| str::from_utf8(x).unwrap()) .flat_map(|x| ["_", x]) .skip(1) .collect() diff --git a/tests/cli.rs b/tests/cli.rs index e82f6246..d89ba9dc 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -14,7 +14,7 @@ use hvmc::{ use insta::assert_display_snapshot; fn get_arithmetic_program_path() -> String { - return env!("CARGO_MANIFEST_DIR").to_owned() + "/examples/arithmetic.hvmc"; + env!("CARGO_MANIFEST_DIR").to_owned() + "/examples/arithmetic.hvmc" } fn execute_hvmc(args: &[&str]) -> Result<(ExitStatus, String), Box> { @@ -216,35 +216,34 @@ fn test_apply_tree() { let root_port = run::Trg::port(run::Port::new_var(rnet.root.addr())); host.encode_net(&mut rnet, root_port, &fun); rnet.normal(); - let got_result = host.readback(&rnet); - got_result + host.readback(&rnet) } assert_display_snapshot!( - eval_with_args("(a a)", &vec!["(a a)"]), + eval_with_args("(a a)", &["(a a)"]), @"(a a)" ); assert_display_snapshot!( - eval_with_args("b & (a b) ~ a", &vec!["(a a)"]), + eval_with_args("b & (a b) ~ a", &["(a a)"]), @"a" ); assert_display_snapshot!( - eval_with_args("(z0 z0)", &vec!["(z1 z1)"]), + eval_with_args("(z0 z0)", &["(z1 z1)"]), @"(a a)" ); assert_display_snapshot!( - eval_with_args("(* #1)", &vec!["(a a)"]), + eval_with_args("(* #1)", &["(a a)"]), @"#1" ); assert_display_snapshot!( - eval_with_args("(<+ a b> (a b))", &vec!["#1", "#2"]), + eval_with_args("(<+ a b> (a b))", &["#1", "#2"]), @"#3" ); assert_display_snapshot!( - eval_with_args("(<* a b> (a b))", &vec!["#2", "#3"]), + eval_with_args("(<* a b> (a b))", &["#2", "#3"]), @"#6" ); assert_display_snapshot!( - eval_with_args("(<* a b> (a b))", &vec!["#2"]), + eval_with_args("(<* a b> (a b))", &["#2"]), @"(<* #2 a> a)" ); } @@ -254,7 +253,7 @@ fn test_cli_compile() { // Test normal-form expressions if !Command::new(env!("CARGO_BIN_EXE_hvmc")) - .args(&["compile", &get_arithmetic_program_path()]) + .args(["compile", &get_arithmetic_program_path()]) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .spawn() @@ -268,7 +267,7 @@ fn test_cli_compile() { let mut output_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); output_path.push("examples/arithmetic"); - let mut child = Command::new(&output_path).args(&["#40", "#3"]).stdout(Stdio::piped()).spawn().unwrap(); + let mut child = Command::new(&output_path).args(["#40", "#3"]).stdout(Stdio::piped()).spawn().unwrap(); let mut stdout = child.stdout.take().ok_or("Couldn't capture stdout!").unwrap(); child.wait().unwrap(); diff --git a/tests/fuzz.rs b/tests/fuzz.rs index 3d3ef5a4..651db8a9 100644 --- a/tests/fuzz.rs +++ b/tests/fuzz.rs @@ -23,11 +23,11 @@ fn fuzz_var_link_link_var() { let x = net.alloc(); let y = net.alloc(); let z = net.alloc(); - let a = Port::new_var(x.clone()); + let a = Port::new_var(x); let b = Port::new_var(x.other_half()); - let c = Port::new_var(y.clone()); + let c = Port::new_var(y); let d = Port::new_var(y.other_half()); - let e = Port::new_var(z.clone()); + let e = Port::new_var(z); let f = Port::new_var(z.other_half()); net.link_port_port(a.clone(), b.clone()); net.link_port_port(c.clone(), d.clone()); @@ -66,7 +66,7 @@ fn fuzz_pri_link_link_pri() { let heap = Heap::new_exact(16).unwrap(); let mut net = Net::new(&heap); let x = net.alloc(); - let a = Port::new_var(x.clone()); + let a = Port::new_var(x); let b = Port::new_var(x.other_half()); net.link_port_port(a.clone(), b.clone()); let mut nets = net.fork(2); @@ -98,9 +98,9 @@ fn fuzz_var_link_link_pri() { let mut net = Net::new(&heap); let x = net.alloc(); let y = net.alloc(); - let a = Port::new_var(x.clone()); + let a = Port::new_var(x); let b = Port::new_var(x.other_half()); - let c = Port::new_var(y.clone()); + let c = Port::new_var(y); let d = Port::new_var(y.other_half()); net.link_port_port(a.clone(), b.clone()); net.link_port_port(c.clone(), d.clone()); @@ -141,13 +141,13 @@ fn fuzz_var_link_link_link_var() { let y = net.alloc(); let z = net.alloc(); let w = net.alloc(); - let a = Port::new_var(x.clone()); + let a = Port::new_var(x); let b = Port::new_var(x.other_half()); - let c = Port::new_var(y.clone()); + let c = Port::new_var(y); let d = Port::new_var(y.other_half()); - let e = Port::new_var(z.clone()); + let e = Port::new_var(z); let f = Port::new_var(z.other_half()); - let g = Port::new_var(w.clone()); + let g = Port::new_var(w); let h = Port::new_var(w.other_half()); net.link_port_port(a.clone(), b.clone()); net.link_port_port(c.clone(), d.clone()); diff --git a/tests/lists.rs b/tests/lists.rs index f1dcfe2f..02d3ce0e 100644 --- a/tests/lists.rs +++ b/tests/lists.rs @@ -11,7 +11,7 @@ fn list_got(index: u32) -> Book { let def = book.get_mut("GenGotIndex").unwrap(); def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("S{index}") }); let def = book.get_mut("main").unwrap(); - def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("GenGotIndex") }); + def.apply_tree(hvmc::ast::Tree::Ref { nam: "GenGotIndex".to_string() }); book } @@ -22,7 +22,7 @@ fn list_put(index: u32, value: u32) -> Book { def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("S{index}") }); def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("S{value}") }); let def = book.get_mut("main").unwrap(); - def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("GenPutIndexValue") }); + def.apply_tree(hvmc::ast::Tree::Ref { nam: "GenPutIndexValue".to_string() }); book } diff --git a/tests/tests.rs b/tests/tests.rs index bbde5091..8e5124e5 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -112,7 +112,7 @@ fn test_pre_reduce_run(path: &str, mut book: Book) { } fn test_path(path: &Path) { - let code = fs::read_to_string(&path).unwrap(); + let code = fs::read_to_string(path).unwrap(); let book = ast::Book::from_str(&code).unwrap(); let host = hvmc::stdlib::create_host(&book);