diff --git a/contrib/snixbolt/Cargo.nix b/contrib/snixbolt/Cargo.nix index ac5388830..85e67eb84 100644 --- a/contrib/snixbolt/Cargo.nix +++ b/contrib/snixbolt/Cargo.nix @@ -4031,7 +4031,7 @@ rec { "snix-eval" = rec { crateName = "snix-eval"; version = "0.1.0"; - edition = "2021"; + edition = "2024"; src = lib.cleanSourceWith { filter = sourceFilter; src = ../../snix/eval; }; libName = "snix_eval"; dependencies = [ @@ -4166,7 +4166,7 @@ rec { "snix-eval-builtin-macros" = rec { crateName = "snix-eval-builtin-macros"; version = "0.0.1"; - edition = "2021"; + edition = "2024"; src = lib.cleanSourceWith { filter = sourceFilter; src = ../../snix/eval/builtin-macros; }; procMacro = true; libName = "snix_eval_builtin_macros"; diff --git a/snix/Cargo.nix b/snix/Cargo.nix index 712f9367c..a415d62bd 100644 --- a/snix/Cargo.nix +++ b/snix/Cargo.nix @@ -14138,7 +14138,7 @@ rec { "snix-eval" = rec { crateName = "snix-eval"; version = "0.1.0"; - edition = "2021"; + edition = "2024"; src = lib.cleanSourceWith { filter = sourceFilter; src = ./eval; }; libName = "snix_eval"; dependencies = [ @@ -14307,7 +14307,7 @@ rec { "snix-eval-builtin-macros" = rec { crateName = "snix-eval-builtin-macros"; version = "0.0.1"; - edition = "2021"; + edition = "2024"; src = lib.cleanSourceWith { filter = sourceFilter; src = ./eval/builtin-macros; }; procMacro = true; libName = "snix_eval_builtin_macros"; diff --git a/snix/eval/Cargo.toml b/snix/eval/Cargo.toml index 1d907ae75..6a29197cc 100644 --- a/snix/eval/Cargo.toml +++ b/snix/eval/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "snix-eval" version = "0.1.0" -edition = "2021" +edition = "2024" [lib] name = "snix_eval" diff --git a/snix/eval/benches/eval.rs b/snix/eval/benches/eval.rs index 35a8082cb..79d503017 100644 --- a/snix/eval/benches/eval.rs +++ b/snix/eval/benches/eval.rs @@ -1,4 +1,4 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use criterion::{Criterion, black_box, criterion_group, criterion_main}; use itertools::Itertools; use mimalloc::MiMalloc; diff --git a/snix/eval/builtin-macros/Cargo.toml b/snix/eval/builtin-macros/Cargo.toml index 8091658ce..242e9f739 100644 --- a/snix/eval/builtin-macros/Cargo.toml +++ b/snix/eval/builtin-macros/Cargo.toml @@ -2,7 +2,7 @@ name = "snix-eval-builtin-macros" version = "0.0.1" authors = ["Griffin Smith "] -edition = "2021" +edition = "2024" [dependencies] syn = { version = "1.0.109", features = [ diff --git a/snix/eval/builtin-macros/rustfmt.toml b/snix/eval/builtin-macros/rustfmt.toml new file mode 100644 index 000000000..4dfc1d2b7 --- /dev/null +++ b/snix/eval/builtin-macros/rustfmt.toml @@ -0,0 +1,2 @@ +# FUTUREWORK: move to .. once all crates are migrated (#114) +edition = "2024" diff --git a/snix/eval/builtin-macros/src/lib.rs b/snix/eval/builtin-macros/src/lib.rs index e12ba1aeb..592fa81a0 100644 --- a/snix/eval/builtin-macros/src/lib.rs +++ b/snix/eval/builtin-macros/src/lib.rs @@ -2,12 +2,12 @@ extern crate proc_macro; use proc_macro::TokenStream; use proc_macro2::Span; -use quote::{quote, quote_spanned, ToTokens}; +use quote::{ToTokens, quote, quote_spanned}; use syn::parse::Parse; use syn::spanned::Spanned; use syn::{ - parse2, parse_macro_input, parse_quote, parse_quote_spanned, Attribute, FnArg, Ident, Item, - ItemMod, LitStr, Meta, Pat, PatIdent, PatType, Token, Type, + Attribute, FnArg, Ident, Item, ItemMod, LitStr, Meta, Pat, PatIdent, PatType, Token, Type, + parse_macro_input, parse_quote, parse_quote_spanned, parse2, }; /// Description of a single argument passed to a builtin @@ -188,7 +188,9 @@ pub fn builtins(args: TokenStream, item: TokenStream) -> TokenStream { if let Pat::Ident(PatIdent { ident, .. }) = pat.as_ref() { if *ident == "state" { if state_type.is_none() { - panic!("builtin captures a `state` argument, but no state type was defined"); + panic!( + "builtin captures a `state` argument, but no state type was defined" + ); } captures_state = true; diff --git a/snix/eval/rustfmt.toml b/snix/eval/rustfmt.toml new file mode 100644 index 000000000..4dfc1d2b7 --- /dev/null +++ b/snix/eval/rustfmt.toml @@ -0,0 +1,2 @@ +# FUTUREWORK: move to .. once all crates are migrated (#114) +edition = "2024" diff --git a/snix/eval/src/builtins/hash.rs b/snix/eval/src/builtins/hash.rs index d0145f1e7..99073a3f4 100644 --- a/snix/eval/src/builtins/hash.rs +++ b/snix/eval/src/builtins/hash.rs @@ -2,7 +2,7 @@ use bstr::ByteSlice; use data_encoding::HEXLOWER; use md5::Md5; use sha1::Sha1; -use sha2::{digest::Output, Digest, Sha256, Sha512}; +use sha2::{Digest, Sha256, Sha512, digest::Output}; use crate::ErrorKind; diff --git a/snix/eval/src/builtins/impure.rs b/snix/eval/src/builtins/impure.rs index 8e6385624..0cac27dfc 100644 --- a/snix/eval/src/builtins/impure.rs +++ b/snix/eval/src/builtins/impure.rs @@ -4,11 +4,10 @@ use genawaiter::rc::Gen; use std::time::{SystemTime, UNIX_EPOCH}; use crate::{ - self as snix_eval, + self as snix_eval, NixString, Value, errors::ErrorKind, value::NixAttrs, vm::generators::{self, GenCo}, - NixString, Value, }; #[builtins] diff --git a/snix/eval/src/builtins/mod.rs b/snix/eval/src/builtins/mod.rs index a71b20f72..552c544b2 100644 --- a/snix/eval/src/builtins/mod.rs +++ b/snix/eval/src/builtins/mod.rs @@ -101,12 +101,12 @@ fn cached_regex(pattern: &str) -> Result { mod pure_builtins { use std::ffi::OsString; - use bstr::{BString, ByteSlice, B}; + use bstr::{B, BString, ByteSlice}; use itertools::Itertools; use os_str_bytes::OsStringBytes; use rustc_hash::{FxHashMap, FxHashSet}; - use crate::{value::PointerEquality, AddContext, NixContext, NixContextElement}; + use crate::{AddContext, NixContext, NixContextElement, value::PointerEquality}; use super::*; @@ -355,11 +355,7 @@ mod pure_builtins { .rfind_char('/') .map(|last_slash| { let x = &str[..last_slash]; - if x.is_empty() { - B("/") - } else { - x - } + if x.is_empty() { B("/") } else { x } }) .unwrap_or(b"."); if is_path { diff --git a/snix/eval/src/builtins/to_xml.rs b/snix/eval/src/builtins/to_xml.rs index 02c04992e..66cbe45da 100644 --- a/snix/eval/src/builtins/to_xml.rs +++ b/snix/eval/src/builtins/to_xml.rs @@ -85,7 +85,7 @@ fn value_variant_to_xml(w: &mut XmlEmitter, value: &Value) -> Resul if formals.ellipsis { attrs.push(("ellipsis", "1")); } - if let Some(ref name) = &formals.name { + if let Some(name) = &formals.name { attrs.push(("name", name.as_str())); } @@ -126,7 +126,7 @@ fn value_variant_to_xml(w: &mut XmlEmitter, value: &Value) -> Resul return Err(ErrorKind::SnixBug { msg: "internal value variant encountered in builtins.toXML", metadata: Some(Rc::new(value.clone())), - }) + }); } Value::Catchable(_) => { diff --git a/snix/eval/src/builtins/versions.rs b/snix/eval/src/builtins/versions.rs index bb3fd9f03..91a308ac0 100644 --- a/snix/eval/src/builtins/versions.rs +++ b/snix/eval/src/builtins/versions.rs @@ -1,8 +1,8 @@ use std::cmp::Ordering; -use std::iter::{once, Chain, Once}; +use std::iter::{Chain, Once, once}; use std::ops::RangeInclusive; -use bstr::{BStr, ByteSlice, B}; +use bstr::{B, BStr, ByteSlice}; /// Version strings can be broken up into Parts. /// One Part represents either a string of digits or characters. @@ -97,10 +97,10 @@ impl<'a> Iterator for VersionPartsIter<'a> { match cached_part { InternalPart::Break => return None, InternalPart::Number { range } => { - return Some(VersionPart::Number(&self.version[range])) + return Some(VersionPart::Number(&self.version[range])); } InternalPart::Word { range } => { - return Some(VersionPart::Word(&self.version[range])) + return Some(VersionPart::Word(&self.version[range])); } } } diff --git a/snix/eval/src/compiler/bindings.rs b/snix/eval/src/compiler/bindings.rs index c8f969e77..29886f782 100644 --- a/snix/eval/src/compiler/bindings.rs +++ b/snix/eval/src/compiler/bindings.rs @@ -114,7 +114,7 @@ impl Binding { value: ast::Expr, ) { match self { - Binding::InheritFrom { name, ref span, .. } => { + Binding::InheritFrom { name, span, .. } => { c.emit_error(span, ErrorKind::UnmergeableInherit { name: name.clone() }) } diff --git a/snix/eval/src/compiler/import.rs b/snix/eval/src/compiler/import.rs index 862e792df..f76fa094b 100644 --- a/snix/eval/src/compiler/import.rs +++ b/snix/eval/src/compiler/import.rs @@ -10,12 +10,12 @@ use genawaiter::rc::Gen; use std::rc::Weak; use crate::{ + ErrorKind, SourceCode, Value, builtins::coerce_value_to_path, generators::pin_generator, observer::NoOpObserver, value::{Builtin, Thunk}, vm::generators::{self, GenCo}, - ErrorKind, SourceCode, Value, }; async fn import_impl( diff --git a/snix/eval/src/compiler/mod.rs b/snix/eval/src/compiler/mod.rs index f86889758..0b978160b 100644 --- a/snix/eval/src/compiler/mod.rs +++ b/snix/eval/src/compiler/mod.rs @@ -26,6 +26,8 @@ use std::collections::BTreeMap; use std::path::{Path, PathBuf}; use std::rc::{Rc, Weak}; +use crate::CoercionKind; +use crate::SourceCode; use crate::chunk::Chunk; use crate::errors::{CatchableErrorKind, Error, ErrorKind, EvalResult}; use crate::observer::CompilerObserver; @@ -33,8 +35,6 @@ use crate::opcode::{CodeIdx, Op, Position, UpvalueIdx}; use crate::spans::ToSpan; use crate::value::{Closure, Formals, Lambda, NixAttrs, Thunk, Value}; use crate::warnings::{EvalWarning, WarningKind}; -use crate::CoercionKind; -use crate::SourceCode; use self::scope::{LocalIdx, LocalPosition, Scope, Upvalue, UpvalueKind}; @@ -1141,16 +1141,20 @@ impl Compiler<'_, '_> { if self.scope()[tracked_formal.local_idx()].needs_finaliser { let stack_idx = self.scope().stack_index(tracked_formal.local_idx()); match tracked_formal { - TrackedFormal::NoDefault { .. } => - panic!("Snix bug: local for pattern formal needs finaliser, but has no default expr"), - TrackedFormal::WithDefault { finalise_request_idx, .. } => { - let finalise_request_stack_idx = self.scope().stack_index(*finalise_request_idx); + TrackedFormal::NoDefault { .. } => panic!( + "Snix bug: local for pattern formal needs finaliser, but has no default expr" + ), + TrackedFormal::WithDefault { + finalise_request_idx, + .. + } => { + let finalise_request_stack_idx = + self.scope().stack_index(*finalise_request_idx); // TODO(sterni): better spans self.push_op(Op::GetLocal, pattern); self.push_uvarint(finalise_request_stack_idx.0 as u64); - let jump_over_finalise = - self.push_op(Op::JumpIfNoFinaliseRequest, pattern); + let jump_over_finalise = self.push_op(Op::JumpIfNoFinaliseRequest, pattern); self.push_u16(0); self.push_op(Op::Finalise, pattern); self.push_uvarint(stack_idx.0 as u64); @@ -1502,7 +1506,7 @@ fn expr_static_attr_str(node: &ast::Attr) -> Option { // about the dynamic wrapper when determining whether the node // itself is dynamic, it depends solely on the expression inside // (i.e. `let ${"a"} = 1; in a` is valid). - ast::Attr::Dynamic(ref dynamic) => match dynamic.expr().unwrap() { + ast::Attr::Dynamic(dynamic) => match dynamic.expr().unwrap() { ast::Expr::Str(s) => expr_static_str(&s), _ => None, }, diff --git a/snix/eval/src/lib.rs b/snix/eval/src/lib.rs index b550c48e6..d5c6b17ac 100644 --- a/snix/eval/src/lib.rs +++ b/snix/eval/src/lib.rs @@ -47,13 +47,13 @@ use crate::value::Lambda; use crate::vm::run_lambda; // Re-export the public interface used by other crates. -pub use crate::compiler::{compile, prepare_globals, CompilationOutput, GlobalsMap}; +pub use crate::compiler::{CompilationOutput, GlobalsMap, compile, prepare_globals}; pub use crate::errors::{AddContext, CatchableErrorKind, Error, ErrorKind, EvalResult}; pub use crate::io::{DummyIO, EvalIO, FileType}; pub use crate::pretty_ast::pretty_print_expr; pub use crate::source::SourceCode; pub use crate::value::{NixContext, NixContextElement}; -pub use crate::vm::{generators, EvalMode}; +pub use crate::vm::{EvalMode, generators}; pub use crate::warnings::{EvalWarning, WarningKind}; pub use builtin_macros; use smol_str::SmolStr; diff --git a/snix/eval/src/nix_search_path.rs b/snix/eval/src/nix_search_path.rs index 2aefebdaa..cb78a032e 100644 --- a/snix/eval/src/nix_search_path.rs +++ b/snix/eval/src/nix_search_path.rs @@ -3,8 +3,8 @@ use std::convert::Infallible; use std::path::{Path, PathBuf}; use std::str::FromStr; -use crate::errors::{CatchableErrorKind, ErrorKind}; use crate::EvalIO; +use crate::errors::{CatchableErrorKind, ErrorKind}; #[derive(Debug, Clone, PartialEq, Eq)] enum NixSearchPathEntry { diff --git a/snix/eval/src/observer.rs b/snix/eval/src/observer.rs index f78ea33e5..47f5812b0 100644 --- a/snix/eval/src/observer.rs +++ b/snix/eval/src/observer.rs @@ -11,12 +11,12 @@ use std::rc::Rc; use std::time::Instant; use tabwriter::TabWriter; +use crate::SourceCode; +use crate::Value; use crate::chunk::Chunk; use crate::generators::VMRequest; use crate::opcode::{CodeIdx, Op}; use crate::value::Lambda; -use crate::SourceCode; -use crate::Value; /// Implemented by types that wish to observe internal happenings of /// the Snix compiler. diff --git a/snix/eval/src/pretty_ast.rs b/snix/eval/src/pretty_ast.rs index 8032f9b12..9e5c45ea3 100644 --- a/snix/eval/src/pretty_ast.rs +++ b/snix/eval/src/pretty_ast.rs @@ -4,7 +4,7 @@ //! printed in either minimised or well-formatted style. use rnix::ast::{self, AstToken, HasEntry}; -use serde::{ser::SerializeMap, Serialize, Serializer}; +use serde::{Serialize, Serializer, ser::SerializeMap}; pub fn pretty_print_expr(expr: &ast::Expr) -> String { serde_json::ser::to_string_pretty(&SerializeAST(expr)) diff --git a/snix/eval/src/tests/nix_tests.rs b/snix/eval/src/tests/nix_tests.rs index 834a7bf82..06860c1c0 100644 --- a/snix/eval/src/tests/nix_tests.rs +++ b/snix/eval/src/tests/nix_tests.rs @@ -1,4 +1,4 @@ -use crate::{value::Value, EvalIO, FileType}; +use crate::{EvalIO, FileType, value::Value}; use builtin_macros::builtins; use pretty_assertions::assert_eq; use rstest::rstest; @@ -91,7 +91,7 @@ where #[cfg(feature = "impure")] fn eval_test(code_path: PathBuf, expect_success: bool) { - use crate::{vm::EvalMode, StdIO}; + use crate::{StdIO, vm::EvalMode}; eprintln!("path: {}", code_path.display()); assert_eq!( @@ -180,7 +180,7 @@ fn eval_test(code_path: PathBuf, expect_success: bool) { #[cfg(feature = "impure")] #[rstest] fn identity(#[files("src/tests/snix_tests/identity-*.nix")] code_path: PathBuf) { - use crate::{vm::EvalMode, EvalIO}; + use crate::{EvalIO, vm::EvalMode}; let code = std::fs::read_to_string(code_path).expect("should be able to read test code"); diff --git a/snix/eval/src/upvalues.rs b/snix/eval/src/upvalues.rs index 687d6850c..327ea9803 100644 --- a/snix/eval/src/upvalues.rs +++ b/snix/eval/src/upvalues.rs @@ -9,7 +9,7 @@ use std::ops::Index; -use crate::{opcode::UpvalueIdx, Value}; +use crate::{Value, opcode::UpvalueIdx}; /// Structure for carrying upvalues of an UpvalueCarrier. The /// implementation of this struct encapsulates the logic for diff --git a/snix/eval/src/value/arbitrary.rs b/snix/eval/src/value/arbitrary.rs index 6dd9e07bd..e789c73b7 100644 --- a/snix/eval/src/value/arbitrary.rs +++ b/snix/eval/src/value/arbitrary.rs @@ -4,7 +4,7 @@ use proptest::collection::{btree_map, vec}; use proptest::{prelude::*, strategy::BoxedStrategy}; use std::ffi::OsString; -use super::{attrs::AttrsRep, NixAttrs, NixList, NixString, Value}; +use super::{NixAttrs, NixList, NixString, Value, attrs::AttrsRep}; #[derive(Clone)] pub enum Parameters { diff --git a/snix/eval/src/value/attrs.rs b/snix/eval/src/value/attrs.rs index f43018523..f91ecd051 100644 --- a/snix/eval/src/value/attrs.rs +++ b/snix/eval/src/value/attrs.rs @@ -6,7 +6,7 @@ //! Due to this, construction and management of attribute sets has //! some peculiarities that are encapsulated within this module. use std::borrow::Borrow; -use std::collections::{hash_map, BTreeMap}; +use std::collections::{BTreeMap, hash_map}; use std::iter::FromIterator; use std::rc::Rc; use std::sync::LazyLock; @@ -14,15 +14,15 @@ use std::sync::LazyLock; use bstr::BStr; use itertools::Itertools as _; use rustc_hash::FxHashMap; -use serde::de::{Deserializer, Error, Visitor}; use serde::Deserialize; +use serde::de::{Deserializer, Error, Visitor}; -use super::string::NixString; -use super::thunk::ThunkSet; use super::TotalDisplay; use super::Value; -use crate::errors::ErrorKind; +use super::string::NixString; +use super::thunk::ThunkSet; use crate::CatchableErrorKind; +use crate::errors::ErrorKind; static NAME: LazyLock = LazyLock::new(|| "name".into()); static VALUE: LazyLock = LazyLock::new(|| "value".into()); @@ -300,10 +300,7 @@ impl NixAttrs { AttrsRep::Map(map) => KeyValue::Map(map.iter()), AttrsRep::Empty => KeyValue::Empty, - AttrsRep::KV { - ref name, - ref value, - } => KeyValue::KV { + AttrsRep::KV { name, value } => KeyValue::KV { name, value, at: IterKV::default(), @@ -320,10 +317,7 @@ impl NixAttrs { let sorted = map.iter().sorted_by_key(|x| x.0); KeyValue::Sorted(sorted) } - AttrsRep::KV { - ref name, - ref value, - } => KeyValue::KV { + AttrsRep::KV { name, value } => KeyValue::KV { name, value, at: IterKV::default(), diff --git a/snix/eval/src/value/json.rs b/snix/eval/src/value/json.rs index 807dc55f9..8b44a7112 100644 --- a/snix/eval/src/value/json.rs +++ b/snix/eval/src/value/json.rs @@ -4,13 +4,13 @@ /// as there is internal Nix logic that must happen within the /// serialisation methods. use super::{CoercionKind, Value}; +use crate::NixContext; use crate::errors::ErrorKind; use crate::generators::{self, GenCo}; -use crate::NixContext; use bstr::ByteSlice; -use serde_json::value::to_value; use serde_json::Value as Json; // name clash with *our* `Value` +use serde_json::value::to_value; use serde_json::{Map, Number}; impl Value { @@ -110,7 +110,7 @@ impl Value { | val @ Value::DeferredUpvalue(_) | val @ Value::UnresolvedPath(_) | val @ Value::FinaliseRequest(_) => { - return Err(ErrorKind::NotSerialisableToJson(val.type_of())) + return Err(ErrorKind::NotSerialisableToJson(val.type_of())); } }; Ok((value, context)) diff --git a/snix/eval/src/value/list.rs b/snix/eval/src/value/list.rs index 3e4b23a93..bb6a80006 100644 --- a/snix/eval/src/value/list.rs +++ b/snix/eval/src/value/list.rs @@ -4,9 +4,9 @@ use std::rc::Rc; use serde::Deserialize; -use super::thunk::ThunkSet; use super::TotalDisplay; use super::Value; +use super::thunk::ThunkSet; #[repr(transparent)] #[derive(Clone, Debug, Deserialize)] diff --git a/snix/eval/src/value/mod.rs b/snix/eval/src/value/mod.rs index bb18c6088..2cdf59db4 100644 --- a/snix/eval/src/value/mod.rs +++ b/snix/eval/src/value/mod.rs @@ -23,10 +23,10 @@ mod path; mod string; mod thunk; +use crate::AddContext; use crate::errors::{CatchableErrorKind, ErrorKind}; use crate::opcode::StackIdx; use crate::vm::generators::{self, GenCo}; -use crate::AddContext; pub use attrs::NixAttrs; pub use builtin::{Builtin, BuiltinResult}; pub(crate) use function::Formals; @@ -810,7 +810,7 @@ impl Value { return Err(ErrorKind::Incomparable { lhs: lhs.type_of(), rhs: rhs.type_of(), - }) + }); } }; if result != Ordering::Equal { diff --git a/snix/eval/src/value/string/mod.rs b/snix/eval/src/value/string/mod.rs index a03ea1505..d86f7968c 100644 --- a/snix/eval/src/value/string/mod.rs +++ b/snix/eval/src/value/string/mod.rs @@ -9,7 +9,7 @@ use rnix::ast; use rustc_hash::FxHashSet; use rustc_hash::FxHasher; use std::alloc::dealloc; -use std::alloc::{alloc, handle_alloc_error, Layout}; +use std::alloc::{Layout, alloc, handle_alloc_error}; use std::borrow::{Borrow, Cow}; use std::cell::RefCell; use std::ffi::c_void; @@ -19,8 +19,8 @@ use std::ops::Deref; use std::ptr::{self, NonNull}; use std::slice; -use serde::de::{Deserializer, Visitor}; use serde::Deserialize; +use serde::de::{Deserializer, Visitor}; mod context; @@ -75,12 +75,14 @@ impl NixStringInner { /// This function must only be called on a pointer that has been properly initialized with /// [`Self::alloc`]. The data buffer may not necessarily be initialized unsafe fn layout_of(this: NonNull) -> (Layout, usize, usize) { - let layout = Layout::new::>>(); - let (_, len_offset) = layout.extend(Layout::new::()).unwrap(); - // SAFETY: Layouts are linear, so even though we haven't involved data at all yet, we know - // the len_offset is a valid offset into the second field of the allocation - let len = *(this.as_ptr().add(len_offset) as *const usize); - Self::layout(len) + unsafe { + let layout = Layout::new::>>(); + let (_, len_offset) = layout.extend(Layout::new::()).unwrap(); + // SAFETY: Layouts are linear, so even though we haven't involved data at all yet, we know + // the len_offset is a valid offset into the second field of the allocation + let len = *(this.as_ptr().add(len_offset) as *const usize); + Self::layout(len) + } } /// Allocate an *uninitialized* nix string with the given length. Writes the length to the @@ -121,9 +123,11 @@ impl NixStringInner { /// This function must only be called with a pointer that has been properly initialized with /// [`Self::alloc`] unsafe fn dealloc(this: NonNull) { - let (layout, _, _) = Self::layout_of(this); - // SAFETY: okay because of the safety guarantees of this method - dealloc(this.as_ptr() as *mut u8, layout) + unsafe { + let (layout, _, _) = Self::layout_of(this); + // SAFETY: okay because of the safety guarantees of this method + dealloc(this.as_ptr() as *mut u8, layout) + } } /// Return the length of the Nix string at the given pointer @@ -133,11 +137,13 @@ impl NixStringInner { /// This function must only be called with a pointer that has been properly initialized with /// [`Self::alloc`] unsafe fn len(this: NonNull) -> usize { - let (_, len_offset, _) = Self::layout_of(this); - // SAFETY: As long as the safety guarantees of this method are upheld, we've allocated with - // a layout that causes the len_offset to be in-bounds and writeable, and if the allocation - // succeeded it won't wrap - *(this.as_ptr().add(len_offset) as *const usize) + unsafe { + let (_, len_offset, _) = Self::layout_of(this); + // SAFETY: As long as the safety guarantees of this method are upheld, we've allocated with + // a layout that causes the len_offset to be in-bounds and writeable, and if the allocation + // succeeded it won't wrap + *(this.as_ptr().add(len_offset) as *const usize) + } } /// Return a pointer to the context value within the given Nix string pointer @@ -162,7 +168,7 @@ impl NixStringInner { /// Also, all the normal Rust rules about pointer-to-reference conversion apply. See /// [`NonNull::as_ref`] for more. unsafe fn context_ref<'a>(this: NonNull) -> &'a Option> { - Self::context_ptr(this).as_ref().unwrap() + unsafe { Self::context_ptr(this).as_ref().unwrap() } } /// Construct a mutable reference to the context value within the given Nix string pointer @@ -176,7 +182,7 @@ impl NixStringInner { /// Also, all the normal Rust rules about pointer-to-reference conversion apply. See /// [`NonNull::as_mut`] for more. unsafe fn context_mut<'a>(this: NonNull) -> &'a mut Option> { - Self::context_ptr(this).as_mut().unwrap() + unsafe { Self::context_ptr(this).as_mut().unwrap() } } /// Return a pointer to the data array within the given Nix string pointer @@ -186,9 +192,11 @@ impl NixStringInner { /// This function must only be called with a pointer that has been properly initialized with /// [`Self::alloc`] unsafe fn data_ptr(this: NonNull) -> *mut u8 { - let (_, _, data_offset) = Self::layout_of(this); - // SAFETY: data is the third field in the layout of the allocation - this.as_ptr().add(data_offset) as *mut u8 + unsafe { + let (_, _, data_offset) = Self::layout_of(this); + // SAFETY: data is the third field in the layout of the allocation + this.as_ptr().add(data_offset) as *mut u8 + } } /// Construct a shared reference to the data slice within the given Nix string pointer @@ -202,9 +210,11 @@ impl NixStringInner { /// Also, all the normal Rust rules about pointer-to-reference conversion apply. See /// [`slice::from_raw_parts`] for more. unsafe fn data_slice<'a>(this: NonNull) -> &'a [u8] { - let len = Self::len(this); - let data = Self::data_ptr(this); - slice::from_raw_parts(data, len) + unsafe { + let len = Self::len(this); + let data = Self::data_ptr(this); + slice::from_raw_parts(data, len) + } } /// Construct a mutable reference to the data slice within the given Nix string pointer @@ -219,9 +229,11 @@ impl NixStringInner { /// [`slice::from_raw_parts_mut`] for more. #[allow(dead_code)] unsafe fn data_slice_mut<'a>(this: NonNull) -> &'a mut [u8] { - let len = Self::len(this); - let data = Self::data_ptr(this); - slice::from_raw_parts_mut(data, len) + unsafe { + let len = Self::len(this); + let data = Self::data_ptr(this); + slice::from_raw_parts_mut(data, len) + } } /// Clone the Nix string pointed to by this pointer, and return a pointer to a new Nix string @@ -234,14 +246,16 @@ impl NixStringInner { /// pointer returned from [`Self::context_ptr`]), and the data array has been properly /// initialized (by writing to the pointer returned from [`Self::data_ptr`]). unsafe fn clone(this: NonNull) -> NonNull { - let (layout, _, _) = Self::layout_of(this); - let ptr = alloc(layout); - if let Some(new) = NonNull::new(ptr as *mut _) { - ptr::copy_nonoverlapping(this.as_ptr(), new.as_ptr(), layout.size()); - Self::context_ptr(new).write(Self::context_ref(this).clone()); - new - } else { - handle_alloc_error(layout); + unsafe { + let (layout, _, _) = Self::layout_of(this); + let ptr = alloc(layout); + if let Some(new) = NonNull::new(ptr as *mut _) { + ptr::copy_nonoverlapping(this.as_ptr(), new.as_ptr(), layout.size()); + Self::context_ptr(new).write(Self::context_ref(this).clone()); + new + } else { + handle_alloc_error(layout); + } } } } @@ -543,7 +557,7 @@ impl Deref for NixString { #[cfg(feature = "arbitrary")] mod arbitrary { use super::*; - use proptest::prelude::{any_with, Arbitrary}; + use proptest::prelude::{Arbitrary, any_with}; use proptest::strategy::{BoxedStrategy, Strategy}; impl Arbitrary for NixString { diff --git a/snix/eval/src/value/thunk.rs b/snix/eval/src/value/thunk.rs index 38392ef16..f727d088d 100644 --- a/snix/eval/src/value/thunk.rs +++ b/snix/eval/src/value/thunk.rs @@ -26,12 +26,12 @@ use std::{ }; use crate::{ + Value, errors::ErrorKind, opcode::Op, upvalues::Upvalues, value::Closure, vm::generators::{self, GenCo}, - Value, }; use super::{Lambda, TotalDisplay}; @@ -240,7 +240,7 @@ impl Thunk { first_force: forced_at, suspended_at, content_span, - }) + }); } // If there is a native function stored in the thunk, evaluate it diff --git a/snix/eval/src/vm/generators.rs b/snix/eval/src/vm/generators.rs index f80f79fb1..f13dcb884 100644 --- a/snix/eval/src/vm/generators.rs +++ b/snix/eval/src/vm/generators.rs @@ -14,10 +14,10 @@ use std::ffi::OsString; use std::fmt::Display; use std::future::Future; -use crate::value::PointerEquality; -use crate::warnings::{EvalWarning, WarningKind}; use crate::FileType; use crate::NixString; +use crate::value::PointerEquality; +use crate::warnings::{EvalWarning, WarningKind}; use super::*; @@ -254,7 +254,7 @@ where } /// Helper function to enqueue a new generator. - pub(super) fn enqueue_generator(&mut self, name: &'static str, span: Span, gen: G) + pub(super) fn enqueue_generator(&mut self, name: &'static str, span: Span, r#gen: G) where F: Future> + 'static, G: FnOnce(GenCo) -> F, @@ -263,7 +263,7 @@ where name, span, state: GeneratorState::Running, - generator: Gen::new(|co| pin_generator(gen(co))), + generator: Gen::new(|co| pin_generator(r#gen(co))), }); } diff --git a/snix/eval/src/vm/mod.rs b/snix/eval/src/vm/mod.rs index 63d3e2156..d5efcf847 100644 --- a/snix/eval/src/vm/mod.rs +++ b/snix/eval/src/vm/mod.rs @@ -19,7 +19,7 @@ use serde_json::json; use std::{cmp::Ordering, ops::DerefMut, path::PathBuf, rc::Rc}; use crate::{ - arithmetic_op, + NixString, SourceCode, arithmetic_op, chunk::Chunk, cmp_op, compiler::GlobalsMap, @@ -36,10 +36,9 @@ use crate::{ }, vm::generators::GenCo, warnings::{EvalWarning, WarningKind}, - NixString, SourceCode, }; -use generators::{call_functor, Generator, GeneratorState}; +use generators::{Generator, GeneratorState, call_functor}; use self::generators::{VMRequest, VMResponse}; @@ -641,8 +640,11 @@ where if !finalise { frame.ip += offset; } - }, - val => panic!("Snix bug: OpJumIfNoFinaliseRequest: expected FinaliseRequest, but got {}", val.type_of()), + } + val => panic!( + "Snix bug: OpJumIfNoFinaliseRequest: expected FinaliseRequest, but got {}", + val.type_of() + ), } }