diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 000000000..9f3373c55 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# Resolved all lints and formatted the codebase +9444e62df9820d6bfd96dbd8849e177bc5cecc2e diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d40c145c6..d91610525 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,47 @@ on: workflow_dispatch: jobs: + style: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@master + id: toolchain + with: + toolchain: nightly + targets: x86_64-unknown-linux-gnu + components: clippy, rustfmt + - run: cargo install cargo2junit --force + - uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: style_rustc-${{ steps.toolchain.outputs.cachekey }}_cargo-${{ hashFiles('**/Cargo.lock') }} + + - name: Check formatting + run: cargo fmt --check + - name: Check clippy + run: cargo clippy --no-deps --all-targets + - name: Test and report + run: | + RUSTC_BOOTSTRAP=1 cargo test --all -- -Z unstable-options --format json --report-time | cargo2junit > cargo_test_results.xml + - name: Publish cargo test results artifact + uses: actions/upload-artifact@v3 + with: + name: cargo-test-results + path: cargo_test_results.xml + - name: Publish cargo test summary + uses: EnricoMi/publish-unit-test-result-action/composite@master + with: + check_name: Cargo test summary + files: cargo_test_results.xml + fail_on: nothing + comment_mode: off + build-test: runs-on: ${{ matrix.os }} strategy: @@ -18,13 +59,13 @@ jobs: matrix: include: # operating systems - - { os: windows-latest, rust-version: stable, publish: true, target: 'x86_64-pc-windows-msvc'} - - { os: macos-11, rust-version: stable, publish: true, target: 'x86_64-apple-darwin' } - - { os: ubuntu-20.04, rust-version: stable, publish: true, target: 'x86_64-unknown-linux-gnu' } + - { os: windows-latest, rust-version: stable, target: 'x86_64-pc-windows-msvc', publish: true } + - { os: macos-11, rust-version: stable, target: 'x86_64-apple-darwin', publish: true } + - { os: ubuntu-20.04, rust-version: stable, target: 'x86_64-unknown-linux-gnu', publish: true } # architectures - - { os: ubuntu-22.04, rust-version: stable, publish: true, target: 'x86_64-unknown-linux-gnu', extra: true } - - { os: ubuntu-22.04, rust-version: stable, publish: true, target: 'i686-unknown-linux-gnu' } - - { os: ubuntu-22.04, rust-version: nightly, publish: true, target: 'wasm32-unknown-unknown', args: '--no-default-features' } + - { os: ubuntu-22.04, rust-version: stable, target: 'x86_64-unknown-linux-gnu', publish: true } + - { os: ubuntu-22.04, rust-version: stable, target: 'i686-unknown-linux-gnu', publish: true } + - { os: ubuntu-22.04, rust-version: nightly, target: 'wasm32-unknown-unknown', publish: true, args: '--no-default-features' } # rust versions - { os: ubuntu-22.04, rust-version: "1.70", target: 'x86_64-unknown-linux-gnu'} - { os: ubuntu-22.04, rust-version: beta, target: 'x86_64-unknown-linux-gnu'} @@ -39,7 +80,6 @@ jobs: with: toolchain: ${{ matrix.rust-version }} targets: ${{ matrix.target }} - components: clippy, rustfmt - name: Install i686 dependencies if: "contains(matrix.target,'i686')" run: | @@ -62,36 +102,8 @@ jobs: - name: Build library run: cargo rustc --lib --target ${{ matrix.target }} ${{ matrix.args }} --verbose - name: Test - if: "!matrix.extra" run: cargo test --target ${{ matrix.target }} ${{ matrix.args }} --all --verbose || echo "::warning ::Tests failed" - # Extra steps only run once to avoid duplication, when matrix.extra is true - - name: Test and report - if: matrix.extra - run: | - cargo install cargo2junit --force - RUSTC_BOOTSTRAP=1 cargo test --all -- -Z unstable-options --format json --report-time | cargo2junit > cargo_test_results.xml - - name: Publish cargo test results artifact - if: matrix.extra - uses: actions/upload-artifact@v3 - with: - name: cargo-test-results - path: cargo_test_results.xml - - name: Publish cargo test summary - if: matrix.extra - uses: EnricoMi/publish-unit-test-result-action/composite@master - with: - check_name: Cargo test summary - files: cargo_test_results.xml - fail_on: nothing - comment_mode: off - - name: Check formatting - if: matrix.extra - run: cargo fmt --check || echo "::warning ::cargo fmt found some formatting changes that may improve readability" - - name: Check clippy - if: matrix.extra - run: cargo clippy --no-deps || echo "::warning ::cargo clippy found some code style changes that may be more idiomatic" - # On stable rust builds, build a binary and publish as a github actions # artifact. These binaries could be useful for testing the pipeline but # are only retained by github for 90 days. diff --git a/Cargo.lock b/Cargo.lock index f4a4952a6..315f61a73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2032,7 +2032,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "scryer-prolog" -version = "0.9.2" +version = "0.9.3" dependencies = [ "assert_cmd", "base64 0.12.3", diff --git a/build/instructions_template.rs b/build/instructions_template.rs index 517df8f64..69cf2d12e 100644 --- a/build/instructions_template.rs +++ b/build/instructions_template.rs @@ -31,6 +31,7 @@ struct Level; struct NextOrFail; struct RegType; +#[allow(clippy::enum_variant_names)] #[allow(dead_code)] #[derive(ToDeriveInput, EnumDiscriminants)] #[strum_discriminants(derive(EnumProperty, EnumString))] @@ -49,6 +50,7 @@ enum CompareNumber { NumberEqual(ArithmeticTerm, ArithmeticTerm), } +#[allow(clippy::enum_variant_names)] #[allow(dead_code)] #[derive(ToDeriveInput, EnumDiscriminants)] #[strum_discriminants(derive(EnumProperty, EnumString))] @@ -206,6 +208,7 @@ enum REPLCodePtr { AddNonCountedBacktracking, } +#[allow(clippy::upper_case_acronyms)] #[allow(dead_code)] #[derive(ToDeriveInput, EnumDiscriminants)] #[strum_discriminants(derive(EnumProperty, EnumString))] @@ -897,13 +900,13 @@ fn generate_instruction_preface() -> TokenStream { } impl ArithmeticTerm { - fn into_functor(&self, arena: &mut Arena) -> MachineStub { + fn into_functor(self, arena: &mut Arena) -> MachineStub { match self { - &ArithmeticTerm::Reg(r) => reg_type_into_functor(r), - &ArithmeticTerm::Interm(i) => { + ArithmeticTerm::Reg(r) => reg_type_into_functor(r), + ArithmeticTerm::Interm(i) => { functor!(atom!("intermediate"), [fixnum(i)]) } - &ArithmeticTerm::Number(n) => { + ArithmeticTerm::Number(n) => { vec![HeapCellValue::from((n, arena))] } } @@ -925,26 +928,17 @@ fn generate_instruction_preface() -> TokenStream { impl NextOrFail { #[inline] pub fn is_next(&self) -> bool { - if let NextOrFail::Next(_) = self { - true - } else { - false - } + matches!(self, NextOrFail::Next(_)) } } - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] + #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Death { Finite(usize), + #[default] Infinity, } - impl Default for Death { - fn default() -> Self { - Death::Infinity - } - } - #[derive(Clone, Copy, Debug)] pub enum IndexedChoiceInstruction { Retry(usize), @@ -956,30 +950,30 @@ fn generate_instruction_preface() -> TokenStream { impl IndexedChoiceInstruction { pub(crate) fn offset(&self) -> usize { - match self { - &IndexedChoiceInstruction::Retry(offset) => offset, - &IndexedChoiceInstruction::Trust(offset) => offset, - &IndexedChoiceInstruction::Try(offset) => offset, - &IndexedChoiceInstruction::DefaultRetry(offset) => offset, - &IndexedChoiceInstruction::DefaultTrust(offset) => offset, + match *self { + IndexedChoiceInstruction::Retry(offset) => offset, + IndexedChoiceInstruction::Trust(offset) => offset, + IndexedChoiceInstruction::Try(offset) => offset, + IndexedChoiceInstruction::DefaultRetry(offset) => offset, + IndexedChoiceInstruction::DefaultTrust(offset) => offset, } } - pub(crate) fn to_functor(&self) -> MachineStub { + pub(crate) fn to_functor(self) -> MachineStub { match self { - &IndexedChoiceInstruction::Try(offset) => { + IndexedChoiceInstruction::Try(offset) => { functor!(atom!("try"), [fixnum(offset)]) } - &IndexedChoiceInstruction::Trust(offset) => { + IndexedChoiceInstruction::Trust(offset) => { functor!(atom!("trust"), [fixnum(offset)]) } - &IndexedChoiceInstruction::Retry(offset) => { + IndexedChoiceInstruction::Retry(offset) => { functor!(atom!("retry"), [fixnum(offset)]) } - &IndexedChoiceInstruction::DefaultTrust(offset) => { + IndexedChoiceInstruction::DefaultTrust(offset) => { functor!(atom!("default_trust"), [fixnum(offset)]) } - &IndexedChoiceInstruction::DefaultRetry(offset) => { + IndexedChoiceInstruction::DefaultRetry(offset) => { functor!(atom!("default_retry"), [fixnum(offset)]) } } @@ -1038,7 +1032,7 @@ fn generate_instruction_preface() -> TokenStream { ] ) } - &IndexingInstruction::SwitchOnConstant(ref constants) => { + IndexingInstruction::SwitchOnConstant(constants) => { let mut key_value_list_stub = vec![]; let orig_h = h; @@ -1066,7 +1060,7 @@ fn generate_instruction_preface() -> TokenStream { [key_value_list_stub] ) } - &IndexingInstruction::SwitchOnStructure(ref structures) => { + IndexingInstruction::SwitchOnStructure(structures) => { let mut key_value_list_stub = vec![]; let orig_h = h; @@ -1177,7 +1171,7 @@ fn generate_instruction_preface() -> TokenStream { #[inline] pub fn is_head_instr(&self) -> bool { - match self { + matches!(self, Instruction::Deallocate | Instruction::GetConstant(..) | Instruction::GetList(..) | @@ -1201,9 +1195,7 @@ fn generate_instruction_preface() -> TokenStream { Instruction::SetLocalValue(..) | Instruction::SetVariable(..) | Instruction::SetValue(..) | - Instruction::SetVoid(..) => true, - _ => false, - } + Instruction::SetVoid(..)) } pub fn enqueue_functors( @@ -1213,7 +1205,7 @@ fn generate_instruction_preface() -> TokenStream { functors: &mut Vec, ) { match self { - &Instruction::IndexingCode(ref indexing_instrs) => { + Instruction::IndexingCode(indexing_instrs) => { for indexing_instr in indexing_instrs { match indexing_instr { IndexingLine::Indexing(indexing_instr) => { @@ -2331,7 +2323,7 @@ pub fn generate_instructions_rs() -> TokenStream { let mut is_inlined_arms = vec![]; is_inbuilt_arms.push(quote! { - (atom!(":-"), 1 | 2) => true + (atom!(":-"), 1 | 2) }); for (name, arity, variant) in instr_data.compare_number_variants { @@ -2388,11 +2380,11 @@ pub fn generate_instructions_rs() -> TokenStream { }); is_inbuilt_arms.push(quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) }); is_inlined_arms.push(quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) }); } @@ -2421,7 +2413,7 @@ pub fn generate_instructions_rs() -> TokenStream { }); is_inbuilt_arms.push(quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) }); } @@ -2487,7 +2479,7 @@ pub fn generate_instructions_rs() -> TokenStream { }); is_inbuilt_arms.push(quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) }); } @@ -2549,16 +2541,17 @@ pub fn generate_instructions_rs() -> TokenStream { }); is_inbuilt_arms.push(quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) }); is_inlined_arms.push(quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) }); } for (name, arity, variant) in instr_data.system_clause_type_variants { let ident = variant.ident.clone(); + let ident_s = ident.to_string(); let variant_fields: Vec<_> = variant .fields @@ -2574,19 +2567,13 @@ pub fn generate_instructions_rs() -> TokenStream { .collect(); clause_type_from_name_and_arity_arms.push(if !variant_fields.is_empty() { - if ident.to_string() == "SetCutPoint" { - quote! { - (atom!(#name), #arity) => ClauseType::System( - SystemClauseType::#ident(temp_v!(1)) - ) - } - } else if ident.to_string() == "SetCutPointByDefault" { + if ident_s == "SetCutPoint" || ident_s == "SetCutPointByDefault" { quote! { (atom!(#name), #arity) => ClauseType::System( SystemClauseType::#ident(temp_v!(1)) ) } - } else if ident.to_string() == "InlineCallN" { + } else if ident_s == "InlineCallN" { quote! { (atom!(#name), arity) => ClauseType::System( SystemClauseType::#ident(arity) @@ -2649,11 +2636,11 @@ pub fn generate_instructions_rs() -> TokenStream { is_inbuilt_arms.push(if let Arity::Ident("arity") = &arity { quote! { - (atom!(#name), _arity) => true + (atom!(#name), _) } } else { quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) } }); } @@ -2724,7 +2711,7 @@ pub fn generate_instructions_rs() -> TokenStream { }); is_inbuilt_arms.push(quote! { - (atom!(#name), #arity) => true + (atom!(#name), #arity) }); } @@ -2798,7 +2785,7 @@ pub fn generate_instructions_rs() -> TokenStream { }); is_inbuilt_arms.push(quote! { - (atom!(#name), _arity) => true + (atom!(#name), _) }); } @@ -2819,8 +2806,8 @@ pub fn generate_instructions_rs() -> TokenStream { let placeholder_ids: Vec<_> = (0..enum_arity).map(|n| format_ident!("f_{}", n)).collect(); - if variant_string.starts_with("Call") { - let execute_ident = format_ident!("Execute{}", variant_string["Call".len()..]); + if let Some(variant_suffix) = variant_string.strip_prefix("Call") { + let execute_ident = format_ident!("Execute{}", variant_suffix); Some(if enum_arity == 0 { quote! { @@ -2833,9 +2820,8 @@ pub fn generate_instructions_rs() -> TokenStream { Instruction::#execute_ident(#(#placeholder_ids),*) } }) - } else if variant_string.starts_with("DefaultCall") { - let execute_ident = - format_ident!("DefaultExecute{}", variant_string["DefaultCall".len()..]); + } else if let Some(variant_suffix) = variant_string.strip_prefix("DefaultCall") { + let execute_ident = format_ident!("DefaultExecute{}", variant_suffix); Some(if enum_arity == 0 { quote! { @@ -2868,29 +2854,20 @@ pub fn generate_instructions_rs() -> TokenStream { 0 }; - if variant_string.starts_with("Execute") { + if variant_string.starts_with("Execute") || variant_string.starts_with("DefaultExecute") + { Some(if enum_arity == 0 { quote! { - Instruction::#variant_ident => true + Instruction::#variant_ident } } else { quote! { - Instruction::#variant_ident(..) => true - } - }) - } else if variant_string.starts_with("DefaultExecute") { - Some(if enum_arity == 0 { - quote! { - Instruction::#variant_ident => true - } - } else { - quote! { - Instruction::#variant_ident(..) => true + Instruction::#variant_ident(..) } }) } else if variant_string == "JmpByExecute" { Some(quote! { - Instruction::#variant_ident(..) => true + Instruction::#variant_ident(..) }) } else { None @@ -2955,11 +2932,11 @@ pub fn generate_instructions_rs() -> TokenStream { Some(if enum_arity == 0 { quote! { - Instruction::#variant_ident => true + Instruction::#variant_ident } } else { quote! { - Instruction::#variant_ident(..) => true + Instruction::#variant_ident(..) } }) }) @@ -2970,7 +2947,7 @@ pub fn generate_instructions_rs() -> TokenStream { .iter() .rev() // produce default, execute & default & execute cases first. .cloned() - .filter_map(|(name, arity, _, variant)| { + .map(|(name, arity, _, variant)| { let variant_ident = variant.ident.clone(); let variant_string = variant.ident.to_string(); let arity = match arity { @@ -2978,6 +2955,7 @@ pub fn generate_instructions_rs() -> TokenStream { _ => 1, }; + #[allow(clippy::collapsible_else_if)] Some(if variant_string.starts_with("Execute") { if arity == 0 { quote! { @@ -3066,10 +3044,10 @@ pub fn generate_instructions_rs() -> TokenStream { match arity { Arity::Static(_) if enum_arity == 0 => { - quote! { &Instruction::#ident => (atom!(#name), #arity) } + quote! { Instruction::#ident => (atom!(#name), #arity) } } Arity::Static(_) => { - quote! { &Instruction::#ident(..) => (atom!(#name), #arity) } + quote! { Instruction::#ident(..) => (atom!(#name), #arity) } } Arity::Ident(_) if enum_arity == 0 => { quote! { &Instruction::#ident(#arity) => (atom!(#name), #arity) } @@ -3169,12 +3147,9 @@ pub fn generate_instructions_rs() -> TokenStream { } pub fn is_inbuilt(name: Atom, arity: usize) -> bool { - match (name, arity) { - #( - #is_inbuilt_arms, - )* - _ => false, - } + matches!((name, arity), + #(#is_inbuilt_arms)|* + ) } pub fn name(&self) -> Atom { @@ -3186,12 +3161,9 @@ pub fn generate_instructions_rs() -> TokenStream { } pub fn is_inlined(name: Atom, arity: usize) -> bool { - match (name, arity) { - #( - #is_inlined_arms, - )* - _ => false, - } + matches!((name, arity), + #(#is_inlined_arms)|* + ) } } @@ -3230,29 +3202,23 @@ pub fn generate_instructions_rs() -> TokenStream { } pub fn is_execute(&self) -> bool { - match self { - #( - #is_execute_arms, - )* - _ => false, - } + matches!(self, + #(#is_execute_arms)|* + ) } pub fn is_ctrl_instr(&self) -> bool { - match self { - &Instruction::Allocate(_) | - &Instruction::Deallocate | - &Instruction::Proceed | - &Instruction::RevJmpBy(_) => true, - #( - #control_flow_arms, - )* - _ => false, - } + matches!(self, + Instruction::Allocate(_) | + Instruction::Deallocate | + Instruction::Proceed | + Instruction::RevJmpBy(_) | + #(#control_flow_arms)|* + ) } pub fn is_query_instr(&self) -> bool { - match self { + matches!(self, &Instruction::GetVariable(..) | &Instruction::PutConstant(..) | &Instruction::PutList(..) | @@ -3265,9 +3231,8 @@ pub fn generate_instructions_rs() -> TokenStream { &Instruction::SetLocalValue(..) | &Instruction::SetVariable(..) | &Instruction::SetValue(..) | - &Instruction::SetVoid(..) => true, - _ => false, - } + &Instruction::SetVoid(..) + ) } } @@ -3335,7 +3300,8 @@ enum Arity { impl From<&'static str> for Arity { fn from(arity: &'static str) -> Self { - usize::from_str_radix(&arity, 10) + arity + .parse::() .map(Arity::Static) .unwrap_or_else(|_| Arity::Ident(arity)) } @@ -3442,13 +3408,12 @@ impl InstructionData { panic!("type ID is: {}", id); }; - let v_string = variant.ident.to_string(); - - let v_ident = if v_string.starts_with("Call") { - format_ident!("{}", v_string["Call".len()..]) - } else { - variant.ident.clone() - }; + let v_ident = variant + .ident + .to_string() + .strip_prefix("Call") + .map(|s| format_ident!("{}", s)) + .unwrap_or_else(|| variant.ident.clone()); let generated_variant = create_instr_variant(format_ident!("{}{}", prefix, v_ident), variant.clone()); diff --git a/build/main.rs b/build/main.rs index 7e3fe1ab9..9bcbac157 100644 --- a/build/main.rs +++ b/build/main.rs @@ -55,7 +55,7 @@ fn main() { let out_dir = env::var("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("libraries.rs"); - let mut libraries = File::create(&dest_path).unwrap(); + let mut libraries = File::create(dest_path).unwrap(); let lib_path = Path::new("src/lib"); libraries @@ -66,7 +66,7 @@ fn main() { ) .unwrap(); - find_prolog_files(&mut libraries, "", &lib_path); + find_prolog_files(&mut libraries, "", lib_path); libraries.write_all(b"\n m\n };\n}\n").unwrap(); let instructions_path = Path::new(&out_dir).join("instructions.rs"); diff --git a/build/static_string_indexing.rs b/build/static_string_indexing.rs index 43bd813e9..e6ed11eb6 100644 --- a/build/static_string_indexing.rs +++ b/build/static_string_indexing.rs @@ -35,7 +35,7 @@ impl Parse for ReadHeapCellExprAndArms { arms.push(input.parse()?); while !input.is_empty() { - if let Ok(_) = input.parse::() {} + let _ = input.parse::(); arms.push(input.parse()?); } @@ -52,7 +52,7 @@ impl Parse for MacroFnArgs { } while !input.is_empty() { - if let Ok(_) = input.parse::() {} + let _ = input.parse::(); args.push(input.parse()?); } @@ -65,22 +65,20 @@ impl<'ast> Visit<'ast> for StaticStrVisitor { let Macro { path, .. } = m; if path.is_ident("atom") { - if let Some(Lit::Str(string)) = m.parse_body::().ok() { + if let Ok(Lit::Str(string)) = m.parse_body::() { self.static_strs.insert(string.value()); } } else if path.is_ident("read_heap_cell") || path.is_ident("match_untyped_arena_ptr") { - if let Some(m) = m.parse_body::().ok() { + if let Ok(m) = m.parse_body::() { self.visit_expr(&m.expr); for e in m.arms { self.visit_arm(&e); } } - } else { - if let Some(m) = m.parse_body::().ok() { - for e in m.args { - self.visit_expr(&e); - } + } else if let Ok(m) = m.parse_body::() { + for e in m.args { + self.visit_expr(&e); } } } @@ -147,9 +145,8 @@ pub fn index_static_strings(instruction_rs_path: &std::path::Path) -> TokenStrea visitor.visit_file(&syntax); } - match process_filepath(instruction_rs_path) { - Ok(syntax) => visitor.visit_file(&syntax), - Err(_) => {} + if let Ok(syntax) = process_filepath(instruction_rs_path) { + visitor.visit_file(&syntax) } let indices = (0..visitor.static_strs.len()).map(|i| (i << 3) as u64); @@ -161,7 +158,7 @@ pub fn index_static_strings(instruction_rs_path: &std::path::Path) -> TokenStrea quote! { use phf; - static STRINGS: [&'static str; #static_strs_len] = [ + static STRINGS: [&str; #static_strs_len] = [ #( #static_strs, )* diff --git a/src/allocator.rs b/src/allocator.rs index f689a802c..e45c36b65 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -24,6 +24,7 @@ pub(crate) trait Allocator { code: &mut CodeDeque, ); + #[allow(clippy::too_many_arguments)] fn mark_reserved_var<'a, Target: CompilationTarget<'a>>( &mut self, var_num: usize, @@ -48,7 +49,7 @@ pub(crate) trait Allocator { fn reset(&mut self); fn reset_arg(&mut self, arg_num: usize); - fn reset_at_head(&mut self, args: &Vec); + fn reset_at_head(&mut self, args: &[Term]); fn reset_contents(&mut self); fn advance_arg(&mut self); diff --git a/src/arena.rs b/src/arena.rs index 77d82a360..40c1b0290 100644 --- a/src/arena.rs +++ b/src/arena.rs @@ -91,14 +91,14 @@ pub fn lookup_float( ) -> RcuRef, UnsafeCell>> { let f64table = global_f64table() .read() - .unwrap() + .unwrap() .upgrade() .expect("We should only be looking up floats while there is a float table"); RcuRef::try_map(f64table.block.active_epoch(), |raw_block| unsafe { raw_block .base - .offset(offset.0 as isize) + .add(offset.0) .cast_mut() .cast::>>() .as_ref() @@ -129,6 +129,7 @@ impl F64Table { } } + #[allow(clippy::missing_safety_doc)] pub unsafe fn build_with(&self, value: f64) -> F64Offset { let update_guard = self.update.lock(); @@ -152,9 +153,7 @@ impl F64Table { ptr::write(ptr as *mut OrderedFloat, OrderedFloat(value)); - let float = F64Offset { - 0: ptr as usize - block_epoch.base as usize, - }; + let float = F64Offset(ptr as usize - block_epoch.base as usize); // atometable would have to update the index table at this point @@ -230,7 +229,7 @@ impl PartialOrd for TypedArenaPtr { impl PartialEq for TypedArenaPtr { fn eq(&self, other: &TypedArenaPtr) -> bool { - self.0 == other.0 || &**self == &**other + self.0 == other.0 || **self == **other } } @@ -245,13 +244,13 @@ impl Ord for TypedArenaPtr { impl Hash for TypedArenaPtr { #[inline(always)] fn hash(&self, hasher: &mut H) { - (&*self as &T).hash(hasher) + (self as &T).hash(hasher) } } impl Clone for TypedArenaPtr { fn clone(&self) -> Self { - TypedArenaPtr(self.0) + *self } } @@ -279,10 +278,10 @@ impl fmt::Display for TypedArenaPtr { impl TypedArenaPtr { // data must be allocated in the arena already. + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] pub const fn new(data: *mut T) -> Self { - let result = unsafe { TypedArenaPtr(ptr::NonNull::new_unchecked(data)) }; - result + unsafe { TypedArenaPtr(ptr::NonNull::new_unchecked(data)) } } #[inline] @@ -347,6 +346,7 @@ pub trait ArenaAllocated: Sized { mem::size_of::() } + #[allow(clippy::missing_safety_doc)] unsafe fn alloc(arena: &mut Arena, value: Self) -> Self::PtrToAllocated { let size = value.size() + mem::size_of::(); @@ -363,7 +363,7 @@ pub trait ArenaAllocated: Sized { (*slab).header = ArenaHeader::build_with(value.size() as u64, Self::tag()); let offset = (*slab).payload_offset(); - let result = value.copy_to_arena(offset as *mut Self); + let result = value.copy_to_arena(offset); arena.base = slab; @@ -390,7 +390,7 @@ impl Eq for F64Ptr {} impl PartialOrd for F64Ptr { fn partial_cmp(&self, other: &Self) -> Option { - (**self).partial_cmp(&**other) + Some(self.cmp(other)) } } @@ -403,13 +403,13 @@ impl Ord for F64Ptr { impl Hash for F64Ptr { #[inline(always)] fn hash(&self, hasher: &mut H) { - (&*self as &OrderedFloat).hash(hasher) + (self as &OrderedFloat).hash(hasher) } } impl fmt::Display for F64Ptr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", *self) + write!(f, "{}", self as &OrderedFloat) } } @@ -418,7 +418,7 @@ impl Deref for F64Ptr { #[inline] fn deref(&self) -> &Self::Target { - unsafe { &*self.0.get().as_ref().unwrap() } + unsafe { self.0.get().as_ref().unwrap() } } } @@ -478,7 +478,7 @@ impl Eq for F64Offset {} impl PartialOrd for F64Offset { #[inline(always)] fn partial_cmp(&self, other: &Self) -> Option { - self.as_ptr().partial_cmp(&other.as_ptr()) + Some(self.cmp(other)) } } @@ -515,11 +515,12 @@ impl ArenaAllocated for Integer { mem::size_of::() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { ptr::write(dst, self); - TypedArenaPtr::new(dst as *mut Self) + TypedArenaPtr::new(dst) } } } @@ -537,11 +538,12 @@ impl ArenaAllocated for Rational { mem::size_of::() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { ptr::write(dst, self); - TypedArenaPtr::new(dst as *mut Self) + TypedArenaPtr::new(dst) } } } @@ -559,11 +561,12 @@ impl ArenaAllocated for LiveLoadState { mem::size_of::() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { ptr::write(dst, self); - TypedArenaPtr::new(dst as *mut Self) + TypedArenaPtr::new(dst) } } } @@ -581,11 +584,12 @@ impl ArenaAllocated for TcpListener { mem::size_of::() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { ptr::write(dst, self); - TypedArenaPtr::new(dst as *mut Self) + TypedArenaPtr::new(dst) } } } @@ -604,11 +608,12 @@ impl ArenaAllocated for HttpListener { mem::size_of::() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { ptr::write(dst, self); - TypedArenaPtr::new(dst as *mut Self) + TypedArenaPtr::new(dst) } } } @@ -627,11 +632,12 @@ impl ArenaAllocated for HttpResponse { mem::size_of::() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { ptr::write(dst, self); - TypedArenaPtr::new(dst as *mut Self) + TypedArenaPtr::new(dst) } } } @@ -649,11 +655,12 @@ impl ArenaAllocated for IndexPtr { mem::size_of::() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { ptr::write(dst, self); - TypedArenaPtr::new(dst as *mut Self) + TypedArenaPtr::new(dst) } } @@ -672,7 +679,10 @@ impl ArenaAllocated for IndexPtr { (*slab).next = arena.base; - let result = value.copy_to_arena(mem::transmute::<_, *mut IndexPtr>(&(*slab).header)); + let result = value.copy_to_arena( + &(*slab).header as *const crate::arena::ArenaHeader + as *mut crate::machine::machine_indices::IndexPtr, + ); arena.base = slab; result @@ -697,6 +707,7 @@ pub struct Arena { unsafe impl Send for Arena {} unsafe impl Sync for Arena {} +#[allow(clippy::new_without_default)] impl Arena { #[inline] pub fn new() -> Self { @@ -837,12 +848,12 @@ mod tests { let mut cell = HeapCellValue::from(fp.clone()); assert_eq!(cell.get_tag(), HeapCellValueTag::F64); - assert_eq!(cell.get_mark_bit(), false); + assert!(!cell.get_mark_bit()); assert_eq!(fp.deref(), &OrderedFloat(f)); cell.set_mark_bit(true); - assert_eq!(cell.get_mark_bit(), true); + assert!(cell.get_mark_bit()); read_heap_cell!(cell, (HeapCellValueTag::F64, ptr) => { @@ -874,7 +885,7 @@ mod tests { ); } None => { - assert!(false); + unreachable!(); } } @@ -890,7 +901,7 @@ mod tests { ); } None => { - assert!(false); + unreachable!(); } } } @@ -912,7 +923,6 @@ mod tests { let untyped_arena_ptr = match cell.to_untyped_arena_ptr() { Some(ptr) => ptr, None => { - assert!(false); unreachable!() } }; @@ -954,7 +964,7 @@ mod tests { ); } None => { - assert!(false); // we fail. + unreachable!(); } } @@ -991,7 +1001,7 @@ mod tests { assert_eq!(&*atom.as_str(), "f"); } None => { - assert!(false); + unreachable!(); } } @@ -1026,7 +1036,7 @@ mod tests { assert_eq!(&*pstr.as_str_from(0), "ronan"); } None => { - assert!(false); + unreachable!(); } } @@ -1046,7 +1056,7 @@ mod tests { match fixnum_cell.to_fixnum() { Some(n) => assert_eq!(n.get_num(), 3), - None => assert!(false), + None => unreachable!(), } read_heap_cell!(fixnum_cell, @@ -1062,52 +1072,48 @@ mod tests { match fixnum_b_cell.to_fixnum() { Some(n) => assert_eq!(n.get_num(), 1 << 54), - None => assert!(false), + None => unreachable!(), } - match Fixnum::build_with_checked(1 << 56) { - Ok(_) => assert!(false), - _ => assert!(true), + if Fixnum::build_with_checked(1 << 56).is_ok() { + unreachable!() } - match Fixnum::build_with_checked(i64::MAX) { - Ok(_) => assert!(false), - _ => assert!(true), + if Fixnum::build_with_checked(i64::MAX).is_ok() { + unreachable!() } - match Fixnum::build_with_checked(i64::MIN) { - Ok(_) => assert!(false), - _ => assert!(true), + if Fixnum::build_with_checked(i64::MIN).is_ok() { + unreachable!() } match Fixnum::build_with_checked(-1) { Ok(n) => assert_eq!(n.get_num(), -1), - _ => assert!(false), + _ => unreachable!(), } match Fixnum::build_with_checked((1 << 55) - 1) { Ok(n) => assert_eq!(n.get_num(), (1 << 55) - 1), - _ => assert!(false), + _ => unreachable!(), } match Fixnum::build_with_checked(-(1 << 55)) { Ok(n) => assert_eq!(n.get_num(), -(1 << 55)), - _ => assert!(false), + _ => unreachable!(), } - match Fixnum::build_with_checked(-(1 << 55) - 1) { - Ok(_n) => assert!(false), - _ => assert!(true), + if Fixnum::build_with_checked(-(1 << 55) - 1).is_ok() { + unreachable!() } match Fixnum::build_with_checked(-1) { Ok(n) => assert_eq!(-n, Fixnum::build_with(1)), - _ => assert!(false), + _ => unreachable!(), } // float - let float = 3.1415926f64; + let float = std::f64::consts::PI; let float_ptr = float_alloc!(float, wam.machine_st.arena); let cell = HeapCellValue::from(float_ptr); diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 5aa7b1b04..799dfbd65 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -268,7 +268,7 @@ impl<'a> ArithmeticEvaluator<'a> { let ninterm = if a1.interm_or(0) == 0 { self.incr_interm() } else { - self.interm.push(a1.clone()); + self.interm.push(a1); a1.interm_or(0) }; @@ -312,9 +312,8 @@ impl<'a> ArithmeticEvaluator<'a> { arg: usize, ) -> Result { let mut code = CodeDeque::new(); - let mut iter = src.iter()?; - while let Some(term_ref) = iter.next() { + for term_ref in src.iter()? { match term_ref? { ArithTermRef::Literal(c) => push_literal(&mut self.interm, c)?, ArithTermRef::Var(lvl, cell, name) => { @@ -353,17 +352,17 @@ impl<'a> ArithmeticEvaluator<'a> { } // integer division rounding function -- 9.1.3.1. -pub(crate) fn rnd_i<'a>(n: &'a Number, arena: &mut Arena) -> Number { +pub(crate) fn rnd_i(n: &'_ Number, arena: &mut Arena) -> Number { match n { &Number::Integer(i) => { let result = (&*i).try_into(); - if let Ok(value) = result{ + if let Ok(value) = result { fixnum!(Number, value, arena) } else { *n } } - &Number::Fixnum(_) => *n, + Number::Fixnum(_) => *n, &Number::Float(f) => { let f = f.floor(); @@ -376,7 +375,7 @@ pub(crate) fn rnd_i<'a>(n: &'a Number, arena: &mut Arena) -> Number { Number::Integer(arena_alloc!(Integer::from(f.0 as i64), arena)) } } - &Number::Rational(ref r) => { + Number::Rational(ref r) => { let (_, floor) = (r.fract(), r.floor()); if let Ok(value) = (&floor).try_into() { @@ -399,9 +398,9 @@ impl From for Integer { pub(crate) fn rnd_f(n: &Number) -> f64 { match n { &Number::Fixnum(n) => n.get_num() as f64, - &Number::Integer(ref n) => n.to_f64().value(), + Number::Integer(ref n) => n.to_f64().value(), &Number::Float(OrderedFloat(f)) => f, - &Number::Rational(ref r) => r.to_f64().value(), + Number::Rational(ref r) => r.to_f64().value(), } } @@ -529,47 +528,51 @@ impl PartialEq for Number { fn eq(&self, rhs: &Self) -> bool { match (self, rhs) { (&Number::Fixnum(n1), &Number::Fixnum(n2)) => n1.eq(&n2), - (&Number::Fixnum(n1), &Number::Integer(ref n2)) => n1.get_num().num_eq(&**n2), - (&Number::Integer(ref n1), &Number::Fixnum(n2)) => (&**n1).num_eq(&n2.get_num()), - (&Number::Fixnum(n1), &Number::Rational(ref n2)) => Integer::from(n1.get_num()).num_eq(&**n2), - (&Number::Rational(ref n1), &Number::Fixnum(n2)) => (&**n1).num_eq(&Integer::from(n2.get_num())), + (&Number::Fixnum(n1), Number::Integer(ref n2)) => n1.get_num().num_eq(&**n2), + (Number::Integer(ref n1), &Number::Fixnum(n2)) => n1.num_eq(&n2.get_num()), + (&Number::Fixnum(n1), Number::Rational(ref n2)) => { + Integer::from(n1.get_num()).num_eq(&**n2) + } + (Number::Rational(ref n1), &Number::Fixnum(n2)) => { + n1.num_eq(&Integer::from(n2.get_num())) + } (&Number::Fixnum(n1), &Number::Float(n2)) => OrderedFloat(n1.get_num() as f64).eq(&n2), (&Number::Float(n1), &Number::Fixnum(n2)) => n1.eq(&OrderedFloat(n2.get_num() as f64)), - (&Number::Integer(ref n1), &Number::Integer(ref n2)) => n1.eq(n2), - (&Number::Integer(ref n1), Number::Float(n2)) => { + (Number::Integer(ref n1), Number::Integer(ref n2)) => n1.eq(n2), + (Number::Integer(ref n1), Number::Float(n2)) => { OrderedFloat(n1.to_f64().value()).eq(n2) } - (&Number::Float(n1), &Number::Integer(ref n2)) => { + (&Number::Float(n1), Number::Integer(ref n2)) => { n1.eq(&OrderedFloat(n2.to_f64().value())) } - (&Number::Integer(ref n1), &Number::Rational(ref n2)) => { + (Number::Integer(ref n1), Number::Rational(ref n2)) => { #[cfg(feature = "num")] { &Rational::from(&**n1) == &**n2 } #[cfg(not(feature = "num"))] { - (&**n1).num_eq(&**n2) + n1.num_eq(&**n2) } } - (&Number::Rational(ref n1), &Number::Integer(ref n2)) => { + (Number::Rational(ref n1), Number::Integer(ref n2)) => { #[cfg(feature = "num")] { - &**n1 == &Rational::from(&**n2) + n1 == &Rational::from(&**n2) } #[cfg(not(feature = "num"))] { - (&**n1).num_eq(&**n2) + n1.num_eq(&**n2) } } - (&Number::Rational(ref n1), &Number::Float(n2)) => { + (Number::Rational(ref n1), &Number::Float(n2)) => { OrderedFloat(n1.to_f64().value()).eq(&n2) } - (&Number::Float(n1), &Number::Rational(ref n2)) => { + (&Number::Float(n1), Number::Rational(ref n2)) => { n1.eq(&OrderedFloat(n2.to_f64().value())) } (&Number::Float(f1), &Number::Float(f2)) => f1.eq(&f2), - (&Number::Rational(ref r1), &Number::Rational(ref r2)) => r1.eq(&r2), + (Number::Rational(ref r1), Number::Rational(ref r2)) => r1.eq(r2), } } } @@ -589,8 +592,8 @@ impl PartialOrd for Number { (n as usize).partial_cmp(rhs) } } - Number::Integer(n) => Some((&**n).num_cmp(rhs)), - Number::Rational(r) => Some((&**r).num_cmp(&Integer::from(*rhs))), + Number::Integer(n) => Some((n).num_cmp(rhs)), + Number::Rational(r) => Some((r).num_cmp(&Integer::from(*rhs))), Number::Float(f) => f.partial_cmp(&OrderedFloat(*rhs as f64)), } } @@ -609,8 +612,8 @@ impl PartialEq for Number { (n as usize).eq(rhs) } } - Number::Integer(n) => (&**n).num_eq(rhs), - Number::Rational(r) => (&**r).num_eq(&Integer::from(*rhs)), + Number::Integer(n) => (n).num_eq(rhs), + Number::Rational(r) => (r).num_eq(&Integer::from(*rhs)), Number::Float(f) => f.eq(&OrderedFloat(*rhs as f64)), } } @@ -626,17 +629,17 @@ impl Ord for Number { fn cmp(&self, rhs: &Number) -> Ordering { match (self, rhs) { (&Number::Fixnum(n1), &Number::Fixnum(n2)) => n1.get_num().cmp(&n2.get_num()), - (&Number::Fixnum(n1), Number::Integer(n2)) => Integer::from(n1.get_num()).cmp(&*n2), - (Number::Integer(n1), &Number::Fixnum(n2)) => (&**n1).cmp(&Integer::from(n2.get_num())), - (&Number::Fixnum(n1), Number::Rational(n2)) => Rational::from(n1.get_num()).cmp(&*n2), + (&Number::Fixnum(n1), Number::Integer(n2)) => Integer::from(n1.get_num()).cmp(n2), + (Number::Integer(n1), &Number::Fixnum(n2)) => (**n1).cmp(&Integer::from(n2.get_num())), + (&Number::Fixnum(n1), Number::Rational(n2)) => Rational::from(n1.get_num()).cmp(n2), (Number::Rational(n1), &Number::Fixnum(n2)) => { - (&**n1).cmp(&Rational::from(n2.get_num())) + (**n1).cmp(&Rational::from(n2.get_num())) } (&Number::Fixnum(n1), &Number::Float(n2)) => OrderedFloat(n1.get_num() as f64).cmp(&n2), (&Number::Float(n1), &Number::Fixnum(n2)) => n1.cmp(&OrderedFloat(n2.get_num() as f64)), (&Number::Integer(n1), &Number::Integer(n2)) => (*n1).cmp(&*n2), (&Number::Integer(n1), Number::Float(n2)) => OrderedFloat(n1.to_f64().value()).cmp(n2), - (&Number::Float(n1), &Number::Integer(ref n2)) => { + (&Number::Float(n1), Number::Integer(ref n2)) => { n1.cmp(&OrderedFloat(n2.to_f64().value())) } (&Number::Integer(n1), &Number::Rational(n2)) => { @@ -646,7 +649,7 @@ impl Ord for Number { } #[cfg(not(feature = "num"))] { - (&*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less) + (*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less) } } (&Number::Rational(n1), &Number::Integer(n2)) => { @@ -656,7 +659,7 @@ impl Ord for Number { } #[cfg(not(feature = "num"))] { - (&*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less) + (*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less) } } (&Number::Rational(n1), &Number::Float(n2)) => { diff --git a/src/atom_table.rs b/src/atom_table.rs index db38f642e..e62ca9276 100644 --- a/src/atom_table.rs +++ b/src/atom_table.rs @@ -186,7 +186,7 @@ impl Atom { unsafe { AtomTableRef::try_map(atom_table.buf(), |buf| { (buf as *const u8) - .offset(((self.index as usize) - (STRINGS.len() << 3)) as isize) + .add((self.index as usize) - (STRINGS.len() << 3)) .as_ref() }) } @@ -209,9 +209,13 @@ impl Atom { } } + pub fn is_empty(self) -> bool { + self.len() == 0 + } + #[inline(always)] pub fn flat_index(self) -> u64 { - (self.index >> 3) as u64 + self.index >> 3 } pub fn as_char(self) -> Option { @@ -232,20 +236,17 @@ impl Atom { pub fn as_str(&self) -> AtomString<'static> { if self.is_static() { AtomString::Static(STRINGS[(self.index >> 3) as usize]) + } else if let Some(ptr) = self.as_ptr() { + AtomString::Dynamic(AtomTableRef::map(ptr, |ptr| { + let header = + unsafe { ptr::read::(ptr as *const u8 as *const AtomHeader) }; + let len = header.len() as usize; + let buf = unsafe { (ptr as *const u8).add(mem::size_of::()) }; + + unsafe { str::from_utf8_unchecked(slice::from_raw_parts(buf, len)) } + })) } else { - if let Some(ptr) = self.as_ptr() { - AtomString::Dynamic(AtomTableRef::map(ptr, |ptr| { - let header = - unsafe { ptr::read::(ptr as *const u8 as *const AtomHeader) }; - let len = header.len() as usize; - let buf = - unsafe { (ptr as *const u8).offset(mem::size_of::() as isize) }; - - unsafe { str::from_utf8_unchecked(slice::from_raw_parts(buf, len)) } - })) - } else { - AtomString::Static(&STRINGS[(self.index >> 3) as usize]) - } + AtomString::Static(STRINGS[(self.index >> 3) as usize]) } } @@ -258,14 +259,14 @@ impl Atom { return *self; }; - AtomTable::build_with(&atom_tbl, &sub_str) + AtomTable::build_with(atom_tbl, sub_str) } } unsafe fn write_to_ptr(string: &str, ptr: *mut u8) { ptr::write(ptr as *mut _, AtomHeader::build_with(string.len() as u64)); let str_ptr = (ptr as usize + mem::size_of::()) as *mut u8; - ptr::copy_nonoverlapping(string.as_ptr(), str_ptr as *mut u8, string.len()); + ptr::copy_nonoverlapping(string.as_ptr(), str_ptr, string.len()); } impl PartialOrd for Atom { diff --git a/src/bin/scryer-prolog.rs b/src/bin/scryer-prolog.rs index aa507cfd2..572eb2fa5 100644 --- a/src/bin/scryer-prolog.rs +++ b/src/bin/scryer-prolog.rs @@ -1,6 +1,6 @@ fn main() -> std::process::ExitCode { - use scryer_prolog::*; use scryer_prolog::atom_table::Atom; + use scryer_prolog::*; use std::sync::atomic::Ordering; #[cfg(feature = "repl")] diff --git a/src/codegen.rs b/src/codegen.rs index d918de528..ae4756836 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -111,7 +111,7 @@ impl BranchCodeStack { jump_span -= code.len() + 1; } else { jump_span -= code.len() + 1; - code.push_back(instr!("jmp_by_call", jump_span as usize)); + code.push_back(instr!("jmp_by_call", jump_span)); jump_span -= 1; } @@ -124,9 +124,9 @@ impl BranchCodeStack { for mut branch_arm in self.stack.drain(self.stack.len() - depth..).rev() { let num_branch_arms = branch_arm.len(); - branch_arm - .last_mut() - .map(|code| code.extend(combined_code.drain(..))); + if let Some(code) = branch_arm.last_mut() { + code.extend(combined_code.drain(..)) + } for (idx, code) in branch_arm.into_iter().enumerate() { combined_code.push_back(if idx == 0 { @@ -376,7 +376,7 @@ impl<'b> CodeGenerator<'b> { Target: crate::targets::CompilationTarget<'a>, { if let Some(ref mut instr) = target.back_mut() { - if Target::is_void_instr(&*instr) { + if Target::is_void_instr(instr) { Target::incr_void_instr(instr); return; } @@ -418,10 +418,10 @@ impl<'b> CodeGenerator<'b> { .mark_non_var::(Level::Deep, term_loc, cell, target); target.push_back(Target::clause_arg_to_instr(cell.get())); } - &Term::Literal(_, ref constant) => { - target.push_back(Target::constant_subterm(constant.clone())); + Term::Literal(_, ref constant) => { + target.push_back(Target::constant_subterm(*constant)); } - &Term::Var(ref cell, ref var_ptr) => { + Term::Var(ref cell, ref var_ptr) => { self.deep_var_instr::( cell, var_ptr.to_var_num().unwrap(), @@ -509,7 +509,7 @@ impl<'b> CodeGenerator<'b> { TermRef::PartialString(lvl, cell, string, tail) => { self.marker .mark_non_var::(lvl, term_loc, cell, &mut target); - let atom = AtomTable::build_with(&self.atom_tbl, &string); + let atom = AtomTable::build_with(self.atom_tbl, string); target.push_back(Target::to_pstr(lvl, atom, cell.get(), true)); self.subterm_to_instr::(tail, term_loc, &mut target); @@ -558,10 +558,10 @@ impl<'b> CodeGenerator<'b> { } } - fn compile_inlined<'a>( + fn compile_inlined( &mut self, ct: &InlinedClauseType, - terms: &'a Vec, + terms: &'_ [Term], term_loc: GenContext, code: &mut CodeDeque, ) -> Result<(), CompilationError> { @@ -585,13 +585,13 @@ impl<'b> CodeGenerator<'b> { compare_number_instr!(cmp, at_1, at_2) } - &InlinedClauseType::IsAtom(..) => match &terms[0] { - &Term::Literal(_, Literal::Char(_)) - | &Term::Literal(_, Literal::Atom(atom!("[]"))) - | &Term::Literal(_, Literal::Atom(..)) => { + InlinedClauseType::IsAtom(..) => match &terms[0] { + Term::Literal(_, Literal::Char(_)) + | Term::Literal(_, Literal::Atom(atom!("[]"))) + | Term::Literal(_, Literal::Atom(..)) => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -608,21 +608,21 @@ impl<'b> CodeGenerator<'b> { instr!("$fail") } }, - &InlinedClauseType::IsAtomic(..) => match &terms[0] { - &Term::AnonVar - | &Term::Clause(..) - | &Term::Cons(..) - | &Term::PartialString(..) - | &Term::CompleteString(..) => { + InlinedClauseType::IsAtomic(..) => match &terms[0] { + Term::AnonVar + | Term::Clause(..) + | Term::Cons(..) + | Term::PartialString(..) + | Term::CompleteString(..) => { instr!("$fail") } - &Term::Literal(_, Literal::String(_)) => { + Term::Literal(_, Literal::String(_)) => { instr!("$fail") } - &Term::Literal(..) => { + Term::Literal(..) => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -636,15 +636,15 @@ impl<'b> CodeGenerator<'b> { instr!("atomic", r) } }, - &InlinedClauseType::IsCompound(..) => match &terms[0] { - &Term::Clause(..) - | &Term::Cons(..) - | &Term::PartialString(..) - | &Term::CompleteString(..) - | &Term::Literal(_, Literal::String(..)) => { + InlinedClauseType::IsCompound(..) => match &terms[0] { + Term::Clause(..) + | Term::Cons(..) + | Term::PartialString(..) + | Term::CompleteString(..) + | Term::Literal(_, Literal::String(..)) => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -661,11 +661,11 @@ impl<'b> CodeGenerator<'b> { instr!("$fail") } }, - &InlinedClauseType::IsRational(..) => match &terms[0] { - &Term::Literal(_, Literal::Rational(_)) => { + InlinedClauseType::IsRational(..) => match terms[0] { + Term::Literal(_, Literal::Rational(_)) => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( name.to_var_num().unwrap(), @@ -680,11 +680,11 @@ impl<'b> CodeGenerator<'b> { instr!("$fail") } }, - &InlinedClauseType::IsFloat(..) => match &terms[0] { - &Term::Literal(_, Literal::Float(_)) => { + InlinedClauseType::IsFloat(..) => match terms[0] { + Term::Literal(_, Literal::Float(_)) => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -701,14 +701,14 @@ impl<'b> CodeGenerator<'b> { instr!("$fail") } }, - &InlinedClauseType::IsNumber(..) => match &terms[0] { - &Term::Literal(_, Literal::Float(_)) - | &Term::Literal(_, Literal::Rational(_)) - | &Term::Literal(_, Literal::Integer(_)) - | &Term::Literal(_, Literal::Fixnum(_)) => { + InlinedClauseType::IsNumber(..) => match terms[0] { + Term::Literal(_, Literal::Float(_)) + | Term::Literal(_, Literal::Rational(_)) + | Term::Literal(_, Literal::Integer(_)) + | Term::Literal(_, Literal::Fixnum(_)) => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -725,11 +725,11 @@ impl<'b> CodeGenerator<'b> { instr!("$fail") } }, - &InlinedClauseType::IsNonVar(..) => match &terms[0] { - &Term::AnonVar => { + InlinedClauseType::IsNonVar(..) => match terms[0] { + Term::AnonVar => { instr!("$fail") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -746,11 +746,11 @@ impl<'b> CodeGenerator<'b> { instr!("$succeed") } }, - &InlinedClauseType::IsInteger(..) => match &terms[0] { - &Term::Literal(_, Literal::Integer(_)) | &Term::Literal(_, Literal::Fixnum(_)) => { + InlinedClauseType::IsInteger(..) => match &terms[0] { + Term::Literal(_, Literal::Integer(_)) | Term::Literal(_, Literal::Fixnum(_)) => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -767,18 +767,18 @@ impl<'b> CodeGenerator<'b> { instr!("$fail") } }, - &InlinedClauseType::IsVar(..) => match &terms[0] { - &Term::Literal(..) - | &Term::Clause(..) - | &Term::Cons(..) - | &Term::PartialString(..) - | &Term::CompleteString(..) => { + InlinedClauseType::IsVar(..) => match terms[0] { + Term::Literal(..) + | Term::Clause(..) + | Term::Cons(..) + | Term::PartialString(..) + | Term::CompleteString(..) => { instr!("$fail") } - &Term::AnonVar => { + Term::AnonVar => { instr!("$succeed") } - &Term::Var(ref vr, ref name) => { + Term::Var(ref vr, ref name) => { self.marker.reset_arg(1); let r = self.marker.mark_non_callable( @@ -813,7 +813,7 @@ impl<'b> CodeGenerator<'b> { fn compile_is_call( &mut self, - terms: &Vec, + terms: &[Term], code: &mut CodeDeque, term_loc: GenContext, call_policy: CallPolicy, @@ -828,8 +828,8 @@ impl<'b> CodeGenerator<'b> { self.marker.reset_arg(2); - let at = match &terms[0] { - &Term::Var(ref vr, ref name) => { + let at = match terms[0] { + Term::Var(ref vr, ref name) => { let var_num = name.to_var_num().unwrap(); if self.marker.var_data.records[var_num].num_occurrences > 1 { @@ -871,7 +871,7 @@ impl<'b> CodeGenerator<'b> { compile_expr!(self, &terms[1], term_loc, code) } } - &Term::Literal( + Term::Literal( _, c @ Literal::Integer(_) | c @ Literal::Float(_) @@ -896,7 +896,7 @@ impl<'b> CodeGenerator<'b> { Ok(()) } - fn compile_seq<'a>( + fn compile_seq( &mut self, clauses: &ChunkedTermVec, code: &mut CodeDeque, @@ -1066,7 +1066,7 @@ impl<'b> CodeGenerator<'b> { self.marker.reset_at_head(args); - let iter = FactIterator::from_rule_head_clause(&args); + let iter = FactIterator::from_rule_head_clause(args); let fact = self.compile_target::(iter, GenContext::Head); if self.marker.max_reg_allocated() > MAX_ARITY { @@ -1074,7 +1074,7 @@ impl<'b> CodeGenerator<'b> { } self.marker.reset_free_list(); - code.extend(fact.into_iter()); + code.extend(fact); self.compile_seq(clauses, &mut code)?; @@ -1099,7 +1099,7 @@ impl<'b> CodeGenerator<'b> { return Err(CompilationError::ExceededMaxArity); } - code.extend(compiled_fact.into_iter()); + code.extend(compiled_fact); } code.push(instr!("proceed")); @@ -1112,7 +1112,7 @@ impl<'b> CodeGenerator<'b> { let iter = QueryIterator::new(term); let query = self.compile_target::(iter, term_loc); - code.extend(query.into_iter()); + code.extend(query); match term { &QueryTerm::Clause(_, ref ct, _, call_policy) => { @@ -1204,12 +1204,12 @@ impl<'b> CodeGenerator<'b> { let clause_code = match clause { PredicateClause::Fact(fact, var_data) => { - let var_data = std::mem::replace(var_data, VarData::default()); - self.compile_fact(&fact, var_data)? + let var_data = std::mem::take(var_data); + self.compile_fact(fact, var_data)? } PredicateClause::Rule(rule, var_data) => { - let var_data = std::mem::replace(var_data, VarData::default()); - self.compile_rule(&rule, var_data)? + let var_data = std::mem::take(var_data); + self.compile_rule(rule, var_data)? } }; @@ -1237,9 +1237,7 @@ impl<'b> CodeGenerator<'b> { skip_stub_try_me_else = !self.settings.is_dynamic(); } - let arg = clause - .args() - .and_then(|args| args.iter().nth(optimal_index)); + let arg = clause.args().and_then(|args| args.get(optimal_index)); if let Some(arg) = arg { let index = code.len(); diff --git a/src/debray_allocator.rs b/src/debray_allocator.rs index 54f4283b3..01ec4da79 100644 --- a/src/debray_allocator.rs +++ b/src/debray_allocator.rs @@ -295,11 +295,9 @@ impl DebrayAllocator { let mut result = 0; for reg in self.temp_lb.. { - if !self.is_in_use(reg) { - if !temp_var_data.no_use_set.contains(reg) { - result = reg; - break; - } + if !self.is_in_use(reg) && !temp_var_data.no_use_set.contains(reg) { + result = reg; + break; } } @@ -321,13 +319,12 @@ impl DebrayAllocator { let mut result = 0; for reg in self.temp_lb.. { - if !self.is_in_use(reg) { - if !temp_var_data.no_use_set.contains(reg) { - if !temp_var_data.conflict_set.contains(reg) { - result = reg; - break; - } - } + if !self.is_in_use(reg) + && !temp_var_data.no_use_set.contains(reg) + && !temp_var_data.conflict_set.contains(reg) + { + result = reg; + break; } } @@ -349,16 +346,15 @@ impl DebrayAllocator { // consider its use set. T == par_k iff // (GenContext::Last(_), k) is in t_var.use_set. - match &self.var_data.records[t_var].allocation { - VarAlloc::Temp { temp_var_data, .. } => { - if !temp_var_data - .use_set - .contains(&(GenContext::Last(chunk_num), k)) - { - return Some((t_var, self.alloc_with_ca(t_var))); - } + if let VarAlloc::Temp { temp_var_data, .. } = + &self.var_data.records[t_var].allocation + { + if !temp_var_data + .use_set + .contains(&(GenContext::Last(chunk_num), k)) + { + return Some((t_var, self.alloc_with_ca(t_var))); } - _ => {} } None @@ -372,25 +368,22 @@ impl DebrayAllocator { chunk_num: usize, code: &mut CodeDeque, ) { - match self.alloc_in_last_goal_hint(chunk_num) { - Some((var_num, r)) => { - let k = self.arg_c; + if let Some((var_num, r)) = self.alloc_in_last_goal_hint(chunk_num) { + let k = self.arg_c; - if r != k { - let r = RegType::Temp(r); + if r != k { + let r = RegType::Temp(r); - code.push_back(Target::move_to_register(r, k)); + code.push_back(Target::move_to_register(r, k)); - self.shallow_temp_mappings.swap_remove(&k); - self.shallow_temp_mappings.insert(r.reg_num(), var_num); + self.shallow_temp_mappings.swap_remove(&k); + self.shallow_temp_mappings.insert(r.reg_num(), var_num); - self.var_data.records[var_num] - .allocation - .set_register(r.reg_num()); - self.in_use.insert(r.reg_num()); - } + self.var_data.records[var_num] + .allocation + .set_register(r.reg_num()); + self.in_use.insert(r.reg_num()); } - _ => {} }; } @@ -493,11 +486,8 @@ impl DebrayAllocator { } fn add_perm_to_free_list(&mut self, chunk_num: usize, var_num: usize) { - match &self.var_data.records[var_num].allocation { - VarAlloc::Perm(..) => { - self.perm_free_list.push_back((chunk_num, var_num)); - } - _ => {} + if let VarAlloc::Perm(..) = &self.var_data.records[var_num].allocation { + self.perm_free_list.push_back((chunk_num, var_num)); } } @@ -521,12 +511,9 @@ impl DebrayAllocator { } pub(crate) fn free_var(&mut self, chunk_num: usize, var_num: usize) { - match &mut self.var_data.records[var_num].allocation { - VarAlloc::Perm(_, allocation) => { - *allocation = PermVarAllocation::Pending; - self.add_perm_to_free_list(chunk_num, var_num); - } - _ => {} + if let VarAlloc::Perm(_, allocation) = &mut self.var_data.records[var_num].allocation { + *allocation = PermVarAllocation::Pending; + self.add_perm_to_free_list(chunk_num, var_num); } } @@ -570,18 +557,15 @@ impl DebrayAllocator { *shallow_safety = VarSafetyStatus::unneeded(branch_designator); } else if term_loc == GenContext::Head { *shallow_safety = VarSafetyStatus::GloballyUnneeded; - } else { - if let Some(temp_var_num) = self.shallow_temp_mappings.get(&self.arg_c).cloned() - { - match &mut self.var_data.records[temp_var_num].allocation { - VarAlloc::Temp { - ref mut to_perm_var_num, - .. - } => { - *to_perm_var_num = Some(var_num); - } - _ => unreachable!(), + } else if let Some(&temp_var_num) = self.shallow_temp_mappings.get(&self.arg_c) { + match &mut self.var_data.records[temp_var_num].allocation { + VarAlloc::Temp { + ref mut to_perm_var_num, + .. + } => { + *to_perm_var_num = Some(var_num); } + _ => unreachable!(), } } } @@ -886,12 +870,12 @@ impl Allocator for DebrayAllocator { self.arg_c += 1; } - fn reset_at_head(&mut self, args: &Vec) { + fn reset_at_head(&mut self, args: &[Term]) { self.reset_arg(args.len()); self.arity = args.len(); for (idx, arg) in args.iter().enumerate() { - if let &Term::Var(_, ref var) = arg { + if let Term::Var(_, ref var) = arg { let var_num = var.to_var_num().unwrap(); let r = self.get_binding(var_num); diff --git a/src/ffi.rs b/src/ffi.rs index 8fd55dbcc..64879e58a 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -28,7 +28,8 @@ use std::convert::TryFrom; use std::error::Error; use std::ffi::{c_void, CString}; -use libffi::low::{ffi_abi_FFI_DEFAULT_ABI, ffi_cif, ffi_type, prep_cif, type_tag, types, CodePtr}; +use libffi::low::type_tag::STRUCT; +use libffi::low::{ffi_abi_FFI_DEFAULT_ABI, ffi_cif, ffi_type, prep_cif, types, CodePtr}; use libloading::{Library, Symbol}; pub struct FunctionDefinition { @@ -69,11 +70,13 @@ impl ForeignFunctionTable { } pub fn define_struct(&mut self, name: &str, atom_fields: Vec) { - let mut fields: Vec<_> = atom_fields.iter().map(|x| self.map_type_ffi(&x)).collect(); + let mut fields: Vec<_> = atom_fields.iter().map(|x| self.map_type_ffi(x)).collect(); fields.push(std::ptr::null_mut::()); - let mut struct_type: ffi_type = Default::default(); - struct_type.type_ = type_tag::STRUCT; - struct_type.elements = fields.as_mut_ptr(); + let struct_type = ffi_type { + type_: STRUCT, + elements: fields.as_mut_ptr(), + ..Default::default() + }; self.structs.insert( name.to_string(), StructImpl { @@ -121,11 +124,7 @@ impl ForeignFunctionTable { let symbol_name: CString = CString::new(function.name.clone())?; let code_ptr: Symbol<*mut c_void> = library.get(&symbol_name.into_bytes_with_nul())?; - let mut args: Vec<_> = function - .args - .iter() - .map(|x| self.map_type_ffi(&x)) - .collect(); + let mut args: Vec<_> = function.args.iter().map(|x| self.map_type_ffi(x)).collect(); let mut cif: ffi_cif = Default::default(); prep_cif( &mut cif, @@ -163,7 +162,7 @@ impl ForeignFunctionTable { fn build_pointer_args( args: &mut Vec, - type_args: &Vec<*mut ffi_type>, + type_args: &[*mut ffi_type], structs_table: &mut HashMap, ) -> Result { let mut pointers = Vec::with_capacity(args.len()); @@ -237,6 +236,7 @@ impl ForeignFunctionTable { let ptr = alloc(layout) as *mut c_void; let mut field_ptr = ptr; + #[allow(clippy::needless_range_loop)] for i in 0..(struct_type.fields.len() - 1) { macro_rules! try_write_int { ($type:ty) => {{ @@ -283,7 +283,7 @@ impl ForeignFunctionTable { std::ptr::copy( &*struct_ptr as *const _ as *const c_void, - field_ptr as *mut c_void, + field_ptr, struct_size, ); field_ptr = field_ptr.add(struct_size); @@ -293,12 +293,13 @@ impl ForeignFunctionTable { } } } - return Ok((Box::from_raw(ptr), size, align)); + #[allow(clippy::from_raw_with_void_ptr)] + Ok((Box::from_raw(ptr), size, align)) } else { - return Err(FFIError::InvalidStructName); + Err(FFIError::InvalidStructName) } } - _ => return Err(FFIError::ValueCast), + _ => Err(FFIError::ValueCast), } } } @@ -336,7 +337,7 @@ impl ForeignFunctionTable { &mut function_impl.cif, Some(*function_impl.code_ptr.as_safe_fun()), &mut *n as *mut _ as *mut c_void, - pointer_args.pointers.as_mut_ptr() as *mut *mut c_void, + pointer_args.pointers.as_mut_ptr(), ); Ok(Value::Int( i64::try_from(*n).map_err(|_| FFIError::ValueDontFit)?, @@ -350,7 +351,7 @@ impl ForeignFunctionTable { &mut function_impl.cif, Some(*function_impl.code_ptr.as_safe_fun()), &mut *n as *mut _ as *mut c_void, - pointer_args.pointers.as_mut_ptr() as *mut *mut c_void, + pointer_args.pointers.as_mut_ptr(), ); Ok(Value::Float((*n).into())) } @@ -360,7 +361,7 @@ impl ForeignFunctionTable { &mut function_impl.cif, Some(*function_impl.code_ptr.as_safe_fun()), &mut *n as *mut _ as *mut c_void, - pointer_args.pointers.as_mut_ptr() as *mut *mut c_void, + pointer_args.pointers.as_mut_ptr(), ); Ok(Value::Float(*n)) } @@ -380,10 +381,11 @@ impl ForeignFunctionTable { libffi::raw::ffi_call( &mut function_impl.cif, Some(*function_impl.code_ptr.as_safe_fun()), - &mut *ptr as *mut _ as *mut c_void, - pointer_args.pointers.as_mut_ptr() as *mut *mut c_void, + &mut *ptr as *mut _, + pointer_args.pointers.as_mut_ptr(), ); let struct_val = self.read_struct(ptr, name, struct_type); + #[allow(clippy::from_raw_with_void_ptr)] drop(Box::from_raw(ptr)); struct_val } @@ -441,7 +443,7 @@ impl ForeignFunctionTable { .ok_or(FFIError::StructNotFound)?; field_ptr = field_ptr .add(field_ptr.align_offset(struct_type.ffi_type.alignment as usize)); - let struct_val = self.read_struct(field_ptr, &*substruct, struct_type); + let struct_val = self.read_struct(field_ptr, &substruct, struct_type); returns.push(struct_val?); field_ptr = field_ptr.add(struct_type.ffi_type.size); } diff --git a/src/forms.rs b/src/forms.rs index 6b8771976..50a24b23c 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -11,6 +11,7 @@ use crate::parser::dashu::{Integer, Rational}; use crate::parser::parser::CompositeOpDesc; use crate::types::*; +use dashu::base::Signed; use fxhash::FxBuildHasher; use indexmap::{IndexMap, IndexSet}; @@ -99,11 +100,7 @@ pub enum RootIterationPolicy { impl RootIterationPolicy { #[inline(always)] pub fn iterable(&self) -> bool { - if let RootIterationPolicy::Iterated = self { - true - } else { - false - } + matches!(self, RootIterationPolicy::Iterated) } } @@ -151,6 +148,7 @@ impl DerefMut for ChunkedTermVec { } impl ChunkedTermVec { + #[allow(clippy::new_without_default)] #[inline] pub fn new() -> Self { Self { @@ -202,8 +200,8 @@ pub enum QueryTerm { // register, clause type, subterms, clause call policy. Clause(Cell, ClauseType, Vec, CallPolicy), Fail, - LocalCut { var_num: usize, cut_prev: bool }, // var_num - GlobalCut(usize), // var_num + LocalCut { var_num: usize, cut_prev: bool }, // var_num + GlobalCut(usize), // var_num GetCutPoint { var_num: usize, prev_b: bool }, GetLevel(usize), // var_num } @@ -211,7 +209,7 @@ pub enum QueryTerm { impl QueryTerm { pub(crate) fn arity(&self) -> usize { match self { - &QueryTerm::Clause(_, _, ref subterms, ..) => subterms.len(), + QueryTerm::Clause(_, _, subterms, ..) => subterms.len(), &QueryTerm::GetLevel(_) | &QueryTerm::GetCutPoint { .. } => 1, _ => 0, } @@ -316,15 +314,15 @@ impl ClauseInfo for Rule { impl ClauseInfo for PredicateClause { fn name(&self) -> Option { match self { - &PredicateClause::Fact(ref term, ..) => term.head.name(), - &PredicateClause::Rule(ref rule, ..) => rule.name(), + PredicateClause::Fact(ref term, ..) => term.head.name(), + PredicateClause::Rule(ref rule, ..) => rule.name(), } } fn arity(&self) -> usize { match self { - &PredicateClause::Fact(ref term, ..) => term.head.arity(), - &PredicateClause::Rule(ref rule, ..) => rule.arity(), + PredicateClause::Fact(ref term, ..) => term.head.arity(), + PredicateClause::Rule(ref rule, ..) => rule.arity(), } } } @@ -339,7 +337,7 @@ impl PredicateClause { pub(crate) fn args(&self) -> Option<&[Term]> { match self { PredicateClause::Fact(term, ..) => match &term.head { - Term::Clause(_, _, args) => Some(&args), + Term::Clause(_, _, args) => Some(args), _ => None, }, PredicateClause::Rule(rule, ..) => { @@ -433,13 +431,10 @@ impl OpDecl { pub(crate) fn insert_into_op_dir(&self, op_dir: &mut OpDir) -> Option { let key = (self.name, fixity(self.op_desc.get_spec() as u32)); - match op_dir.get_mut(&key) { - Some(cell) => { - let (old_prec, old_spec) = cell.get(); - cell.set(self.op_desc.get_prec(), self.op_desc.get_spec()); - return Some(OpDesc::build_with(old_prec, old_spec)); - } - None => {} + if let Some(cell) = op_dir.get_mut(&key) { + let (old_prec, old_spec) = cell.get(); + cell.set(self.op_desc.get_prec(), self.op_desc.get_spec()); + return Some(OpDesc::build_with(old_prec, old_spec)); } op_dir.insert(key, self.op_desc) @@ -450,7 +445,7 @@ impl OpDecl { existing_desc: Option, op_dir: &mut OpDir, ) -> Result<(), SessionError> { - let (spec, name) = (self.op_desc.get_spec(), self.name.clone()); + let (spec, name) = (self.op_desc.get_spec(), self.name); if is_infix!(spec as u32) { if let Some(desc) = existing_desc { @@ -484,7 +479,7 @@ impl AtomOrString { pub fn as_atom(&self, atom_tbl: &AtomTable) -> Atom { match self { &AtomOrString::Atom(atom) => atom, - AtomOrString::String(string) => AtomTable::build_with(atom_tbl, &string), + AtomOrString::String(string) => AtomTable::build_with(atom_tbl, string), } } @@ -496,10 +491,11 @@ impl AtomOrString { AtomOrString::String(string) => AtomString::Static(string.as_str()), } } +} - #[inline] - pub fn to_string(self) -> String { - match self { +impl From for String { + fn from(val: AtomOrString) -> Self { + match val { AtomOrString::Atom(atom) => atom.as_str().to_owned(), AtomOrString::String(string) => string, } @@ -543,7 +539,7 @@ pub(crate) fn fetch_op_spec(name: Atom, arity: usize, op_dir: &OpDir) -> Option< } }), 1 => { - if let Some(op_desc) = op_dir.get(&(name.clone(), Fixity::Pre)) { + if let Some(op_desc) = op_dir.get(&(name, Fixity::Pre)) { if op_desc.get_prec() > 0 { return Some(*op_desc); } @@ -744,8 +740,8 @@ impl ArenaFrom for HeapCellValue { impl Number { pub(crate) fn sign(&self) -> Number { match self { - &Number::Float(f) if f == 0.0 => Number::Float(OrderedFloat(0f64)), - &Number::Float(f) => Number::Float(OrderedFloat(f.signum())), + Number::Float(f) if *f == 0.0 => Number::Float(OrderedFloat(0f64)), + Number::Float(f) => Number::Float(OrderedFloat(f.signum())), _ => { if self.is_positive() { Number::Fixnum(Fixnum::build_with(1)) @@ -761,39 +757,36 @@ impl Number { #[inline] pub(crate) fn is_positive(&self) -> bool { match self { - &Number::Fixnum(n) => n.get_num() > 0, - &Number::Integer(ref n) => &**n > &Integer::from(0), - &Number::Float(f) => f.is_sign_positive(), - &Number::Rational(ref r) => &**r > &Rational::from(0), + Number::Fixnum(n) => n.get_num() > 0, + Number::Integer(ref n) => n.is_positive(), + Number::Float(f) => f.is_sign_positive(), + Number::Rational(ref r) => r.is_positive(), } } #[inline] pub(crate) fn is_negative(&self) -> bool { match self { - &Number::Fixnum(n) => n.get_num() < 0, - &Number::Integer(ref n) => &**n < &Integer::from(0), - &Number::Float(OrderedFloat(f)) => f.is_sign_negative() && OrderedFloat(f) != -0f64, - &Number::Rational(ref r) => &**r < &Rational::from(0), + Number::Fixnum(n) => n.get_num() < 0, + Number::Integer(ref n) => n.is_negative(), + &Number::Float(OrderedFloat(f)) => f.is_sign_negative() && f != -0f64, + Number::Rational(ref r) => r.is_negative(), } } #[inline] pub(crate) fn is_zero(&self) -> bool { match self { - &Number::Fixnum(n) => n.get_num() == 0, - &Number::Integer(ref n) => &**n == &Integer::from(0), - &Number::Float(f) => f == OrderedFloat(0f64) || f == OrderedFloat(-0f64), - &Number::Rational(ref r) => &**r == &Rational::from(0), + Number::Fixnum(n) => n.get_num() == 0, + Number::Integer(ref n) => n.is_zero(), + &Number::Float(OrderedFloat(f)) => f == 0.0 || f == -0.0, + Number::Rational(ref r) => r.is_zero(), } } #[inline] pub(crate) fn is_integer(&self) -> bool { - match self { - Number::Fixnum(_) | Number::Integer(_) => true, - _ => false, - } + matches!(self, Number::Fixnum(_) | Number::Integer(_)) } } @@ -963,7 +956,7 @@ impl LocalPredicateSkeleton { #[inline] pub(crate) fn add_retracted_dynamic_clause_info(&mut self, clause_info: ClauseIndexInfo) { - debug_assert_eq!(self.is_dynamic, true); + debug_assert!(self.is_dynamic); if self.retracted_dynamic_clauses.is_none() { self.retracted_dynamic_clauses = Some(vec![]); @@ -1008,7 +1001,7 @@ impl PredicateSkeleton { ) -> Option { let search_result = self.core.clause_clause_locs.make_contiguous() [0..self.core.clause_assert_margin] - .binary_search_by(|loc| clause_clause_loc.cmp(&loc)); + .binary_search_by(|loc| clause_clause_loc.cmp(loc)); match search_result { Ok(loc) => Some(loc), diff --git a/src/heap_iter.rs b/src/heap_iter.rs index 2eeba0077..4713aa631 100644 --- a/src/heap_iter.rs +++ b/src/heap_iter.rs @@ -43,7 +43,7 @@ impl<'a> Drop for EagerStackfulPreOrderHeapIter<'a> { self.start_value.set_mark_bit(true); self.iter_stack.push(self.start_value); - while let Some(_) = self.follow() {} + while self.follow().is_some() {} } } @@ -87,22 +87,22 @@ impl<'a> EagerStackfulPreOrderHeapIter<'a> { let arity = cell_as_atom_cell!(self.heap[s]).get_arity(); for idx in (s + 1 .. s + arity + 1).rev() { - if self.heap[idx].get_mark_bit() != self.mark_phase { + if self.heap[idx].get_mark_bit() != self.mark_phase { self.iter_stack.push(self.heap[idx]); self.heap[idx].set_mark_bit(self.mark_phase); - } + } } } (HeapCellValueTag::Lis, l) => { - if self.heap[l+1].get_mark_bit() != self.mark_phase { - self.iter_stack.push(self.heap[l+1]); - self.heap[l+1].set_mark_bit(self.mark_phase); - } - - if self.heap[l].get_mark_bit() != self.mark_phase { - self.iter_stack.push(self.heap[l]); - self.heap[l].set_mark_bit(self.mark_phase); - } + if self.heap[l+1].get_mark_bit() != self.mark_phase { + self.iter_stack.push(self.heap[l+1]); + self.heap[l+1].set_mark_bit(self.mark_phase); + } + + if self.heap[l].get_mark_bit() != self.mark_phase { + self.iter_stack.push(self.heap[l]); + self.heap[l].set_mark_bit(self.mark_phase); + } } (HeapCellValueTag::AttrVar | HeapCellValueTag::Var, h) => { let var_value = self.heap[h]; @@ -270,7 +270,9 @@ pub trait FocusedHeapIter: Iterator { fn focus(&self) -> IterStackLoc; } -impl<'a, ElideLists: ListElisionPolicy> FocusedHeapIter for StackfulPreOrderHeapIter<'a, ElideLists> { +impl<'a, ElideLists: ListElisionPolicy> FocusedHeapIter + for StackfulPreOrderHeapIter<'a, ElideLists> +{ #[inline] fn focus(&self) -> IterStackLoc { self.h @@ -506,10 +508,10 @@ impl<'a, ElideLists: ListElisionPolicy> Iterator for StackfulPreOrderHeapIter<'a } #[inline(always)] -pub(crate) fn cycle_detecting_stackless_preorder_iter<'a>( - heap: &'a mut [HeapCellValue], +pub(crate) fn cycle_detecting_stackless_preorder_iter( + heap: &'_ mut [HeapCellValue], start: usize, -) -> CycleDetectingIter<'a, true> { +) -> CycleDetectingIter<'_, true> { // const generics argument of true so that cycle discovery stops // the iterator. CycleDetectingIter::new(heap, start) @@ -660,25 +662,25 @@ pub(crate) fn stackful_post_order_iter<'a, ElideLists: ListElisionPolicy>( #[cfg(test)] mod tests { use super::*; + use crate::machine::gc::IteratorUMP; use crate::machine::mock_wam::*; - use crate::machine::gc::{IteratorUMP}; pub(crate) type RightistPostOrderHeapIter<'a> = PostOrderIterator>; #[inline(always)] pub(crate) fn stackless_preorder_iter( - heap: &mut Vec, + heap: &mut [HeapCellValue], start: usize, ) -> StacklessPreOrderHeapIter { StacklessPreOrderHeapIter::::new(heap, start) } #[inline] - pub(crate) fn stackless_post_order_iter<'a>( - heap: &'a mut Heap, + pub(crate) fn stackless_post_order_iter( + heap: &'_ mut Heap, start: usize, - ) -> RightistPostOrderHeapIter<'a> { + ) -> RightistPostOrderHeapIter { PostOrderIterator::new(stackless_preorder_iter(heap, start)) } @@ -954,7 +956,10 @@ mod tests { let pstr_offset_cell = pstr_offset_as_cell!(0); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_offset_cell); - assert_eq!(unmark_cell_bits!(iter.next().unwrap()), fixnum_as_cell!(Fixnum::build_with(2))); + assert_eq!( + unmark_cell_bits!(iter.next().unwrap()), + fixnum_as_cell!(Fixnum::build_with(2)) + ); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_cell); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_second_cell); @@ -999,11 +1004,17 @@ mod tests { let pstr_offset_cell = pstr_offset_as_cell!(0); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_cell); - assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_loc_as_cell!(4)); + assert_eq!( + unmark_cell_bits!(iter.next().unwrap()), + pstr_loc_as_cell!(4) + ); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_offset_cell); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_offset_cell); - assert_eq!(unmark_cell_bits!(iter.next().unwrap()), fixnum_as_cell!(Fixnum::build_with(0))); + assert_eq!( + unmark_cell_bits!(iter.next().unwrap()), + fixnum_as_cell!(Fixnum::build_with(0)) + ); assert_eq!(iter.next(), None); } @@ -1016,7 +1027,10 @@ mod tests { let mut iter = stackless_preorder_iter(&mut wam.machine_st.heap, 6); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_cell); - assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_loc_as_cell!(4)); + assert_eq!( + unmark_cell_bits!(iter.next().unwrap()), + pstr_loc_as_cell!(4) + ); assert_eq!( unmark_cell_bits!(iter.next().unwrap()), @@ -1035,7 +1049,10 @@ mod tests { } assert_eq!(wam.machine_st.heap[4], pstr_offset_as_cell!(0)); - assert_eq!(wam.machine_st.heap[5], fixnum_as_cell!(Fixnum::build_with(1i64))); + assert_eq!( + wam.machine_st.heap[5], + fixnum_as_cell!(Fixnum::build_with(1i64)) + ); all_cells_unmarked(&wam.machine_st.heap); @@ -1501,7 +1518,9 @@ mod tests { wam.machine_st.heap.clear(); { - wam.machine_st.heap.push(fixnum_as_cell!(Fixnum::build_with(0))); + wam.machine_st + .heap + .push(fixnum_as_cell!(Fixnum::build_with(0))); let mut iter = stackless_preorder_iter(&mut wam.machine_st.heap, 0); @@ -1536,7 +1555,10 @@ mod tests { atom_as_cell!(atom!("y")) ); - assert_eq!(unmark_cell_bits!(iter.next().unwrap()), heap_loc_as_cell!(0)); + assert_eq!( + unmark_cell_bits!(iter.next().unwrap()), + heap_loc_as_cell!(0) + ); assert!(iter.next().is_none()); } @@ -1685,10 +1707,7 @@ mod tests { wam.machine_st.heap.push(heap_loc_as_cell!(0)); { - let mut iter = stackless_preorder_iter( - &mut wam.machine_st.heap, - 9, - ); + let mut iter = stackless_preorder_iter(&mut wam.machine_st.heap, 9); /* while let Some(_) = iter.next() { @@ -2889,8 +2908,7 @@ mod tests { { wam.machine_st.heap.push(heap_loc_as_cell!(0)); - let mut iter = - stackless_post_order_iter(&mut wam.machine_st.heap, 0); + let mut iter = stackless_post_order_iter(&mut wam.machine_st.heap, 0); assert_eq!( unmark_cell_bits!(iter.next().unwrap()), @@ -2931,8 +2949,7 @@ mod tests { wam.machine_st.heap.push(empty_list_as_cell!()); { - let mut iter = - stackless_post_order_iter(&mut wam.machine_st.heap, 0); + let mut iter = stackless_post_order_iter(&mut wam.machine_st.heap, 0); assert_eq!( unmark_cell_bits!(iter.next().unwrap()), @@ -2964,8 +2981,7 @@ mod tests { wam.machine_st.heap.push(heap_loc_as_cell!(0)); { - let mut iter = - stackless_post_order_iter(&mut wam.machine_st.heap, 0); + let mut iter = stackless_post_order_iter(&mut wam.machine_st.heap, 0); // the cycle will be iterated twice before being detected. assert_eq!( @@ -2993,8 +3009,7 @@ mod tests { } { - let mut iter = - stackless_post_order_iter(&mut wam.machine_st.heap, 0); + let mut iter = stackless_post_order_iter(&mut wam.machine_st.heap, 0); // cut the iteration short to check that all cells are // unmarked and unforwarded by the Drop instance of @@ -3031,8 +3046,7 @@ mod tests { wam.machine_st.heap.push(pstr_loc_as_cell!(0)); { - let mut iter = - stackless_post_order_iter(&mut wam.machine_st.heap, 2); + let mut iter = stackless_post_order_iter(&mut wam.machine_st.heap, 2); assert_eq!( unmark_cell_bits!(iter.next().unwrap()), @@ -3119,10 +3133,7 @@ mod tests { unmark_cell_bits!(iter.next().unwrap()), heap_loc_as_cell!(3) ); - assert_eq!( - unmark_cell_bits!(iter.next().unwrap()), - pstr_second_cell - ); + assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_second_cell); assert_eq!(unmark_cell_bits!(iter.next().unwrap()), pstr_cell); assert_eq!(iter.next(), None); } diff --git a/src/heap_print.rs b/src/heap_print.rs index 2d92cd2d5..cbfbbd495 100644 --- a/src/heap_print.rs +++ b/src/heap_print.rs @@ -1,9 +1,9 @@ use crate::arena::*; use crate::atom_table::*; use crate::parser::ast::*; -use crate::parser::dashu::{ibig, Integer, Rational}; use crate::parser::dashu::base::RemEuclid; use crate::parser::dashu::integer::Sign; +use crate::parser::dashu::{ibig, Integer, Rational}; use crate::{ alpha_numeric_char, capital_letter_char, cut_char, decimal_digit_char, graphic_token_char, is_fx, is_infix, is_postfix, is_prefix, is_xf, is_xfx, is_xfy, is_yfx, semicolon_char, @@ -20,6 +20,7 @@ use crate::machine::stack::*; use crate::machine::streams::*; use crate::types::*; +use dashu::base::Signed; use ordered_float::OrderedFloat; use indexmap::IndexMap; @@ -258,9 +259,7 @@ pub(crate) fn requires_space(atom: &str, op: &str) -> bool { oc == '(' || alpha_numeric_char!(oc) } else if graphic_token_char!(ac) { graphic_token_char!(oc) - } else if variable_indicator_char!(ac) { - alpha_numeric_char!(oc) - } else if capital_letter_char!(ac) { + } else if variable_indicator_char!(ac) || capital_letter_char!(ac) { alpha_numeric_char!(oc) } else if sign_char!(ac) { sign_char!(oc) || decimal_digit_char!(oc) @@ -277,7 +276,7 @@ pub(crate) fn requires_space(atom: &str, op: &str) -> bool { fn non_quoted_graphic_token>(mut iter: Iter, c: char) -> bool { if c == '/' { - return match iter.next() { + match iter.next() { None => true, Some('*') => false, // if we start with comment token, we must quote. Some(c) => { @@ -287,9 +286,9 @@ fn non_quoted_graphic_token>(mut iter: Iter, c: char false } } - }; + } } else if c == '.' { - return match iter.next() { + match iter.next() { None => false, Some(c) => { if graphic_token_char!(c) { @@ -298,7 +297,7 @@ fn non_quoted_graphic_token>(mut iter: Iter, c: char false } } - }; + } } else { iter.all(|c| graphic_token_char!(c)) } @@ -310,9 +309,7 @@ pub(super) fn non_quoted_token>(mut iter: Iter) -> b iter.all(|c| alpha_numeric_char!(c)) } else if graphic_token_char!(c) { non_quoted_graphic_token(iter, c) - } else if semicolon_char!(c) { - iter.next().is_none() - } else if cut_char!(c) { + } else if semicolon_char!(c) || cut_char!(c) { iter.next().is_none() } else if c == '[' { iter.next() == Some(']') && iter.next().is_none() @@ -328,6 +325,7 @@ pub(super) fn non_quoted_token>(mut iter: Iter) -> b } } +#[allow(clippy::len_without_is_empty)] pub trait HCValueOutputter { type Output; @@ -370,7 +368,7 @@ impl HCValueOutputter for PrinterOutputter { } fn begin_new_var(&mut self) { - if self.contents.len() != 0 { + if !self.contents.is_empty() { self.contents += ", "; } } @@ -415,9 +413,9 @@ fn negated_op_needs_bracketing( op.is_negative_sign() && iter.leftmost_leaf_has_property(op_dir, |addr| match Number::try_from(addr) { Ok(Number::Fixnum(n)) => n.get_num() > 0, - Ok(Number::Float(f)) => f > OrderedFloat(0f64), - Ok(Number::Integer(n)) => &*n > &Integer::from(0), - Ok(Number::Rational(n)) => &*n > &Rational::from(0), + Ok(Number::Float(OrderedFloat(f))) => f > 0f64, + Ok(Number::Integer(n)) => n.is_positive(), + Ok(Number::Rational(n)) => n.is_positive(), _ => false, }) } else { @@ -537,20 +535,10 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option } match Number::try_from(addr) { - Ok(Number::Fixnum(n)) => { - if n.get_num() >= 0 { - Some(numbervar(offset + Integer::from(n.get_num()))) - } else { - None - } - } - Ok(Number::Integer(n)) => { - if &*n >= &Integer::from(0) { - Some(numbervar(Integer::from(offset + &*n))) - } else { - None - } + Ok(Number::Fixnum(n)) if n.get_num() >= 0 => { + Some(numbervar(offset + Integer::from(n.get_num()))) } + Ok(Number::Integer(n)) if !n.is_negative() => Some(numbervar(Integer::from(offset + &*n))), _ => None, } } @@ -640,12 +628,9 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { self.state_stack.push(TokenOrRedirect::Op(name, spec)); } } else { - match &*name.as_str() { - "|" => { - self.format_bar_separator_op(max_depth, name, spec); - return; - } - _ => {} + if let "|" = &*name.as_str() { + self.format_bar_separator_op(max_depth, name, spec); + return; }; if self.max_depth_exhausted(max_depth) { @@ -657,22 +642,19 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { if is_xfy!(spec.get_spec()) { let left_directed_op = DirectedOp::Left(name, spec); - self.state_stack.push(TokenOrRedirect::CompositeRedirect( - 0, - left_directed_op, - )); + self.state_stack + .push(TokenOrRedirect::CompositeRedirect(0, left_directed_op)); self.state_stack.push(TokenOrRedirect::Op(name, spec)); self.state_stack.push(TokenOrRedirect::StackPop); - } else { // is_yfx! + } else { + // is_yfx! let right_directed_op = DirectedOp::Right(name, spec); self.state_stack.push(TokenOrRedirect::StackPop); self.state_stack.push(TokenOrRedirect::Op(name, spec)); - self.state_stack.push(TokenOrRedirect::CompositeRedirect( - 0, - right_directed_op, - )); + self.state_stack + .push(TokenOrRedirect::CompositeRedirect(0, right_directed_op)); } } else { let left_directed_op = DirectedOp::Left(name, spec); @@ -783,7 +765,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { let h = self.iter.stack_last().unwrap(); let cell = self.iter.read_cell(h); - let cell = heap_bound_store(&self.iter.heap, heap_bound_deref(&self.iter.heap, cell)); + let cell = heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, cell)); // 7.10.4 if let Some(var) = numbervar(&self.numbervars_offset, cell) { @@ -802,20 +784,16 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { name: Atom, op_desc: Option, ) -> bool { - if self.numbervars && is_numbered_var(name, arity) { - if self.format_numbered_vars() { - return true; - } + if self.numbervars && is_numbered_var(name, arity) && self.format_numbered_vars() { + return true; } let dot_atom = atom!("."); if let Some(spec) = op_desc { - if dot_atom == name && is_infix!(spec.get_spec()) { - if !self.ignore_ops { - self.push_list(max_depth); - return true; - } + if dot_atom == name && is_infix!(spec.get_spec()) && !self.ignore_ops { + self.push_list(max_depth); + return true; } if !self.ignore_ops && spec.get_prec() > 0 { @@ -824,10 +802,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { } } - return match (name, arity) { + match (name, arity) { (atom!("{}"), 1) if !self.ignore_ops => self.format_curly_braces(max_depth), _ => self.format_struct(max_depth, arity, name), - }; + } } fn offset_as_string(&mut self, h: IterStackLoc) -> Option { @@ -866,7 +844,8 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { loop { let is_cyclic = orig_cell.get_forwarding_bit(); - let cell = heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, orig_cell)); + let cell = + heap_bound_store(self.iter.heap, heap_bound_deref(self.iter.heap, orig_cell)); let cell = unmark_cell_bits!(cell); match self.var_names.get(&cell).cloned() { @@ -933,7 +912,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { } let h = cell.get_value() as usize; - self.iter.push_stack(IterStackLoc::iterable_loc(h, HeapOrStackTag::Heap)); + self.iter.push_stack(IterStackLoc::iterable_loc( + h, + HeapOrStackTag::Heap, + )); if let Some(cell) = self.iter.next() { orig_cell = cell; @@ -951,13 +933,13 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { } } } else { - while let Some(_) = self.iter.pop_stack() {} + while self.iter.pop_stack().is_none() {} None } } fn print_impromptu_atom(&mut self, atom: Atom) { - let result = self.print_op_addendum(&*atom.as_str()); + let result = self.print_op_addendum(&atom.as_str()); push_space_if_amb!(self, result.as_str(), { append_str!(self, &result); @@ -1145,8 +1127,6 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { self.state_stack.push(TokenOrRedirect::Open); self.state_stack.push(TokenOrRedirect::Atom(rdiv_ct)); } - - return; } _ => { unreachable!() @@ -1242,7 +1222,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { let mut heap_pstr_iter = HeapPStrIter::new(self.iter.heap, focus.value() as usize); if heap_pstr_iter.next().is_some() { - while let Some(_) = heap_pstr_iter.next() {} + for _ in heap_pstr_iter.by_ref() {} } else { return self.push_list(max_depth); } @@ -1258,11 +1238,9 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { let at_cdr = self.outputter.ends_with("|"); - if self.double_quotes { - if !self.ignore_ops && end_cell.is_string_terminator(&self.iter.heap) { - self.remove_list_children(focus.value() as usize); - return self.print_proper_string(focus.value() as usize, max_depth); - } + if self.double_quotes && !self.ignore_ops && end_cell.is_string_terminator(self.iter.heap) { + self.remove_list_children(focus.value() as usize); + return self.print_proper_string(focus.value() as usize, max_depth); } if self.ignore_ops { @@ -1275,8 +1253,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { append_str!(self, "[]"); } } else { - self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth)); - self.iter.push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap)); + self.state_stack + .push(TokenOrRedirect::FunctorRedirect(max_depth)); + self.iter + .push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap)); } } } else { @@ -1287,7 +1267,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { read_heap_cell!(value, (HeapCellValueTag::Lis) => { - return self.push_list(max_depth); + self.push_list(max_depth) } _ => { let switch = Rc::new(Cell::new((!at_cdr, 0))); @@ -1386,13 +1366,16 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { let switch = self.close_list(cell); - self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth)); + self.state_stack + .push(TokenOrRedirect::FunctorRedirect(max_depth)); self.state_stack.push(TokenOrRedirect::HeadTailSeparator); // bar - self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth + 1)); + self.state_stack + .push(TokenOrRedirect::FunctorRedirect(max_depth + 1)); self.open_list(switch); } + #[allow(clippy::too_many_arguments)] fn handle_op_as_struct( &mut self, name: Atom, @@ -1448,7 +1431,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { for op in &[op, parent_op] { if let Some(ref op) = &op { if op.is_left() - && (op.is_prefix() || requires_space(&*op.as_atom().as_str(), "(")) + && (op.is_prefix() || requires_space(&op.as_atom().as_str(), "(")) { self.state_stack.push(TokenOrRedirect::Space); return; @@ -1463,7 +1446,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { #[allow(dead_code)] fn print_tcp_listener(&mut self, tcp_listener: &TcpListener, max_depth: usize) { - let (ip, port) = if let Some(addr) = tcp_listener.local_addr().ok() { + let (ip, port) = if let Ok(addr) = tcp_listener.local_addr() { (addr.ip(), addr.port()) } else { let disconnected_atom = atom!("$disconnected_tcp_listener"); @@ -1545,24 +1528,33 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { } fn print_comma_separated_char_list(&mut self, char_list: CommaSeparatedCharList) { - let CommaSeparatedCharList { pstr, offset, max_depth, end_cell, end_h } = char_list; + let CommaSeparatedCharList { + pstr, + offset, + max_depth, + end_cell, + end_h, + } = char_list; let pstr_str = pstr.as_str_from(offset); if let Some(c) = pstr_str.chars().next() { let offset = offset + c.len_utf8(); if !self.max_depth_exhausted(max_depth) { - self.state_stack.push(TokenOrRedirect::CommaSeparatedCharList(CommaSeparatedCharList { - pstr, - offset, - max_depth: max_depth.saturating_sub(1), - end_cell, - end_h, - })); + self.state_stack + .push(TokenOrRedirect::CommaSeparatedCharList( + CommaSeparatedCharList { + pstr, + offset, + max_depth: max_depth.saturating_sub(1), + end_cell, + end_h, + }, + )); let max_depth_allows = self.max_depth == 0 || max_depth > 1; - if max_depth_allows && pstr_str.chars().skip(1).next().is_some() { + if max_depth_allows && pstr_str.chars().nth(1).is_some() { self.state_stack.push(TokenOrRedirect::Comma); } @@ -1576,10 +1568,12 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { self.state_stack.push(TokenOrRedirect::HeadTailSeparator); } else if end_cell != empty_list_as_cell!() { if let Some(end_h) = end_h { - self.iter.push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap)); + self.iter + .push_stack(IterStackLoc::iterable_loc(end_h, HeapOrStackTag::Heap)); } - self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth + 1)); + self.state_stack + .push(TokenOrRedirect::FunctorRedirect(max_depth + 1)); self.state_stack.push(TokenOrRedirect::HeadTailSeparator); } } @@ -1599,13 +1593,12 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { let print_struct = |printer: &mut Self, name: Atom, arity: usize| { if name == atom!("[]") && arity == 0 { - match printer.state_stack.last() { - Some(TokenOrRedirect::CloseList(_) | TokenOrRedirect::ChildCloseList) => { - if printer.at_cdr("") { - return; - } + if let Some(TokenOrRedirect::CloseList(_) | TokenOrRedirect::ChildCloseList) = + printer.state_stack.last() + { + if printer.at_cdr("") { + return; } - _ => {} } append_str!(printer, "[]"); @@ -1642,7 +1635,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { result.push('('); } - result += &printer.print_op_addendum(&*name.as_str()); + result += &printer.print_op_addendum(&name.as_str()); if op.is_some() { result.push(')'); @@ -1652,14 +1645,14 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { append_str!(printer, &result); }); } else { - push_space_if_amb!(printer, &*name.as_str(), { + push_space_if_amb!(printer, &name.as_str(), { printer.print_impromptu_atom(name); }); } }; if !addr.is_var() - && !addr.is_compound(&self.iter.heap) + && !addr.is_compound(self.iter.heap) && self.max_depth_exhausted(max_depth) { if !(addr == atom_as_cell!(atom!("[]")) && self.at_cdr("")) { @@ -1772,7 +1765,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { TokenOrRedirect::BarAsOp => append_str!(self, " | "), TokenOrRedirect::Char(c) => print_char!(self, self.quoted, c), TokenOrRedirect::Op(atom, op) => { - self.print_op(&*atom.as_str()); + self.print_op(&atom.as_str()); if is_prefix!(op.get_spec()) { self.set_parent_of_first_op(Some(DirectedOp::Left(atom, op))); diff --git a/src/http.rs b/src/http.rs index 46888f122..cb1f2d9c7 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,5 +1,5 @@ -use std::sync::{Arc, Mutex, Condvar}; use std::io::BufRead; +use std::sync::{Arc, Condvar, Mutex}; use warp::http; diff --git a/src/indexing.rs b/src/indexing.rs index 6a52fc97a..5956dcfc9 100644 --- a/src/indexing.rs +++ b/src/indexing.rs @@ -665,7 +665,7 @@ pub(crate) fn merge_clause_index( pub(crate) fn remove_constant_indices( constant: Literal, overlapping_constants: &[Literal], - indexing_code: &mut Vec, + indexing_code: &mut [IndexingLine], offset: usize, ) { let mut index = 0; @@ -811,7 +811,7 @@ pub(crate) fn remove_constant_indices( pub(crate) fn remove_structure_index( name: Atom, arity: usize, - indexing_code: &mut Vec, + indexing_code: &mut [IndexingLine], offset: usize, ) { let mut index = 0; @@ -843,10 +843,10 @@ pub(crate) fn remove_structure_index( IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(ref mut structures)) => { structures_index = index; - match structures.get(&(name.clone(), arity)).cloned() { + match structures.get(&(name, arity)).cloned() { Some(IndexingCodePtr::DynamicExternal(_)) | Some(IndexingCodePtr::External(_)) => { - structures.remove(&(name.clone(), arity)); + structures.remove(&(name, arity)); break; } Some(IndexingCodePtr::Internal(o)) => { @@ -877,7 +877,7 @@ pub(crate) fn remove_structure_index( IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure( ref mut structures, )) => { - structures.insert((name.clone(), arity), ext); + structures.insert((name, arity), ext); } _ => { unreachable!() @@ -908,7 +908,7 @@ pub(crate) fn remove_structure_index( IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure( ref mut structures, )) => { - structures.insert((name.clone(), arity), ext); + structures.insert((name, arity), ext); } _ => { unreachable!() @@ -948,7 +948,7 @@ pub(crate) fn remove_structure_index( } } -pub(crate) fn remove_list_index(indexing_code: &mut Vec, offset: usize) { +pub(crate) fn remove_list_index(indexing_code: &mut [IndexingLine], offset: usize) { let mut index = 0; match &mut indexing_code[index] { @@ -1028,7 +1028,7 @@ pub(crate) fn remove_list_index(indexing_code: &mut Vec, offset: u pub(crate) fn remove_index( opt_arg_index_key: &OptArgIndexKey, - indexing_code: &mut Vec, + indexing_code: &mut [IndexingLine], clause_loc: usize, ) { match opt_arg_index_key { @@ -1049,46 +1049,50 @@ pub(crate) fn remove_index( #[inline] fn cap_choice_seq(prelude: &mut [IndexedChoiceInstruction]) { - prelude.first_mut().map(|instr| { + if let Some(instr) = prelude.first_mut() { *instr = IndexedChoiceInstruction::Try(instr.offset()); - }); + } cap_choice_seq_with_trust(prelude); } #[inline] fn cap_choice_seq_with_trust(prelude: &mut [IndexedChoiceInstruction]) { - prelude.last_mut().map(|instr| match instr { - IndexedChoiceInstruction::Retry(i) => { - *instr = IndexedChoiceInstruction::Trust(*i); - } - IndexedChoiceInstruction::DefaultRetry(i) => { - *instr = IndexedChoiceInstruction::DefaultTrust(*i); + if let Some(instr) = prelude.last_mut() { + match instr { + IndexedChoiceInstruction::Retry(i) => { + *instr = IndexedChoiceInstruction::Trust(*i); + } + IndexedChoiceInstruction::DefaultRetry(i) => { + *instr = IndexedChoiceInstruction::DefaultTrust(*i); + } + _ => {} } - _ => {} - }); + } } #[inline] fn uncap_choice_seq_with_trust(prelude: &mut [IndexedChoiceInstruction]) { - prelude.last_mut().map(|instr| match instr { - IndexedChoiceInstruction::Trust(i) => { - *instr = IndexedChoiceInstruction::Retry(*i); - } - IndexedChoiceInstruction::DefaultTrust(i) => { - *instr = IndexedChoiceInstruction::DefaultRetry(*i); + if let Some(instr) = prelude.last_mut() { + match instr { + IndexedChoiceInstruction::Trust(i) => { + *instr = IndexedChoiceInstruction::Retry(*i); + } + IndexedChoiceInstruction::DefaultTrust(i) => { + *instr = IndexedChoiceInstruction::DefaultRetry(*i); + } + _ => {} } - _ => {} - }); + } } #[inline] fn uncap_choice_seq_with_try(prelude: &mut [IndexedChoiceInstruction]) { - prelude.first_mut().map(|instr| { + if let Some(instr) = prelude.first_mut() { if let IndexedChoiceInstruction::Try(i) = instr { *instr = IndexedChoiceInstruction::Retry(*i); } - }); + } } pub(crate) fn constant_key_alternatives( @@ -1105,7 +1109,7 @@ pub(crate) fn constant_key_alternatives( } } Literal::Char(c) => { - let atom = AtomTable::build_with(&atom_tbl, &c.to_string()); + let atom = AtomTable::build_with(atom_tbl, &c.to_string()); constants.push(Literal::Atom(atom)); } /* @@ -1124,9 +1128,11 @@ pub(crate) fn constant_key_alternatives( Literal::Integer(ref n) => { let result = (&**n).try_into(); if let Ok(value) = result { - Fixnum::build_with_checked(value).map(|n| { - constants.push(Literal::Fixnum(n)); - }).unwrap(); + Fixnum::build_with_checked(value) + .map(|n| { + constants.push(Literal::Fixnum(n)); + }) + .unwrap(); } } _ => {} @@ -1245,10 +1251,8 @@ impl Indexer for StaticCodeIndices { index_locs.insert(key, IndexingCodePtr::Internal(prelude.len() + 1)); cap_choice_seq_with_trust(code.make_contiguous()); prelude.push_back(IndexingLine::from(code)); - } else { - code.front().map(|i| { - index_locs.insert(key, IndexingCodePtr::External(i.offset())); - }); + } else if let Some(i) = code.front() { + index_locs.insert(key, IndexingCodePtr::External(i.offset())); } } @@ -1285,7 +1289,7 @@ impl Indexer for StaticCodeIndices { ) -> IndexingCodePtr { if lists.len() > 1 { cap_choice_seq_with_trust(lists.make_contiguous()); - let lists = mem::replace(lists, VecDeque::new()); + let lists = std::mem::take(lists); prelude.push_back(IndexingLine::from(lists)); IndexingCodePtr::Internal(1) @@ -1361,10 +1365,8 @@ impl Indexer for DynamicCodeIndices { prelude.push_back(IndexingLine::DynamicIndexedChoice( code.into_iter().collect(), )); - } else { - code.front().map(|i| { - index_locs.insert(key, IndexingCodePtr::DynamicExternal(*i)); - }); + } else if let Some(i) = code.front() { + index_locs.insert(key, IndexingCodePtr::DynamicExternal(*i)); } } @@ -1400,7 +1402,7 @@ impl Indexer for DynamicCodeIndices { prelude: &mut VecDeque, ) -> IndexingCodePtr { if lists.len() > 1 { - let lists = mem::replace(lists, VecDeque::new()); + let lists = std::mem::take(lists); prelude.push_back(IndexingLine::DynamicIndexedChoice( lists.into_iter().collect(), )); @@ -1458,11 +1460,7 @@ impl CodeOffsets { index: usize, ) -> Vec { let overlapping_constants = constant_key_alternatives(constant, atom_tbl); - let code = self - .indices - .constants() - .entry(constant) - .or_insert(VecDeque::new()); + let code = self.indices.constants().entry(constant).or_default(); let is_initial_index = code.is_empty(); code.push_back(I::compute_index( @@ -1472,11 +1470,7 @@ impl CodeOffsets { )); for constant in &overlapping_constants { - let code = self - .indices - .constants() - .entry(*constant) - .or_insert(VecDeque::new()); + let code = self.indices.constants().entry(*constant).or_default(); let is_initial_index = code.is_empty(); let index = I::compute_index(is_initial_index, index, self.non_counted_bt); @@ -1488,11 +1482,7 @@ impl CodeOffsets { } fn index_structure(&mut self, name: Atom, arity: usize, index: usize) -> usize { - let code = self - .indices - .structures() - .entry((name.clone(), arity)) - .or_insert(VecDeque::new()); + let code = self.indices.structures().entry((name, arity)).or_default(); let code_len = code.len(); let is_initial_index = code.is_empty(); @@ -1523,7 +1513,7 @@ impl CodeOffsets { } &Term::Clause(_, name, ref terms) => { clause_index_info.opt_arg_index_key = - OptArgIndexKey::Structure(self.optimal_index, 0, name.clone(), terms.len()); + OptArgIndexKey::Structure(self.optimal_index, 0, name, terms.len()); self.index_structure(name, terms.len(), index); } @@ -1575,20 +1565,14 @@ impl CodeOffsets { &mut prelude, ); - match &mut str_loc { - IndexingCodePtr::Internal(ref mut i) => { - *i += emitted_switch_on_constant as usize; // con_loc.is_internal() as usize; - } - _ => {} - }; + if let IndexingCodePtr::Internal(ref mut i) = &mut str_loc { + *i += emitted_switch_on_constant as usize; // con_loc.is_internal() as usize; + } - match &mut lst_loc { - IndexingCodePtr::Internal(ref mut i) => { - *i += emitted_switch_on_constant as usize; // con_loc.is_internal() as usize; - *i += emitted_switch_on_structure as usize; // str_loc.is_internal() as usize; - } - _ => {} - }; + if let IndexingCodePtr::Internal(ref mut i) = &mut lst_loc { + *i += emitted_switch_on_constant as usize; // con_loc.is_internal() as usize; + *i += emitted_switch_on_structure as usize; // str_loc.is_internal() as usize; + } let var_offset = 1 + skip_stub_try_me_else as usize; diff --git a/src/iterators.rs b/src/iterators.rs index 0961355dc..b3140ee93 100644 --- a/src/iterators.rs +++ b/src/iterators.rs @@ -8,6 +8,7 @@ use std::collections::VecDeque; use std::iter::*; use std::vec::Vec; +#[allow(clippy::borrowed_box)] #[derive(Debug, Clone)] pub(crate) enum TermRef<'a> { AnonVar(Level), @@ -35,6 +36,7 @@ impl<'a> TermRef<'a> { } */ +#[allow(clippy::borrowed_box)] #[derive(Debug)] pub(crate) enum TermIterState<'a> { AnonVar(Level), @@ -113,11 +115,11 @@ impl<'a> QueryIterator<'a> { fn extend_state(&mut self, lvl: Level, term: &'a QueryTerm) { match term { - &QueryTerm::Clause(ref cell, ClauseType::CallN(_), ref terms, _) => { + QueryTerm::Clause(ref cell, ClauseType::CallN(_), ref terms, _) => { self.state_stack .push(TermIterState::Clause(lvl, 1, cell, atom!("$call"), terms)); } - &QueryTerm::Clause(ref cell, ref ct, ref terms, _) => { + QueryTerm::Clause(ref cell, ref ct, ref terms, _) => { self.state_stack .push(TermIterState::Clause(lvl, 0, cell, ct.name(), terms)); } @@ -214,7 +216,7 @@ impl<'a> FactIterator<'a> { .push_back(TermIterState::subterm_to_state(lvl, term)); } - pub(crate) fn from_rule_head_clause(terms: &'a Vec) -> Self { + pub(crate) fn from_rule_head_clause(terms: &'a [Term]) -> Self { let state_queue = terms .iter() .map(|bt| TermIterState::subterm_to_state(Level::Shallow, bt)) @@ -312,14 +314,14 @@ impl<'a> Iterator for FactIterator<'a> { } } -pub(crate) fn post_order_iter<'a>(term: &'a Term) -> QueryIterator<'a> { +pub(crate) fn post_order_iter(term: &'_ Term) -> QueryIterator { QueryIterator::from_term(term) } -pub(crate) fn breadth_first_iter<'a>( - term: &'a Term, +pub(crate) fn breadth_first_iter( + term: &'_ Term, iterable_root: RootIterationPolicy, -) -> FactIterator<'a> { +) -> FactIterator { FactIterator::new(term, iterable_root) } @@ -343,7 +345,7 @@ pub(crate) struct ClauseIterator<'a> { remaining_chunks_on_stack: usize, } -fn state_from_chunked_terms<'a>(chunk_vec: &'a VecDeque) -> ClauseIteratorState<'a> { +fn state_from_chunked_terms(chunk_vec: &'_ VecDeque) -> ClauseIteratorState { if chunk_vec.len() == 1 { if let Some(ChunkedTerms::Branch(ref branches)) = chunk_vec.front() { return ClauseIteratorState::RemainingBranches(branches, 0); @@ -422,7 +424,7 @@ impl<'a> Iterator for ClauseIterator<'a> { if focus < branches.len() => { self.state_stack - .push(ClauseIteratorState::RemainingBranches(&branches, focus + 1)); + .push(ClauseIteratorState::RemainingBranches(branches, focus + 1)); let state = state_from_chunked_terms(&branches[focus]); if let ClauseIteratorState::RemainingChunks(..) = &state { diff --git a/src/lib.rs b/src/lib.rs index 3579fcf23..bf7ddc57c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,8 @@ #[macro_use] extern crate static_assertions; #[cfg(test)] -#[macro_use] extern crate maplit; +#[macro_use] +extern crate maplit; #[macro_use] pub mod macros; @@ -50,6 +51,7 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn eval_code(s: &str) -> String { use machine::mock_wam::*; + use web_sys::console; let mut wam = Machine::with_test_streams(); let bytes = wam.test_load_string(s); diff --git a/src/machine/args.rs b/src/machine/args.rs index 40c355a59..6e24c87a2 100644 --- a/src/machine/args.rs +++ b/src/machine/args.rs @@ -14,3 +14,9 @@ impl MachineArgs { } } } + +impl Default for MachineArgs { + fn default() -> Self { + Self::new() + } +} diff --git a/src/machine/arithmetic_ops.rs b/src/machine/arithmetic_ops.rs index 677fa5816..c3453bda4 100644 --- a/src/machine/arithmetic_ops.rs +++ b/src/machine/arithmetic_ops.rs @@ -1,6 +1,6 @@ use dashu::base::{Abs, Gcd, Signed, UnsignedAbs}; -use dashu::integer::IBig; use dashu::integer::fast_div::ConstDivisor; +use dashu::integer::IBig; use divrem::*; use num_order::NumOrd; @@ -84,18 +84,18 @@ fn numerical_type_error( fn isize_gcd(n1: isize, n2: isize) -> Option { if n1 == 0 { - return n2.checked_abs().map(|n| n as isize); + return n2.checked_abs(); } if n2 == 0 { - return n1.checked_abs().map(|n| n as isize); + return n1.checked_abs(); } let n1 = n1.checked_abs(); let n2 = n2.checked_abs(); - let mut n1 = if let Some(n1) = n1 { n1 } else { return None }; - let mut n2 = if let Some(n2) = n2 { n2 } else { return None }; + let mut n1 = n1?; + let mut n2 = n2?; let mut shift = 0; @@ -115,9 +115,7 @@ fn isize_gcd(n1: isize, n2: isize) -> Option { } if n1 > n2 { - let t = n2; - n2 = n1; - n1 = t; + std::mem::swap(&mut n2, &mut n1); } n2 -= n1; @@ -350,22 +348,18 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result { let n1_i = n1.get_num(); - if !(n1_i == 1 || n1_i == 0 || n1_i == -1) && &*n2 < &Integer::from(0) { + if !(n1_i == 1 || n1_i == 0 || n1_i == -1) && n2.is_zero() { let n = Number::Fixnum(n1); Err(numerical_type_error(ValidType::Float, n, stub_gen)) } else { let n1 = Integer::from(n1_i); - Ok(Number::arena_from(binary_pow(n1, &*n2), arena)) + Ok(Number::arena_from(binary_pow(n1, &n2), arena)) } } (Number::Integer(n1), Number::Fixnum(n2)) => { let n2_i = n2.get_num(); - if !(&*n1 == &Integer::from(1) - || &*n1 == &Integer::from(0) - || &*n1 == &Integer::from(-1)) - && n2_i < 0 - { + if !(*n1 == Integer::from(1) || n1.is_zero() || *n1 == Integer::from(-1)) && n2_i < 0 { let n = Number::Integer(n1); Err(numerical_type_error(ValidType::Float, n, stub_gen)) } else { @@ -374,15 +368,13 @@ pub(crate) fn int_pow(n1: Number, n2: Number, arena: &mut Arena) -> Result { - if !(&*n1 == &Integer::from(1) - || &*n1 == &Integer::from(0) - || &*n1 == &Integer::from(-1)) - && &*n2 < &Integer::from(0) + if !(*n1 == Integer::from(1) || n1.is_zero() || *n1 == Integer::from(-1)) + && n2.is_zero() { let n = Number::Integer(n1); Err(numerical_type_error(ValidType::Float, n, stub_gen)) } else { - Ok(Number::arena_from(binary_pow((*n1).clone(), &*n2), arena)) + Ok(Number::arena_from(binary_pow((*n1).clone(), &n2), arena)) } } (n1, Number::Integer(n2)) => { @@ -455,14 +447,14 @@ pub(crate) fn max(n1: Number, n2: Number) -> Result { } } (Number::Fixnum(n1), Number::Integer(n2)) => { - if (&*n2).num_gt(&n1.get_num()) { + if (*n2).num_gt(&n1.get_num()) { Ok(Number::Integer(n2)) } else { Ok(Number::Fixnum(n1)) } } (Number::Integer(n1), Number::Fixnum(n2)) => { - if (&*n1).num_gt(&n2.get_num()) { + if (*n1).num_gt(&n2.get_num()) { Ok(Number::Integer(n1)) } else { Ok(Number::Fixnum(n2)) @@ -499,14 +491,14 @@ pub(crate) fn min(n1: Number, n2: Number) -> Result { } } (Number::Fixnum(n1), Number::Integer(n2)) => { - if (&*n2).num_lt(&n1.get_num()) { + if (*n2).num_lt(&n1.get_num()) { Ok(Number::Integer(n2)) } else { Ok(Number::Fixnum(n1)) } } (Number::Integer(n1), Number::Fixnum(n2)) => { - if (&*n1).num_lt(&n2.get_num()) { + if (*n1).num_lt(&n2.get_num()) { Ok(Number::Integer(n1)) } else { Ok(Number::Fixnum(n2)) @@ -583,15 +575,13 @@ pub(crate) fn idiv(n1: Number, n2: Number, arena: &mut Arena) -> Result { if n2.get_num() == 0 { Err(zero_divisor_eval_error(stub_gen)) + } else if let Some(result) = n1.get_num().checked_div(n2.get_num()) { + Ok(Number::arena_from(result, arena)) } else { - if let Some(result) = n1.get_num().checked_div(n2.get_num()) { - Ok(Number::arena_from(result, arena)) - } else { - let n1 = Integer::from(n1.get_num()); - let n2 = Integer::from(n2.get_num()); + let n1 = Integer::from(n1.get_num()); + let n2 = Integer::from(n2.get_num()); - Ok(Number::arena_from(n1 / n2, arena)) - } + Ok(Number::arena_from(n1 / n2, arena)) } } (Number::Fixnum(n1), Number::Integer(n2)) => { @@ -656,9 +646,9 @@ pub(crate) fn shr(n1: Number, n2: Number, arena: &mut Arena) -> Result> n2, arena)); + Ok(Number::arena_from(n1 >> n2, arena)) } else { - return Ok(Number::arena_from(n1 >> usize::max_value(), arena)); + Ok(Number::arena_from(n1 >> usize::max_value(), arena)) } } (Number::Fixnum(n1), Number::Integer(n2)) => { @@ -667,12 +657,8 @@ pub(crate) fn shr(n1: Number, n2: Number, arena: &mut Arena) -> Result = (&*n2).try_into(); match result { - Ok(n2) => { - Ok(Number::arena_from(n1 >> n2, arena)) - } - Err(_) => { - Ok(Number::arena_from(n1 >> usize::max_value(), arena)) - } + Ok(n2) => Ok(Number::arena_from(n1 >> n2, arena)), + Err(_) => Ok(Number::arena_from(n1 >> usize::max_value(), arena)), } } (Number::Integer(n1), Number::Fixnum(n2)) => match usize::try_from(n2.get_num()) { @@ -686,14 +672,13 @@ pub(crate) fn shr(n1: Number, n2: Number, arena: &mut Arena) -> Result = (&*n2).try_into(); match result { - Ok(n2) => { - Ok(Number::arena_from(Integer::from(&*n1 >> n2), arena)) - } - Err(_) => { - Ok(Number::arena_from(Integer::from(&*n1 >> usize::max_value()), arena)) - } + Ok(n2) => Ok(Number::arena_from(Integer::from(&*n1 >> n2), arena)), + Err(_) => Ok(Number::arena_from( + Integer::from(&*n1 >> usize::max_value()), + arena, + )), } - }, + } (Number::Integer(_), n2) => Err(numerical_type_error(ValidType::Integer, n2, stub_gen)), (Number::Fixnum(_), n2) => Err(numerical_type_error(ValidType::Integer, n2, stub_gen)), (n1, _) => Err(numerical_type_error(ValidType::Integer, n1, stub_gen)), @@ -718,9 +703,9 @@ pub(crate) fn shl(n1: Number, n2: Number, arena: &mut Arena) -> Result { @@ -730,10 +715,8 @@ pub(crate) fn shl(n1: Number, n2: Number, arena: &mut Arena) -> Result { let n1: u64 = n1.try_into().unwrap(); Ok(Number::arena_from(n1 << n2, arena)) - }, - _ => { - Ok(Number::arena_from(n1 << usize::max_value(), arena)) - } + } + _ => Ok(Number::arena_from(n1 << usize::max_value(), arena)), } } (Number::Integer(n1), Number::Fixnum(n2)) => match usize::try_from(n2.get_num()) { @@ -747,10 +730,11 @@ pub(crate) fn shl(n1: Number, n2: Number, arena: &mut Arena) -> Result { let n1: u64 = (&*n1).try_into().unwrap(); Ok(Number::arena_from(Integer::from(n1 << n2), arena)) - }, - _ => { - Ok(Number::arena_from(Integer::from(&*n1 << usize::max_value()),arena)) - } + } + _ => Ok(Number::arena_from( + Integer::from(&*n1 << usize::max_value()), + arena, + )), }, (Number::Integer(_), n2) => Err(numerical_type_error(ValidType::Integer, n2, stub_gen)), (Number::Fixnum(_), n2) => Err(numerical_type_error(ValidType::Integer, n2, stub_gen)), @@ -882,7 +866,7 @@ pub(crate) fn modulus(x: Number, y: Number, arena: &mut Arena) -> Result { @@ -892,14 +876,14 @@ pub(crate) fn modulus(x: Number, y: Number, arena: &mut Arena) -> Result { if n2.is_zero() { Err(zero_divisor_eval_error(stub_gen)) } else { - Ok(Number::arena_from(ibig_rem_floor(&*n1, &*n2), arena)) + Ok(Number::arena_from(ibig_rem_floor(&n1, &n2), arena)) } } (Number::Integer(_), n2) | (Number::Fixnum(_), n2) => { @@ -1145,7 +1129,7 @@ impl MachineState { &mut self.interms[i - 1], Number::Fixnum(Fixnum::build_with(0)), )), - &ArithmeticTerm::Number(n) => Ok(n), + ArithmeticTerm::Number(n) => Ok(*n), } } @@ -1167,8 +1151,8 @@ impl MachineState { value: HeapCellValue, ) -> Result { let stub_gen = || functor_stub(atom!("is"), 2); - let mut iter = stackful_post_order_iter:: - (&mut self.heap, &mut self.stack, value); + let mut iter = + stackful_post_order_iter::(&mut self.heap, &mut self.stack, value); while let Some(value) = iter.next() { if value.get_forwarding_bit() { diff --git a/src/machine/attributed_variables.rs b/src/machine/attributed_variables.rs index a09a790e7..2f117afcf 100644 --- a/src/machine/attributed_variables.rs +++ b/src/machine/attributed_variables.rs @@ -133,8 +133,8 @@ impl MachineState { let mut seen_set = IndexSet::new(); let mut seen_vars = vec![]; - let mut iter = stackful_preorder_iter:: - (&mut self.heap, &mut self.stack, cell); + let mut iter = + stackful_preorder_iter::(&mut self.heap, &mut self.stack, cell); while let Some(value) = iter.next() { read_heap_cell!(value, diff --git a/src/machine/compile.rs b/src/machine/compile.rs index 2245ef41a..296d076f0 100644 --- a/src/machine/compile.rs +++ b/src/machine/compile.rs @@ -282,28 +282,25 @@ fn merge_indexed_subsequences( .unwrap(), ); - match &mut code[inner_try_me_else_loc] { - Instruction::TryMeElse(ref mut o) => { - retraction_info.push_record(RetractionRecord::ModifiedTryMeElse( - inner_try_me_else_loc, - *o, - )); + if let Instruction::TryMeElse(ref mut o) = &mut code[inner_try_me_else_loc] { + retraction_info.push_record(RetractionRecord::ModifiedTryMeElse( + inner_try_me_else_loc, + *o, + )); - match *o { - 0 => { - code[inner_try_me_else_loc] = Instruction::TrustMe(0); - } - o => match &code[inner_try_me_else_loc + o] { - Instruction::RevJmpBy(0) => { - code[inner_try_me_else_loc] = Instruction::TrustMe(o); - } - _ => { - code[inner_try_me_else_loc] = Instruction::RetryMeElse(o); - } - }, + match *o { + 0 => { + code[inner_try_me_else_loc] = Instruction::TrustMe(0); } + o => match &code[inner_try_me_else_loc + o] { + Instruction::RevJmpBy(0) => { + code[inner_try_me_else_loc] = Instruction::TrustMe(o); + } + _ => { + code[inner_try_me_else_loc] = Instruction::RetryMeElse(o); + } + }, } - _ => {} } thread_choice_instr_at_to( @@ -333,8 +330,8 @@ fn merge_indexed_subsequences( retraction_info, ); } - None => match &mut code[outer_threaded_choice_instr_loc] { - Instruction::TryMeElse(ref mut o) => { + None => { + if let Instruction::TryMeElse(ref mut o) = &mut code[outer_threaded_choice_instr_loc] { retraction_info .push_record(RetractionRecord::ModifiedTryMeElse(inner_trust_me_loc, *o)); @@ -342,8 +339,7 @@ fn merge_indexed_subsequences( return Some(IndexPtr::index(outer_threaded_choice_instr_loc + 1)); } - _ => {} - }, + } } None @@ -919,7 +915,7 @@ fn prepend_compiled_clause( retraction_info, ); - code.extend(prepend_queue.into_iter()); + code.extend(prepend_queue); if skeleton.core.is_dynamic { clause_loc @@ -975,7 +971,7 @@ fn prepend_compiled_clause( internalize_choice_instr_at(code, old_clause_start, retraction_info); - code.extend(prepend_queue.into_iter()); + code.extend(prepend_queue); clause_loc // + (outer_thread_choice_offset == 0 as usize) } @@ -1004,7 +1000,7 @@ fn prepend_compiled_clause( internalize_choice_instr_at(code, old_clause_start, retraction_info); - code.extend(prepend_queue.into_iter()); + code.extend(prepend_queue); // skeleton.clauses[0].opt_arg_index_key += clause_loc; skeleton.clauses[0].clause_start = clause_loc; @@ -1029,7 +1025,7 @@ fn prepend_compiled_clause( internalize_choice_instr_at(code, old_clause_start, retraction_info); - code.extend(prepend_queue.into_iter()); + code.extend(prepend_queue); // skeleton.clauses[0].opt_arg_index_key += clause_loc; skeleton.clauses[0].clause_start = clause_loc; @@ -1134,21 +1130,18 @@ fn append_compiled_clause( skeleton.clauses[target_pos].opt_arg_index_key += clause_loc; code.extend(clause_code.drain(1..)); - match skeleton.clauses[target_pos] + if let Some(index_loc) = skeleton.clauses[target_pos] .opt_arg_index_key .switch_on_term_loc() { - Some(index_loc) => { - // point to the inner-threaded TryMeElse(0) if target_pos is - // indexed, and make switch_on_term point one line after it in - // its variable offset. - skeleton.clauses[target_pos].clause_start += 2; - - if !skeleton.core.is_dynamic { - set_switch_var_offset(code, index_loc, 2, retraction_info); - } + // point to the inner-threaded TryMeElse(0) if target_pos is + // indexed, and make switch_on_term point one line after it in + // its variable offset. + skeleton.clauses[target_pos].clause_start += 2; + + if !skeleton.core.is_dynamic { + set_switch_var_offset(code, index_loc, 2, retraction_info); } - None => {} } match skeleton.clauses[lower_bound] @@ -1302,11 +1295,8 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { clause_clause_locs.push_back(clause_index_info.clause_start); } - match &mut code[0] { - Instruction::TryMeElse(0) => { - code_ptr += 1; - } - _ => {} + if let Instruction::TryMeElse(0) = &mut code[0] { + code_ptr += 1; } match self @@ -1317,7 +1307,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { Some(skeleton) => { let skeleton_clause_len = skeleton.clauses.len(); - skeleton.clauses.extend(cg.skeleton.clauses.into_iter()); + skeleton.clauses.extend(cg.skeleton.clauses); skeleton .core .clause_clause_locs @@ -1371,7 +1361,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { index_ptr, ); - self.wam_prelude.code.extend(code.into_iter()); + self.wam_prelude.code.extend(code); Ok(code_index) } @@ -1563,7 +1553,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { let global_clock = LS::machine_st(&mut self.payload).global_clock; let result = append_compiled_clause( - &mut self.wam_prelude.code, + self.wam_prelude.code, clause_code, skeleton, &mut self.payload.retraction_info, @@ -1603,7 +1593,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { let global_clock = LS::machine_st(&mut self.payload).global_clock; let new_code_ptr = prepend_compiled_clause( - &mut self.wam_prelude.code, + self.wam_prelude.code, compilation_target, key, clause_code, @@ -1646,7 +1636,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { .switch_on_term_loc() { Some(index_loc) => find_inner_choice_instr( - &self.wam_prelude.code, + self.wam_prelude.code, skeleton.clauses[target_pos].clause_start, index_loc, ), @@ -1687,115 +1677,109 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { if target_pos == 0 || (lower_bound + 1 == target_pos && lower_bound_is_unindexed) { // the clause preceding target_pos, if there is one, is of // key type OptArgIndexKey::None. - match skeleton.clauses[target_pos] + if let Some(index_loc) = skeleton.clauses[target_pos] .opt_arg_index_key .switch_on_term_loc() { - Some(index_loc) => { - let inner_clause_start = find_inner_choice_instr( - code, - skeleton.clauses[target_pos].clause_start, - index_loc, - ); + let inner_clause_start = find_inner_choice_instr( + code, + skeleton.clauses[target_pos].clause_start, + index_loc, + ); - remove_index_from_subsequence( - code, - &skeleton.clauses[target_pos].opt_arg_index_key, - inner_clause_start, - &mut self.payload.retraction_info, - ); + remove_index_from_subsequence( + code, + &skeleton.clauses[target_pos].opt_arg_index_key, + inner_clause_start, + &mut self.payload.retraction_info, + ); - match derelictize_try_me_else( - code, - inner_clause_start, - &mut self.payload.retraction_info, - ) { - Some(offset) => { - let instr_loc = find_inner_choice_instr( - code, - inner_clause_start + offset, - index_loc, - ); + match derelictize_try_me_else( + code, + inner_clause_start, + &mut self.payload.retraction_info, + ) { + Some(offset) => { + let instr_loc = + find_inner_choice_instr(code, inner_clause_start + offset, index_loc); - let clause_loc = blunt_leading_choice_instr( - code, - instr_loc, - &mut self.payload.retraction_info, - ); + let clause_loc = blunt_leading_choice_instr( + code, + instr_loc, + &mut self.payload.retraction_info, + ); - set_switch_var_offset( - code, - index_loc, - clause_loc - index_loc, - &mut self.payload.retraction_info, - ); + set_switch_var_offset( + code, + index_loc, + clause_loc - index_loc, + &mut self.payload.retraction_info, + ); - self.payload.retraction_info.push_record( - RetractionRecord::SkeletonClauseStartReplaced( - payload_compilation_target, - key, - target_pos + 1, - skeleton.clauses[target_pos + 1].clause_start, - ), - ); + self.payload.retraction_info.push_record( + RetractionRecord::SkeletonClauseStartReplaced( + payload_compilation_target, + key, + target_pos + 1, + skeleton.clauses[target_pos + 1].clause_start, + ), + ); - skeleton.clauses[target_pos + 1].clause_start = - skeleton.clauses[target_pos].clause_start; + skeleton.clauses[target_pos + 1].clause_start = + skeleton.clauses[target_pos].clause_start; - let update_code_index = target_pos == 0 - && skeleton.clauses[target_pos + 1] - .opt_arg_index_key - .switch_on_term_loc() - .is_none(); + let update_code_index = target_pos == 0 + && skeleton.clauses[target_pos + 1] + .opt_arg_index_key + .switch_on_term_loc() + .is_none(); - let index_ptr_opt = if update_code_index { - Some(IndexPtr::index(clause_loc)) - } else { - None - }; + let index_ptr_opt = if update_code_index { + Some(IndexPtr::index(clause_loc)) + } else { + None + }; - return finalize_retract( - key, - payload_compilation_target, - skeleton, - code_index, - target_pos, - index_ptr_opt, + return finalize_retract( + key, + payload_compilation_target, + skeleton, + code_index, + target_pos, + index_ptr_opt, + &mut self.payload.retraction_info, + ); + } + None => { + let index_ptr_opt = if target_pos > 0 { + let preceding_choice_instr_loc = + skeleton.clauses[target_pos - 1].clause_start; + + remove_non_leading_clause( + code, + preceding_choice_instr_loc, + skeleton.clauses[target_pos].clause_start - 2, &mut self.payload.retraction_info, - ); - } - None => { - let index_ptr_opt = if target_pos > 0 { - let preceding_choice_instr_loc = - skeleton.clauses[target_pos - 1].clause_start; - - remove_non_leading_clause( - code, - preceding_choice_instr_loc, - skeleton.clauses[target_pos].clause_start - 2, - &mut self.payload.retraction_info, - ) - } else { - remove_leading_unindexed_clause( - code, - skeleton.clauses[target_pos].clause_start - 2, - &mut self.payload.retraction_info, - ) - }; - - return finalize_retract( - key, - payload_compilation_target, - skeleton, - code_index, - target_pos, - index_ptr_opt, + ) + } else { + remove_leading_unindexed_clause( + code, + skeleton.clauses[target_pos].clause_start - 2, &mut self.payload.retraction_info, - ); - } + ) + }; + + return finalize_retract( + key, + payload_compilation_target, + skeleton, + code_index, + target_pos, + index_ptr_opt, + &mut self.payload.retraction_info, + ); } } - None => {} } } @@ -1824,16 +1808,13 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { Instruction::RevJmpBy(target_indexing_loc - later_indexing_loc), ); - match target_indexing_line { - Instruction::IndexingCode(indexing_code) => { - self.payload.retraction_info.push_record( - RetractionRecord::ReplacedIndexingLine( - target_indexing_loc, - indexing_code, - ), - ); - } - _ => {} + if let Instruction::IndexingCode(indexing_code) = target_indexing_line { + self.payload.retraction_info.push_record( + RetractionRecord::ReplacedIndexingLine( + target_indexing_loc, + indexing_code, + ), + ); } result = merge_indexed_subsequences( @@ -1977,16 +1958,15 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { &mut self.payload.retraction_info, ); - match &mut code[preceding_choice_instr_loc] { - Instruction::TryMeElse(0) => { - set_switch_var_offset( - code, - index_loc, - preceding_choice_instr_loc + 1 - index_loc, - &mut self.payload.retraction_info, - ); - } - _ => {} + if let Instruction::TryMeElse(0) = + &mut code[preceding_choice_instr_loc] + { + set_switch_var_offset( + code, + index_loc, + preceding_choice_instr_loc + 1 - index_loc, + &mut self.payload.retraction_info, + ); } } } @@ -2068,16 +2048,11 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { { Some(skeleton) if append_or_prepend.is_append() => { let tail_num = skeleton.core.clause_clause_locs.len() - num_clause_predicates; - skeleton.core.clause_clause_locs.make_contiguous()[tail_num..] - .iter() - .cloned() - .collect() + skeleton.core.clause_clause_locs.make_contiguous()[tail_num..].to_vec() } Some(skeleton) => skeleton.core.clause_clause_locs.make_contiguous() [0..num_clause_predicates] - .iter() - .cloned() - .collect(), + .to_vec(), None => { unreachable!() } @@ -2205,46 +2180,47 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { )?; } } else { - if is_cross_module_clause { - if !local_predicate_info.is_extensible { - if predicate_info.is_multifile { - println!( - "Warning: overwriting multifile predicate {}:{}/{} because \ - it was not locally declared multifile.", - self.payload.predicates.compilation_target, - key.0.as_str(), - key.1 - ); - } - - if let Some(skeleton) = self.wam_prelude.indices.remove_predicate_skeleton( - &self.payload.predicates.compilation_target, - &key, - ) { - let compilation_target = self.payload.predicates.compilation_target; - - if predicate_info.is_dynamic { - let clause_clause_compilation_target = match compilation_target { - CompilationTarget::User => { - CompilationTarget::Module(atom!("builtins")) - } - module => module, - }; - - self.retract_local_clauses_by_locs( - clause_clause_compilation_target, - (atom!("$clause"), 2), - (0..skeleton.clauses.len()).map(Some).collect(), - false, // the builtin M:'$clause'/2 is never dynamic. - ); - - predicate_info.is_dynamic = false; - } + if is_cross_module_clause && !local_predicate_info.is_extensible { + if predicate_info.is_multifile { + println!( + "Warning: overwriting multifile predicate {}:{}/{} because \ + it was not locally declared multifile.", + self.payload.predicates.compilation_target, + key.0.as_str(), + key.1 + ); + } - self.payload.retraction_info.push_record( - RetractionRecord::RemovedSkeleton(compilation_target, key, skeleton), + if let Some(skeleton) = self + .wam_prelude + .indices + .remove_predicate_skeleton(&self.payload.predicates.compilation_target, &key) + { + let compilation_target = self.payload.predicates.compilation_target; + + if predicate_info.is_dynamic { + let clause_clause_compilation_target = match compilation_target { + CompilationTarget::User => CompilationTarget::Module(atom!("builtins")), + module => module, + }; + + self.retract_local_clauses_by_locs( + clause_clause_compilation_target, + (atom!("$clause"), 2), + (0..skeleton.clauses.len()).map(Some).collect(), + false, // the builtin M:'$clause'/2 is never dynamic. ); + + predicate_info.is_dynamic = false; } + + self.payload + .retraction_info + .push_record(RetractionRecord::RemovedSkeleton( + compilation_target, + key, + skeleton, + )); } } @@ -2262,20 +2238,17 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { let code_index = self.compile(key, predicates, settings)?; if let Some(filename) = self.listing_src_file_name() { - match self.wam_prelude.indices.modules.get_mut(&filename) { - Some(ref mut module) => { - let index_ptr = code_index.get(); - let code_index = module.code_dir.entry(key).or_insert(code_index).clone(); + if let Some(ref mut module) = self.wam_prelude.indices.modules.get_mut(&filename) { + let index_ptr = code_index.get(); + let code_index = *module.code_dir.entry(key).or_insert(code_index); - set_code_index( - &mut self.payload.retraction_info, - &CompilationTarget::Module(filename), - key, - code_index, - index_ptr, - ); - } - None => {} + set_code_index( + &mut self.payload.retraction_info, + &CompilationTarget::Module(filename), + key, + code_index, + index_ptr, + ); } } } @@ -2344,7 +2317,7 @@ impl Machine { }; let StandaloneCompileResult { clause_code, .. } = compile()?; - self.code.extend(clause_code.into_iter()); + self.code.extend(clause_code); Ok(()) } diff --git a/src/machine/copier.rs b/src/machine/copier.rs index 01f7e8bda..be433ba14 100644 --- a/src/machine/copier.rs +++ b/src/machine/copier.rs @@ -174,7 +174,7 @@ impl CopyTermState { fn copy_attr_var_lists(&mut self) { while !self.attr_var_list_locs.is_empty() { - let iter = mem::replace(&mut self.attr_var_list_locs, vec![]); + let iter = std::mem::take(&mut self.attr_var_list_locs); for (threshold, list_loc) in iter { self.target[threshold] = list_loc_as_cell!(self.target.threshold()); diff --git a/src/machine/cycle_detection.rs b/src/machine/cycle_detection.rs index 692186cba..6df242ac6 100644 --- a/src/machine/cycle_detection.rs +++ b/src/machine/cycle_detection.rs @@ -73,7 +73,7 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> { fn traverse_subterm(&mut self, h: usize, arity: usize) -> Option { let mut last_cell_loc = h + arity - 1; - for idx in (h .. h + arity).rev() { + for idx in (h..h + arity).rev() { if self.heap[idx].get_forwarding_bit() { if self.cycle_detection_active() { self.cycle_found = true; @@ -93,8 +93,8 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> { #[inline] fn continue_forwarding(&self) -> bool { - self.heap[self.current].get_mark_bit() != self.mark_phase || - self.heap[self.current].get_forwarding_bit() + self.heap[self.current].get_mark_bit() != self.mark_phase + || self.heap[self.current].get_forwarding_bit() } fn forward(&mut self) -> Option { @@ -150,7 +150,7 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> { } if self.cycle_detection_active() { - for idx in (h + 1 .. last_cell_loc).rev() { + for idx in (h + 1..last_cell_loc).rev() { if self.heap[idx].get_forwarding_bit() { self.cycle_found = true; return None; @@ -176,7 +176,7 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> { }; if self.cycle_detection_active() { - for idx in (self.next as usize .. last_cell_loc).rev() { + for idx in (self.next as usize..last_cell_loc).rev() { if self.heap[idx].get_forwarding_bit() { self.cycle_found = true; return None; @@ -309,19 +309,19 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> { HeapCellValueTag::Str => { let mut new_str_back_link = self.current; - for idx in (0 .. self.current).rev() { - if self.heap[idx].get_tag() == HeapCellValueTag::Atom { - if cell_as_atom_cell!(self.heap[idx]).get_arity() > 0 { - new_str_back_link = idx; - break; - } + for idx in (0..self.current).rev() { + if self.heap[idx].get_tag() == HeapCellValueTag::Atom + && cell_as_atom_cell!(self.heap[idx]).get_arity() > 0 + { + new_str_back_link = idx; + break; } - if self.heap[idx].get_mark_bit() != self.mark_phase { - if !self.heap[idx].get_forwarding_bit() { - new_str_back_link = idx; - break; - } + if self.heap[idx].get_mark_bit() != self.mark_phase + && !self.heap[idx].get_forwarding_bit() + { + new_str_back_link = idx; + break; } } @@ -402,7 +402,7 @@ impl<'a, const STOP_AT_CYCLES: bool> CycleDetectingIter<'a, STOP_AT_CYCLES> { self.next = self.heap[self.start].get_value(); self.current = self.start; - while let Some(_) = self.forward() {} + while self.forward().is_some() {} } } @@ -415,7 +415,6 @@ impl<'a, const STOP_AT_CYCLES: bool> Iterator for CycleDetectingIter<'a, STOP_AT } } - impl<'a, const STOP_AT_CYCLES: bool> Drop for CycleDetectingIter<'a, STOP_AT_CYCLES> { fn drop(&mut self) { self.invert_marker(); diff --git a/src/machine/disjuncts.rs b/src/machine/disjuncts.rs index f68ee75c8..e07122655 100644 --- a/src/machine/disjuncts.rs +++ b/src/machine/disjuncts.rs @@ -226,7 +226,7 @@ fn merge_branch_seq(branches: impl Iterator) -> BranchInfo { for mut branch in branches { branch_info.branch_num = branch.branch_num; - branch_info.chunks.extend(branch.chunks.drain(..)); + branch_info.chunks.append(&mut branch.chunks); } branch_info.branch_num.delta = branch_info.branch_num.delta * Integer::from(2); @@ -298,7 +298,7 @@ impl VariableClassifier { fn merge_branches(&mut self) { for branches in self.branch_map.values_mut() { - let mut old_branches = std::mem::replace(branches, vec![]); + let mut old_branches = std::mem::take(branches); while let Some(last_branch_num) = old_branches.last().map(|bi| &bi.branch_num) { let mut old_branches_len = old_branches.len(); @@ -361,10 +361,7 @@ impl VariableClassifier { .current_chunk_type .to_gen_context(self.current_chunk_num); - let branch_info_v = self - .branch_map - .entry(var_info.var_ptr.clone()) - .or_insert_with(|| vec![]); + let branch_info_v = self.branch_map.entry(var_info.var_ptr.clone()).or_default(); let needs_new_branch = if let Some(last_bi) = branch_info_v.last() { !self.root_set.contains(&last_bi.branch_num) @@ -420,56 +417,49 @@ impl VariableClassifier { arity: term.arity(), }; - match term { - Term::Clause(_, _, terms) => { - for term in terms.into_iter() { - for term_ref in breadth_first_iter(term, RootIterationPolicy::Iterated) { - if let TermRef::Var(lvl, _, var_ptr) = term_ref { - // a body term, so we need the child level here. - let lvl = lvl.child_level(); - - // the body of the if let here is an inlined - // "probe_head_var". note the difference between it - // and "probe_body_var". - let branch_info_v = self - .branch_map - .entry(var_ptr.clone()) - .or_insert_with(|| vec![]); - - let needs_new_branch = branch_info_v.is_empty(); - - if needs_new_branch { - branch_info_v - .push(BranchInfo::new(self.current_branch_num.clone())); - } + if let Term::Clause(_, _, terms) = term { + for term in terms.iter() { + for term_ref in breadth_first_iter(term, RootIterationPolicy::Iterated) { + if let TermRef::Var(lvl, _, var_ptr) = term_ref { + // a body term, so we need the child level here. + let lvl = lvl.child_level(); - let branch_info = branch_info_v.last_mut().unwrap(); - let needs_new_chunk = branch_info.chunks.is_empty(); + // the body of the if let here is an inlined + // "probe_head_var". note the difference between it + // and "probe_body_var". + let branch_info_v = self.branch_map.entry(var_ptr.clone()).or_default(); - if needs_new_chunk { - branch_info.chunks.push(ChunkInfo { - chunk_num: self.current_chunk_num, - term_loc: GenContext::Head, - vars: vec![], - }); - } + let needs_new_branch = branch_info_v.is_empty(); - let chunk_info = branch_info.chunks.last_mut().unwrap(); - let var_info = VarInfo { - var_ptr, - classify_info, - chunk_type: self.current_chunk_type, - lvl, - }; + if needs_new_branch { + branch_info_v.push(BranchInfo::new(self.current_branch_num.clone())); + } - chunk_info.vars.push(var_info); + let branch_info = branch_info_v.last_mut().unwrap(); + let needs_new_chunk = branch_info.chunks.is_empty(); + + if needs_new_chunk { + branch_info.chunks.push(ChunkInfo { + chunk_num: self.current_chunk_num, + term_loc: GenContext::Head, + vars: vec![], + }); } - } - classify_info.arg_c += 1; + let chunk_info = branch_info.chunks.last_mut().unwrap(); + let var_info = VarInfo { + var_ptr, + classify_info, + chunk_type: self.current_chunk_type, + lvl, + }; + + chunk_info.vars.push(var_info); + } } + + classify_info.arg_c += 1; } - _ => {} } Ok(()) @@ -538,7 +528,10 @@ impl VariableClassifier { build_stack.push_chunk_term(if is_global { QueryTerm::GlobalCut(var_num) } else { - QueryTerm::LocalCut { var_num, cut_prev: false } + QueryTerm::LocalCut { + var_num, + cut_prev: false, + } }); } TraversalState::CutPrev(var_num) => { @@ -548,7 +541,10 @@ impl VariableClassifier { self.probe_in_situ_var(var_num); - build_stack.push_chunk_term(QueryTerm::LocalCut { var_num, cut_prev: true }); + build_stack.push_chunk_term(QueryTerm::LocalCut { + var_num, + cut_prev: true, + }); } TraversalState::Fail => { build_stack.push_chunk_term(QueryTerm::Fail); @@ -705,7 +701,9 @@ impl VariableClassifier { state_stack.push(TraversalState::BuildDisjunct(build_stack_len)); state_stack.push(TraversalState::Fail); state_stack.push(TraversalState::CutPrev(self.var_num)); - state_stack.push(TraversalState::ResetGlobalCutVarOverride(self.global_cut_var_num_override)); + state_stack.push(TraversalState::ResetGlobalCutVarOverride( + self.global_cut_var_num_override, + )); state_stack.push(TraversalState::Term(not_term)); state_stack.push(TraversalState::OverrideGlobalCutVar(self.var_num)); state_stack.push(TraversalState::GetCutPoint { diff --git a/src/machine/dispatch.rs b/src/machine/dispatch.rs index 191a023fd..6afce9263 100644 --- a/src/machine/dispatch.rs +++ b/src/machine/dispatch.rs @@ -313,8 +313,8 @@ impl MachineState { impl Machine { pub(super) fn find_living_dynamic_else(&self, mut p: usize) -> Option<(usize, usize)> { loop { - match &self.code[p] { - &Instruction::DynamicElse(birth, death, NextOrFail::Next(i)) => { + match self.code[p] { + Instruction::DynamicElse(birth, death, NextOrFail::Next(i)) => { if birth < self.machine_st.cc && Death::Finite(self.machine_st.cc) <= death { return Some((p, i)); } else if i > 0 { @@ -323,14 +323,14 @@ impl Machine { return None; } } - &Instruction::DynamicElse(birth, death, NextOrFail::Fail(_)) => { + Instruction::DynamicElse(birth, death, NextOrFail::Fail(_)) => { if birth < self.machine_st.cc && Death::Finite(self.machine_st.cc) <= death { return Some((p, 0)); } else { return None; } } - &Instruction::DynamicInternalElse(birth, death, NextOrFail::Next(i)) => { + Instruction::DynamicInternalElse(birth, death, NextOrFail::Next(i)) => { if birth < self.machine_st.cc && Death::Finite(self.machine_st.cc) <= death { return Some((p, i)); } else if i > 0 { @@ -339,14 +339,14 @@ impl Machine { return None; } } - &Instruction::DynamicInternalElse(birth, death, NextOrFail::Fail(_)) => { + Instruction::DynamicInternalElse(birth, death, NextOrFail::Fail(_)) => { if birth < self.machine_st.cc && Death::Finite(self.machine_st.cc) <= death { return Some((p, 0)); } else { return None; } } - &Instruction::RevJmpBy(i) => { + Instruction::RevJmpBy(i) => { p -= i; } _ => { @@ -395,25 +395,14 @@ impl Machine { fn execute_switch_on_term(&mut self) { #[inline(always)] fn dynamic_external_of_clause_is_valid(machine: &mut Machine, p: usize) -> bool { - match &machine.code[p] { - Instruction::DynamicInternalElse(..) => { - machine.machine_st.dynamic_mode = FirstOrNext::First; - return true; - } - _ => {} + if let Instruction::DynamicInternalElse(..) = machine.code[p] { + machine.machine_st.dynamic_mode = FirstOrNext::First; + return true; } - match &machine.code[p - 1] { - &Instruction::DynamicInternalElse(birth, death, _) => { - if birth < machine.machine_st.cc - && Death::Finite(machine.machine_st.cc) <= death - { - return true; - } else { - return false; - } - } - _ => {} + if let Instruction::DynamicInternalElse(birth, death, _) = machine.code[p - 1] { + return birth < machine.machine_st.cc + && Death::Finite(machine.machine_st.cc) <= death; } true @@ -1896,7 +1885,7 @@ impl Machine { self.machine_st.backtrack(); } } - &Instruction::CallNumberLessThanOrEqual(ref at_1, ref at_2) => { + Instruction::CallNumberLessThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -1910,7 +1899,7 @@ impl Machine { } } } - &Instruction::ExecuteNumberLessThanOrEqual(ref at_1, ref at_2) => { + Instruction::ExecuteNumberLessThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -1924,7 +1913,7 @@ impl Machine { } } } - &Instruction::CallNumberEqual(ref at_1, ref at_2) => { + Instruction::CallNumberEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -1938,7 +1927,7 @@ impl Machine { } } } - &Instruction::ExecuteNumberEqual(ref at_1, ref at_2) => { + Instruction::ExecuteNumberEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -1952,7 +1941,7 @@ impl Machine { } } } - &Instruction::CallNumberNotEqual(ref at_1, ref at_2) => { + Instruction::CallNumberNotEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -1966,7 +1955,7 @@ impl Machine { } } } - &Instruction::ExecuteNumberNotEqual(ref at_1, ref at_2) => { + Instruction::ExecuteNumberNotEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -1980,7 +1969,7 @@ impl Machine { } } } - &Instruction::CallNumberGreaterThanOrEqual(ref at_1, ref at_2) => { + Instruction::CallNumberGreaterThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -1994,7 +1983,7 @@ impl Machine { } } } - &Instruction::ExecuteNumberGreaterThanOrEqual(ref at_1, ref at_2) => { + Instruction::ExecuteNumberGreaterThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2008,7 +1997,7 @@ impl Machine { } } } - &Instruction::CallNumberGreaterThan(ref at_1, ref at_2) => { + Instruction::CallNumberGreaterThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2022,7 +2011,7 @@ impl Machine { } } } - &Instruction::ExecuteNumberGreaterThan(ref at_1, ref at_2) => { + Instruction::ExecuteNumberGreaterThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2036,7 +2025,7 @@ impl Machine { } } } - &Instruction::CallNumberLessThan(ref at_1, ref at_2) => { + Instruction::CallNumberLessThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2050,7 +2039,7 @@ impl Machine { } } } - &Instruction::ExecuteNumberLessThan(ref at_1, ref at_2) => { + Instruction::ExecuteNumberLessThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2064,7 +2053,7 @@ impl Machine { } } } - &Instruction::DefaultCallNumberLessThanOrEqual(ref at_1, ref at_2) => { + Instruction::DefaultCallNumberLessThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2077,7 +2066,7 @@ impl Machine { } } } - &Instruction::DefaultExecuteNumberLessThanOrEqual(ref at_1, ref at_2) => { + Instruction::DefaultExecuteNumberLessThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2090,7 +2079,7 @@ impl Machine { } } } - &Instruction::DefaultCallNumberNotEqual(ref at_1, ref at_2) => { + Instruction::DefaultCallNumberNotEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2103,7 +2092,7 @@ impl Machine { } } } - &Instruction::DefaultExecuteNumberNotEqual(ref at_1, ref at_2) => { + Instruction::DefaultExecuteNumberNotEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2116,7 +2105,7 @@ impl Machine { } } } - &Instruction::DefaultCallNumberEqual(ref at_1, ref at_2) => { + Instruction::DefaultCallNumberEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2129,7 +2118,7 @@ impl Machine { } } } - &Instruction::DefaultExecuteNumberEqual(ref at_1, ref at_2) => { + Instruction::DefaultExecuteNumberEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2142,7 +2131,7 @@ impl Machine { } } } - &Instruction::DefaultCallNumberGreaterThanOrEqual(ref at_1, ref at_2) => { + Instruction::DefaultCallNumberGreaterThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2155,7 +2144,7 @@ impl Machine { } } } - &Instruction::DefaultExecuteNumberGreaterThanOrEqual(ref at_1, ref at_2) => { + Instruction::DefaultExecuteNumberGreaterThanOrEqual(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2168,7 +2157,7 @@ impl Machine { } } } - &Instruction::DefaultCallNumberGreaterThan(ref at_1, ref at_2) => { + Instruction::DefaultCallNumberGreaterThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2181,7 +2170,7 @@ impl Machine { } } } - &Instruction::DefaultExecuteNumberGreaterThan(ref at_1, ref at_2) => { + Instruction::DefaultExecuteNumberGreaterThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2194,7 +2183,7 @@ impl Machine { } } } - &Instruction::DefaultCallNumberLessThan(ref at_1, ref at_2) => { + Instruction::DefaultCallNumberLessThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2207,7 +2196,7 @@ impl Machine { } } } - &Instruction::DefaultExecuteNumberLessThan(ref at_1, ref at_2) => { + Instruction::DefaultExecuteNumberLessThan(ref at_1, ref at_2) => { let n1 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_1)); let n2 = try_or_throw!(self.machine_st, self.machine_st.get_number(at_2)); @@ -2955,7 +2944,7 @@ impl Machine { self.machine_st.p += 1; } - &Instruction::IndexingCode(ref indexing_lines) => { + Instruction::IndexingCode(ref indexing_lines) => { match &indexing_lines[self.machine_st.oip as usize] { IndexingLine::Indexing(_) => { self.execute_switch_on_term(); @@ -2965,22 +2954,22 @@ impl Machine { } } IndexingLine::IndexedChoice(ref indexed_choice) => { - match &indexed_choice[self.machine_st.iip as usize] { - &IndexedChoiceInstruction::Try(offset) => { + match indexed_choice[self.machine_st.iip as usize] { + IndexedChoiceInstruction::Try(offset) => { self.indexed_try(offset); } - &IndexedChoiceInstruction::Retry(l) => { + IndexedChoiceInstruction::Retry(l) => { self.retry(l); increment_call_count!(self.machine_st); } - &IndexedChoiceInstruction::DefaultRetry(l) => { + IndexedChoiceInstruction::DefaultRetry(l) => { self.retry(l); } - &IndexedChoiceInstruction::Trust(l) => { + IndexedChoiceInstruction::Trust(l) => { self.trust(l); increment_call_count!(self.machine_st); } - &IndexedChoiceInstruction::DefaultTrust(l) => { + IndexedChoiceInstruction::DefaultTrust(l) => { self.trust(l); } } @@ -5089,16 +5078,13 @@ impl Machine { .get_predicate_skeleton_mut(&compilation_target, &key) .unwrap(); - match skeleton.target_pos_of_clause_clause_loc(l) { - Some(n) => { - let r = self - .machine_st - .store(self.machine_st.deref(self.machine_st.registers[5])); + if let Some(n) = skeleton.target_pos_of_clause_clause_loc(l) { + let r = self + .machine_st + .store(self.machine_st.deref(self.machine_st.registers[5])); - self.machine_st - .unify_fixnum(Fixnum::build_with(n as i64), r); - } - None => {} + self.machine_st + .unify_fixnum(Fixnum::build_with(n as i64), r); } self.machine_st.call_at_index(2, p); @@ -5135,16 +5121,13 @@ impl Machine { .get_predicate_skeleton_mut(&compilation_target, &key) .unwrap(); - match skeleton.target_pos_of_clause_clause_loc(l) { - Some(n) => { - let r = self - .machine_st - .store(self.machine_st.deref(self.machine_st.registers[5])); + if let Some(n) = skeleton.target_pos_of_clause_clause_loc(l) { + let r = self + .machine_st + .store(self.machine_st.deref(self.machine_st.registers[5])); - self.machine_st - .unify_fixnum(Fixnum::build_with(n as i64), r); - } - None => {} + self.machine_st + .unify_fixnum(Fixnum::build_with(n as i64), r); } self.machine_st.execute_at_index(2, p); @@ -5226,7 +5209,7 @@ impl Machine { // So we only have access to a runtime handle in here and can't shut it down. // Since I'm not aware of the consequences of deactivating this new code which came in while PR 1880 // was not merged, I'm only deactivating it for now. - + //#[cfg(not(target_arch = "wasm32"))] //let runtime = tokio::runtime::Runtime::new().unwrap(); //#[cfg(target_arch = "wasm32")] diff --git a/src/machine/gc.rs b/src/machine/gc.rs index 715adde71..32142e1dd 100644 --- a/src/machine/gc.rs +++ b/src/machine/gc.rs @@ -9,14 +9,22 @@ pub(crate) trait UnmarkPolicy { fn forward_attr_var(iter: &mut StacklessPreOrderHeapIter) -> Option where Self: Sized; - fn invert_marker(iter: &mut StacklessPreOrderHeapIter) where Self: Sized; + fn invert_marker(iter: &mut StacklessPreOrderHeapIter) + where + Self: Sized; fn mark_phase(&self) -> bool; #[inline] - fn report_var_link(iter: &StacklessPreOrderHeapIter) -> bool where Self: Sized { + fn report_var_link(iter: &StacklessPreOrderHeapIter) -> bool + where + Self: Sized, + { iter.heap[iter.next as usize].get_mark_bit() == iter.iter_state.mark_phase() } #[inline(always)] - fn record_focus(_iter: &mut StacklessPreOrderHeapIter) where Self: Sized { + fn record_focus(_iter: &mut StacklessPreOrderHeapIter) + where + Self: Sized, + { } } @@ -34,7 +42,7 @@ fn invert_marker(iter: &mut StacklessPreOrderHeapIter) { iter.next = iter.heap[iter.start].get_value(); iter.current = iter.start; - while let Some(_) = iter.forward() {} + while iter.forward().is_some() {} } impl UnmarkPolicy for IteratorUMP { @@ -139,7 +147,7 @@ impl<'a> StacklessPreOrderHeapIter<'a, IteratorUMP> { start, current: start, next, - iter_state: IteratorUMP { mark_phase: true,}, + iter_state: IteratorUMP { mark_phase: true }, } } } @@ -189,11 +197,9 @@ impl<'a, UMP: UnmarkPolicy> StacklessPreOrderHeapIter<'a, UMP> { return Some(cell); } - if self.next < self.heap.len() as u64 { - if UMP::report_var_link(self) { - let tag = HeapCellValueTag::AttrVar; - return Some(HeapCellValue::build_with(tag, next as u64)); - } + if self.next < self.heap.len() as u64 && UMP::report_var_link(self) { + let tag = HeapCellValueTag::AttrVar; + return Some(HeapCellValue::build_with(tag, next as u64)); } } HeapCellValueTag::Var => { @@ -203,11 +209,9 @@ impl<'a, UMP: UnmarkPolicy> StacklessPreOrderHeapIter<'a, UMP> { return Some(cell); } - if self.next < self.heap.len() as u64 { - if UMP::report_var_link(self) { - let tag = HeapCellValueTag::Var; - return Some(HeapCellValue::build_with(tag, next as u64)); - } + if self.next < self.heap.len() as u64 && UMP::report_var_link(self) { + let tag = HeapCellValueTag::Var; + return Some(HeapCellValue::build_with(tag, next as u64)); } } HeapCellValueTag::Str => { @@ -311,10 +315,8 @@ impl<'a, UMP: UnmarkPolicy> StacklessPreOrderHeapIter<'a, UMP> { return Some(self.backward_and_return()); } } - } else { - if self.backward() { - return None; - } + } else if self.backward() { + return None; } } } @@ -358,7 +360,7 @@ impl<'a, UMP: UnmarkPolicy> Iterator for StacklessPreOrderHeapIter<'a, UMP> { pub fn mark_cells(heap: &mut Heap, start: usize) { let mut iter = StacklessPreOrderHeapIter::::new(heap, start); - while let Some(_) = iter.forward() {} + while iter.forward().is_some() {} } #[cfg(test)] @@ -665,14 +667,18 @@ mod tests { wam.machine_st.heap.push(pstr_loc_as_cell!(1)); - let pstr_var_cell = put_partial_string(&mut wam.machine_st.heap, "abc ", &wam.machine_st.atom_tbl); + let pstr_var_cell = + put_partial_string(&mut wam.machine_st.heap, "abc ", &wam.machine_st.atom_tbl); let pstr_cell = wam.machine_st.heap[pstr_var_cell.get_value() as usize]; mark_cells(&mut wam.machine_st.heap, 0); all_cells_marked_and_unforwarded(&wam.machine_st.heap); - assert_eq!(unmark_cell_bits!(wam.machine_st.heap[0]), pstr_loc_as_cell!(1)); + assert_eq!( + unmark_cell_bits!(wam.machine_st.heap[0]), + pstr_loc_as_cell!(1) + ); assert_eq!(unmark_cell_bits!(wam.machine_st.heap[1]), pstr_cell); assert_eq!( unmark_cell_bits!(wam.machine_st.heap[2]), @@ -720,7 +726,7 @@ mod tests { mark_cells(&mut wam.machine_st.heap, 7); - all_cells_marked_and_unforwarded(&wam.machine_st.heap[1 ..]); + all_cells_marked_and_unforwarded(&wam.machine_st.heap[1..]); assert_eq!(unmark_cell_bits!(wam.machine_st.heap[1]), pstr_cell); assert_eq!( @@ -1536,10 +1542,10 @@ mod tests { mark_cells(&mut wam.machine_st.heap, 0); - all_cells_marked_and_unforwarded(&mut wam.machine_st.heap[0..24]); + all_cells_marked_and_unforwarded(&wam.machine_st.heap[0..24]); for cell in &wam.machine_st.heap[24..] { - assert_eq!(cell.get_mark_bit(), false); + assert!(!cell.get_mark_bit()); } assert_eq!( diff --git a/src/machine/heap.rs b/src/machine/heap.rs index 858e44878..7ba02da92 100644 --- a/src/machine/heap.rs +++ b/src/machine/heap.rs @@ -169,7 +169,7 @@ pub(crate) fn allocate_pstr(heap: &mut Heap, mut src: &str, atom_tbl: &AtomTable let orig_h = heap.len(); loop { - if src == "" { + if src.is_empty() { return if orig_h == heap.len() { None } else { @@ -199,7 +199,7 @@ pub(crate) fn allocate_pstr(heap: &mut Heap, mut src: &str, atom_tbl: &AtomTable heap.push(string_as_pstr_cell!(pstr)); - if rest_src != "" { + if !rest_src.is_empty() { heap.push(pstr_loc_as_cell!(h + 2)); src = rest_src; } else { @@ -249,7 +249,7 @@ pub(crate) fn to_local_code_ptr(heap: &Heap, addr: HeapCellValue) -> Option { let value: usize = (&*n).try_into().unwrap(); Some(value) - }, + } _ => None, } }; diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index 78ff50c76..3a27b3d29 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -3,18 +3,17 @@ use std::sync::Arc; use crate::atom_table; use crate::heap_print::{HCPrinter, HCValueOutputter, PrinterOutputter}; -use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS}; +use crate::machine::machine_indices::VarKey; use crate::machine::mock_wam::CompositeOpDir; +use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS}; +use crate::parser::ast::{Var, VarPtr}; use crate::parser::parser::{Parser, Tokens}; use crate::read::write_term_to_heap; -use crate::machine::machine_indices::VarKey; -use crate::parser::ast::{Var, VarPtr}; use indexmap::IndexMap; use super::{ - Machine, MachineConfig, QueryResult, QueryResolutionLine, - Atom, AtomCell, HeapCellValue, HeapCellValueTag, Value, QueryResolution, - streams::Stream + streams::Stream, Atom, AtomCell, HeapCellValue, HeapCellValueTag, Machine, MachineConfig, + QueryResolution, QueryResolutionLine, QueryResult, Value, }; impl Machine { @@ -30,7 +29,10 @@ impl Machine { pub fn consult_module_string(&mut self, module_name: &str, program: String) { let stream = Stream::from_owned_string(program, &mut self.machine_st.arena); self.machine_st.registers[1] = stream_as_cell!(stream); - self.machine_st.registers[2] = atom_as_cell!(&atom_table::AtomTable::build_with(&self.machine_st.atom_tbl, module_name)); + self.machine_st.registers[2] = atom_as_cell!(&atom_table::AtomTable::build_with( + &self.machine_st.atom_tbl, + module_name + )); self.run_module_predicate(atom!("loader"), (atom!("consult_stream"), 2)); } @@ -62,21 +64,33 @@ impl Machine { // Parse the query so we can analyze and then call the term let mut parser = Parser::new( Stream::from_owned_string(query, &mut self.machine_st.arena), - &mut self.machine_st + &mut self.machine_st, ); let op_dir = CompositeOpDir::new(&self.indices.op_dir, None); - let term = parser.read_term(&op_dir, Tokens::Default).expect("Failed to parse query"); + let term = parser + .read_term(&op_dir, Tokens::Default) + .expect("Failed to parse query"); // Write parsed term to heap - let term_write_result = write_term_to_heap(&term, &mut self.machine_st.heap, &mut self.machine_st.atom_tbl).expect("couldn't write term to heap"); + let term_write_result = + write_term_to_heap(&term, &mut self.machine_st.heap, &self.machine_st.atom_tbl) + .expect("couldn't write term to heap"); // Write term to heap self.machine_st.registers[1] = self.machine_st.heap[term_write_result.heap_loc]; self.machine_st.cp = LIB_QUERY_SUCCESS; // BREAK_FROM_DISPATCH_LOOP_LOC; - self.machine_st.p = self.indices.code_dir.get(&(atom!("call"), 1)).expect("couldn't get code index").local().unwrap(); - - let var_names: IndexMap<_, _> = term_write_result.var_dict.iter() + self.machine_st.p = self + .indices + .code_dir + .get(&(atom!("call"), 1)) + .expect("couldn't get code index") + .local() + .unwrap(); + + let var_names: IndexMap<_, _> = term_write_result + .var_dict + .iter() .map(|(var_key, cell)| match var_key { // NOTE: not the intention behind Var::InSitu here but // we can hijack it to store anonymous variables @@ -99,27 +113,29 @@ impl Machine { //println!("stub_b: {}", stub_b); //println!("fail: {}", self.machine_st.fail); - if self.machine_st.ball.stub.len() != 0 { + if !self.machine_st.ball.stub.is_empty() { // NOTE: this means an exception was thrown, at which // point we backtracked to the stub choice point. // this should halt the search for solutions as it // does in the Scryer top-level. the exception term is // contained in self.machine_st.ball. - let error_string = self.machine_st.ball.stub + let error_string = self + .machine_st + .ball + .stub .iter() - .filter(|h| match h.get_tag() { - HeapCellValueTag::Atom => true, - HeapCellValueTag::Fixnum => true, - _ => false, + .filter(|h| { + matches!( + h.get_tag(), + HeapCellValueTag::Atom | HeapCellValueTag::Fixnum + ) }) .map(|h| match h.get_tag() { HeapCellValueTag::Atom => { let (name, _) = cell_as_atom_cell!(h).get_name_and_arity(); name.as_str().to_string() } - HeapCellValueTag::Fixnum => { - h.get_value().clone().to_string() - }, + HeapCellValueTag::Fixnum => h.get_value().clone().to_string(), _ => unreachable!(), }) .collect::>() @@ -154,7 +170,7 @@ impl Machine { let mut bindings: BTreeMap = BTreeMap::new(); for (var_key, term_to_be_printed) in &term_write_result.var_dict { - if var_key.to_string().starts_with("_") { + if var_key.to_string().starts_with('_') { continue; } let mut printer = HCPrinter::new( @@ -210,7 +226,7 @@ mod tests { use ordered_float::OrderedFloat; use super::*; - use crate::machine::{QueryMatch, Value, QueryResolution}; + use crate::machine::{QueryMatch, QueryResolution, Value}; #[test] fn programatic_query() { @@ -258,7 +274,9 @@ mod tests { let output = machine.run_query(query); assert_eq!( output, - Err(String::from("error existence_error procedure / triple 3 / triple 3")) + Err(String::from( + "error existence_error procedure / triple 3 / triple 3" + )) ); } @@ -278,26 +296,30 @@ mod tests { constructor(xyz, '[{action: "addLink", source: "this", predicate: "recipe://title", target: "literal://string:Meta%20Muffins"}]'). "#.to_string()); - let result = machine.run_query(String::from("subject_class(\"Todo\", C), constructor(C, Actions).")); + let result = machine.run_query(String::from( + "subject_class(\"Todo\", C), constructor(C, Actions).", + )); assert_eq!( result, - Ok(QueryResolution::Matches(vec![ - QueryMatch::from(btreemap! { + Ok(QueryResolution::Matches(vec![QueryMatch::from( + btreemap! { "C" => Value::from("c"), "Actions" => Value::from("[{action: \"addLink\", source: \"this\", predicate: \"todo://state\", target: \"todo://ready\"}]"), - }), - ])) + } + ),])) ); - let result = machine.run_query(String::from("subject_class(\"Recipe\", C), constructor(C, Actions).")); + let result = machine.run_query(String::from( + "subject_class(\"Recipe\", C), constructor(C, Actions).", + )); assert_eq!( result, - Ok(QueryResolution::Matches(vec![ - QueryMatch::from(btreemap! { + Ok(QueryResolution::Matches(vec![QueryMatch::from( + btreemap! { "C" => Value::from("xyz"), "Actions" => Value::from("[{action: \"addLink\", source: \"this\", predicate: \"recipe://title\", target: \"literal://string:Meta%20Muffins\"}]"), - }), - ])) + } + ),])) ); let result = machine.run_query(String::from("subject_class(Class, _).")); @@ -319,15 +341,17 @@ mod tests { let mut machine = Machine::new_lib(); machine.load_module_string( "facts", - r#" + r#" list([1,2,3]). - "#.to_string()); + "# + .to_string(), + ); let result = machine.run_query(String::from("list(X).")); assert_eq!( result, - Ok(QueryResolution::Matches(vec![ - QueryMatch::from(btreemap! { + Ok(QueryResolution::Matches(vec![QueryMatch::from( + btreemap! { "X" => Value::List( Vec::from([ Value::Float(OrderedFloat::from(1.0)), @@ -335,12 +359,11 @@ mod tests { Value::Float(OrderedFloat::from(3.0)) ]) ) - }), - ])) + } + ),])) ); } - #[test] fn consult() { let mut machine = Machine::new_lib(); @@ -397,7 +420,6 @@ mod tests { machine.run_query(String::from(r#"triple("a","new","b")."#)), Ok(QueryResolution::True) ); - } #[ignore = "fails on windows"] @@ -462,12 +484,13 @@ mod tests { ), ); - let query = String::from(r#"findall([Predicate, Target], triple(_,Predicate,Target), Result)."#); + let query = + String::from(r#"findall([Predicate, Target], triple(_,Predicate,Target), Result)."#); let output = machine.run_query(query); assert_eq!( output, - Ok(QueryResolution::Matches(vec![ - QueryMatch::from(btreemap! { + Ok(QueryResolution::Matches(vec![QueryMatch::from( + btreemap! { "Predicate" => Value::from("Predicate"), "Result" => Value::List( Vec::from([ @@ -476,9 +499,8 @@ mod tests { ]) ), "Target" => Value::from("Target"), - }), - ])) + } + ),])) ); - } } diff --git a/src/machine/load_state.rs b/src/machine/load_state.rs index 308ddf4c3..3cdc10aad 100644 --- a/src/machine/load_state.rs +++ b/src/machine/load_state.rs @@ -137,10 +137,9 @@ pub(super) fn import_module_exports<'a, LS: LoadState<'a>>( if let Some(src_code_index) = imported_module.code_dir.get(&key).cloned() { let arena = &mut LS::machine_st(payload).arena; - let target_code_index = code_dir + let target_code_index = *code_dir .entry(key) - .or_insert_with(|| CodeIndex::default(arena)) - .clone(); + .or_insert_with(|| CodeIndex::default(arena)); set_code_index( &mut payload.retraction_info, @@ -189,16 +188,15 @@ fn import_module_exports_into_module<'a, LS: LoadState<'a>>( let key = (*name, *arity); if let Some(meta_specs) = imported_module.meta_predicates.get(&key) { - meta_predicates.insert(key.clone(), meta_specs.clone()); + meta_predicates.insert(key, meta_specs.clone()); } if let Some(src_code_index) = imported_module.code_dir.get(&key) { let arena = &mut LS::machine_st(payload).arena; - let target_code_index = code_dir + let target_code_index = *code_dir .entry(key) - .or_insert_with(|| CodeIndex::default(arena)) - .clone(); + .or_insert_with(|| CodeIndex::default(arena)); set_code_index( &mut payload.retraction_info, @@ -209,7 +207,7 @@ fn import_module_exports_into_module<'a, LS: LoadState<'a>>( ); } else { return Err(SessionError::ModuleDoesNotContainExport( - imported_module.module_decl.name.clone(), + imported_module.module_decl.name, (*name, *arity), )); } @@ -243,18 +241,17 @@ fn import_qualified_module_exports<'a, LS: LoadState<'a>>( wam_prelude .indices .meta_predicates - .insert(key.clone(), meta_specs.clone()); + .insert(key, meta_specs.clone()); } if let Some(src_code_index) = imported_module.code_dir.get(&key) { let arena = &mut LS::machine_st(payload).arena; - let target_code_index = wam_prelude + let target_code_index = *wam_prelude .indices .code_dir - .entry(key.clone()) - .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)) - .clone(); + .entry(key) + .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)); set_code_index( &mut payload.retraction_info, @@ -265,7 +262,7 @@ fn import_qualified_module_exports<'a, LS: LoadState<'a>>( ); } else { return Err(SessionError::ModuleDoesNotContainExport( - imported_module.module_decl.name.clone(), + imported_module.module_decl.name, (*name, *arity), )); } @@ -311,10 +308,9 @@ fn import_qualified_module_exports_into_module<'a, LS: LoadState<'a>>( if let Some(src_code_index) = imported_module.code_dir.get(&key) { let arena = &mut LS::machine_st(payload).arena; - let target_code_index = code_dir + let target_code_index = *code_dir .entry(key) - .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)) - .clone(); + .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)); set_code_index( &mut payload.retraction_info, @@ -325,7 +321,7 @@ fn import_qualified_module_exports_into_module<'a, LS: LoadState<'a>>( ); } else { return Err(SessionError::ModuleDoesNotContainExport( - imported_module.module_decl.name.clone(), + imported_module.module_decl.name, (*name, *arity), )); } @@ -423,7 +419,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { payload_compilation_target, clause_clause_compilation_target, key, - mem::replace(&mut skeleton.clause_clause_locs, VecDeque::new()), + std::mem::take(&mut skeleton.clause_clause_locs), ), ); @@ -436,7 +432,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { } }; - self.retract_local_clauses_impl(clause_clause_compilation_target, key, &clause_locs); + self.retract_local_clauses_impl(clause_clause_compilation_target, key, clause_locs); } pub(super) fn try_term_to_tl( @@ -600,30 +596,22 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { key: PredicateKey, ) -> CodeIndex { match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => module - .code_dir - .entry(key) - .or_insert_with(|| { - CodeIndex::new( - IndexPtr::undefined(), - &mut LS::machine_st(&mut self.payload).arena, - ) - }) - .clone(), + Some(ref mut module) => *module.code_dir.entry(key).or_insert_with(|| { + CodeIndex::new( + IndexPtr::undefined(), + &mut LS::machine_st(&mut self.payload).arena, + ) + }), None => { self.add_dynamically_generated_module(module_name); match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => module - .code_dir - .entry(key) - .or_insert_with(|| { - CodeIndex::new( - IndexPtr::undefined(), - &mut LS::machine_st(&mut self.payload).arena, - ) - }) - .clone(), + Some(ref mut module) => *module.code_dir.entry(key).or_insert_with(|| { + CodeIndex::new( + IndexPtr::undefined(), + &mut LS::machine_st(&mut self.payload).arena, + ) + }), None => { unreachable!() } @@ -640,13 +628,12 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { let arena = &mut LS::machine_st(&mut self.payload).arena; match compilation_target { - CompilationTarget::User => self + CompilationTarget::User => *self .wam_prelude .indices .code_dir .entry(key) - .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)) - .clone(), + .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)), CompilationTarget::Module(module_name) => { self.get_or_insert_local_code_index(module_name, key) } @@ -661,13 +648,12 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { let arena = &mut LS::machine_st(&mut self.payload).arena; if module_name == atom!("user") { - return self + return *self .wam_prelude .indices .code_dir .entry(key) - .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)) - .clone(); + .or_insert_with(|| CodeIndex::new(IndexPtr::undefined(), arena)); } else { self.get_or_insert_local_code_index(module_name, key) } @@ -694,7 +680,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { } CompilationTarget::Module(module_name) => { if let Some(module) = self.wam_prelude.indices.modules.get_mut(&module_name) { - module.extensible_predicates.insert(key.clone(), skeleton); + module.extensible_predicates.insert(key, skeleton); let record = RetractionRecord::AddedExtensiblePredicate( CompilationTarget::Module(module_name), @@ -747,11 +733,10 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { match payload_compilation_target { CompilationTarget::User => { if let Some(filename) = listing_src_file_name { - match self.wam_prelude.indices.modules.get_mut(&filename) { - Some(ref mut module) => { - op_decl.insert_into_op_dir(&mut module.op_dir); - } - None => {} + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&filename) + { + op_decl.insert_into_op_dir(&mut module.op_dir); } } @@ -855,48 +840,43 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { } } } - _ => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - match module.meta_predicates.insert(key.clone(), meta_specs) { - Some(old_meta_specs) => { - self.payload.retraction_info.push_record( - RetractionRecord::ReplacedMetaPredicate( - module_name, - key.0, - old_meta_specs, - ), - ); - } - None => { - self.payload.retraction_info.push_record( - RetractionRecord::AddedMetaPredicate(module_name, key), - ); - } - } + _ => match self.wam_prelude.indices.modules.get_mut(&module_name) { + Some(ref mut module) => match module.meta_predicates.insert(key, meta_specs) { + Some(old_meta_specs) => { + self.payload.retraction_info.push_record( + RetractionRecord::ReplacedMetaPredicate( + module_name, + key.0, + old_meta_specs, + ), + ); } None => { - self.add_dynamically_generated_module(module_name); - - if let Some(module) = self.wam_prelude.indices.modules.get_mut(&module_name) - { - module.meta_predicates.insert(key.clone(), meta_specs); - } else { - unreachable!() - } + self.payload + .retraction_info + .push_record(RetractionRecord::AddedMetaPredicate(module_name, key)); + } + }, + None => { + self.add_dynamically_generated_module(module_name); - self.payload.retraction_info.push_record( - RetractionRecord::AddedMetaPredicate(module_name.clone(), key), - ); + if let Some(module) = self.wam_prelude.indices.modules.get_mut(&module_name) { + module.meta_predicates.insert(key, meta_specs); + } else { + unreachable!() } + + self.payload + .retraction_info + .push_record(RetractionRecord::AddedMetaPredicate(module_name, key)); } - } + }, } } pub(super) fn add_dynamically_generated_module(&mut self, module_name: Atom) { let module_decl = ModuleDecl { - name: module_name.clone(), + name: module_name, exports: vec![], }; @@ -912,12 +892,9 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { self.payload .retraction_info - .push_record(RetractionRecord::AddedModule(module_name.clone())); + .push_record(RetractionRecord::AddedModule(module_name)); - self.wam_prelude - .indices - .modules - .insert(module_name.clone(), module); + self.wam_prelude.indices.modules.insert(module_name, module); } fn import_builtins_in_module( @@ -956,51 +933,48 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { self.remove_module_exports(module_name); self.remove_replaced_in_situ_module(module_name); - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(module) => { - let old_module_decl = mem::replace(&mut module.module_decl, module_decl.clone()); + if let Some(module) = self.wam_prelude.indices.modules.get_mut(&module_name) { + let old_module_decl = mem::replace(&mut module.module_decl, module_decl.clone()); + + let local_extensible_predicates = mem::replace( + &mut module.local_extensible_predicates, + LocalExtensiblePredicates::with_hasher(FxBuildHasher::default()), + ); - let local_extensible_predicates = mem::replace( - &mut module.local_extensible_predicates, - LocalExtensiblePredicates::with_hasher(FxBuildHasher::default()), + for ((compilation_target, key), skeleton) in local_extensible_predicates.iter() { + self.retract_local_clauses_impl( + *compilation_target, + *key, + &skeleton.clause_clause_locs, ); - for ((compilation_target, key), skeleton) in local_extensible_predicates.iter() { - self.retract_local_clauses_impl( - *compilation_target, - *key, + let is_dynamic = self + .wam_prelude + .indices + .get_predicate_skeleton(compilation_target, key) + .map(|skeleton| skeleton.core.is_dynamic) + .unwrap_or(false); + + if is_dynamic { + let clause_clause_compilation_target = match compilation_target { + CompilationTarget::User => CompilationTarget::Module(atom!("builtins")), + module => *module, + }; + + self.retract_local_clause_clauses( + clause_clause_compilation_target, &skeleton.clause_clause_locs, ); - - let is_dynamic = self - .wam_prelude - .indices - .get_predicate_skeleton(compilation_target, key) - .map(|skeleton| skeleton.core.is_dynamic) - .unwrap_or(false); - - if is_dynamic { - let clause_clause_compilation_target = match compilation_target { - CompilationTarget::User => CompilationTarget::Module(atom!("builtins")), - module => module.clone(), - }; - - self.retract_local_clause_clauses( - clause_clause_compilation_target, - &skeleton.clause_clause_locs, - ); - } } - - self.payload - .retraction_info - .push_record(RetractionRecord::ReplacedModule( - old_module_decl, - listing_src.clone(), - local_extensible_predicates, - )); } - None => {} + + self.payload + .retraction_info + .push_record(RetractionRecord::ReplacedModule( + old_module_decl, + listing_src.clone(), + local_extensible_predicates, + )); } } @@ -1180,11 +1154,11 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { } ModuleSource::Library(library) => match LIBRARIES.borrow().get(&*library.as_str()) { Some(code) => { - if let Some(ref module) = self.wam_prelude.indices.modules.get(&library) { + if let Some(module) = self.wam_prelude.indices.modules.get(&library) { if let ListingSource::DynamicallyGenerated = &module.listing_src { ( Stream::from_static_string( - *code, + code, &mut LS::machine_st(&mut self.payload).arena, ), ListingSource::User, @@ -1195,7 +1169,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { } else { ( Stream::from_static_string( - *code, + code, &mut LS::machine_st(&mut self.payload).arena, ), ListingSource::User, @@ -1266,7 +1240,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { } else { ( Stream::from_static_string( - *code, + code, &mut LS::machine_st(&mut self.payload).arena, ), ListingSource::User, diff --git a/src/machine/loader.rs b/src/machine/loader.rs index 07ba30785..c795c973f 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -19,7 +19,6 @@ use std::cell::Cell; use std::collections::VecDeque; use std::convert::TryFrom; use std::fmt; -use std::mem; use std::ops::{Deref, DerefMut}; /* @@ -136,7 +135,7 @@ impl RetractionInfo { Self { orig_code_extent, - records: mem::replace(&mut self.records, vec![]), + records: std::mem::take(&mut self.records), } } } @@ -207,8 +206,8 @@ impl PredicateQueue { #[inline] pub(super) fn take(&mut self) -> Self { Self { - predicates: mem::replace(&mut self.predicates, vec![]), - compilation_target: self.compilation_target.clone(), + predicates: std::mem::take(&mut self.predicates), + compilation_target: self.compilation_target, } } @@ -404,7 +403,7 @@ impl<'a> LoadState<'a> for BootstrappingLoadState<'a> { #[inline(always)] fn machine_st(loader: &mut Self::LoaderFieldType) -> &mut MachineState { - &mut loader.term_stream.parser.lexer.machine_st + loader.term_stream.parser.lexer.machine_st } #[inline(always)] @@ -467,7 +466,7 @@ impl<'a> LoadState<'a> for InlineLoadState<'a> { #[inline(always)] fn machine_st(load_state: &mut Self::LoaderFieldType) -> &mut MachineState { - &mut load_state.machine_st + load_state.machine_st } #[inline(always)] @@ -639,22 +638,19 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { RetractionRecord::AddedDiscontiguousPredicate(compilation_target, key) => { match compilation_target { CompilationTarget::User => { - self.wam_prelude - .indices - .extensible_predicates - .get_mut(&key) - .map(|skeleton| { - skeleton.core.is_discontiguous = false; - }); + if let Some(skeleton) = + self.wam_prelude.indices.extensible_predicates.get_mut(&key) + { + skeleton.core.is_discontiguous = false; + } } CompilationTarget::Module(module_name) => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - module.extensible_predicates.get_mut(&key).map(|skeleton| { - skeleton.core.is_discontiguous = false; - }); + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&module_name) + { + if let Some(skeleton) = module.extensible_predicates.get_mut(&key) { + skeleton.core.is_discontiguous = false; } - None => {} } } } @@ -662,23 +658,20 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { RetractionRecord::AddedDynamicPredicate(compilation_target, key) => { match compilation_target { CompilationTarget::User => { - self.wam_prelude - .indices - .extensible_predicates - .get_mut(&key) - .map(|skeleton| { - skeleton.core.is_dynamic = false; - }); + if let Some(skeleton) = + self.wam_prelude.indices.extensible_predicates.get_mut(&key) + { + skeleton.core.is_dynamic = false; + } } CompilationTarget::Module(module_name) => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - module.extensible_predicates.get_mut(&key).map(|skeleton| { - skeleton.core.is_dynamic = false; - skeleton.core.retracted_dynamic_clauses = None; - }); - } - None => {} + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&module_name) + { + if let Some(skeleton) = module.extensible_predicates.get_mut(&key) { + skeleton.core.is_dynamic = false; + skeleton.core.retracted_dynamic_clauses = None; + }; } } } @@ -686,60 +679,52 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { RetractionRecord::AddedMultifilePredicate(compilation_target, key) => { match compilation_target { CompilationTarget::User => { - self.wam_prelude - .indices - .extensible_predicates - .get_mut(&key) - .map(|skeleton| { - skeleton.core.is_multifile = false; - }); + if let Some(skeleton) = + self.wam_prelude.indices.extensible_predicates.get_mut(&key) + { + skeleton.core.is_multifile = false; + } } CompilationTarget::Module(module_name) => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - module.extensible_predicates.get_mut(&key).map(|skeleton| { - skeleton.core.is_multifile = false; - }); + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&module_name) + { + if let Some(skeleton) = module.extensible_predicates.get_mut(&key) { + skeleton.core.is_multifile = false; } - None => {} } } } } RetractionRecord::AddedModuleOp(module_name, mut op_decl) => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - op_decl.remove(&mut module.op_dir); - } - None => {} + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&module_name) + { + op_decl.remove(&mut module.op_dir); } } RetractionRecord::ReplacedModuleOp(module_name, mut op_decl, op_desc) => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - op_decl.op_desc = op_desc; - op_decl.insert_into_op_dir(&mut module.op_dir); - } - None => {} + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&module_name) + { + op_decl.op_desc = op_desc; + op_decl.insert_into_op_dir(&mut module.op_dir); } } RetractionRecord::AddedModulePredicate(module_name, key) => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - module.code_dir.remove(&key); - } - None => {} + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&module_name) + { + module.code_dir.remove(&key); } } RetractionRecord::ReplacedModulePredicate(module_name, key, old_code_idx) => { - match self.wam_prelude.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - module - .code_dir - .get_mut(&key) - .map(|code_idx| code_idx.set(old_code_idx)); + if let Some(ref mut module) = + self.wam_prelude.indices.modules.get_mut(&module_name) + { + if let Some(code_idx) = module.code_dir.get_mut(&key) { + code_idx.set(old_code_idx) } - None => {} } } RetractionRecord::AddedExtensiblePredicate(compilation_target, key) => { @@ -758,11 +743,9 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { self.wam_prelude.indices.code_dir.remove(&key); } RetractionRecord::ReplacedUserPredicate(key, old_code_idx) => { - self.wam_prelude - .indices - .code_dir - .get_mut(&key) - .map(|code_idx| code_idx.set(old_code_idx)); + if let Some(code_idx) = self.wam_prelude.indices.code_dir.get_mut(&key) { + code_idx.set(old_code_idx) + } } RetractionRecord::AddedIndex(index_key, clause_loc) => { if let Some(index_loc) = index_key.switch_on_term_loc() { @@ -832,20 +815,17 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { }; } RetractionRecord::ReplacedSwitchOnTermVarIndex(index_loc, old_v) => { - match self.wam_prelude.code[index_loc] { - Instruction::IndexingCode(ref mut indexing_code) => { - match &mut indexing_code[0] { - IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm( - _, - ref mut v, - .., - )) => { - *v = old_v; - } - _ => {} - } + if let Instruction::IndexingCode(ref mut indexing_code) = + self.wam_prelude.code[index_loc] + { + if let IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm( + _, + ref mut v, + .., + )) = &mut indexing_code[0] + { + *v = old_v; } - _ => {} } } RetractionRecord::ModifiedTryMeElse(instr_loc, o) => { @@ -858,30 +838,24 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { self.wam_prelude.code[instr_loc] = Instruction::RevJmpBy(o); } RetractionRecord::SkeletonClausePopBack(compilation_target, key) => { - match self + if let Some(skeleton) = self .wam_prelude .indices .get_predicate_skeleton_mut(&compilation_target, &key) { - Some(skeleton) => { - skeleton.clauses.pop_back(); - skeleton.core.clause_clause_locs.pop_back(); - } - None => {} + skeleton.clauses.pop_back(); + skeleton.core.clause_clause_locs.pop_back(); } } RetractionRecord::SkeletonClausePopFront(compilation_target, key) => { - match self + if let Some(skeleton) = self .wam_prelude .indices .get_predicate_skeleton_mut(&compilation_target, &key) { - Some(skeleton) => { - skeleton.clauses.pop_front(); - skeleton.core.clause_clause_locs.pop_front(); - skeleton.core.clause_assert_margin -= 1; - } - None => {} + skeleton.clauses.pop_front(); + skeleton.core.clause_clause_locs.pop_front(); + skeleton.core.clause_assert_margin -= 1; } } RetractionRecord::SkeletonLocalClauseClausePopFront( @@ -891,16 +865,15 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { ) => { let listing_src_file_name = self.listing_src_file_name(); - match self.wam_prelude.indices.get_local_predicate_skeleton_mut( - src_compilation_target, - local_compilation_target, - listing_src_file_name, - key, - ) { - Some(skeleton) => { - skeleton.clause_clause_locs.pop_front(); - } - None => {} + if let Some(skeleton) = + self.wam_prelude.indices.get_local_predicate_skeleton_mut( + src_compilation_target, + local_compilation_target, + listing_src_file_name, + key, + ) + { + skeleton.clause_clause_locs.pop_front(); } } RetractionRecord::SkeletonLocalClauseClausePopBack( @@ -910,16 +883,15 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { ) => { let listing_src_file_name = self.listing_src_file_name(); - match self.wam_prelude.indices.get_local_predicate_skeleton_mut( - src_compilation_target, - local_compilation_target, - listing_src_file_name, - key, - ) { - Some(skeleton) => { - skeleton.clause_clause_locs.pop_back(); - } - None => {} + if let Some(skeleton) = + self.wam_prelude.indices.get_local_predicate_skeleton_mut( + src_compilation_target, + local_compilation_target, + listing_src_file_name, + key, + ) + { + skeleton.clause_clause_locs.pop_back(); } } RetractionRecord::SkeletonLocalClauseTruncateBack( @@ -930,29 +902,25 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { ) => { let listing_src_file_name = self.listing_src_file_name(); - match self.wam_prelude.indices.get_local_predicate_skeleton_mut( - src_compilation_target, - local_compilation_target, - listing_src_file_name, - key, - ) { - Some(skeleton) => { - skeleton.clause_clause_locs.truncate(len); - } - None => {} + if let Some(skeleton) = + self.wam_prelude.indices.get_local_predicate_skeleton_mut( + src_compilation_target, + local_compilation_target, + listing_src_file_name, + key, + ) + { + skeleton.clause_clause_locs.truncate(len); } } RetractionRecord::SkeletonClauseTruncateBack(compilation_target, key, len) => { - match self + if let Some(skeleton) = self .wam_prelude .indices .get_predicate_skeleton_mut(&compilation_target, &key) { - Some(skeleton) => { - skeleton.clauses.truncate(len); - skeleton.core.clause_clause_locs.truncate(len); - } - None => {} + skeleton.clauses.truncate(len); + skeleton.core.clause_clause_locs.truncate(len); } } RetractionRecord::SkeletonClauseStartReplaced( @@ -961,15 +929,12 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { target_pos, clause_start, ) => { - match self + if let Some(skeleton) = self .wam_prelude .indices .get_predicate_skeleton_mut(&compilation_target, &key) { - Some(skeleton) => { - skeleton.clauses[target_pos].clause_start = clause_start; - } - None => {} + skeleton.clauses[target_pos].clause_start = clause_start; } } RetractionRecord::RemovedDynamicSkeletonClause( @@ -978,26 +943,22 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { target_pos, clause_clause_loc, ) => { - match self + if let Some(skeleton) = self .wam_prelude .indices .get_predicate_skeleton_mut(&compilation_target, &key) { - Some(skeleton) => { - if let Some(removed_clauses) = - &mut skeleton.core.retracted_dynamic_clauses - { - let clause_index_info = removed_clauses.pop().unwrap(); + if let Some(removed_clauses) = &mut skeleton.core.retracted_dynamic_clauses + { + let clause_index_info = removed_clauses.pop().unwrap(); - skeleton - .core - .clause_clause_locs - .insert(target_pos, clause_clause_loc); + skeleton + .core + .clause_clause_locs + .insert(target_pos, clause_clause_loc); - skeleton.clauses.insert(target_pos, clause_index_info); - } + skeleton.clauses.insert(target_pos, clause_index_info); } - None => {} } } RetractionRecord::RemovedSkeletonClause( @@ -1007,19 +968,16 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { clause_index_info, clause_clause_loc, ) => { - match self + if let Some(skeleton) = self .wam_prelude .indices .get_predicate_skeleton_mut(&compilation_target, &key) { - Some(skeleton) => { - skeleton - .core - .clause_clause_locs - .insert(target_pos, clause_clause_loc); - skeleton.clauses.insert(target_pos, clause_index_info); - } - None => {} + skeleton + .core + .clause_clause_locs + .insert(target_pos, clause_clause_loc); + skeleton.clauses.insert(target_pos, clause_index_info); } } RetractionRecord::ReplacedIndexingLine(index_loc, indexing_code) => { @@ -1033,14 +991,15 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { ) => { let listing_src_file_name = self.listing_src_file_name(); - match self.wam_prelude.indices.get_local_predicate_skeleton_mut( - compilation_target, - local_compilation_target, - listing_src_file_name, - key, - ) { - Some(skeleton) => skeleton.clause_clause_locs = clause_locs, - None => {} + if let Some(skeleton) = + self.wam_prelude.indices.get_local_predicate_skeleton_mut( + compilation_target, + local_compilation_target, + listing_src_file_name, + key, + ) + { + skeleton.clause_clause_locs = clause_locs } } RetractionRecord::RemovedSkeleton(compilation_target, key, skeleton) => { @@ -1091,7 +1050,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { let export_list = machine_st.read_term_from_heap(cell); let atom_tbl = &mut LS::machine_st(&mut self.payload).atom_tbl; - let export_list = setup_module_export_list(export_list, &atom_tbl)?; + let export_list = setup_module_export_list(export_list, atom_tbl)?; Ok(export_list.into_iter().collect()) } @@ -1363,7 +1322,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> { *key, ) { Some(skeleton) if !skeleton.clause_clause_locs.is_empty() => { - mem::replace(&mut skeleton.clause_clause_locs, VecDeque::new()) + std::mem::take(&mut skeleton.clause_clause_locs) } _ => return, }; @@ -1400,9 +1359,7 @@ impl<'a> MachinePreludeView<'a> { CompilationTarget::User => CompositeOpDir::new(&self.indices.op_dir, None), CompilationTarget::Module(ref module_name) => { match self.indices.modules.get(module_name) { - Some(ref module) => { - CompositeOpDir::new(&self.indices.op_dir, Some(&module.op_dir)) - } + Some(module) => CompositeOpDir::new(&self.indices.op_dir, Some(&module.op_dir)), None => { unreachable!() } @@ -1413,13 +1370,10 @@ impl<'a> MachinePreludeView<'a> { } impl MachineState { - pub(super) fn read_term_from_heap( - &mut self, - term_addr: HeapCellValue, - ) -> Term { + pub(super) fn read_term_from_heap(&mut self, term_addr: HeapCellValue) -> Term { let mut term_stack = vec![]; - let mut iter = stackful_post_order_iter:: - (&mut self.heap, &mut self.stack, term_addr); + let mut iter = + stackful_post_order_iter::(&mut self.heap, &mut self.stack, term_addr); while let Some(addr) = iter.next() { let addr = unmark_cell_bits!(addr); @@ -1652,10 +1606,10 @@ impl Machine { let arity = self.deref_register(3); let arity = match Number::try_from(arity) { - Ok(Number::Integer(n)) if &*n >= &Integer::ZERO && &*n <= &Integer::from(MAX_ARITY) => { + Ok(Number::Integer(n)) if *n >= Integer::ZERO && *n <= Integer::from(MAX_ARITY) => { let value: usize = (&*n).try_into().unwrap(); Ok(value) - }, + } Ok(Number::Fixnum(n)) if n.get_num() >= 0 && n.get_num() <= MAX_ARITY as i64 => { Ok(usize::try_from(n.get_num()).unwrap()) } @@ -1770,14 +1724,11 @@ impl Machine { &ListingSource::DynamicallyGenerated, ); - match loader.wam_prelude.indices.modules.get_mut(&module_name) { - Some(module) => { - for (key, value) in module.op_dir.drain(0..) { - let mut op_decl = OpDecl::new(value, key.0); - op_decl.remove(&mut loader.wam_prelude.indices.op_dir); - } + if let Some(module) = loader.wam_prelude.indices.modules.get_mut(&module_name) { + for (key, value) in module.op_dir.drain(0..) { + let mut op_decl = OpDecl::new(value, key.0); + op_decl.remove(&mut loader.wam_prelude.indices.op_dir); } - None => {} } } } @@ -1789,10 +1740,10 @@ impl Machine { self.restore_load_state_payload(result) } - pub(crate) fn loader_from_heap_evacuable<'a>( - &'a mut self, + pub(crate) fn loader_from_heap_evacuable( + &mut self, r: RegType, - ) -> Loader<'a, LiveLoadAndMachineState<'a>> { + ) -> Loader<'_, LiveLoadAndMachineState<'_>> { let mut load_state = cell_as_load_state_payload!(self .machine_st .store(self.machine_st.deref(self.machine_st[r]))); @@ -1868,7 +1819,7 @@ impl Machine { let path = cell_as_atom!(self.deref_register(2)); self.load_contexts - .push(LoadContext::new(&*path.as_str(), stream)); + .push(LoadContext::new(&path.as_str(), stream)); Ok(()) } @@ -2021,8 +1972,8 @@ impl Machine { loader.payload.compilation_target = compilation_target; - let head = LiveLoadAndMachineState::machine_st(&mut loader.payload) - .read_term_from_heap(head); + let head = + LiveLoadAndMachineState::machine_st(&mut loader.payload).read_term_from_heap(head); let name = if let Some(name) = head.name() { name @@ -2218,7 +2169,7 @@ impl Machine { Ok(Number::Integer(n)) => { let value: usize = (&*n).try_into().unwrap(); value - }, + } Ok(Number::Fixnum(n)) => usize::try_from(n.get_num()).unwrap(), _ => unreachable!(), }; @@ -2520,7 +2471,7 @@ pub(super) fn load_module( import_module_exports::( &mut payload, - &compilation_target, + compilation_target, module, code_dir, op_dir, diff --git a/src/machine/machine_errors.rs b/src/machine/machine_errors.rs index 9a9d6cbf0..f509b46c9 100644 --- a/src/machine/machine_errors.rs +++ b/src/machine/machine_errors.rs @@ -80,7 +80,7 @@ impl ValidType { #[derive(Debug, Clone, Copy)] pub(crate) enum ResourceError { FiniteMemory(HeapCellValue), - OutOfFiles + OutOfFiles, } pub(crate) trait TypeError { @@ -170,7 +170,11 @@ impl PermissionError for Atom { ) -> MachineError { let stub = functor!( atom!("permission_error"), - [atom(perm.as_atom()), atom(index_atom), cell(atom_as_cell!(self))] + [ + atom(perm.as_atom()), + atom(index_atom), + cell(atom_as_cell!(self)) + ] ); MachineError { @@ -319,10 +323,7 @@ impl MachineState { ) } ResourceError::OutOfFiles => { - functor!( - atom!("resource_error"), - [atom(atom!("file_descriptors"))] - ) + functor!(atom!("resource_error"), [atom(atom!("file_descriptors"))]) } }; @@ -355,13 +356,21 @@ impl MachineState { from: ErrorProvenance::Received, } } - ExistenceError::QualifiedProcedure { module_name, name, arity } => { + ExistenceError::QualifiedProcedure { + module_name, + name, + arity, + } => { let h = self.heap.len(); let ind_stub = functor!(atom!("/"), [atom(name), fixnum(arity)]); let res_stub = functor!(atom!(":"), [atom(module_name), str(h + 3, 0)], [ind_stub]); - let stub = functor!(atom!("existence_error"), [atom(atom!("procedure")), str(h, 0)], [res_stub]); + let stub = functor!( + atom!("existence_error"), + [atom(atom!("procedure")), str(h, 0)], + [res_stub] + ); MachineError { stub, @@ -472,21 +481,15 @@ impl MachineState { pub(super) fn session_error(&mut self, err: SessionError) -> MachineError { match err { - SessionError::CannotOverwriteBuiltIn(key) => { - self.permission_error( - Permission::Modify, - atom!("static_procedure"), - functor_stub(key.0, key.1) - .into_iter() - .collect::(), - ) - } + SessionError::CannotOverwriteBuiltIn(key) => self.permission_error( + Permission::Modify, + atom!("static_procedure"), + functor_stub(key.0, key.1) + .into_iter() + .collect::(), + ), SessionError::CannotOverwriteBuiltInModule(module) => { - self.permission_error( - Permission::Modify, - atom!("static_module"), - module, - ) + self.permission_error(Permission::Modify, atom!("static_module"), module) } SessionError::ExistenceError(err) => self.existence_error(err), SessionError::ModuleDoesNotContainExport(..) => { @@ -641,7 +644,7 @@ impl MachineState { self.ball.boundary = 0; self.ball.stub.truncate(0); - self.heap.extend(err.into_iter()); + self.heap.extend(err); self.registers[1] = if err_len == 1 { heap_loc_as_cell!(h) @@ -705,58 +708,58 @@ impl From for CompilationError { impl CompilationError { pub(crate) fn line_and_col_num(&self) -> Option<(usize, usize)> { match self { - &CompilationError::ParserError(ref err) => err.line_and_col_num(), + CompilationError::ParserError(err) => err.line_and_col_num(), _ => None, } } pub(crate) fn as_functor(&self) -> MachineStub { match self { - &CompilationError::Arithmetic(..) => { + CompilationError::Arithmetic(..) => { functor!(atom!("arithmetic_error")) } - &CompilationError::CannotParseCyclicTerm => { + CompilationError::CannotParseCyclicTerm => { functor!(atom!("cannot_parse_cyclic_term")) } - &CompilationError::ExceededMaxArity => { + CompilationError::ExceededMaxArity => { functor!(atom!("exceeded_max_arity")) } - &CompilationError::ExpectedRel => { + CompilationError::ExpectedRel => { functor!(atom!("expected_relation")) } - &CompilationError::InadmissibleFact => { + CompilationError::InadmissibleFact => { // TODO: type_error(callable, _). functor!(atom!("inadmissible_fact")) } - &CompilationError::InadmissibleQueryTerm => { + CompilationError::InadmissibleQueryTerm => { // TODO: type_error(callable, _). functor!(atom!("inadmissible_query_term")) } - &CompilationError::InconsistentEntry => { + CompilationError::InconsistentEntry => { functor!(atom!("inconsistent_entry")) } - &CompilationError::InvalidMetaPredicateDecl => { + CompilationError::InvalidMetaPredicateDecl => { functor!(atom!("invalid_meta_predicate_decl")) } - &CompilationError::InvalidModuleDecl => { + CompilationError::InvalidModuleDecl => { functor!(atom!("invalid_module_declaration")) } - &CompilationError::InvalidModuleExport => { + CompilationError::InvalidModuleExport => { functor!(atom!("invalid_module_export")) } - &CompilationError::InvalidModuleResolution(ref module_name) => { + CompilationError::InvalidModuleResolution(ref module_name) => { functor!(atom!("no_such_module"), [atom(module_name)]) } - &CompilationError::InvalidRuleHead => { + CompilationError::InvalidRuleHead => { functor!(atom!("invalid_head_of_rule")) // TODO: type_error(callable, _). } - &CompilationError::InvalidUseModuleDecl => { + CompilationError::InvalidUseModuleDecl => { functor!(atom!("invalid_use_module_declaration")) } - &CompilationError::ParserError(ref err) => { + CompilationError::ParserError(ref err) => { functor!(err.as_atom()) } - &CompilationError::UnreadableTerm => { + CompilationError::UnreadableTerm => { functor!(atom!("unreadable_term")) } } @@ -986,7 +989,11 @@ pub enum ExistenceError { Module(Atom), ModuleSource(ModuleSource), Procedure(Atom, usize), - QualifiedProcedure { module_name: Atom, name: Atom, arity: usize }, + QualifiedProcedure { + module_name: Atom, + name: Atom, + arity: usize, + }, SourceSink(HeapCellValue), Stream(HeapCellValue), } diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index 84dbdbd70..e5fdc9558 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -118,18 +118,12 @@ impl IndexPtr { #[inline(always)] pub(crate) fn is_undefined(&self) -> bool { - match self.tag() { - IndexPtrTag::Undefined => true, - _ => false, - } + matches!(self.tag(), IndexPtrTag::Undefined) } #[inline(always)] pub(crate) fn is_dynamic_undefined(&self) -> bool { - match self.tag() { - IndexPtrTag::DynamicUndefined => true, - _ => false, - } + matches!(self.tag(), IndexPtrTag::DynamicUndefined) } } @@ -231,6 +225,7 @@ pub enum VarKey { } impl VarKey { + #[allow(clippy::inherent_to_string)] #[inline] pub(crate) fn to_string(&self) -> String { match self { @@ -241,11 +236,7 @@ impl VarKey { #[inline(always)] pub(crate) fn is_anon(&self) -> bool { - if let VarKey::AnonVar(_) = self { - true - } else { - false - } + matches!(self, VarKey::AnonVar(_)) } } @@ -429,9 +420,9 @@ impl IndexStore { match compilation_target { CompilationTarget::User => self.meta_predicates.get(&(name, arity)), CompilationTarget::Module(ref module_name) => match self.modules.get(module_name) { - Some(ref module) => module + Some(module) => module .meta_predicates - .get(&(name.clone(), arity)) + .get(&(name, arity)) .or_else(|| self.meta_predicates.get(&(name, arity))), None => self.meta_predicates.get(&(name, arity)), }, @@ -446,7 +437,7 @@ impl IndexStore { .map(|skeleton| skeleton.core.is_dynamic) .unwrap_or(false), _ => match self.modules.get(&module_name) { - Some(ref module) => module + Some(module) => module .extensible_predicates .get(&key) .map(|skeleton| skeleton.core.is_dynamic) diff --git a/src/machine/machine_state.rs b/src/machine/machine_state.rs index 94e85af87..7f682e0a7 100644 --- a/src/machine/machine_state.rs +++ b/src/machine/machine_state.rs @@ -413,7 +413,7 @@ impl MachineState { } pub(crate) fn increment_call_count(&mut self) -> bool { - if self.cwil.inference_limit_exceeded || self.ball.stub.len() > 0 { + if self.cwil.inference_limit_exceeded || !self.ball.stub.is_empty() { return true; } @@ -590,7 +590,9 @@ impl MachineState { let mut singleton_var_set: IndexMap = IndexMap::new(); - for cell in stackful_preorder_iter::(&mut self.heap, &mut self.stack, heap_loc) { + for cell in + stackful_preorder_iter::(&mut self.heap, &mut self.stack, heap_loc) + { let cell = unmark_cell_bits!(cell); if let Some(var) = cell.as_var() { @@ -644,10 +646,8 @@ impl MachineState { ) -> Result { self.eof_action(self.registers[2], stream, atom!("read_term"), 3)?; - if stream.options().eof_action() == EOFAction::Reset { - if self.fail == false { - return Ok(OnEOF::Continue); - } + if stream.options().eof_action() == EOFAction::Reset && !self.fail { + return Ok(OnEOF::Continue); } Ok(OnEOF::Return) @@ -674,10 +674,10 @@ impl MachineState { if let Stream::Byte(_) = stream { return self.read_term( - stream, - indices, - MachineState::read_term_from_user_input_eof_handler - ) + stream, + indices, + MachineState::read_term_from_user_input_eof_handler, + ); } unreachable!("Stream must be a Stream::Readline(_)") @@ -691,10 +691,8 @@ impl MachineState { } else if stream.past_end_of_stream() { self.eof_action(self.registers[2], stream, atom!("read_term"), 3)?; - if stream.options().eof_action() == EOFAction::Reset { - if self.fail == false { - return Ok(OnEOF::Continue); - } + if stream.options().eof_action() == EOFAction::Reset && !self.fail { + return Ok(OnEOF::Continue); } } @@ -716,11 +714,7 @@ impl MachineState { )?; if stream.past_end_of_stream() { - if EOFAction::Reset != stream.options().eof_action() { - return Ok(()); - } else if self.fail { - return Ok(()); - } + return Ok(()); } loop { @@ -970,6 +964,7 @@ impl MachineState { } } +#[allow(clippy::upper_case_acronyms)] #[derive(Debug)] pub(crate) struct CWIL { count: Integer, diff --git a/src/machine/machine_state_impl.rs b/src/machine/machine_state_impl.rs index 5007e85a6..772a96420 100644 --- a/src/machine/machine_state_impl.rs +++ b/src/machine/machine_state_impl.rs @@ -149,7 +149,7 @@ impl MachineState { TrailRef::BlackboardEntry(key_atom) => { self.trail.push(TrailEntry::build_with( TrailEntryTag::TrailedBlackboardEntry, - key_atom.index as u64, + key_atom.index, )); self.tr += 1; @@ -157,7 +157,7 @@ impl MachineState { TrailRef::BlackboardOffset(key_atom, value_cell) => { self.trail.push(TrailEntry::build_with( TrailEntryTag::TrailedBlackboardOffset, - key_atom.index as u64, + key_atom.index, )); self.trail @@ -432,8 +432,7 @@ impl MachineState { pub fn compare_term_test(&mut self, var_comparison: VarComparison) -> Option { let mut tabu_list = IndexSet::new(); - while !self.pdl.is_empty() { - let s1 = self.pdl.pop().unwrap(); + while let Some(s1) = self.pdl.pop() { let s1 = self.deref(s1); let s2 = self.pdl.pop().unwrap(); @@ -896,7 +895,7 @@ impl MachineState { let s = string.as_str(); - match heap_pstr_iter.compare_pstr_to_string(&*s) { + match heap_pstr_iter.compare_pstr_to_string(&s) { Some(PStrPrefixCmpResult { focus, offset, @@ -1142,7 +1141,7 @@ impl MachineState { let cycle_found = { let mut iter = cycle_detecting_stackless_preorder_iter(&mut self.heap, h); - while let Some(_) = iter.next() {} + for _ in iter.by_ref() {} iter.cycle_found() }; @@ -1376,7 +1375,7 @@ impl MachineState { let mut type_error = |arity| { let err = self.type_error(ValidType::Integer, arity); - return Err(self.error_form(err, stub_gen())); + Err(self.error_form(err, stub_gen())) }; let arity = match Number::try_from(arity) { @@ -1573,7 +1572,7 @@ impl MachineState { ) -> Result, MachineStub> { let mut heap_pstr_iter = HeapPStrIter::new(&self.heap, h); - while let Some(iteratee) = heap_pstr_iter.next() { + for iteratee in heap_pstr_iter.by_ref() { match iteratee { PStrIteratee::Char(_, c) => chars.push(char_as_cell!(c)), PStrIteratee::PStrSegment(_, pstr_atom, n) => { @@ -1644,10 +1643,11 @@ impl MachineState { let addr = self.store(self.deref(addr)); match Number::try_from(addr) { - Ok(Number::Fixnum(n)) => match u8::try_from(n.get_num()) { - Ok(b) => bytes.push(b), - Err(_) => {} - }, + Ok(Number::Fixnum(n)) => { + if let Ok(b) = u8::try_from(n.get_num()) { + bytes.push(b) + } + } Ok(Number::Integer(n)) => { let b: u8 = (&*n).try_into().unwrap(); diff --git a/src/machine/mock_wam.rs b/src/machine/mock_wam.rs index 4b3de5ed6..94eab6ff2 100644 --- a/src/machine/mock_wam.rs +++ b/src/machine/mock_wam.rs @@ -82,6 +82,12 @@ impl MockWAM { } } +impl Default for MockWAM { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] pub struct TermCopyingMockWAM<'a> { pub wam: &'a mut MockWAM, @@ -109,14 +115,14 @@ impl<'a> Deref for TermCopyingMockWAM<'a> { type Target = MockWAM; fn deref(&self) -> &Self::Target { - &self.wam + self.wam } } #[cfg(test)] impl<'a> DerefMut for TermCopyingMockWAM<'a> { fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.wam + self.wam } } @@ -165,9 +171,8 @@ impl<'a> CopierTarget for TermCopyingMockWAM<'a> { #[cfg(test)] pub fn all_cells_marked_and_unforwarded(heap: &[HeapCellValue]) { for (idx, cell) in heap.iter().enumerate() { - assert_eq!( + assert!( cell.get_mark_bit(), - true, "cell {:?} at index {} is not marked", cell, idx @@ -230,20 +235,16 @@ impl Machine { &mut self.machine_st.arena, ); - self.load_file(file.into(), stream); + self.load_file(file, stream); self.user_output.bytes().map(|b| b.unwrap()).collect() } pub fn test_load_string(&mut self, code: &str) -> Vec { - let stream = Stream::from_owned_string( - code.to_owned(), - &mut self.machine_st.arena, - ); + let stream = Stream::from_owned_string(code.to_owned(), &mut self.machine_st.arena); - self.load_file("".into(), stream); + self.load_file("", stream); self.user_output.bytes().map(|b| b.unwrap()).collect() } - } #[cfg(test)] diff --git a/src/machine/mod.rs b/src/machine/mod.rs index b4f1687e9..6801575df 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -53,6 +53,8 @@ use indexmap::IndexMap; use lazy_static::lazy_static; use ordered_float::OrderedFloat; +use rand::rngs::StdRng; +use rand::SeedableRng; use std::cmp::Ordering; use std::env; use std::io::Read; @@ -61,8 +63,6 @@ use std::sync::atomic::AtomicBool; use self::config::MachineConfig; use self::parsed_results::*; -use rand::rngs::StdRng; -use rand::SeedableRng; lazy_static! { pub static ref INTERRUPT: AtomicBool = AtomicBool::new(false); @@ -172,7 +172,7 @@ pub(crate) fn import_builtin_impls(code_dir: &CodeDir, builtins: &mut Module) { for key in keys { let idx = code_dir.get(&key).unwrap(); - builtins.code_dir.insert(key, idx.clone()); + builtins.code_dir.insert(key, *idx); builtins .module_decl .exports @@ -225,7 +225,7 @@ impl Machine { key: PredicateKey, ) -> std::process::ExitCode { if let Some(module) = self.indices.modules.get(&module_name) { - if let Some(ref code_index) = module.code_dir.get(&key) { + if let Some(code_index) = module.code_dir.get(&key) { let p = code_index.local().unwrap(); self.machine_st.cp = BREAK_FROM_DISPATCH_LOOP_LOC; @@ -252,9 +252,7 @@ impl Machine { path_buf.push("src/toplevel.pl"); let path = path_buf.to_str().unwrap(); - let toplevel_stream = - Stream::from_static_string(program, &mut self.machine_st.arena); - + let toplevel_stream = Stream::from_static_string(program, &mut self.machine_st.arena); self.load_file(path, toplevel_stream); @@ -300,7 +298,11 @@ impl Machine { } } - pub fn run_top_level(&mut self, module_name: Atom, key: PredicateKey) -> std::process::ExitCode { + pub fn run_top_level( + &mut self, + module_name: Atom, + key: PredicateKey, + ) -> std::process::ExitCode { let mut arg_pstrs = vec![]; for arg in env::args() { @@ -400,57 +402,51 @@ impl Machine { pub(crate) fn add_impls_to_indices(&mut self) { let impls_offset = self.code.len() + 4; - self.code.extend( - vec![ - Instruction::BreakFromDispatchLoop, - Instruction::InstallVerifyAttr, - Instruction::VerifyAttrInterrupt, - Instruction::BreakFromDispatchLoop, // the location of LIB_QUERY_SUCCESS - Instruction::ExecuteTermGreaterThan, - Instruction::ExecuteTermLessThan, - Instruction::ExecuteTermGreaterThanOrEqual, - Instruction::ExecuteTermLessThanOrEqual, - Instruction::ExecuteTermEqual, - Instruction::ExecuteTermNotEqual, - Instruction::ExecuteNumberGreaterThan(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), - Instruction::ExecuteNumberLessThan(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), - Instruction::ExecuteNumberGreaterThanOrEqual( - ar_reg!(temp_v!(1)), - ar_reg!(temp_v!(2)), - ), - Instruction::ExecuteNumberLessThanOrEqual(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), - Instruction::ExecuteNumberEqual(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), - Instruction::ExecuteNumberNotEqual(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), - Instruction::ExecuteIs(temp_v!(1), ar_reg!(temp_v!(2))), - Instruction::ExecuteAcyclicTerm, - Instruction::ExecuteArg, - Instruction::ExecuteCompare, - Instruction::ExecuteCopyTerm, - Instruction::ExecuteFunctor, - Instruction::ExecuteGround, - Instruction::ExecuteKeySort, - Instruction::ExecuteSort, - Instruction::ExecuteN(1), - Instruction::ExecuteN(2), - Instruction::ExecuteN(3), - Instruction::ExecuteN(4), - Instruction::ExecuteN(5), - Instruction::ExecuteN(6), - Instruction::ExecuteN(7), - Instruction::ExecuteN(8), - Instruction::ExecuteN(9), - Instruction::ExecuteIsAtom(temp_v!(1)), - Instruction::ExecuteIsAtomic(temp_v!(1)), - Instruction::ExecuteIsCompound(temp_v!(1)), - Instruction::ExecuteIsInteger(temp_v!(1)), - Instruction::ExecuteIsNumber(temp_v!(1)), - Instruction::ExecuteIsRational(temp_v!(1)), - Instruction::ExecuteIsFloat(temp_v!(1)), - Instruction::ExecuteIsNonVar(temp_v!(1)), - Instruction::ExecuteIsVar(temp_v!(1)), - ] - .into_iter(), - ); + self.code.extend(vec![ + Instruction::BreakFromDispatchLoop, + Instruction::InstallVerifyAttr, + Instruction::VerifyAttrInterrupt, + Instruction::BreakFromDispatchLoop, // the location of LIB_QUERY_SUCCESS + Instruction::ExecuteTermGreaterThan, + Instruction::ExecuteTermLessThan, + Instruction::ExecuteTermGreaterThanOrEqual, + Instruction::ExecuteTermLessThanOrEqual, + Instruction::ExecuteTermEqual, + Instruction::ExecuteTermNotEqual, + Instruction::ExecuteNumberGreaterThan(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), + Instruction::ExecuteNumberLessThan(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), + Instruction::ExecuteNumberGreaterThanOrEqual(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), + Instruction::ExecuteNumberLessThanOrEqual(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), + Instruction::ExecuteNumberEqual(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), + Instruction::ExecuteNumberNotEqual(ar_reg!(temp_v!(1)), ar_reg!(temp_v!(2))), + Instruction::ExecuteIs(temp_v!(1), ar_reg!(temp_v!(2))), + Instruction::ExecuteAcyclicTerm, + Instruction::ExecuteArg, + Instruction::ExecuteCompare, + Instruction::ExecuteCopyTerm, + Instruction::ExecuteFunctor, + Instruction::ExecuteGround, + Instruction::ExecuteKeySort, + Instruction::ExecuteSort, + Instruction::ExecuteN(1), + Instruction::ExecuteN(2), + Instruction::ExecuteN(3), + Instruction::ExecuteN(4), + Instruction::ExecuteN(5), + Instruction::ExecuteN(6), + Instruction::ExecuteN(7), + Instruction::ExecuteN(8), + Instruction::ExecuteN(9), + Instruction::ExecuteIsAtom(temp_v!(1)), + Instruction::ExecuteIsAtomic(temp_v!(1)), + Instruction::ExecuteIsCompound(temp_v!(1)), + Instruction::ExecuteIsInteger(temp_v!(1)), + Instruction::ExecuteIsNumber(temp_v!(1)), + Instruction::ExecuteIsRational(temp_v!(1)), + Instruction::ExecuteIsFloat(temp_v!(1)), + Instruction::ExecuteIsNonVar(temp_v!(1)), + Instruction::ExecuteIsVar(temp_v!(1)), + ]); for (p, instr) in self.code[impls_offset..].iter().enumerate() { let key = instr.to_name_and_arity(); @@ -464,6 +460,7 @@ impl Machine { } } + #[allow(clippy::new_without_default)] pub fn new(config: MachineConfig) -> Self { use ref_thread_local::RefThreadLocal; @@ -1048,7 +1045,7 @@ impl Machine { self.reset_attr_var_state(or_frame.prelude.attr_var_queue_len); self.machine_st.hb = target_h; - self.machine_st.p = self.machine_st.p + offset; + self.machine_st.p += offset; self.machine_st.stack.truncate(b); self.machine_st.heap.truncate(target_h); @@ -1174,21 +1171,23 @@ impl Machine { } else { Err(self.machine_st.throw_undefined_error(name, arity)) } - } else { - if let Some(module) = self.indices.modules.get(&module_name) { - if let Some(idx) = module.code_dir.get(&(name, arity)).cloned() { - self.try_call(name, arity, idx.get()) - } else { - self.undefined_procedure(name, arity) - } + } else if let Some(module) = self.indices.modules.get(&module_name) { + if let Some(idx) = module.code_dir.get(&(name, arity)).cloned() { + self.try_call(name, arity, idx.get()) } else { - let stub = functor_stub(name, arity); - let err = self - .machine_st - .existence_error(ExistenceError::QualifiedProcedure { module_name, name, arity }); - - Err(self.machine_st.error_form(err, stub)) + self.undefined_procedure(name, arity) } + } else { + let stub = functor_stub(name, arity); + let err = self + .machine_st + .existence_error(ExistenceError::QualifiedProcedure { + module_name, + name, + arity, + }); + + Err(self.machine_st.error_form(err, stub)) } } @@ -1202,21 +1201,23 @@ impl Machine { } else { self.undefined_procedure(name, arity) } - } else { - if let Some(module) = self.indices.modules.get(&module_name) { - if let Some(idx) = module.code_dir.get(&(name, arity)).cloned() { - self.try_execute(name, arity, idx.get()) - } else { - self.undefined_procedure(name, arity) - } + } else if let Some(module) = self.indices.modules.get(&module_name) { + if let Some(idx) = module.code_dir.get(&(name, arity)).cloned() { + self.try_execute(name, arity, idx.get()) } else { - let stub = functor_stub(name, arity); - let err = self - .machine_st - .existence_error(ExistenceError::QualifiedProcedure { module_name, name, arity }); - - Err(self.machine_st.error_form(err, stub)) + self.undefined_procedure(name, arity) } + } else { + let stub = functor_stub(name, arity); + let err = self + .machine_st + .existence_error(ExistenceError::QualifiedProcedure { + module_name, + name, + arity, + }); + + Err(self.machine_st.error_form(err, stub)) } } diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 38632855b..24dccc5e6 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -1,6 +1,6 @@ use crate::atom_table::*; -use ordered_float::OrderedFloat; use dashu::*; +use ordered_float::OrderedFloat; use std::collections::BTreeMap; use std::collections::HashMap; @@ -67,13 +67,10 @@ impl From> for QueryResolution { // If there is only one line, and it is an empty match, return true. if query_result_lines.len() == 1 { - match query_result_lines[0].clone() { - QueryResolutionLine::Match(m) => { - if m.is_empty() { - return QueryResolution::True; - } + if let QueryResolutionLine::Match(m) = query_result_lines[0].clone() { + if m.is_empty() { + return QueryResolution::True; } - _ => {} } } @@ -81,13 +78,9 @@ impl From> for QueryResolution { if query_result_lines .iter() .any(|l| l == &QueryResolutionLine::True) - && !query_result_lines.iter().any(|l| { - if let &QueryResolutionLine::Match(_) = l { - true - } else { - false - } - }) + && !query_result_lines + .iter() + .any(|l| matches!(l, QueryResolutionLine::Match(_))) { return QueryResolution::True; } @@ -95,13 +88,7 @@ impl From> for QueryResolution { // If there is at least one match, return all matches. let all_matches = query_result_lines .into_iter() - .filter(|l| { - if let &QueryResolutionLine::Match(_) = l { - true - } else { - false - } - }) + .filter(|l| matches!(l, QueryResolutionLine::Match(_))) .map(|l| match l { QueryResolutionLine::Match(m) => QueryMatch::from(m), _ => unreachable!(), @@ -132,7 +119,11 @@ fn split_response_string(input: &str) -> Vec { ')' => level_parenthesis -= 1, '"' => in_double_quotes = !in_double_quotes, '\'' => in_single_quotes = !in_single_quotes, - ',' if level_bracket == 0 && level_parenthesis == 0 && !in_double_quotes && !in_single_quotes => { + ',' if level_bracket == 0 + && level_parenthesis == 0 + && !in_double_quotes + && !in_single_quotes => + { result.push(input[start..i].trim().to_string()); start = i + 1; } @@ -167,13 +158,13 @@ fn parse_prolog_response(input: &str) -> HashMap { let key = result.0; let value = result.1; // cut off at given characters/strings: - let value = value.split("\n").next().unwrap().to_string(); - let value = value.split(" ").next().unwrap().to_string(); - let value = value.split("\t").next().unwrap().to_string(); + let value = value.split('\n').next().unwrap().to_string(); + let value = value.split(' ').next().unwrap().to_string(); + let value = value.split('\t').next().unwrap().to_string(); let value = value.split("error").next().unwrap().to_string(); map.insert(key, value); } - + map } @@ -192,9 +183,8 @@ impl TryFrom for QueryResolutionLine { Ok((key, Value::try_from(value)?)) }) .filter_map(Result::ok) - .collect::>() - ) - ), + .collect::>(), + )), } } } @@ -229,25 +219,25 @@ impl TryFrom for Value { Ok(Value::Float(OrderedFloat(float_value))) } else if let Ok(int_value) = string.parse::() { Ok(Value::Integer(int_value.into())) - } else if trimmed.starts_with("'") && trimmed.ends_with("'") { - Ok(Value::String(trimmed[1..trimmed.len() - 1].into())) - } else if trimmed.starts_with("\"") && trimmed.ends_with("\"") { + } else if trimmed.starts_with('\'') && trimmed.ends_with('\'') + || trimmed.starts_with('"') && trimmed.ends_with('"') + { Ok(Value::String(trimmed[1..trimmed.len() - 1].into())) - } else if trimmed.starts_with("[") && trimmed.ends_with("]") { + } else if trimmed.starts_with('[') && trimmed.ends_with(']') { let split = split_nested_list(&trimmed[1..trimmed.len() - 1]); - + let values = split .into_iter() .map(Value::try_from) .collect::, _>>()?; Ok(Value::List(values)) - } else if trimmed.starts_with("{") && trimmed.ends_with("}") { - let mut iter = trimmed[1..trimmed.len() - 1].split(","); + } else if trimmed.starts_with('{') && trimmed.ends_with('}') { + let iter = trimmed[1..trimmed.len() - 1].split(','); let mut values = vec![]; - while let Some(value) = iter.next() { - let items: Vec<_> = value.split(":").collect(); + for value in iter { + let items: Vec<_> = value.split(':').collect(); if items.len() == 2 { let _key = items[0].to_string(); let value = items[1].to_string(); @@ -257,11 +247,11 @@ impl TryFrom for Value { Ok(Value::Structure(atom!("{}"), values)) } else if trimmed.starts_with("<<") && trimmed.ends_with(">>") { - let mut iter = trimmed[2..trimmed.len() - 2].split(","); + let iter = trimmed[2..trimmed.len() - 2].split(','); let mut values = vec![]; - while let Some(value) = iter.next() { - let items: Vec<_> = value.split(":").collect(); + for value in iter { + let items: Vec<_> = value.split(':').collect(); if items.len() == 2 { let _key = items[0].to_string(); let value = items[1].to_string(); @@ -270,7 +260,7 @@ impl TryFrom for Value { } Ok(Value::Structure(atom!("<<>>"), values)) - } else if !trimmed.contains(",") && !trimmed.contains("'") && !trimmed.contains("\"") { + } else if !trimmed.contains(',') && !trimmed.contains('\'') && !trimmed.contains('"') { Ok(Value::String(trimmed.into())) } else { Err(()) diff --git a/src/machine/partial_string.rs b/src/machine/partial_string.rs index 8d03eefe8..2123513d3 100644 --- a/src/machine/partial_string.rs +++ b/src/machine/partial_string.rs @@ -34,10 +34,10 @@ impl From for PartialString { } } -impl Into for PartialString { +impl From for Atom { #[inline] - fn into(self: Self) -> Atom { - self.0 + fn from(val: PartialString) -> Self { + val.0 } } @@ -45,7 +45,7 @@ impl PartialString { #[inline] pub(super) fn new<'a>(src: &'a str, atom_tbl: &AtomTable) -> Option<(Self, &'a str)> { let terminator_idx = scan_for_terminator(src.chars()); - let pstr = PartialString(AtomTable::build_with(&atom_tbl, &src[..terminator_idx])); + let pstr = PartialString(AtomTable::build_with(atom_tbl, &src[..terminator_idx])); Some(if terminator_idx < src.as_bytes().len() { (pstr, &src[terminator_idx + 1..]) } else { @@ -154,13 +154,13 @@ impl<'a> HeapPStrIter<'a> { let s = &s[result.prefix_len..]; if s.len() >= t.len() { - if (&*s).starts_with(&*t) { + if s.starts_with(&*t) { result.prefix_len += t.len(); result.offset += t.len(); } else { return None; } - } else if t.starts_with(&s) { + } else if t.starts_with(s) { result.prefix_len += s.len(); result.offset += s.len(); @@ -218,10 +218,11 @@ impl<'a> HeapPStrIter<'a> { self.brent_st.hare = orig_hare; } + #[allow(clippy::inherent_to_string)] pub fn to_string(&mut self) -> String { let mut buf = String::with_capacity(32); - while let Some(iteratee) = self.next() { + for iteratee in self.by_ref() { match iteratee { PStrIteratee::Char(_, c) => { buf.push(c); @@ -334,14 +335,10 @@ impl<'a> HeapPStrIter<'a> { heap_bound_deref(self.heap, self.heap[h]), ); - return if let Some(c) = value.as_char() { - Some(PStrIterStep { + return value.as_char().map(|c| PStrIterStep { iteratee: PStrIteratee::Char(curr_hare, c), next_hare: h+1, - }) - } else { - None - }; + }); } (HeapCellValueTag::Str, s) => { let (name, arity) = cell_as_atom_cell!(self.heap[s]) @@ -353,14 +350,10 @@ impl<'a> HeapPStrIter<'a> { heap_bound_deref(self.heap, self.heap[s+1]), ); - if let Some(c) = value.as_char() { - Some(PStrIterStep { + value.as_char().map(|c| PStrIterStep { iteratee: PStrIteratee::Char(curr_hare, c), next_hare: s+2, }) - } else { - None - } } else { None }; @@ -405,10 +398,7 @@ impl<'a> HeapPStrIter<'a> { match self.brent_st.step(next_hare) { Some(cycle_result) => { - debug_assert!(match cycle_result { - CycleSearchResult::Cyclic(..) => true, - _ => false, - }); + debug_assert!(matches!(cycle_result, CycleSearchResult::Cyclic(..))); self.walk_hare_to_cycle_end(); self.stepper = HeapPStrIter::post_cycle_discovery_stepper; @@ -550,11 +540,7 @@ pub enum PStrCmpResult { impl PStrCmpResult { #[inline] pub fn is_second_iter(&self) -> bool { - if let PStrCmpResult::SecondIterContinuable(_) = self { - true - } else { - false - } + matches!(self, PStrCmpResult::SecondIterContinuable(_)) } } @@ -600,8 +586,8 @@ pub fn compare_pstr_prefixes<'a>( return PStrCmpResult::Ordered(c1.cmp(&c2)); } - cycle_detection_step(i1, i2, &step_1); - let both_cyclic = cycle_detection_step(i2, i1, &step_2); + cycle_detection_step(i1, i2, step_1); + let both_cyclic = cycle_detection_step(i2, i1, step_2); r1 = step(i1, i1.brent_st.hare); r2 = step(i2, i2.brent_st.hare); @@ -623,15 +609,15 @@ pub fn compare_pstr_prefixes<'a>( if n1 < pstr_atom.len() { step_2.iteratee = PStrIteratee::PStrSegment(f2, pstr_atom, n1); - let c1_result = cycle_detection_step(i1, i2, &step_1); + let c1_result = cycle_detection_step(i1, i2, step_1); r1 = step(i1, i1.brent_st.hare); if !c1_result { continue; } } else { - cycle_detection_step(i1, i2, &step_1); - let both_cyclic = cycle_detection_step(i2, i1, &step_2); + cycle_detection_step(i1, i2, step_1); + let both_cyclic = cycle_detection_step(i2, i1, step_2); r1 = step(i1, i1.brent_st.hare); r2 = step(i2, i2.brent_st.hare); @@ -641,7 +627,7 @@ pub fn compare_pstr_prefixes<'a>( } } } else { - let c2_result = cycle_detection_step(i2, i1, &step_2); + let c2_result = cycle_detection_step(i2, i1, step_2); r2 = step(i2, i2.brent_st.hare); if !c2_result { @@ -662,15 +648,15 @@ pub fn compare_pstr_prefixes<'a>( if n1 < pstr_atom.len() { step_1.iteratee = PStrIteratee::PStrSegment(f1, pstr_atom, n1); - let c2_result = cycle_detection_step(i2, i1, &step_2); + let c2_result = cycle_detection_step(i2, i1, step_2); r2 = step(i2, step_2.next_hare); if !c2_result { continue; } } else { - cycle_detection_step(i1, i2, &step_1); - let both_cyclic = cycle_detection_step(i2, i1, &step_2); + cycle_detection_step(i1, i2, step_1); + let both_cyclic = cycle_detection_step(i2, i1, step_2); r1 = step(i1, i1.brent_st.hare); r2 = step(i2, i2.brent_st.hare); @@ -680,7 +666,7 @@ pub fn compare_pstr_prefixes<'a>( } } } else { - let c1_result = cycle_detection_step(i1, i2, &step_1); + let c1_result = cycle_detection_step(i1, i2, step_1); r1 = step(i1, i1.brent_st.hare); if !c1_result { @@ -693,8 +679,8 @@ pub fn compare_pstr_prefixes<'a>( PStrIteratee::PStrSegment(f2, pstr2_atom, n2), ) => { if pstr1_atom == pstr2_atom && n1 == n2 { - cycle_detection_step(i1, i2, &step_1); - let both_cyclic = cycle_detection_step(i2, i1, &step_2); + cycle_detection_step(i1, i2, step_1); + let both_cyclic = cycle_detection_step(i2, i1, step_2); r1 = step(i1, i1.brent_st.hare); r2 = step(i2, i2.brent_st.hare); @@ -713,9 +699,9 @@ pub fn compare_pstr_prefixes<'a>( let str2 = pstr2.as_str_from(n2); match str1.len().cmp(&str2.len()) { - Ordering::Equal if &*str1 == &*str2 => { - cycle_detection_step(i1, i2, &step_1); - let both_cyclic = cycle_detection_step(i2, i1, &step_2); + Ordering::Equal if *str1 == *str2 => { + cycle_detection_step(i1, i2, step_1); + let both_cyclic = cycle_detection_step(i2, i1, step_2); r1 = step(i1, i1.brent_st.hare); r2 = step(i2, i2.brent_st.hare); @@ -727,7 +713,7 @@ pub fn compare_pstr_prefixes<'a>( Ordering::Less if str2.starts_with(&*str1) => { step_2.iteratee = PStrIteratee::PStrSegment(f2, pstr2_atom, n2 + str1.len()); - let c1_result = cycle_detection_step(i1, i2, &step_1); + let c1_result = cycle_detection_step(i1, i2, step_1); r1 = step(i1, i1.brent_st.hare); if !c1_result { @@ -737,7 +723,7 @@ pub fn compare_pstr_prefixes<'a>( Ordering::Greater if str1.starts_with(&*str2) => { step_1.iteratee = PStrIteratee::PStrSegment(f1, pstr1_atom, n1 + str2.len()); - let c2_result = cycle_detection_step(i2, i1, &step_2); + let c2_result = cycle_detection_step(i2, i1, step_2); r2 = step(i2, i2.brent_st.hare); if !c2_result { @@ -786,12 +772,10 @@ pub fn compare_pstr_prefixes<'a>( } else { PStrCmpResult::FirstIterContinuable(r1.unwrap().iteratee) } + } else if i1.is_continuable() && i2.is_continuable() { + PStrCmpResult::Ordered(Ordering::Equal) } else { - if i1.is_continuable() && i2.is_continuable() { - PStrCmpResult::Ordered(Ordering::Equal) - } else { - PStrCmpResult::Unordered - } + PStrCmpResult::Unordered } } @@ -885,7 +869,7 @@ mod test { { let mut iter = HeapPStrIter::new(&wam.machine_st.heap, 0); - while let Some(_) = iter.next() {} + for _ in iter.by_ref() {} assert!(!iter.at_string_terminator()); } @@ -1009,7 +993,7 @@ mod test { unify!(wam.machine_st, cstr_var_cell, heap_loc_as_cell!(1)); - assert_eq!(wam.machine_st.fail, false); + assert!(!wam.machine_st.fail); assert_eq!(wam.machine_st.heap[2], char_as_cell!('a'),); @@ -1032,7 +1016,7 @@ mod test { unify!(wam.machine_st, cstr_var_cell, heap_loc_as_cell!(1)); - assert_eq!(wam.machine_st.fail, false); + assert!(!wam.machine_st.fail); // test "abc" = [X,b,Z]. @@ -1054,7 +1038,7 @@ mod test { unify!(wam.machine_st, cstr_var_cell, heap_loc_as_cell!(1)); - assert_eq!(wam.machine_st.fail, false); + assert!(!wam.machine_st.fail); assert_eq!(wam.machine_st.heap[2], char_as_cell!('a'),); @@ -1075,7 +1059,7 @@ mod test { print_heap_terms(wam.machine_st.heap.iter(), 0); - assert_eq!(wam.machine_st.fail, false); + assert!(!wam.machine_st.fail); assert_eq!(wam.machine_st.heap[2], pstr_loc_as_cell!(5)); assert_eq!(wam.machine_st.heap[3], pstr_loc_as_cell!(1)); @@ -1107,7 +1091,7 @@ mod test { // assert!(iter.next().is_none()); - while let Some(_) = iter.next() {} + for _ in iter {} } } } diff --git a/src/machine/preprocessor.rs b/src/machine/preprocessor.rs index 271523235..2681e8c0c 100644 --- a/src/machine/preprocessor.rs +++ b/src/machine/preprocessor.rs @@ -100,7 +100,7 @@ fn setup_module_export( } pub(crate) fn build_rule_body(vars: &[Term], body_term: Term) -> Term { - let head_term = Term::Clause(Cell::default(), atom!(""), vars.iter().cloned().collect()); + let head_term = Term::Clause(Cell::default(), atom!(""), vars.to_vec()); let rule = vec![head_term, body_term]; Term::Clause(Cell::default(), atom!(":-"), rule) @@ -238,7 +238,7 @@ fn setup_meta_predicate<'a, LS: LoadState<'a>>( ) -> Result<(Atom, Vec), CompilationError> { let mut meta_specs = vec![]; - for meta_spec in terms.into_iter() { + for meta_spec in terms.iter_mut() { match meta_spec { Term::Literal(_, Literal::Atom(meta_spec)) => { let meta_spec = match meta_spec { @@ -310,11 +310,11 @@ pub(super) fn setup_declaration<'a, LS: LoadState<'a>>( } (atom!("module"), 2) => { let atom_tbl = &mut LS::machine_st(&mut loader.payload).atom_tbl; - Ok(Declaration::Module(setup_module_decl(terms, &atom_tbl)?)) + Ok(Declaration::Module(setup_module_decl(terms, atom_tbl)?)) } (atom!("op"), 3) => { let atom_tbl = &mut LS::machine_st(&mut loader.payload).atom_tbl; - Ok(Declaration::Op(setup_op_decl(terms, &atom_tbl)?)) + Ok(Declaration::Op(setup_op_decl(terms, atom_tbl)?)) } (atom!("non_counted_backtracking"), 1) => { let (name, arity) = setup_predicate_indicator(&mut terms.pop().unwrap())?; @@ -323,7 +323,7 @@ pub(super) fn setup_declaration<'a, LS: LoadState<'a>>( (atom!("use_module"), 1) => Ok(Declaration::UseModule(setup_use_module_decl(terms)?)), (atom!("use_module"), 2) => { let atom_tbl = &mut LS::machine_st(&mut loader.payload).atom_tbl; - let (name, exports) = setup_qualified_import(terms, &atom_tbl)?; + let (name, exports) = setup_qualified_import(terms, atom_tbl)?; Ok(Declaration::UseQualifiedModule(name, exports)) } diff --git a/src/machine/stack.rs b/src/machine/stack.rs index 387c40d7f..46638beea 100644 --- a/src/machine/stack.rs +++ b/src/machine/stack.rs @@ -56,7 +56,7 @@ impl Index for AndFrame { let index_offset = (index - 1) * mem::size_of::(); unsafe { - let ptr = mem::transmute::<&AndFrame, *const u8>(self); + let ptr = self as *const crate::machine::stack::AndFrame as *const u8; let ptr = ptr as usize + prelude_offset + index_offset; &*(ptr as *const HeapCellValue) @@ -70,7 +70,7 @@ impl IndexMut for AndFrame { let index_offset = (index - 1) * mem::size_of::(); unsafe { - let ptr = mem::transmute::<&mut AndFrame, *const u8>(self); + let ptr = self as *mut crate::machine::stack::AndFrame as *const u8; let ptr = ptr as usize + prelude_offset + index_offset; &mut *(ptr as *mut HeapCellValue) @@ -129,7 +129,7 @@ impl Index for OrFrame { let index_offset = index * mem::size_of::(); unsafe { - let ptr = mem::transmute::<&OrFrame, *const u8>(self); + let ptr = self as *const crate::machine::stack::OrFrame as *const u8; let ptr = ptr as usize + prelude_offset + index_offset; &*(ptr as *const HeapCellValue) @@ -144,7 +144,7 @@ impl IndexMut for OrFrame { let index_offset = index * mem::size_of::(); unsafe { - let ptr = mem::transmute::<&mut OrFrame, *const u8>(self); + let ptr = self as *mut crate::machine::stack::OrFrame as *const u8; let ptr = ptr as usize + prelude_offset + index_offset; &mut *(ptr as *mut HeapCellValue) diff --git a/src/machine/streams.rs b/src/machine/streams.rs index 4f9550506..88f4cb36e 100644 --- a/src/machine/streams.rs +++ b/src/machine/streams.rs @@ -21,9 +21,9 @@ use std::fmt::Debug; use std::fs::{File, OpenOptions}; use std::hash::Hash; use std::io; -use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write}; #[cfg(feature = "http")] use std::io::BufRead; +use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write}; use std::mem; use std::net::{Shutdown, TcpStream}; use std::ops::{Deref, DerefMut}; @@ -161,7 +161,7 @@ impl StreamLayout> { // its pending buffer length from position. self.get_mut() .file - .seek(SeekFrom::Current(0)) + .stream_position() .map(|pos| pos - self.stream.rem_buf_len() as u64) .ok() } @@ -317,33 +317,31 @@ impl Write for HttpWriteStream { #[inline] fn flush(&mut self) -> std::io::Result<()> { - Ok(()) + Ok(()) } } #[cfg(feature = "http")] impl HttpWriteStream { fn drop(&mut self) { - let headers = unsafe { mem::ManuallyDrop::take(&mut self.headers) }; - let buffer = unsafe { mem::ManuallyDrop::take(&mut self.buffer) }; - - let (ready, response, cvar) = &**self.response; - - let mut ready = ready.lock().unwrap(); - { - let mut response = response.lock().unwrap(); - - let mut response_ = warp::http::Response::builder() - .status(self.status_code); - *response_.headers_mut().unwrap() = headers; - *response = Some(response_.body(warp::hyper::Body::from(buffer)).unwrap()); - } - *ready = true; - cvar.notify_one(); + let headers = unsafe { mem::ManuallyDrop::take(&mut self.headers) }; + let buffer = unsafe { mem::ManuallyDrop::take(&mut self.buffer) }; + + let (ready, response, cvar) = &**self.response; + + let mut ready = ready.lock().unwrap(); + { + let mut response = response.lock().unwrap(); + + let mut response_ = warp::http::Response::builder().status(self.status_code); + *response_.headers_mut().unwrap() = headers; + *response = Some(response_.body(warp::hyper::Body::from(buffer)).unwrap()); + } + *ready = true; + cvar.notify_one(); } } - #[derive(Debug)] pub struct StandardOutputStream {} @@ -389,7 +387,7 @@ impl StreamOptions { #[inline] pub fn get_alias(self) -> Option { if self.has_alias() { - Some(Atom::from((self.alias() as u64) << 3)) + Some(Atom::from(self.alias() << 3)) } else { None } @@ -466,6 +464,7 @@ macro_rules! arena_allocated_impl_for_stream { mem::size_of::>() } + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn copy_to_arena(self, dst: *mut Self) -> Self::PtrToAllocated { unsafe { @@ -585,29 +584,17 @@ impl Stream { #[inline] pub fn is_stderr(&self) -> bool { - if let Stream::StandardError(_) = self { - true - } else { - false - } + matches!(self, Stream::StandardError(_)) } #[inline] pub fn is_stdout(&self) -> bool { - if let Stream::StandardOutput(_) = self { - true - } else { - false - } + matches!(self, Stream::StandardOutput(_)) } #[inline] pub fn is_stdin(&self) -> bool { - if let Stream::Readline(_) = self { - true - } else { - false - } + matches!(self, Stream::Readline(_)) } pub fn as_ptr(&self) -> *const ArenaHeader { @@ -831,7 +818,7 @@ impl CharRead for Stream { impl Read for Stream { #[inline] fn read(&mut self, buf: &mut [u8]) -> std::io::Result { - let bytes_read = match self { + match self { Stream::InputFile(file) => (*file).read(buf), Stream::NamedTcp(tcp_stream) => (*tcp_stream).read(buf), #[cfg(feature = "tls")] @@ -853,9 +840,7 @@ impl Read for Stream { ErrorKind::PermissionDenied, StreamError::ReadFromOutputStream, )), - }; - - bytes_read + } } } @@ -984,18 +969,14 @@ fn cursor_position( cursor: &Cursor, cursor_len: u64, ) -> AtEndOfStream { - let position = cursor.position(); - - let at_end_of_stream = match position.cmp(&cursor_len) { + match cursor.position().cmp(&cursor_len) { Ordering::Equal => AtEndOfStream::At, Ordering::Greater => { *past_end_of_stream = true; AtEndOfStream::Past } Ordering::Less => AtEndOfStream::Not, - }; - - at_end_of_stream + } } impl Stream { @@ -1021,26 +1002,23 @@ impl Stream { #[inline] pub(crate) fn set_position(&mut self, position: u64) { - match self { - Stream::InputFile(stream_layout) => { - let StreamLayout { - past_end_of_stream, - stream, - .. - } = &mut **stream_layout; - - stream - .get_mut() - .file - .seek(SeekFrom::Start(position)) - .unwrap(); - stream.reset_buffer(); // flush the internal buffer. - - if let Ok(metadata) = stream.get_ref().file.metadata() { - *past_end_of_stream = position > metadata.len(); - } + if let Stream::InputFile(stream_layout) = self { + let StreamLayout { + past_end_of_stream, + stream, + .. + } = &mut **stream_layout; + + stream + .get_mut() + .file + .seek(SeekFrom::Start(position)) + .unwrap(); + stream.reset_buffer(); // flush the internal buffer. + + if let Ok(metadata) = stream.get_ref().file.metadata() { + *past_end_of_stream = position > metadata.len(); } - _ => {} } } @@ -1257,15 +1235,15 @@ impl Stream { headers: hyper::HeaderMap, arena: &mut Arena, ) -> Self { - Stream::HttpWrite(arena_alloc!( - StreamLayout::new(CharReader::new(HttpWriteStream { - response, - status_code, - headers: mem::ManuallyDrop::new(headers), - buffer: mem::ManuallyDrop::new(Vec::new()), - })), - arena - )) + Stream::HttpWrite(arena_alloc!( + StreamLayout::new(CharReader::new(HttpWriteStream { + response, + status_code, + headers: mem::ManuallyDrop::new(headers), + buffer: mem::ManuallyDrop::new(Vec::new()), + })), + arena + )) } #[inline] @@ -1313,8 +1291,8 @@ impl Stream { Ok(()) } #[cfg(feature = "http")] - Stream::HttpWrite(ref mut http_stream) => { - http_stream.inner_mut().drop(); + Stream::HttpWrite(ref mut http_stream) => { + http_stream.inner_mut().drop(); unsafe { http_stream.set_tag(ArenaHeaderTag::Dropped); std::ptr::drop_in_place(&mut http_stream.inner_mut().buffer as *mut _); @@ -1346,11 +1324,7 @@ impl Stream { #[inline] pub(crate) fn is_null_stream(&self) -> bool { - if let Stream::Null(_) = self { - true - } else { - false - } + matches!(self, Stream::Null(_)) } #[inline] @@ -1391,29 +1365,25 @@ impl Stream { self.set_lines_read(0); self.set_past_end_of_stream(false); - loop { - match self { - Stream::Byte(ref mut cursor) => { - cursor.stream.get_mut().0.set_position(0); - return true; - } - Stream::InputFile(ref mut file_stream) => { - file_stream - .stream - .get_mut() - .file - .seek(SeekFrom::Start(0)) - .unwrap(); - return true; - } - Stream::Readline(ref mut readline_stream) => { - readline_stream.reset(); - return true; - } - _ => { - return false; - } + match self { + Stream::Byte(ref mut cursor) => { + cursor.stream.get_mut().0.set_position(0); + true } + Stream::InputFile(ref mut file_stream) => { + file_stream + .stream + .get_mut() + .file + .seek(SeekFrom::Start(0)) + .unwrap(); + true + } + Stream::Readline(ref mut readline_stream) => { + readline_stream.reset(); + true + } + _ => false, } } @@ -1484,12 +1454,13 @@ impl MachineState { stream.set_past_end_of_stream(true); } - Ok(self.fail = stream.past_end_of_stream()) + self.fail = stream.past_end_of_stream(); + Ok(()) } } } - pub(crate) fn to_stream_options( + pub(crate) fn get_stream_options( &mut self, alias: HeapCellValue, eof_action: HeapCellValue, @@ -1782,9 +1753,9 @@ impl MachineState { caller: Atom, arity: usize, ) -> CallResult { - let opt_err = if input.is_some() && !stream.is_input_stream() { - Some(atom!("stream")) // 8.14.2.3 g) - } else if input.is_none() && !stream.is_output_stream() { + let opt_err = if input.is_some() && !stream.is_input_stream() + || input.is_none() && !stream.is_output_stream() + { Some(atom!("stream")) // 8.14.2.3 g) } else if stream.options().stream_type() != expected_type { Some(expected_type.other().as_atom()) // 8.14.2.3 h) diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index f6e72b93e..aa78f1a10 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -14,7 +14,7 @@ use crate::forms::*; use crate::heap_iter::*; use crate::heap_print::*; #[cfg(feature = "http")] -use crate::http::{HttpRequestData, HttpListener, HttpResponse, HttpRequest}; +use crate::http::{HttpListener, HttpRequest, HttpRequestData, HttpResponse}; use crate::instructions::*; use crate::machine; use crate::machine::code_walker::*; @@ -44,28 +44,28 @@ pub(crate) use ref_thread_local::RefThreadLocal; use std::cell::Cell; use std::cmp::Ordering; -use std::collections::{BTreeSet}; +use std::collections::BTreeSet; use std::convert::TryFrom; use std::env; #[cfg(feature = "ffi")] use std::ffi::CString; use std::fs; use std::hash::{BuildHasher, BuildHasherDefault}; -use std::io::{ErrorKind, Read, Write}; #[cfg(feature = "http")] use std::io::BufRead; +use std::io::{ErrorKind, Read, Write}; use std::iter::{once, FromIterator}; use std::mem; -use std::net::{TcpListener, TcpStream}; #[cfg(feature = "http")] use std::net::{SocketAddr, ToSocketAddrs}; +use std::net::{TcpListener, TcpStream}; use std::num::NonZeroU32; use std::ops::Sub; use std::process; #[cfg(feature = "http")] use std::str::FromStr; #[cfg(feature = "http")] -use std::sync::{Mutex, Arc, Condvar}; +use std::sync::{Arc, Condvar, Mutex}; use chrono::{offset::Local, DateTime}; #[cfg(not(target_arch = "wasm32"))] @@ -99,15 +99,15 @@ use roxmltree; use select; #[cfg(feature = "http")] -use warp::hyper::header::{HeaderValue, HeaderName}; +use futures::future; #[cfg(feature = "http")] -use warp::hyper::{HeaderMap, Method}; +use reqwest::Url; #[cfg(feature = "http")] -use warp::{Buf, Filter}; +use warp::hyper::header::{HeaderName, HeaderValue}; #[cfg(feature = "http")] -use reqwest::Url; +use warp::hyper::{HeaderMap, Method}; #[cfg(feature = "http")] -use futures::future; +use warp::{Buf, Filter}; #[cfg(feature = "repl")] pub(crate) fn get_key() -> KeyEvent { @@ -115,15 +115,13 @@ pub(crate) fn get_key() -> KeyEvent { enable_raw_mode().expect("failed to enable raw mode"); loop { let key_ = read(); - if let Ok(key_) = key_ { - if let Event::Key(key_) = key_ { - match key_.code { - KeyCode::Char(_) | KeyCode::Enter | KeyCode::Tab => { - key = key_; - break; - } - _ => (), + if let Ok(Event::Key(key_)) = key_ { + match key_.code { + KeyCode::Char(_) | KeyCode::Enter | KeyCode::Tab => { + key = key_; + break; } + _ => (), } } } @@ -178,7 +176,7 @@ impl BrentAlgState { #[inline(always)] pub fn num_steps(&self) -> usize { - return self.lam + self.pstr_chars + self.power - 1; + self.lam + self.pstr_chars + self.power - 1 } #[inline(always)] @@ -300,10 +298,10 @@ impl BrentAlgState { read_heap_cell!(value, (HeapCellValueTag::PStrLoc, h) => { - return self.add_pstr_chars_and_step(&heap, h); + return self.add_pstr_chars_and_step(heap, h); } (HeapCellValueTag::CStr | HeapCellValueTag::PStrOffset) => { - return self.add_pstr_chars_and_step(&heap, self.hare); + return self.add_pstr_chars_and_step(heap, self.hare); } (HeapCellValueTag::Lis, h) => { return self.step(h+1); @@ -349,7 +347,7 @@ impl BrentAlgState { offset+1 } (HeapCellValueTag::PStrLoc, h) => { - let (h_offset, n) = pstr_loc_and_offset(&heap, h); + let (h_offset, n) = pstr_loc_and_offset(heap, h); let n = n.get_num() as usize; let pstr = cell_as_string!(heap[h_offset]); @@ -427,7 +425,7 @@ impl BrentAlgState { } } (HeapCellValueTag::PStrLoc, h) => { - let (h_offset, n) = pstr_loc_and_offset(&heap, h); + let (h_offset, n) = pstr_loc_and_offset(heap, h); let n = n.get_num() as usize; let pstr = cell_as_string!(heap[h_offset]); @@ -437,7 +435,7 @@ impl BrentAlgState { debug_assert!(heap[h].get_tag() == HeapCellValueTag::PStrOffset); if heap[h_offset].get_tag() == HeapCellValueTag::CStr { - return if pstr_chars + 1 <= max_steps { + return if pstr_chars < max_steps { CycleSearchResult::ProperList(pstr_chars + 1) } else { CycleSearchResult::UntouchedCStr(pstr.into(), max_steps) @@ -506,7 +504,7 @@ impl BrentAlgState { loop { if brent_st.exhausted_max_steps() { - return brent_st.to_result(&heap); + return brent_st.to_result(heap); } if let Some(result) = brent_st.cycle_step(heap) { @@ -604,7 +602,7 @@ impl MachineState { loop { read_heap_cell!(value, (HeapCellValueTag::PStrLoc, h) => { - let (h_offset, _) = pstr_loc_and_offset(&heap, h); + let (h_offset, _) = pstr_loc_and_offset(heap, h); return h_offset+1; } (HeapCellValueTag::Lis, h) => { @@ -756,12 +754,12 @@ impl MachineState { let value: i64 = (&*n).try_into().unwrap(); Some(value) - }, + } _ => None, }; if let Some(max_steps) = max_steps_n { - if max_steps.abs() as u64 <= 1 << 63 { + if max_steps.unsigned_abs() <= 1 << 63 { if max_steps >= 0 { max_old = max_steps; } else { @@ -791,7 +789,8 @@ impl MachineState { let mut seen_set = IndexSet::new(); { - let mut iter = stackful_post_order_iter::(&mut self.heap, &mut self.stack, term); + let mut iter = + stackful_post_order_iter::(&mut self.heap, &mut self.stack, term); while let Some(value) = iter.next() { if iter.parent_stack_len() >= max_depth { @@ -872,7 +871,7 @@ impl MachineState { use crate::parser::lexer::*; let nx = self.store(self.deref(self.registers[2])); - let add_dot = !string.ends_with("."); + let add_dot = !string.ends_with('.'); let cursor = std::io::Cursor::new(string); let iter = std::io::Read::chain(cursor, { @@ -1075,15 +1074,14 @@ impl MachineState { let addr = self.store(self.deref(addr)); match Number::try_from(addr) { - Ok(Number::Fixnum(n)) => match u32::try_from(n.get_num()) { - Ok(n) => { + Ok(Number::Fixnum(n)) => { + if let Ok(n) = u32::try_from(n.get_num()) { if let Some(c) = std::char::from_u32(n) { string.push(c); continue; } } - _ => {} - }, + } Ok(Number::Integer(n)) => { let n: u32 = (&*n).try_into().unwrap(); if let Some(c) = std::char::from_u32(n) { @@ -1175,9 +1173,9 @@ impl Machine { let bp = or_frame.prelude.bp; match &self.code[bp] { - &Instruction::IndexingCode(ref indexing_code) => { + Instruction::IndexingCode(ref indexing_code) => { match &indexing_code[or_frame.prelude.boip as usize] { - &IndexingLine::IndexedChoice(ref indexed_choice) => { + IndexingLine::IndexedChoice(ref indexed_choice) => { let p = or_frame.prelude.biip as usize - 1; match &indexed_choice[p] { @@ -1228,7 +1226,7 @@ impl Machine { } match &self.code[bp] { - &Instruction::IndexingCode(ref indexing_code) => { + Instruction::IndexingCode(ref indexing_code) => { let indexing_code_ptr = match &indexing_code[0] { &IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm( _, @@ -1251,17 +1249,17 @@ impl Machine { let boip = extract_ptr!(indexing_code_ptr); let boip = match &indexing_code[boip] { - &IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(ref hm)) => { + IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(ref hm)) => { boip + extract_ptr!(hm.get(&key).cloned().unwrap()) } - &IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(ref hm)) => { + IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(ref hm)) => { boip + extract_ptr!(hm.get(&Literal::Atom(key.0)).cloned().unwrap()) } _ => boip, }; match &indexing_code[boip] { - &IndexingLine::IndexedChoice(ref indexed_choice) => { + IndexingLine::IndexedChoice(indexed_choice) => { return ( skeleton.core.clause_clause_locs.back().cloned().unwrap(), bp + indexed_choice.back().unwrap().offset(), @@ -1447,10 +1445,8 @@ impl Machine { heap_loc_as_cell!(0), ); - if module_loc.is_var() || module_loc == atom_as_cell!(atom!("user")) { - if arg_term.is_var() && supp_var.is_var() { - return arg_term == *supp_var; - } + if (module_loc.is_var() || module_loc == atom_as_cell!(atom!("user"))) && arg_term.is_var() && supp_var.is_var() { + return arg_term == *supp_var; } false @@ -1527,7 +1523,8 @@ impl Machine { result.goal } else { let mut unexpanded_vars = IndexSet::with_hasher(FxBuildHasher::default()); - self.machine_st.variable_set(&mut unexpanded_vars, self.machine_st.registers[5]); + self.machine_st + .variable_set(&mut unexpanded_vars, self.machine_st.registers[5]); // all supp_vars must appear later! let vars = IndexSet::>::from_iter( @@ -1736,11 +1733,11 @@ impl Machine { #[inline(always)] pub(crate) fn is_reset_cont_marker(&self, p: usize) -> bool { - match &self.code[p] { + matches!( + &self.code[p], &Instruction::CallResetContinuationMarker - | &Instruction::ExecuteResetContinuationMarker => true, - _ => false, - } + | &Instruction::ExecuteResetContinuationMarker + ) } #[inline(always)] @@ -1751,7 +1748,7 @@ impl Machine { Ok(Number::Integer(n)) => { let value: usize = (&*n).try_into().unwrap(); Some(value) - }, + } _ => { unreachable!() } @@ -1770,27 +1767,28 @@ impl Machine { self.machine_st.fail = true; } + #[cfg(not(target_arch = "wasm32"))] #[inline(always)] pub(crate) fn current_hostname(&mut self) { - #[cfg(feature = "hostname")] - match hostname::get().ok() { - Some(host) => match host.to_str() { - Some(host) => { - let hostname = AtomTable::build_with(&self.machine_st.atom_tbl, host); + if let Ok(host) = hostname::get() { + if let Some(host) = host.to_str() { + let hostname = AtomTable::build_with(&self.machine_st.atom_tbl, host); - let a1 = self.deref_register(1); - self.machine_st.unify_atom(hostname, a1); + let a1 = self.deref_register(1); + self.machine_st.unify_atom(hostname, a1); - return; - } - None => {} - }, - None => {} + return; + } } self.machine_st.fail = true; } + #[cfg(target_arch = "wasm32")] + pub(crate) fn current_hostname(&mut self) { + unimplemented!() + } + #[inline(always)] pub(crate) fn current_input(&mut self) -> CallResult { let addr = self.deref_register(1); @@ -2116,7 +2114,7 @@ impl Machine { } }; - let current_atom = AtomTable::build_with(&self.machine_st.atom_tbl, ¤t); + let current_atom = AtomTable::build_with(&self.machine_st.atom_tbl, current); let a1 = self.deref_register(1); self.machine_st.unify_complete_string(current_atom, a1); @@ -2144,27 +2142,24 @@ impl Machine { .machine_st .value_to_str_like(self.machine_st.registers[1]) { - match fs::canonicalize(&*path.as_str()) { - Ok(canonical) => { - let cs = match canonical.to_str() { - Some(s) => s, - _ => { - let stub = functor_stub(atom!("path_canonical"), 2); - let err = self.machine_st.representation_error(RepFlag::Character); - let err = self.machine_st.error_form(err, stub); + if let Ok(canonical) = fs::canonicalize(&*path.as_str()) { + let cs = match canonical.to_str() { + Some(s) => s, + _ => { + let stub = functor_stub(atom!("path_canonical"), 2); + let err = self.machine_st.representation_error(RepFlag::Character); + let err = self.machine_st.error_form(err, stub); - return Err(err); - } - }; + return Err(err); + } + }; - let canonical_atom = AtomTable::build_with(&self.machine_st.atom_tbl, cs); + let canonical_atom = AtomTable::build_with(&self.machine_st.atom_tbl, cs); - let a2 = self.deref_register(2); - self.machine_st.unify_complete_string(canonical_atom, a2); + let a2 = self.deref_register(2); + self.machine_st.unify_complete_string(canonical_atom, a2); - return Ok(()); - } - _ => {} + return Ok(()); } } @@ -2369,7 +2364,7 @@ impl Machine { let atom_or_string = self.machine_st.value_to_str_like(a1).unwrap(); self.machine_st - .parse_number_from_string(&*atom_or_string.as_str(), &self.indices, stub_gen) + .parse_number_from_string(&atom_or_string.as_str(), &self.indices, stub_gen) } #[inline(always)] @@ -2407,7 +2402,7 @@ impl Machine { let mut iter = HeapPStrIter::new(&self.machine_st.heap, h); - while let Some(_) = iter.next() {} + for _ in iter.by_ref() {} let at_end_of_pstr = iter.focus.is_var() || iter.at_string_terminator(); self.machine_st.fail = !at_end_of_pstr; @@ -2476,11 +2471,7 @@ impl Machine { )?; if stream.past_end_of_stream() { - if EOFAction::Reset != stream.options().eof_action() { - return Ok(()); - } else if self.machine_st.fail { - return Ok(()); - } + return Ok(()); } let addr = self.deref_register(2); @@ -2541,9 +2532,7 @@ impl Machine { 2, )?; - if EOFAction::Reset != stream.options().eof_action() { - break; - } else if self.machine_st.fail { + if EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail { break; } } @@ -2572,12 +2561,10 @@ impl Machine { 2, )?; - if stream.past_end_of_stream() { - if EOFAction::Reset != stream.options().eof_action() { - return Ok(()); - } else if self.machine_st.fail { - return Ok(()); - } + if stream.past_end_of_stream() + && (EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail) + { + return Ok(()); } let a2 = self.deref_register(2); @@ -2637,9 +2624,7 @@ impl Machine { 2, )?; - if EOFAction::Reset != stream.options().eof_action() { - break; - } else if self.machine_st.fail { + if EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail { break; } } @@ -2668,12 +2653,10 @@ impl Machine { 2, )?; - if stream.past_end_of_stream() { - if EOFAction::Reset != stream.options().eof_action() { - return Ok(()); - } else if self.machine_st.fail { - return Ok(()); - } + if stream.past_end_of_stream() + && (EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail) + { + return Ok(()); } let a2 = self.deref_register(2); @@ -2697,7 +2680,7 @@ impl Machine { Ok(Number::Integer(n)) => { let n: u32 = (&*n).try_into().unwrap(); - let n = std::char::from_u32(n).and_then(|_| Some(n)); + let n = std::char::from_u32(n).map(|_| n); if let Some(n) = n { fixnum_as_cell!(Fixnum::build_with(n as i64)) @@ -2709,7 +2692,7 @@ impl Machine { Ok(Number::Fixnum(n)) => { let n = u32::try_from(n.get_num()) .ok() - .and_then(|n| std::char::from_u32(n).and_then(|_| Some(n))); + .and_then(|n| std::char::from_u32(n).map(|_| n)); if let Some(n) = n { fixnum_as_cell!(Fixnum::build_with(n as i64)) @@ -2747,16 +2730,14 @@ impl Machine { 2, )?; - if EOFAction::Reset != stream.options().eof_action() { - break; - } else if self.machine_st.fail { + if EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail { break; } } } } - return Ok(()); + Ok(()) } #[inline(always)] @@ -2779,7 +2760,7 @@ impl Machine { } }; - let chars_atom = AtomTable::build_with(&self.machine_st.atom_tbl, &string.trim()); + let chars_atom = AtomTable::build_with(&self.machine_st.atom_tbl, string.trim()); self.machine_st.unify_complete_string(chars_atom, chs); } @@ -2885,14 +2866,11 @@ impl Machine { return Ok(()); } Ok(Number::Fixnum(n)) => { - match u32::try_from(n.get_num()) { - Ok(n) => { - if let Some(c) = std::char::from_u32(n) { - self.machine_st.unify_char(c, a1); - return Ok(()); - } + if let Ok(n) = u32::try_from(n.get_num()) { + if let Some(c) = std::char::from_u32(n) { + self.machine_st.unify_char(c, a1); + return Ok(()); } - _ => {} } let err = self.machine_st.representation_error(RepFlag::CharacterCode); @@ -3064,7 +3042,7 @@ impl Machine { let h = self.machine_st.heap.len(); let stub = ball.copy_and_align(h); - self.machine_st.heap.extend(stub.into_iter()); + self.machine_st.heap.extend(stub); unify_fn!(self.machine_st, addr, heap_loc_as_cell!(h)); @@ -3101,13 +3079,13 @@ impl Machine { if addr.is_var() { let err = self.machine_st.instantiation_error(); - return Err(self.machine_st.error_form(err, stub_gen())); + Err(self.machine_st.error_form(err, stub_gen())) } else { match Number::try_from(addr) { Ok(Number::Integer(n)) => { let n: u32 = (&*n).try_into().unwrap(); let n = char::try_from(n); - if let Some(c) = n.ok() { + if let Ok(c) = n { write!(&mut stream, "{}", c).unwrap(); return Ok(()); } @@ -3115,7 +3093,7 @@ impl Machine { Ok(Number::Fixnum(n)) => { let n = n.get_num(); - if let Some(c) = u32::try_from(n).ok().and_then(|c| char::from_u32(c)) { + if let Some(c) = u32::try_from(n).ok().and_then(char::from_u32) { write!(&mut stream, "{}", c).unwrap(); return Ok(()); } @@ -3127,7 +3105,7 @@ impl Machine { } let err = self.machine_st.representation_error(RepFlag::CharacterCode); - return Err(self.machine_st.error_form(err, stub_gen())); + Err(self.machine_st.error_form(err, stub_gen())) } } @@ -3153,7 +3131,7 @@ impl Machine { if addr.is_var() { let err = self.machine_st.instantiation_error(); - return Err(self.machine_st.error_form(err, stub_gen())); + Err(self.machine_st.error_form(err, stub_gen())) } else { read_heap_cell!(addr, (HeapCellValueTag::Atom, (name, _arity)) => { @@ -3171,7 +3149,7 @@ impl Machine { ); let err = self.machine_st.type_error(ValidType::Character, addr); - return Err(self.machine_st.error_form(err, stub_gen())); + Err(self.machine_st.error_form(err, stub_gen())) } } @@ -3252,26 +3230,22 @@ impl Machine { Ok(Number::Integer(n)) => { let n: u8 = (&*n).try_into().unwrap(); - match n { - nb => { - match stream.write(&mut [nb]) { - Ok(1) => { - return Ok(()); - } - _ => { - let err = self.machine_st.existence_error( - ExistenceError::Stream(stream_as_cell!(stream)) - ); + match stream.write(&[n]) { + Ok(1) => { + return Ok(()); + } + _ => { + let err = self + .machine_st + .existence_error(ExistenceError::Stream(stream_as_cell!(stream))); - return Err(self.machine_st.error_form(err, stub_gen())); - } - } + return Err(self.machine_st.error_form(err, stub_gen())); } } } Ok(Number::Fixnum(n)) => { if let Ok(nb) = u8::try_from(n.get_num()) { - match stream.write(&mut [nb]) { + match stream.write(&[nb]) { Ok(1) => { return Ok(()); } @@ -3320,9 +3294,7 @@ impl Machine { 2, )?; - if EOFAction::Reset != stream.options().eof_action() { - return Ok(()); - } else if self.machine_st.fail { + if EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail { return Ok(()); } } @@ -3365,21 +3337,17 @@ impl Machine { } }; - loop { - let mut b = [0u8; 1]; + let mut b = [0u8; 1]; - match stream.read(&mut b) { - Ok(1) => { - self.machine_st - .unify_fixnum(Fixnum::build_with(b[0] as i64), addr); - break; - } - _ => { - stream.set_past_end_of_stream(true); - self.machine_st - .unify_fixnum(Fixnum::build_with(-1), self.machine_st.registers[2]); - break; - } + match stream.read(&mut b) { + Ok(1) => { + self.machine_st + .unify_fixnum(Fixnum::build_with(b[0] as i64), addr); + } + _ => { + stream.set_past_end_of_stream(true); + self.machine_st + .unify_fixnum(Fixnum::build_with(-1), self.machine_st.registers[2]); } } @@ -3403,12 +3371,10 @@ impl Machine { 2, )?; - if stream.past_end_of_stream() { - if EOFAction::Reset != stream.options().eof_action() { - return Ok(()); - } else if self.machine_st.fail { - return Ok(()); - } + if stream.past_end_of_stream() + && (EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail) + { + return Ok(()); } let addr = self.deref_register(2); @@ -3473,9 +3439,7 @@ impl Machine { 2, )?; - if EOFAction::Reset != stream.options().eof_action() { - break; - } else if self.machine_st.fail { + if EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail { break; } } @@ -3497,9 +3461,7 @@ impl Machine { let num = match Number::try_from(self.deref_register(2)) { Ok(Number::Fixnum(n)) => usize::try_from(n.get_num()).unwrap(), Ok(Number::Integer(n)) => match (&*n).try_into() as Result { - Ok(u) => { - u - } + Ok(u) => u, _ => { self.machine_st.fail = true; return Ok(()); @@ -3567,12 +3529,10 @@ impl Machine { 2, )?; - if stream.past_end_of_stream() { - if EOFAction::Reset != stream.options().eof_action() { - return Ok(()); - } else if self.machine_st.fail { - return Ok(()); - } + if stream.past_end_of_stream() + && (EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail) + { + return Ok(()); } let addr = self.deref_register(2); @@ -3607,7 +3567,7 @@ impl Machine { Ok(Number::Fixnum(n)) => { let nf = u32::try_from(n.get_num()) .ok() - .and_then(|n| std::char::from_u32(n)); + .and_then(std::char::from_u32); if nf.is_some() { fixnum_as_cell!(n) @@ -3651,9 +3611,7 @@ impl Machine { 2, )?; - if EOFAction::Reset != stream.options().eof_action() { - break; - } else if self.machine_st.fail { + if EOFAction::Reset != stream.options().eof_action() || self.machine_st.fail { break; } } @@ -3852,7 +3810,7 @@ impl Machine { let close_result = stream.close(); - if let Err(_) = close_result { + if close_result.is_err() { let stub = functor_stub(atom!("close"), 1); let addr = stream_as_cell!(stream); let err = self @@ -3953,7 +3911,7 @@ impl Machine { Ok(Number::Integer(n)) => { let value: usize = (&*n).try_into().unwrap(); Some(value) - }, + } _ => None, }; @@ -4057,7 +4015,11 @@ impl Machine { heap.extend(functor!( atom!("op"), - [fixnum(prec), cell(atom_as_cell!(spec_atom)), cell(atom_as_cell!(name))] + [ + fixnum(prec), + cell(atom_as_cell!(spec_atom)), + cell(atom_as_cell!(name)) + ] )); num_functors += 1; @@ -4110,17 +4072,15 @@ impl Machine { + op_descs[1].is_some() as usize + op_descs[2].is_some() as usize; - let op_descs = op_descs.into_iter().filter_map(|op_desc| { - op_desc.map(|op_desc| (orig_op, *op_desc)) - }); + let op_descs = op_descs + .into_iter() + .filter_map(|op_desc| op_desc.map(|op_desc| (orig_op, *op_desc))); if number_of_keys == 0 { self.machine_st.fail = true; } else { - let num_functors = write_op_functors_to_heap( - &mut self.machine_st.heap, - op_descs, - ); + let num_functors = + write_op_functors_to_heap(&mut self.machine_st.heap, op_descs); let h = iter_to_heap_list( &mut self.machine_st.heap, @@ -4241,7 +4201,9 @@ impl Machine { Ok(Number::Integer(n)) => match (&*n).try_into() as Result { Ok(n) => n, Err(_) => { - let err = self.machine_st.resource_error(ResourceError::FiniteMemory(len)); + let err = self + .machine_st + .resource_error(ResourceError::FiniteMemory(len)); return Err(self.machine_st.error_form(err, stub_gen())); } }, @@ -4304,7 +4266,7 @@ impl Machine { (HeapCellValueTag::Str, s) => { let name = cell_as_atom_cell!(self.machine_st.heap[s]).get_name(); let value = self.machine_st.value_to_str_like(self.machine_st.heap[s + 1]).unwrap(); - header_map.insert(HeaderName::from_str(&*name.as_str()).unwrap(), HeaderValue::from_str(&*value.as_str()).unwrap()); + header_map.insert(HeaderName::from_str(&name.as_str()).unwrap(), HeaderValue::from_str(&value.as_str()).unwrap()); } _ => { unreachable!() @@ -4325,7 +4287,7 @@ impl Machine { let mut req = reqwest::blocking::Request::new(method, address); *req.headers_mut() = headers; - if bytes.len() > 0 { + if !bytes.is_empty() { *req.body_mut() = Some(reqwest::blocking::Body::from(bytes)); } @@ -4354,7 +4316,7 @@ impl Machine { )))] ); - self.machine_st.heap.extend(header_term.into_iter()); + self.machine_st.heap.extend(header_term); str_loc_as_cell!(h) }) .collect(); @@ -4406,128 +4368,145 @@ impl Machine { #[inline(always)] pub(crate) fn http_listen(&mut self) -> CallResult { let address_sink = self.deref_register(1); - let tls_key = self.deref_register(3); - let tls_cert = self.deref_register(4); - let content_length_limit = self.deref_register(5); - const CONTENT_LENGTH_LIMIT_DEFAULT: u64 = 32768; - let content_length_limit = match Number::try_from(content_length_limit) { - Ok(Number::Fixnum(n)) => if n.get_num() >= 0 { - n.get_num() as u64 - } else { - CONTENT_LENGTH_LIMIT_DEFAULT - }, - Ok(Number::Integer(n)) => { + let tls_key = self.deref_register(3); + let tls_cert = self.deref_register(4); + let content_length_limit = self.deref_register(5); + const CONTENT_LENGTH_LIMIT_DEFAULT: u64 = 32768; + let content_length_limit = match Number::try_from(content_length_limit) { + Ok(Number::Fixnum(n)) => { + if n.get_num() >= 0 { + n.get_num() as u64 + } else { + CONTENT_LENGTH_LIMIT_DEFAULT + } + } + Ok(Number::Integer(n)) => { let n: Result = (&*n).try_into(); - match n { - Ok(u) => u, - Err(_) => CONTENT_LENGTH_LIMIT_DEFAULT, - } - } - _ => CONTENT_LENGTH_LIMIT_DEFAULT, - }; - - let ssl_server: Option<(String,String)> = { - match self.machine_st.value_to_str_like(tls_key) { - Some(key) => { - match self.machine_st.value_to_str_like(tls_cert) { - Some(cert) => { - let key_str = key.as_str(); - let cert_str = cert.as_str(); - - if key_str.is_empty() || cert_str.is_empty() { - None - } else { - Some((key_str.to_string(), cert_str.to_string())) - } - } - None => None - } - } - None => None - } - }; - - if let Some(address_str) = self.machine_st.value_to_str_like(address_sink) { - let address_string = address_str.as_str(); - let addr: SocketAddr = match address_string.to_socket_addrs().ok().and_then(|mut s| s.next()) { - Some(addr) => addr, - _ => { - self.machine_st.fail = true; - return Ok(()); + match n { + Ok(u) => u, + Err(_) => CONTENT_LENGTH_LIMIT_DEFAULT, + } } + _ => CONTENT_LENGTH_LIMIT_DEFAULT, }; - let (tx, rx) = std::sync::mpsc::sync_channel(1024); - - let runtime = tokio::runtime::Handle::current(); - let _guard = runtime.enter(); - - fn get_reader(body: impl Buf + Send + 'static) -> Box { - Box::new(body.reader()) - } - - let serve = warp::body::aggregate() - .and(warp::header::optional::(warp::http::header::CONTENT_LENGTH.as_str())) - .and(warp::method()) - .and(warp::header::headers_cloned()) - .and(warp::path::full()) - .and(warp::query::raw().or_else(|_| future::ready(Ok::<(String,), warp::Rejection>(("".to_string(),))))) - .map(move |body, content_length, method, headers: warp::http::HeaderMap, path: warp::filters::path::FullPath, query| { - if let Some(content_length) = content_length { - if content_length > content_length_limit { - return warp::http::Response::builder() - .status(413) - .body(warp::hyper::Body::empty()) - .unwrap(); - } - } - - let http_request_data = HttpRequestData { - method, - headers, - path: path.as_str().to_string(), - query, - body: get_reader(body), - }; - let response = Arc::new((Mutex::new(false), Mutex::new(None), Condvar::new())); - let http_request = HttpRequest { request_data: http_request_data, response: Arc::clone(&response) }; - // we send the request to http_accept - tx.send(http_request).unwrap(); - - // we wait for the Response info from Prolog - { - let (ready, _response, cvar) = &*response; - let mut ready = ready.lock().unwrap(); - while !*ready { - ready = cvar.wait(ready).unwrap(); - } - } - { - let (_, response, _) = &*response; - let response = response.lock().unwrap().take(); - response.expect("Data race error in HTTP server") - } - }); - - runtime.spawn(async move { - match ssl_server { - Some((key, cert)) => { - warp::serve(serve).tls().key(key).cert(cert).run(addr).await - } - None => { - warp::serve(serve).run(addr).await - } - } - }); - - let http_listener = HttpListener { incoming: rx }; - let http_listener = arena_alloc!(http_listener, &mut self.machine_st.arena); + let ssl_server: Option<(String, String)> = { + match self.machine_st.value_to_str_like(tls_key) { + Some(key) => match self.machine_st.value_to_str_like(tls_cert) { + Some(cert) => { + let key_str = key.as_str(); + let cert_str = cert.as_str(); - let addr = self.deref_register(2); - self.machine_st.bind( - addr.as_var().unwrap(), - typed_arena_ptr_as_cell!(http_listener), - ); + if key_str.is_empty() || cert_str.is_empty() { + None + } else { + Some((key_str.to_string(), cert_str.to_string())) + } + } + None => None, + }, + None => None, + } + }; + + if let Some(address_str) = self.machine_st.value_to_str_like(address_sink) { + let address_string = address_str.as_str(); + let addr: SocketAddr = match address_string + .to_socket_addrs() + .ok() + .and_then(|mut s| s.next()) + { + Some(addr) => addr, + _ => { + self.machine_st.fail = true; + return Ok(()); + } + }; + + let (tx, rx) = std::sync::mpsc::sync_channel(1024); + + let runtime = tokio::runtime::Handle::current(); + let _guard = runtime.enter(); + + fn get_reader(body: impl Buf + Send + 'static) -> Box { + Box::new(body.reader()) + } + + let serve = warp::body::aggregate() + .and(warp::header::optional::( + warp::http::header::CONTENT_LENGTH.as_str(), + )) + .and(warp::method()) + .and(warp::header::headers_cloned()) + .and(warp::path::full()) + .and(warp::query::raw().or_else(|_| { + future::ready(Ok::<(String,), warp::Rejection>(("".to_string(),))) + })) + .map( + move |body, + content_length, + method, + headers: warp::http::HeaderMap, + path: warp::filters::path::FullPath, + query| { + if let Some(content_length) = content_length { + if content_length > content_length_limit { + return warp::http::Response::builder() + .status(413) + .body(warp::hyper::Body::empty()) + .unwrap(); + } + } + + let http_request_data = HttpRequestData { + method, + headers, + path: path.as_str().to_string(), + query, + body: get_reader(body), + }; + let response = + Arc::new((Mutex::new(false), Mutex::new(None), Condvar::new())); + let http_request = HttpRequest { + request_data: http_request_data, + response: Arc::clone(&response), + }; + // we send the request to http_accept + tx.send(http_request).unwrap(); + + // we wait for the Response info from Prolog + { + let (ready, _response, cvar) = &*response; + let mut ready = ready.lock().unwrap(); + while !*ready { + ready = cvar.wait(ready).unwrap(); + } + } + { + let (_, response, _) = &*response; + let response = response.lock().unwrap().take(); + response.expect("Data race error in HTTP server") + } + }, + ); + + runtime.spawn(async move { + match ssl_server { + Some((key, cert)) => { + warp::serve(serve).tls().key(key).cert(cert).run(addr).await + } + None => warp::serve(serve).run(addr).await, + } + }); + + let http_listener = HttpListener { incoming: rx }; + let http_listener = arena_alloc!(http_listener, &mut self.machine_st.arena); + + let addr = self.deref_register(2); + self.machine_st.bind( + addr.as_var().unwrap(), + typed_arena_ptr_as_cell!(http_listener), + ); } Ok(()) } @@ -4535,36 +4514,36 @@ impl Machine { #[cfg(feature = "http")] #[inline(always)] pub(crate) fn http_accept(&mut self) -> CallResult { - let culprit = self.deref_register(1); - let method = self.deref_register(2); - let path = self.deref_register(3); - let query = self.deref_register(5); - let stream_addr = self.deref_register(6); - let handle_addr = self.deref_register(7); - read_heap_cell!(culprit, - (HeapCellValueTag::Cons, cons_ptr) => { - match_untyped_arena_ptr!(cons_ptr, - (ArenaHeaderTag::HttpListener, http_listener) => { - loop { - match http_listener.incoming.recv_timeout(std::time::Duration::from_millis(200)) { - Ok(request) => { - let method_atom = match request.request_data.method { - Method::GET => atom!("get"), - Method::POST => atom!("post"), - Method::PUT => atom!("put"), - Method::DELETE => atom!("delete"), - Method::PATCH => atom!("patch"), - Method::HEAD => atom!("head"), - Method::OPTIONS => atom!("options"), - Method::TRACE => atom!("trace"), - Method::CONNECT => atom!("connect"), - _ => atom!("unsupported_extension"), - }; - let path_atom = AtomTable::build_with(&self.machine_st.atom_tbl, &request.request_data.path); - let path_cell = atom_as_cstr_cell!(path_atom); - let headers: Vec = request.request_data.headers.iter().map(|(header_name, header_value)| { - let h = self.machine_st.heap.len(); - let header_term = functor!(AtomTable::build_with(&self.machine_st.atom_tbl, header_name.as_str()), [cell(string_as_cstr_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, header_value.to_str().unwrap())))]); + let culprit = self.deref_register(1); + let method = self.deref_register(2); + let path = self.deref_register(3); + let query = self.deref_register(5); + let stream_addr = self.deref_register(6); + let handle_addr = self.deref_register(7); + read_heap_cell!(culprit, + (HeapCellValueTag::Cons, cons_ptr) => { + match_untyped_arena_ptr!(cons_ptr, + (ArenaHeaderTag::HttpListener, http_listener) => { + loop { + match http_listener.incoming.recv_timeout(std::time::Duration::from_millis(200)) { + Ok(request) => { + let method_atom = match request.request_data.method { + Method::GET => atom!("get"), + Method::POST => atom!("post"), + Method::PUT => atom!("put"), + Method::DELETE => atom!("delete"), + Method::PATCH => atom!("patch"), + Method::HEAD => atom!("head"), + Method::OPTIONS => atom!("options"), + Method::TRACE => atom!("trace"), + Method::CONNECT => atom!("connect"), + _ => atom!("unsupported_extension"), + }; + let path_atom = AtomTable::build_with(&self.machine_st.atom_tbl, &request.request_data.path); + let path_cell = atom_as_cstr_cell!(path_atom); + let headers: Vec = request.request_data.headers.iter().map(|(header_name, header_value)| { + let h = self.machine_st.heap.len(); + let header_term = functor!(AtomTable::build_with(&self.machine_st.atom_tbl, header_name.as_str()), [cell(string_as_cstr_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, header_value.to_str().unwrap())))]); self.machine_st.heap.extend(header_term.into_iter()); str_loc_as_cell!(h) @@ -4572,19 +4551,19 @@ impl Machine { let headers_list = iter_to_heap_list(&mut self.machine_st.heap, headers.into_iter()); - let query_str = request.request_data.query; - let query_atom = AtomTable::build_with(&self.machine_st.atom_tbl, &query_str); - let query_cell = string_as_cstr_cell!(query_atom); + let query_str = request.request_data.query; + let query_atom = AtomTable::build_with(&self.machine_st.atom_tbl, &query_str); + let query_cell = string_as_cstr_cell!(query_atom); - let mut stream = Stream::from_http_stream( - path_atom, - request.request_data.body, - &mut self.machine_st.arena - ); - *stream.options_mut() = StreamOptions::default(); - stream.options_mut().set_stream_type(StreamType::Binary); - self.indices.streams.insert(stream); - let stream = stream_as_cell!(stream); + let mut stream = Stream::from_http_stream( + path_atom, + request.request_data.body, + &mut self.machine_st.arena + ); + *stream.options_mut() = StreamOptions::default(); + stream.options_mut().set_stream_type(StreamType::Binary); + self.indices.streams.insert(stream); + let stream = stream_as_cell!(stream); let handle = arena_alloc!(request.response, &mut self.machine_st.arena); @@ -4594,39 +4573,39 @@ impl Machine { self.machine_st.bind(query.as_var().unwrap(), query_cell); self.machine_st.bind(stream_addr.as_var().unwrap(), stream); self.machine_st.bind(handle_addr.as_var().unwrap(), typed_arena_ptr_as_cell!(handle)); - break + break } - Err(std::sync::mpsc::RecvTimeoutError::Timeout) => { - let interrupted = machine::INTERRUPT.load(std::sync::atomic::Ordering::Relaxed); - - match machine::INTERRUPT.compare_exchange( - interrupted, - false, - std::sync::atomic::Ordering::Relaxed, - std::sync::atomic::Ordering::Relaxed, - ) { - Ok(interruption) => { - if interruption { - self.machine_st.throw_interrupt_exception(); - self.machine_st.backtrack(); + Err(std::sync::mpsc::RecvTimeoutError::Timeout) => { + let interrupted = machine::INTERRUPT.load(std::sync::atomic::Ordering::Relaxed); + + match machine::INTERRUPT.compare_exchange( + interrupted, + false, + std::sync::atomic::Ordering::Relaxed, + std::sync::atomic::Ordering::Relaxed, + ) { + Ok(interruption) => { + if interruption { + self.machine_st.throw_interrupt_exception(); + self.machine_st.backtrack(); // We have extracted controll over the Tokio runtime to the calling context for enabling library use case // (see https://github.com/mthom/scryer-prolog/pull/1880) // So we only have access to a runtime handle in here and can't shut it down. // Since I'm not aware of the consequences of deactivating this new code which came in while PR 1880 // was not merged, I'm only deactivating it for now. - //let old_runtime = std::mem::replace(&mut self.runtime, tokio::runtime::Runtime::new().unwrap()); - //old_runtime.shutdown_background(); - break - } - } - Err(_) => unreachable!(), - } - - } - Err(_) => { + //let old_runtime = std::mem::replace(&mut self.runtime, tokio::runtime::Runtime::new().unwrap()); + //old_runtime.shutdown_background(); + break + } + } + Err(_) => unreachable!(), + } + + } + Err(_) => { self.machine_st.fail = true; } - } + } } } _ => { @@ -4644,24 +4623,27 @@ impl Machine { #[cfg(feature = "http")] #[inline(always)] pub(crate) fn http_answer(&mut self) -> CallResult { - let culprit = self.deref_register(1); - let status_code = self.deref_register(2); - let status_code: u16 = match Number::try_from(status_code) { - Ok(Number::Fixnum(n)) => n.get_num() as u16, - Ok(Number::Integer(n)) => { - let n: Result = (&*n).try_into(); - - if let Ok(value) = n { - value - } else { - self.machine_st.fail = true; - return Ok(()); + let culprit = self.deref_register(1); + let status_code = self.deref_register(2); + let status_code: u16 = match Number::try_from(status_code) { + Ok(Number::Fixnum(n)) => n.get_num() as u16, + Ok(Number::Integer(n)) => { + let n: Result = (&*n).try_into(); + + if let Ok(value) = n { + value + } else { + self.machine_st.fail = true; + return Ok(()); + } } - } - _ => unreachable!() - }; - let stub_gen = || functor_stub(atom!("http_listen"), 2); - let headers = match self.machine_st.try_from_list(self.machine_st.registers[3], stub_gen) { + _ => unreachable!(), + }; + let stub_gen = || functor_stub(atom!("http_listen"), 2); + let headers = match self + .machine_st + .try_from_list(self.machine_st.registers[3], stub_gen) + { Ok(addrs) => { let mut header_map = HeaderMap::new(); for heap_cell in addrs { @@ -4669,7 +4651,7 @@ impl Machine { (HeapCellValueTag::Str, s) => { let name = cell_as_atom_cell!(self.machine_st.heap[s]).get_name(); let value = self.machine_st.value_to_str_like(self.machine_st.heap[s + 1]).unwrap(); - header_map.insert(HeaderName::from_str(&*name.as_str()).unwrap(), HeaderValue::from_str(&*value.as_str()).unwrap()); + header_map.insert(HeaderName::from_str(&name.as_str()).unwrap(), HeaderValue::from_str(&value.as_str()).unwrap()); } _ => { unreachable!() @@ -4747,9 +4729,10 @@ impl Machine { } ) } - if let Ok(_) = self + if self .foreign_function_table - .load_library(&*library_name.as_str(), &functions) + .load_library(&library_name.as_str(), &functions) + .is_ok() { return Ok(()); } @@ -4769,10 +4752,7 @@ impl Machine { let return_value = self.deref_register(3); if let Some(function_name) = self.machine_st.value_to_str_like(function_name) { let stub_gen = || functor_stub(atom!("foreign_call"), 3); - fn map_arg( - mut machine_st: &mut MachineState, - source: HeapCellValue, - ) -> crate::ffi::Value { + fn map_arg(machine_st: &mut MachineState, source: HeapCellValue) -> crate::ffi::Value { match Number::try_from(source) { Ok(Number::Fixnum(n)) => Value::Int(n.get_num()), Ok(Number::Float(n)) => Value::Float(n.into_inner()), @@ -4789,7 +4769,7 @@ impl Machine { { Value::Struct( struct_name.as_str().to_string(), - iter.map(|x| map_arg(&mut machine_st, x)).collect(), + iter.map(|x| map_arg(machine_st, x)).collect(), ) } else { unreachable!() @@ -4812,7 +4792,7 @@ impl Machine { .collect(); match self .foreign_function_table - .exec(&*function_name.as_str(), args) + .exec(&function_name.as_str(), args) { Ok(result) => { match result { @@ -4892,7 +4872,7 @@ impl Machine { Err(e) => return Err(e), }; self.foreign_function_table - .define_struct(&*struct_name.as_str(), fields); + .define_struct(&struct_name.as_str(), fields); return Ok(()); } self.machine_st.fail = true; @@ -4913,13 +4893,13 @@ impl Machine { let reposition = self.machine_st.registers[6]; let stream_type = self.machine_st.registers[7]; - let options = self - .machine_st - .to_stream_options(alias, eof_action, reposition, stream_type); + let options = + self.machine_st + .get_stream_options(alias, eof_action, reposition, stream_type); let src_sink = self.deref_register(1); if let Some(file_spec) = self.machine_st.value_to_str_like(src_sink) { - let file_spec = file_spec.as_atom(&*self.machine_st.atom_tbl); + let file_spec = file_spec.as_atom(&self.machine_st.atom_tbl); let mut stream = self.machine_st @@ -4956,7 +4936,7 @@ impl Machine { Ok(Number::Integer(n)) => { let n: u16 = (&*n).try_into().unwrap(); n - }, + } Ok(Number::Fixnum(n)) => u16::try_from(n.get_num()).unwrap(), _ => { unreachable!(); @@ -4982,7 +4962,8 @@ impl Machine { .map_err(SessionError::from) .and_then(|mut op_decl| { if op_decl.op_desc.get_prec() == 0 { - Ok(op_decl.remove(&mut self.indices.op_dir)) + op_decl.remove(&mut self.indices.op_dir); + Ok(()) } else { let spec = get_op_desc( op_decl.name, @@ -5019,9 +5000,9 @@ impl Machine { let reposition = self.machine_st.registers[4]; let stream_type = self.machine_st.registers[5]; - let options = self - .machine_st - .to_stream_options(alias, eof_action, reposition, stream_type); + let options = + self.machine_st + .get_stream_options(alias, eof_action, reposition, stream_type); *stream.options_mut() = options; Ok(()) @@ -5116,7 +5097,7 @@ impl Machine { Ok(Number::Integer(n)) => { let value: usize = (&*n).try_into().unwrap(); Some(value) - }, + } Ok(Number::Fixnum(n)) => usize::try_from(n.get_num()).ok(), _ => { self.machine_st.fail = true; @@ -5150,37 +5131,35 @@ impl Machine { let module = self.deref_register(2); - match self.match_attribute(self.machine_st.heap[attr_var_list], module, attr) { - Some(AttrListMatch { - prev_tail, - match_site: MatchSite::Match(match_site), - }) => { - let prev_tail = if let Some(prev_tail) = prev_tail { - // not at the head. - prev_tail - } else { - if self.machine_st.heap[match_site + 1].is_var() { - let h = attr_var.get_value() as usize; - - self.machine_st.heap[h] = heap_loc_as_cell!(h); - self.machine_st.trail(TrailRef::Ref(Ref::attr_var(h))); - } - - // at the head. - attr_var_list - }; + if let Some(AttrListMatch { + prev_tail, + match_site: MatchSite::Match(match_site), + }) = self.match_attribute(self.machine_st.heap[attr_var_list], module, attr) + { + let prev_tail = if let Some(prev_tail) = prev_tail { + // not at the head. + prev_tail + } else { + if self.machine_st.heap[match_site + 1].is_var() { + let h = attr_var.get_value() as usize; - if self.machine_st.heap[match_site + 1].get_tag() == HeapCellValueTag::Lis { - let prev_tail_value = self.machine_st.heap[match_site + 1].get_value(); - self.machine_st.heap[prev_tail].set_value(prev_tail_value); - } else { - self.machine_st.heap[prev_tail] = heap_loc_as_cell!(prev_tail); + self.machine_st.heap[h] = heap_loc_as_cell!(h); + self.machine_st.trail(TrailRef::Ref(Ref::attr_var(h))); } - self.machine_st - .trail(TrailRef::AttrVarListLink(prev_tail, match_site)); + // at the head. + attr_var_list + }; + + if self.machine_st.heap[match_site + 1].get_tag() == HeapCellValueTag::Lis { + let prev_tail_value = self.machine_st.heap[match_site + 1].get_value(); + self.machine_st.heap[prev_tail].set_value(prev_tail_value); + } else { + self.machine_st.heap[prev_tail] = heap_loc_as_cell!(prev_tail); } - _ => {} + + self.machine_st + .trail(TrailRef::AttrVarListLink(prev_tail, match_site)); } } @@ -5502,7 +5481,7 @@ impl Machine { Ok(Number::Integer(n)) => { let n: u8 = (&*n).try_into().unwrap(); n - }, + } Ok(Number::Rational(r)) => { // n has already been confirmed as an integer, and // internally, Rational is assumed reduced, so its @@ -5541,7 +5520,7 @@ impl Machine { Ok(Number::Integer(n)) => { let value: usize = (&*n).try_into().unwrap(); value - }, + } _ => { let stub = functor_stub(atom!("call_with_inference_limit"), 3); @@ -5555,7 +5534,7 @@ impl Machine { let count = self.machine_st.cwil.add_limit(n, bp); let result = count.try_into(); - if let Ok(value) = result{ + if let Ok(value) = result { self.machine_st.unify_fixnum(Fixnum::build_with(value), a3); } else { let count = arena_alloc!(count.clone(), &mut self.machine_st.arena); @@ -5624,10 +5603,7 @@ impl Machine { .map(|index| index.get()) .unwrap_or(IndexPtr::dynamic_undefined()); - match index.tag() { - IndexPtrTag::DynamicUndefined | IndexPtrTag::Undefined => false, - _ => true, - } + !matches!(index.tag(), IndexPtrTag::DynamicUndefined | IndexPtrTag::Undefined) } } (HeapCellValueTag::Atom, (name, arity)) => { @@ -5644,10 +5620,7 @@ impl Machine { .map(|index| index.get()) .unwrap_or(IndexPtr::dynamic_undefined()); - match index.tag() { - IndexPtrTag::DynamicUndefined => false, - _ => true, - } + !matches!(index.tag(), IndexPtrTag::DynamicUndefined) } } _ => { @@ -5711,7 +5684,7 @@ impl Machine { let count = self.machine_st.cwil.remove_limit(block).clone(); let result = count.clone().try_into(); - if let Ok(value) = result{ + if let Ok(value) = result { self.machine_st.unify_fixnum(Fixnum::build_with(value), a2); } else { let count = arena_alloc!(count.clone(), &mut self.machine_st.arena); @@ -5899,9 +5872,9 @@ impl Machine { let addr = self.deref_register(1); let h = self.machine_st.heap.len(); - if self.machine_st.ball.stub.len() > 0 { + if !self.machine_st.ball.stub.is_empty() { let stub = self.machine_st.ball.copy_and_align(h); - self.machine_st.heap.extend(stub.into_iter()); + self.machine_st.heap.extend(stub); } else { self.machine_st.fail = true; return; @@ -5915,7 +5888,7 @@ impl Machine { #[inline(always)] pub(crate) fn push_ball_stack(&mut self) { - if self.machine_st.ball.stub.len() > 0 { + if !self.machine_st.ball.stub.is_empty() { self.machine_st .ball_stack .push(mem::replace(&mut self.machine_st.ball, Ball::new())); @@ -6103,7 +6076,7 @@ impl Machine { Ok(()) => Ok(()), Err(e) => { self.user_input.reset(); - return Err(e); + Err(e) } } } @@ -6140,8 +6113,8 @@ impl Machine { atom_or_string: AtomOrString, ) -> Result, MachineStub> { let string = match atom_or_string { - AtomOrString::Atom(atom) if atom == atom!("[]") => "".to_owned(), - _ => atom_or_string.to_string(), + AtomOrString::Atom(atom!("[]")) => "".to_owned(), + _ => atom_or_string.into(), }; let chars = CharReader::new(ByteStream::from_string(string)); @@ -6264,14 +6237,14 @@ impl Machine { let n: u64 = Integer::from(n).try_into().unwrap(); let rng: StdRng = SeedableRng::seed_from_u64(n); self.rng = rng; - }, + } Ok(Number::Integer(n)) => { let n: u64 = (&*n).try_into().unwrap(); let rng: StdRng = SeedableRng::seed_from_u64(n); self.rng = rng; - }, + } Ok(Number::Rational(n)) => { - if n.denominator() == &UBig::from(1 as u32) { + if n.denominator() == &UBig::ONE { let n: u64 = n.numerator().try_into().unwrap(); let rng: StdRng = SeedableRng::seed_from_u64(n); self.rng = rng; @@ -6344,9 +6317,9 @@ impl Machine { let reposition = self.machine_st.registers[6]; let stream_type = self.machine_st.registers[7]; - let options = self - .machine_st - .to_stream_options(alias, eof_action, reposition, stream_type); + let options = + self.machine_st + .get_stream_options(alias, eof_action, reposition, stream_type); if options.reposition() { return Err(self @@ -6490,9 +6463,9 @@ impl Machine { let reposition = self.machine_st.registers[6]; let stream_type = self.machine_st.registers[7]; - let options = self - .machine_st - .to_stream_options(alias, eof_action, reposition, stream_type); + let options = + self.machine_st + .get_stream_options(alias, eof_action, reposition, stream_type); if options.reposition() { return Err(self @@ -6574,7 +6547,7 @@ impl Machine { )?; let connector = TlsConnector::new().unwrap(); - let stream = match connector.connect(&*hostname.as_str(), stream0) { + let stream = match connector.connect(&hostname.as_str(), stream0) { Ok(tls_stream) => tls_stream, Err(_) => { return Err(self.machine_st.open_permission_error( @@ -6609,7 +6582,7 @@ impl Machine { .machine_st .value_to_str_like(self.machine_st.registers[2]) { - let identity = match Identity::from_pkcs12(&pkcs12, &*password.as_str()) { + let identity = match Identity::from_pkcs12(&pkcs12, &password.as_str()) { Ok(identity) => identity, Err(_) => { return Err(self.machine_st.open_permission_error( @@ -6680,7 +6653,7 @@ impl Machine { let err = self.machine_st.type_error(ValidType::TcpListener, culprit); let stub = functor_stub(atom!("socket_server_close"), 1); - return Err(self.machine_st.error_form(err, stub)); + Err(self.machine_st.error_form(err, stub)) } #[inline(always)] @@ -6776,7 +6749,7 @@ impl Machine { ] ); - self.machine_st.heap.extend(position_term.into_iter()); + self.machine_st.heap.extend(position_term); str_loc_as_cell!(h) } else { self.machine_st.fail = true; @@ -6969,6 +6942,7 @@ impl Machine { instr.enqueue_functors(h, &mut self.machine_st.arena, &mut functors); let new_len = functors.len(); + #[allow(clippy::needless_range_loop)] for index in old_len..new_len { let functor_len = functors[index].len(); @@ -7007,7 +6981,7 @@ impl Machine { Ok(Number::Integer(n)) => { let value: usize = (&*n).try_into().unwrap(); value - }, + } _ => { unreachable!() } @@ -7031,7 +7005,7 @@ impl Machine { }; let first_idx = match first_idx { - Some(ref idx) if idx.local().is_some() => { + Some(idx) if idx.local().is_some() => { if let Some(idx) = idx.local() { idx } else { @@ -7063,7 +7037,7 @@ impl Machine { Ok(Number::Integer(n)) => { let value: usize = (&*n).try_into().unwrap(); value - }, + } _ => { unreachable!() } @@ -7374,9 +7348,7 @@ impl Machine { }; let salt = hkdf::Salt::new(digest_alg, &salt); - let mut bytes: Vec = Vec::new(); - - bytes.resize(length, 0); + let mut bytes: Vec = vec![0; length]; match salt.extract(&data).expand(&[&info[..]], MyKey(length)) { Ok(r) => { @@ -7423,7 +7395,7 @@ impl Machine { return; } } - }, + } _ => { unreachable!() } @@ -7546,7 +7518,7 @@ impl Machine { } }; - if buffer.len() == 0 { + if buffer.is_empty() { empty_list_as_cell!() } else { atom_as_cstr_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, &buffer)) @@ -7723,7 +7695,7 @@ impl Machine { .machine_st .value_to_str_like(self.machine_st.registers[1]) { - match roxmltree::Document::parse(&*string.as_str()) { + match roxmltree::Document::parse(&string.as_str()) { Ok(doc) => { let result = self.xml_node_to_term(doc.root_element()); unify!(self.machine_st, self.machine_st.registers[2], result); @@ -7836,7 +7808,7 @@ impl Machine { match env::var("SHELL") { Ok(value) => { - let command = process::Command::new(&value) + let command = process::Command::new(value) .arg("-c") .arg(&*command.as_str()) .status(); @@ -7844,7 +7816,7 @@ impl Machine { } _ => match env::var("COMSPEC") { Ok(value) => { - let command = process::Command::new(&value) + let command = process::Command::new(value) .arg("/C") .arg(&*command.as_str()) .status(); @@ -7862,18 +7834,11 @@ impl Machine { let padding = cell_as_atom!(self.deref_register(3)); let charset = cell_as_atom!(self.deref_register(4)); - let config = if padding == atom!("true") { - if charset == atom!("standard") { - base64::STANDARD - } else { - base64::URL_SAFE - } - } else { - if charset == atom!("standard") { - base64::STANDARD_NO_PAD - } else { - base64::URL_SAFE_NO_PAD - } + let config = match (padding, charset) { + (atom!("true"), atom!("standard")) => base64::STANDARD, + (atom!("true"), _) => base64::URL_SAFE, + (_, atom!("standard")) => base64::STANDARD_NO_PAD, + (_, _) => base64::URL_SAFE_NO_PAD, }; if self.deref_register(1).is_var() { @@ -7907,7 +7872,7 @@ impl Machine { } let b64 = base64::encode_config(bytes, config); - let string = self.u8s_to_string(&b64.as_bytes()); + let string = self.u8s_to_string(b64.as_bytes()); unify!(self.machine_st, self.machine_st.registers[2], string); } @@ -8058,8 +8023,10 @@ impl Machine { } Ok(Number::Integer(n)) => { let value: usize = if n.sign() == Sign::Positive { - UBig::from((&*n).clone().into_parts().1).count_ones() - } else { 0 }; + (*n).clone().into_parts().1.count_ones() + } else { + 0 + }; Number::arena_from(value, &mut self.machine_st.arena) } _ => { @@ -8119,7 +8086,7 @@ impl Machine { let name = AtomTable::build_with(&self.machine_st.atom_tbl, attr.name()); let value = put_complete_string( &mut self.machine_st.heap, - &attr.value(), + attr.value(), &self.machine_st.atom_tbl, ); @@ -8175,7 +8142,7 @@ impl Machine { let name = AtomTable::build_with(&self.machine_st.atom_tbl, attr.0); let value = put_complete_string( &mut self.machine_st.heap, - &attr.1, + attr.1, &self.machine_st.atom_tbl, ); @@ -8220,7 +8187,7 @@ impl Machine { pub(super) fn u8s_to_string(&mut self, data: &[u8]) -> HeapCellValue { let buffer = String::from_iter(data.iter().map(|b| *b as char)); - if buffer.len() == 0 { + if buffer.is_empty() { empty_list_as_cell!() } else { atom_as_cstr_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, &buffer)) diff --git a/src/machine/unify.rs b/src/machine/unify.rs index 104c51574..3c100ce43 100644 --- a/src/machine/unify.rs +++ b/src/machine/unify.rs @@ -1,6 +1,6 @@ use crate::arena::*; use crate::forms::*; -use crate::heap_iter::{NonListElider, stackful_preorder_iter}; +use crate::heap_iter::{stackful_preorder_iter, NonListElider}; use crate::machine::machine_state::*; use crate::machine::partial_string::*; use crate::machine::*; @@ -204,7 +204,7 @@ pub(crate) trait Unifier: DerefMut { let mut focus = pstr_iter2.focus; - 'outer: loop { + 'outer: { while let Some(c) = chars_iter.peek() { read_heap_cell!(focus, (HeapCellValueTag::Lis, l) => { @@ -329,8 +329,6 @@ pub(crate) trait Unifier: DerefMut { machine_st.pdl.push(focus); machine_st.pdl.push(chars_iter.iter.focus); - - break; } } PStrCmpResult::Unordered => { @@ -609,10 +607,8 @@ pub(crate) trait Unifier: DerefMut { } } (HeapCellValueTag::Lis, l1) => { - if d2.is_ref() { - if tabu_list.contains(&(d1, d2)) { - continue; - } + if d2.is_ref() && tabu_list.contains(&(d1, d2)) { + continue; } Self::unify_list(self, l1, d2); @@ -720,7 +716,11 @@ fn bind_with_occurs_check(unifier: &mut U, r: Ref, value: HeapCellVa if !value.is_constant() { let machine_st: &mut MachineState = unifier.deref_mut(); - for cell in stackful_preorder_iter::(&mut machine_st.heap, &mut machine_st.stack, value) { + for cell in stackful_preorder_iter::( + &mut machine_st.heap, + &mut machine_st.stack, + value, + ) { let cell = unmark_cell_bits!(cell); if let Some(inner_r) = cell.as_var() { @@ -738,7 +738,7 @@ fn bind_with_occurs_check(unifier: &mut U, r: Ref, value: HeapCellVa U::bind(unifier, r, value); } - return occurs_triggered; + occurs_triggered } #[derive(Deref, DerefMut)] diff --git a/src/parser/ast.rs b/src/parser/ast.rs index f1f150801..4859a65e9 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -247,11 +247,7 @@ impl GenContext { #[inline] pub fn is_last(self) -> bool { - if let GenContext::Last(_) = self { - true - } else { - false - } + matches!(self, GenContext::Last(_)) } } @@ -303,24 +299,16 @@ impl OpDesc { // name and fixity -> operator type and precedence. pub type OpDir = IndexMap<(Atom, Fixity), OpDesc, FxBuildHasher>; -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Default, Clone, Copy)] pub struct MachineFlags { pub double_quotes: DoubleQuotes, pub unknown: Unknown, } -impl Default for MachineFlags { - fn default() -> Self { - MachineFlags { - double_quotes: DoubleQuotes::default(), - unknown: Unknown::default(), - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Default, Clone, Copy, PartialEq)] pub enum DoubleQuotes { Atom, + #[default] Chars, Codes, } @@ -339,12 +327,6 @@ impl DoubleQuotes { } } -impl Default for DoubleQuotes { - fn default() -> Self { - DoubleQuotes::Chars - } -} - #[derive(Debug, Clone, Copy)] pub enum Unknown { Error, @@ -505,7 +487,7 @@ impl<'a, 'b> CompositeOpDir<'a, 'b> { #[inline] pub(crate) fn get(&self, name: Atom, fixity: Fixity) -> Option { - let entry = if let Some(ref primary_op_dir) = &self.primary_op_dir { + let entry = if let Some(primary_op_dir) = &self.primary_op_dir { primary_op_dir.get(&(name, fixity)) } else { None @@ -567,7 +549,7 @@ impl Fixnum { const UPPER_BOUND: i64 = (1 << 55) - 1; const LOWER_BOUND: i64 = -(1 << 55); - if LOWER_BOUND <= num && num <= UPPER_BOUND { + if (LOWER_BOUND..=UPPER_BOUND).contains(&num) { Ok(Fixnum::new() .with_m(false) .with_f(false) @@ -582,7 +564,7 @@ impl Fixnum { pub fn get_num(self) -> i64 { let n = self.num() as i64; let (n, overflowed) = (n << 8).overflowing_shr(8); - debug_assert_eq!(overflowed, false); + debug_assert!(!overflowed); n } } @@ -730,11 +712,12 @@ impl Var { #[inline(always)] pub fn as_str(&self) -> Option<&str> { match self { - Var::Named(value) => Some(&value), + Var::Named(value) => Some(value), _ => None, } } + #[allow(clippy::inherent_to_string)] #[inline(always)] pub fn to_string(&self) -> String { match self { @@ -800,10 +783,8 @@ impl Term { #[inline] pub fn source_arity(terms: &[Term]) -> usize { - if let Some(last_arg) = terms.last() { - if let Term::Literal(_, Literal::CodeIndex(_)) = last_arg { - return terms.len() - 1; - } + if let Some(Term::Literal(_, Literal::CodeIndex(_))) = terms.last() { + return terms.len() - 1; } terms.len() @@ -811,10 +792,8 @@ pub fn source_arity(terms: &[Term]) -> usize { pub(crate) fn unfold_by_str_once(term: &mut Term, s: Atom) -> Option<(Term, Term)> { if let Term::Clause(_, ref name, ref mut subterms) = term { - if let Some(last_arg) = subterms.last() { - if let Term::Literal(_, Literal::CodeIndex(_)) = last_arg { - subterms.pop(); - } + if let Some(Term::Literal(_, Literal::CodeIndex(_))) = subterms.last() { + subterms.pop(); } if name == &s && subterms.len() == 2 { diff --git a/src/parser/char_reader.rs b/src/parser/char_reader.rs index fb6b57447..bf3b8f8e7 100644 --- a/src/parser/char_reader.rs +++ b/src/parser/char_reader.rs @@ -131,15 +131,9 @@ impl CharReader { pub fn peek_byte(&mut self) -> Option> { match self.refresh_buffer() { - Ok(_buf) => {} - Err(e) => return Some(Err(e)), + Ok(_buf) => _buf.first().cloned().map(Ok), + Err(e) => Some(Err(e)), } - - return if let Some(b) = self.buf.get(0).cloned() { - Some(Ok(b)) - } else { - None - }; } } @@ -194,49 +188,47 @@ impl CharRead for CharReader { io::ErrorKind::InvalidData, BadUtf8Error { bytes: badbytes }, ))); - } else { - if self.pos >= self.buf.len() { - return None; - } else if self.buf.len() - self.pos >= 4 { - return match str::from_utf8(&self.buf[self.pos..e.valid_up_to()]) { - Ok(s) => { - let mut chars = s.chars(); - let c = chars.next().unwrap(); - - Some(Ok(c)) - } - Err(e) => { - let badbytes = self.buf[self.pos..e.valid_up_to()].to_vec(); - - Some(Err(io::Error::new( - io::ErrorKind::InvalidData, - BadUtf8Error { bytes: badbytes }, - ))) - } - }; - } else { - let buf_len = self.buf.len(); - - for (c, idx) in (self.pos..buf_len).enumerate() { - self.buf[c] = self.buf[idx]; + } else if self.pos >= self.buf.len() { + return None; + } else if self.buf.len() - self.pos >= 4 { + return match str::from_utf8(&self.buf[self.pos..e.valid_up_to()]) { + Ok(s) => { + let mut chars = s.chars(); + let c = chars.next().unwrap(); + + Some(Ok(c)) } + Err(e) => { + let badbytes = self.buf[self.pos..e.valid_up_to()].to_vec(); - self.buf.truncate(buf_len - self.pos); + Some(Err(io::Error::new( + io::ErrorKind::InvalidData, + BadUtf8Error { bytes: badbytes }, + ))) + } + }; + } else { + let buf_len = self.buf.len(); - let buf_len = self.buf.len(); + for (c, idx) in (self.pos..buf_len).enumerate() { + self.buf[c] = self.buf[idx]; + } - let mut word = [0u8; 4]; - let word_slice = &mut word[buf_len..4]; + self.buf.truncate(buf_len - self.pos); - match self.inner.read(word_slice) { - Err(e) => return Some(Err(e)), - Ok(nread) => { - self.buf.extend_from_slice(&word_slice[0..nread]); - } - } + let buf_len = self.buf.len(); - self.pos = 0; + let mut word = [0u8; 4]; + let word_slice = &mut word[buf_len..4]; + + match self.inner.read(word_slice) { + Err(e) => return Some(Err(e)), + Ok(nread) => { + self.buf.extend_from_slice(&word_slice[0..nread]); + } } + + self.pos = 0; } } else { return None; diff --git a/src/parser/lexer.rs b/src/parser/lexer.rs index b73c8028f..d783c5885 100644 --- a/src/parser/lexer.rs +++ b/src/parser/lexer.rs @@ -48,11 +48,7 @@ pub enum Token { impl Token { #[inline] pub(super) fn is_end(&self) -> bool { - if let Token::End = self { - true - } else { - false - } + matches!(self, Token::End) } } @@ -604,10 +600,7 @@ impl<'a, R: CharRead> Lexer<'a, R> { break; } } - } else if cut_char!(c) { - self.skip_char(c); - token.push(c); - } else if semicolon_char!(c) { + } else if cut_char!(c) || semicolon_char!(c) { self.skip_char(c); token.push(c); } else if single_quote_char!(c) { @@ -690,7 +683,8 @@ impl<'a, R: CharRead> Lexer<'a, R> { if self.reader.peek_char().is_none() { self.return_char('.'); - i64::from_str_radix(&token, 10) + token + .parse::() .map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))) .or_else(|_| { token @@ -720,12 +714,12 @@ impl<'a, R: CharRead> Lexer<'a, R> { token.push(c); let c = match self.lookahead_char() { - Err(_) => return Ok(self.vacate_with_float(token)?), + Err(_) => return self.vacate_with_float(token), Ok(c) => c, }; if !sign_char!(c) && !decimal_digit_char!(c) { - return Ok(self.vacate_with_float(token)?); + return self.vacate_with_float(token); } if sign_char!(c) { @@ -735,14 +729,14 @@ impl<'a, R: CharRead> Lexer<'a, R> { let c = match self.lookahead_char() { Err(_) => { self.return_char(token.pop().unwrap()); - return Ok(self.vacate_with_float(token)?); + return self.vacate_with_float(token); } Ok(c) => c, }; if !decimal_digit_char!(c) { self.return_char(token.pop().unwrap()); - return Ok(self.vacate_with_float(token)?); + return self.vacate_with_float(token); } } @@ -769,7 +763,7 @@ impl<'a, R: CharRead> Lexer<'a, R> { self.machine_st.arena )))) } else { - return Ok(self.vacate_with_float(token)?); + return self.vacate_with_float(token); } } else { let n = parse_lossy::(token.as_bytes())?; @@ -781,7 +775,8 @@ impl<'a, R: CharRead> Lexer<'a, R> { } else { self.return_char('.'); - i64::from_str_radix(&token, 10) + token + .parse::() .map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))) .or_else(|_| { token @@ -795,143 +790,132 @@ impl<'a, R: CharRead> Lexer<'a, R> { .map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num)) }) } - } else { - if token.starts_with('0') && token.len() == 1 { - if c == 'x' { - self.hexadecimal_constant(c).or_else(|e| { - if let ParserError::ParseBigInt(..) = e { - i64::from_str_radix(&token, 10) - .map(|n| { - Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) - }) - .or_else(|_| { - token - .parse::() - .map(|n| { - Token::Literal(Literal::Integer(arena_alloc!( - n, - &mut self.machine_st.arena - ))) - }) - .map_err(|_| { - ParserError::ParseBigInt(self.line_num, self.col_num) - }) - }) - } else { - Err(e) - } - }) - } else if c == 'o' { - self.octal_constant(c).or_else(|e| { - if let ParserError::ParseBigInt(..) = e { - i64::from_str_radix(&token, 10) - .map(|n| { - Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) - }) - .or_else(|_| { - token - .parse::() - .map(|n| { - Token::Literal(Literal::Integer(arena_alloc!( - n, - &mut self.machine_st.arena - ))) - }) - .map_err(|_| { - ParserError::ParseBigInt(self.line_num, self.col_num) - }) - }) - } else { - Err(e) - } - }) - } else if c == 'b' { - self.binary_constant(c).or_else(|e| { - if let ParserError::ParseBigInt(..) = e { - i64::from_str_radix(&token, 10) - .map(|n| { - Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) - }) - .or_else(|_| { - token - .parse::() - .map(|n| { - Token::Literal(Literal::Integer(arena_alloc!( - n, - &mut self.machine_st.arena - ))) - }) - .map_err(|_| { - ParserError::ParseBigInt(self.line_num, self.col_num) - }) - }) - } else { - Err(e) - } - }) - } else if single_quote_char!(c) { + } else if token.starts_with('0') && token.len() == 1 { + if c == 'x' { + self.hexadecimal_constant(c).or_else(|e| { + if let ParserError::ParseBigInt(..) = e { + token + .parse::() + .map(|n| { + Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) + }) + .or_else(|_| { + token + .parse::() + .map(|n| { + Token::Literal(Literal::Integer(arena_alloc!( + n, + &mut self.machine_st.arena + ))) + }) + .map_err(|_| { + ParserError::ParseBigInt(self.line_num, self.col_num) + }) + }) + } else { + Err(e) + } + }) + } else if c == 'o' { + self.octal_constant(c).or_else(|e| { + if let ParserError::ParseBigInt(..) = e { + token + .parse::() + .map(|n| { + Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) + }) + .or_else(|_| { + token + .parse::() + .map(|n| { + Token::Literal(Literal::Integer(arena_alloc!( + n, + &mut self.machine_st.arena + ))) + }) + .map_err(|_| { + ParserError::ParseBigInt(self.line_num, self.col_num) + }) + }) + } else { + Err(e) + } + }) + } else if c == 'b' { + self.binary_constant(c).or_else(|e| { + if let ParserError::ParseBigInt(..) = e { + token + .parse::() + .map(|n| { + Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) + }) + .or_else(|_| { + token + .parse::() + .map(|n| { + Token::Literal(Literal::Integer(arena_alloc!( + n, + &mut self.machine_st.arena + ))) + }) + .map_err(|_| { + ParserError::ParseBigInt(self.line_num, self.col_num) + }) + }) + } else { + Err(e) + } + }) + } else if single_quote_char!(c) { + self.skip_char(c); + let c = self.lookahead_char()?; + + if backslash_char!(c) { self.skip_char(c); let c = self.lookahead_char()?; - if backslash_char!(c) { + if new_line_char!(c) { self.skip_char(c); - let c = self.lookahead_char()?; + self.return_char('\''); - if new_line_char!(c) { - self.skip_char(c); - self.return_char('\''); + return Ok(Token::Literal(Literal::Fixnum(Fixnum::build_with(0)))); + } else { + self.return_char('\\'); + } + } - return Ok(Token::Literal(Literal::Fixnum(Fixnum::build_with(0)))); - } else { - self.return_char('\\'); + self.get_single_quoted_char() + .map(|c| Token::Literal(Literal::Fixnum(Fixnum::build_with(c as i64)))) + .or_else(|err| { + match err { + ParserError::UnexpectedChar('\'', ..) => {} + err => return Err(err), } - } - self.get_single_quoted_char() - .map(|c| Token::Literal(Literal::Fixnum(Fixnum::build_with(c as i64)))) - .or_else(|err| { - match err { - ParserError::UnexpectedChar('\'', ..) => {} - err => return Err(err), - } + self.return_char(c); - self.return_char(c); - - i64::from_str_radix(&token, 10) - .map(|n| { - Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) - }) - .or_else(|_| { - token - .parse::() - .map(|n| { - Token::Literal(Literal::Integer(arena_alloc!( - n, - &mut self.machine_st.arena - ))) - }) - .map_err(|_| { - ParserError::ParseBigInt(self.line_num, self.col_num) - }) - }) - }) - } else { - i64::from_str_radix(&token, 10) - .map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))) - .or_else(|_| { - token - .parse::() - .map(|n| { - Token::Literal(Literal::Integer(arena_alloc!( - n, - &mut self.machine_st.arena - ))) - }) - .map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num)) - }) - } + token + .parse::() + .map(|n| { + Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena)) + }) + .or_else(|_| { + token + .parse::() + .map(|n| { + Token::Literal(Literal::Integer(arena_alloc!( + n, + &mut self.machine_st.arena + ))) + }) + .map_err(|_| { + ParserError::ParseBigInt(self.line_num, self.col_num) + }) + }) + }) } else { - i64::from_str_radix(&token, 10) + token + .parse::() .map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))) .or_else(|_| { token @@ -945,6 +929,21 @@ impl<'a, R: CharRead> Lexer<'a, R> { .map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num)) }) } + } else { + token + .parse::() + .map(|n| Token::Literal(fixnum!(Literal, n, &mut self.machine_st.arena))) + .or_else(|_| { + token + .parse::() + .map(|n| { + Token::Literal(Literal::Integer(arena_alloc!( + n, + &mut self.machine_st.arena + ))) + }) + .map_err(|_| ParserError::ParseBigInt(self.line_num, self.col_num)) + }) } } @@ -953,6 +952,7 @@ impl<'a, R: CharRead> Lexer<'a, R> { c: Option, layout_info: &mut LayoutInfo, ) -> Result<(), ParserError> { + #[allow(clippy::redundant_guards)] match c { Some(c) if layout_char!(c) => { self.skip_char(c); diff --git a/src/parser/macros.rs b/src/parser/macros.rs index 26bd0b2b1..ecf629f8d 100644 --- a/src/parser/macros.rs +++ b/src/parser/macros.rs @@ -77,7 +77,7 @@ macro_rules! cut_char { #[macro_export] macro_rules! decimal_digit_char { ($c: expr) => { - ('0'..='9').contains(&$c) + $c.is_ascii_digit() }; } @@ -125,7 +125,7 @@ macro_rules! graphic_token_char { #[macro_export] macro_rules! hexadecimal_digit_char { ($c: expr) => { - ('0'..='9').contains(&$c) || ('A'..='F').contains(&$c) || ('a'..='f').contains(&$c) + $c.is_ascii_digit() || ('A'..='F').contains(&$c) || ('a'..='f').contains(&$c) }; } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index ef6a8e0a0..c2e23bb74 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -11,4 +11,5 @@ pub mod ast; #[macro_use] pub mod macros; pub mod lexer; +#[allow(clippy::module_inception)] pub mod parser; diff --git a/src/parser/parser.rs b/src/parser/parser.rs index f2de37bc6..de2b04a08 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -464,7 +464,12 @@ impl<'a, R: CharRead> Parser<'a, R> { Token::End => TokenType::End, }; - self.stack.push(TokenDesc { tt, priority, spec, unfold_bounds: 0, }); + self.stack.push(TokenDesc { + tt, + priority, + spec, + unfold_bounds: 0, + }); } fn reduce_op(&mut self, priority: usize) { @@ -472,10 +477,9 @@ impl<'a, R: CharRead> Parser<'a, R> { if let Some(desc1) = self.stack.pop() { if let Some(desc2) = self.stack.pop() { if let Some(desc3) = self.stack.pop() { - if is_xfx!(desc2.spec) && affirm_xfx(priority, desc2, desc3, desc1) { - self.push_binary_op(desc2, LTERM); - continue; - } else if is_yfx!(desc2.spec) && affirm_yfx(priority, desc2, desc3, desc1) { + if is_xfx!(desc2.spec) && affirm_xfx(priority, desc2, desc3, desc1) + || is_yfx!(desc2.spec) && affirm_yfx(priority, desc2, desc3, desc1) + { self.push_binary_op(desc2, LTERM); continue; } else if is_xfy!(desc2.spec) && affirm_xfy(priority, desc2, desc3, desc1) { @@ -555,10 +559,12 @@ impl<'a, R: CharRead> Parser<'a, R> { if self.stack.len() > 2 * arity { let idx = self.stack.len() - 2 * arity - 1; - if is_infix!(self.stack[idx].spec) && idx > 0 { - if !is_op!(self.stack[idx - 1].spec) && !self.stack[idx - 1].tt.is_sep() { - return false; - } + if is_infix!(self.stack[idx].spec) + && idx > 0 + && !is_op!(self.stack[idx - 1].spec) + && !self.stack[idx - 1].tt.is_sep() + { + return false; } } else { return false; @@ -571,59 +577,57 @@ impl<'a, R: CharRead> Parser<'a, R> { let stack_len = self.stack.len() - 2 * arity - 1; let idx = self.terms.len() - arity; - if TokenType::Term == self.stack[stack_len].tt { - if atomize_term(&self.lexer.machine_st.atom_tbl, &self.terms[idx - 1]).is_some() { - self.stack.truncate(stack_len + 1); - - let mut subterms: Vec<_> = self.terms.drain(idx..).collect(); + if TokenType::Term == self.stack[stack_len].tt + && atomize_term(&self.lexer.machine_st.atom_tbl, &self.terms[idx - 1]).is_some() + { + self.stack.truncate(stack_len + 1); - if let Some(name) = self - .terms - .pop() - .and_then(|t| atomize_term(&self.lexer.machine_st.atom_tbl, &t)) - { - // reduce the '.' functor to a cons cell if it applies. - if name == atom!(".") && subterms.len() == 2 { - let tail = subterms.pop().unwrap(); - let head = subterms.pop().unwrap(); - - self.terms.push(match as_partial_string(head, tail) { - Ok((string_buf, Some(tail))) => { - Term::PartialString(Cell::default(), string_buf, tail) - } - Ok((string_buf, None)) => { - let atom = AtomTable::build_with( - &self.lexer.machine_st.atom_tbl, - &string_buf, - ); - Term::CompleteString(Cell::default(), atom) - } - Err(term) => term, - }); - } else { - self.terms - .push(Term::Clause(Cell::default(), name, subterms)); - } + let mut subterms: Vec<_> = self.terms.drain(idx..).collect(); - if let Some(&mut TokenDesc { - ref mut tt, - ref mut priority, - ref mut spec, - ref mut unfold_bounds, - }) = self.stack.last_mut() - { - if *spec == BTERM { - return false; + if let Some(name) = self + .terms + .pop() + .and_then(|t| atomize_term(&self.lexer.machine_st.atom_tbl, &t)) + { + // reduce the '.' functor to a cons cell if it applies. + if name == atom!(".") && subterms.len() == 2 { + let tail = subterms.pop().unwrap(); + let head = subterms.pop().unwrap(); + + self.terms.push(match as_partial_string(head, tail) { + Ok((string_buf, Some(tail))) => { + Term::PartialString(Cell::default(), string_buf, tail) } + Ok((string_buf, None)) => { + let atom = + AtomTable::build_with(&self.lexer.machine_st.atom_tbl, &string_buf); + Term::CompleteString(Cell::default(), atom) + } + Err(term) => term, + }); + } else { + self.terms + .push(Term::Clause(Cell::default(), name, subterms)); + } - *tt = TokenType::Term; - *priority = 0; - *spec = TERM; - *unfold_bounds = 0; + if let Some(&mut TokenDesc { + ref mut tt, + ref mut priority, + ref mut spec, + ref mut unfold_bounds, + }) = self.stack.last_mut() + { + if *spec == BTERM { + return false; } - return true; + *tt = TokenType::Term; + *priority = 0; + *spec = TERM; + *unfold_bounds = 0; } + + return true; } } @@ -642,34 +646,31 @@ impl<'a, R: CharRead> Parser<'a, R> { /* '|' is a head-tail separator here, not * an operator, so expand the * terms it compacted out again. */ - match (term.name(), term.arity()) { - (Some(name), 2) if name == atom!(",") => { - let terms = if op_desc.unfold_bounds == 0 { - unfold_by_str(term, atom!(",")) - } else { - let mut terms = vec![]; + if let (Some(atom!(",")), 2) = (term.name(), term.arity()) { + let terms = if op_desc.unfold_bounds == 0 { + unfold_by_str(term, atom!(",")) + } else { + let mut terms = vec![]; - while let Some((fst, snd)) = unfold_by_str_once(&mut term, atom!(",")) { - terms.push(fst); - term = snd; + while let Some((fst, snd)) = unfold_by_str_once(&mut term, atom!(",")) { + terms.push(fst); + term = snd; - op_desc.unfold_bounds -= 2; + op_desc.unfold_bounds -= 2; - if op_desc.unfold_bounds == 0 { - break; - } + if op_desc.unfold_bounds == 0 { + break; } + } - terms.push(term); - terms - }; + terms.push(term); + terms + }; - let arity = terms.len() - 1; + let arity = terms.len() - 1; - self.terms.extend(terms.into_iter()); - return arity; - } - _ => {} + self.terms.extend(terms); + return arity; } } @@ -692,18 +693,15 @@ impl<'a, R: CharRead> Parser<'a, R> { } else { return None; } - } else { - if desc.tt == TokenType::HeadTailSeparator { - if arity == 1 { - continue; - } - - return None; - } else if desc.tt == TokenType::OpenList { - return Some(arity); - } else if desc.tt != TokenType::Comma { - return None; + } else if desc.tt == TokenType::HeadTailSeparator { + if arity == 1 { + continue; } + return None; + } else if desc.tt == TokenType::OpenList { + return Some(arity); + } else if desc.tt != TokenType::Comma { + return None; } } @@ -882,7 +880,11 @@ impl<'a, R: CharRead> Parser<'a, R> { .push(Term::Literal(Cell::default(), Literal::Atom(atom))); } - self.stack[idx].spec = if self.stack[idx].priority > 0 { TERM } else { BTERM }; + self.stack[idx].spec = if self.stack[idx].priority > 0 { + TERM + } else { + BTERM + }; self.stack[idx].tt = TokenType::Term; self.stack[idx].priority = 0; @@ -1018,13 +1020,11 @@ impl<'a, R: CharRead> Parser<'a, R> { Token::Open => self.shift(Token::Open, 1300, DELIMITER), Token::OpenCT => self.shift(Token::OpenCT, 1300, DELIMITER), Token::Close => { - if !self.reduce_term() { - if !self.reduce_brackets() { - return Err(ParserError::IncompleteReduction( - self.lexer.line_num, - self.lexer.col_num, - )); - } + if !self.reduce_term() && !self.reduce_brackets() { + return Err(ParserError::IncompleteReduction( + self.lexer.line_num, + self.lexer.col_num, + )); } } Token::OpenList => self.shift(Token::OpenList, 1300, DELIMITER), diff --git a/src/raw_block.rs b/src/raw_block.rs index 53b4f198d..41be0706d 100644 --- a/src/raw_block.rs +++ b/src/raw_block.rs @@ -28,6 +28,7 @@ impl RawBlock { } } + #[allow(clippy::new_without_default)] pub fn new() -> Self { let mut block = Self::empty_block(); @@ -71,7 +72,7 @@ impl RawBlock { } else { let allocated = (*self.ptr.get()) as usize - self.base as usize; self.base.copy_to(new_block.base.cast_mut(), allocated); - *new_block.ptr.get_mut() = new_block.base.offset(allocated as isize).cast_mut(); + *new_block.ptr.get_mut() = new_block.base.add(allocated).cast_mut(); Some(new_block) } } diff --git a/src/rcu.rs b/src/rcu.rs index 7631caceb..5c7c3201f 100644 --- a/src/rcu.rs +++ b/src/rcu.rs @@ -6,7 +6,7 @@ use std::{ ptr::NonNull, sync::{ atomic::{AtomicPtr, AtomicU8}, - Arc, Weak, RwLock + Arc, RwLock, Weak, }, }; @@ -52,7 +52,7 @@ impl Rcu { // register the current threads epoch counter on init EPOCH_COUNTERS .write() - .unwrap() + .unwrap() .push(Arc::downgrade(&epoch_counter)); epoch_counter }); diff --git a/src/read.rs b/src/read.rs index 4955c6700..1115bf1a3 100644 --- a/src/read.rs +++ b/src/read.rs @@ -31,8 +31,8 @@ use std::sync::Arc; type SubtermDeque = VecDeque<(usize, usize)>; -pub(crate) fn devour_whitespace<'a, R: CharRead>( - parser: &mut Parser<'a, R>, +pub(crate) fn devour_whitespace( + parser: &mut Parser<'_, R>, ) -> Result { match parser.lexer.scan_for_layout() { Err(e) if e.is_unexpected_eof() => Ok(true), @@ -86,7 +86,7 @@ impl MachineState { static mut PROMPT: bool = false; #[cfg(feature = "repl")] -const HISTORY_FILE: &'static str = ".scryer_history"; +const HISTORY_FILE: &str = ".scryer_history"; pub(crate) fn set_prompt(value: bool) { unsafe { @@ -137,7 +137,7 @@ impl ReadlineStream { ReadlineStream { rl, pending_input: CharReader::new(Cursor::new(pending_input.to_owned())), - add_history: add_history, + add_history, } } @@ -145,7 +145,7 @@ impl ReadlineStream { { ReadlineStream { pending_input: CharReader::new(Cursor::new(pending_input.to_owned())), - add_history: add_history, + add_history, } } } @@ -187,7 +187,7 @@ impl ReadlineStream { PROMPT = false; } - if self.pending_input.get_ref().get_ref().chars().last() != Some('\n') { + if !self.pending_input.get_ref().get_ref().ends_with('\n') { *self.pending_input.get_mut().get_mut() += "\n"; } } @@ -292,9 +292,9 @@ impl CharRead for ReadlineStream { } #[inline] -pub(crate) fn write_term_to_heap<'a, 'b>( - term: &'a Term, - heap: &'b mut Heap, +pub(crate) fn write_term_to_heap( + term: &Term, + heap: &mut Heap, atom_tbl: &AtomTable, ) -> Result { let term_writer = TermWriter::new(heap, atom_tbl); @@ -347,7 +347,7 @@ impl<'a, 'b> TermWriter<'a, 'b> { match term { &TermRef::Cons(..) => list_loc_as_cell!(h), &TermRef::AnonVar(_) | &TermRef::Var(..) => heap_loc_as_cell!(h), - &TermRef::CompleteString(_, _, ref src) => { + TermRef::CompleteString(_, _, src) => { if src.as_str().is_empty() { empty_list_as_cell!() } else if self.heap[h].get_tag() == HeapCellValueTag::CStr { @@ -358,7 +358,7 @@ impl<'a, 'b> TermWriter<'a, 'b> { } &TermRef::PartialString(..) => pstr_loc_as_cell!(h), &TermRef::Literal(_, _, literal) => HeapCellValue::from(*literal), - &TermRef::Clause(_, _, _, subterms) if subterms.len() == 0 => heap_loc_as_cell!(h), + &TermRef::Clause(_, _, _, subterms) if subterms.is_empty() => heap_loc_as_cell!(h), &TermRef::Clause(..) => str_loc_as_cell!(h), } } @@ -390,7 +390,7 @@ impl<'a, 'b> TermWriter<'a, 'b> { return Err(CompilationError::ExceededMaxArity); } - self.heap.push(if subterms.len() == 0 { + self.heap.push(if subterms.is_empty() { heap_loc_as_cell!(heap_loc + 1) } else { str_loc_as_cell!(heap_loc + 1) @@ -438,11 +438,11 @@ impl<'a, 'b> TermWriter<'a, 'b> { continue; } - &TermRef::CompleteString(_, _, ref src) => { + TermRef::CompleteString(_, _, src) => { let src = src.as_str().to_owned(); put_complete_string(self.heap, &src, self.atom_tbl); } - &TermRef::PartialString(lvl, _, ref src, _) => { + &TermRef::PartialString(lvl, _, src, _) => { if let Level::Root = lvl { // Var tags can't refer directly to partial strings, // so a PStrLoc cell must be pushed. @@ -458,7 +458,7 @@ impl<'a, 'b> TermWriter<'a, 'b> { continue; } } - &TermRef::Var(.., ref var) => { + TermRef::Var(.., var) => { if let Some((arity, site_h)) = self.queue.pop_front() { let var_key = VarKey::VarPtr(var.clone()); diff --git a/src/targets.rs b/src/targets.rs index 1596aae2c..4a1ce362f 100644 --- a/src/targets.rs +++ b/src/targets.rs @@ -64,10 +64,7 @@ impl<'a> CompilationTarget<'a> for FactInstruction { } fn is_void_instr(instr: &Instruction) -> bool { - match instr { - &Instruction::UnifyVoid(_) => true, - _ => false, - } + matches!(instr, &Instruction::UnifyVoid(_)) } fn to_pstr(lvl: Level, string: Atom, r: RegType, has_tail: bool) -> Instruction { @@ -75,9 +72,8 @@ impl<'a> CompilationTarget<'a> for FactInstruction { } fn incr_void_instr(instr: &mut Instruction) { - match instr { - &mut Instruction::UnifyVoid(ref mut incr) => *incr += 1, - _ => {} + if let &mut Instruction::UnifyVoid(ref mut incr) = instr { + *incr += 1 } } @@ -146,16 +142,12 @@ impl<'a> CompilationTarget<'a> for QueryInstruction { } fn is_void_instr(instr: &Instruction) -> bool { - match instr { - &Instruction::SetVoid(_) => true, - _ => false, - } + matches!(instr, &Instruction::SetVoid(_)) } fn incr_void_instr(instr: &mut Instruction) { - match instr { - &mut Instruction::SetVoid(ref mut incr) => *incr += 1, - _ => {} + if let &mut Instruction::SetVoid(ref mut incr) = instr { + *incr += 1 } } diff --git a/src/types.rs b/src/types.rs index 043bd221b..9e488bca2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -194,6 +194,7 @@ pub enum TrailRef { BlackboardOffset(Atom, HeapCellValue), // key atom, key value } +#[allow(clippy::enum_variant_names)] #[derive(BitfieldSpecifier, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] #[bits = 6] pub(crate) enum TrailEntryTag { @@ -331,7 +332,7 @@ impl From for HeapCellValue { } } -impl<'a> From<(Number, &mut Arena)> for HeapCellValue { +impl From<(Number, &mut Arena)> for HeapCellValue { #[inline(always)] fn from((n, arena): (Number, &mut Arena)) -> HeapCellValue { use ordered_float::OrderedFloat; @@ -393,16 +394,16 @@ impl HeapCellValue { #[inline] pub fn is_ref(self) -> bool { - match self.get_tag() { + matches!( + self.get_tag(), HeapCellValueTag::Str - | HeapCellValueTag::Lis - | HeapCellValueTag::Var - | HeapCellValueTag::StackVar - | HeapCellValueTag::AttrVar - | HeapCellValueTag::PStrLoc - | HeapCellValueTag::PStrOffset => true, - _ => false, - } + | HeapCellValueTag::Lis + | HeapCellValueTag::Var + | HeapCellValueTag::StackVar + | HeapCellValueTag::AttrVar + | HeapCellValueTag::PStrLoc + | HeapCellValueTag::PStrOffset + ) } #[inline] @@ -491,7 +492,7 @@ impl HeapCellValue { #[inline] pub fn get_value(self) -> u64 { - self.val() as u64 + self.val() } #[inline] @@ -739,10 +740,7 @@ impl UntypedArenaPtr { #[inline] pub fn payload_offset(self) -> *const u8 { - unsafe { - self.get_ptr() - .offset(mem::size_of::() as isize) - } + unsafe { self.get_ptr().add(mem::size_of::()) } } #[inline] @@ -806,7 +804,7 @@ impl Sub for HeapCellValue { | tag @ HeapCellValueTag::PStrLoc | tag @ HeapCellValueTag::Var | tag @ HeapCellValueTag::AttrVar => { - HeapCellValue::build_with(tag, self.get_value() + rhs.abs() as u64) + HeapCellValue::build_with(tag, self.get_value() + rhs.unsigned_abs()) } _ => self, } diff --git a/src/variable_records.rs b/src/variable_records.rs index bb91a7774..b3546f4c8 100644 --- a/src/variable_records.rs +++ b/src/variable_records.rs @@ -74,10 +74,7 @@ impl PermVarAllocation { #[inline] pub(crate) fn pending(&self) -> bool { - match self { - &PermVarAllocation::Pending => true, - _ => false, - } + matches!(self, &PermVarAllocation::Pending) } } @@ -96,9 +93,9 @@ pub enum VarAlloc { impl VarAlloc { #[inline] pub(crate) fn as_reg_type(&self) -> RegType { - match self { - &VarAlloc::Temp { temp_reg, .. } => RegType::Temp(temp_reg), - &VarAlloc::Perm(r, _) => RegType::Perm(r), + match *self { + VarAlloc::Temp { temp_reg, .. } => RegType::Temp(temp_reg), + VarAlloc::Perm(r, _) => RegType::Perm(r), } } @@ -129,7 +126,7 @@ impl TempVarData { } } - return false; + false } pub(crate) fn populate_conflict_set(&mut self) { @@ -201,16 +198,13 @@ impl VariableRecords { IndexMap::new(); for (var_gen_index, record) in self.0.iter_mut().enumerate() { - match &mut record.allocation { - VarAlloc::Temp { temp_var_data, .. } => { - let use_set = std::mem::replace( - &mut temp_var_data.use_set, - IndexSet::with_hasher(FxBuildHasher::default()), - ); - - use_sets.insert(var_gen_index, use_set); - } - _ => {} + if let VarAlloc::Temp { temp_var_data, .. } = &mut record.allocation { + let use_set = std::mem::replace( + &mut temp_var_data.use_set, + IndexSet::with_hasher(FxBuildHasher::default()), + ); + + use_sets.insert(var_gen_index, use_set); } } @@ -219,19 +213,18 @@ impl VariableRecords { for &(term_loc, reg) in &use_set { if let GenContext::Last(cn_u) = term_loc { for (var_gen_index, record) in self.0.iter_mut().enumerate() { - match &mut record.allocation { - VarAlloc::Temp { - term_loc, - temp_var_data, - .. - } => { - if cn_u == term_loc.chunk_num() && u != var_gen_index { - if !temp_var_data.uses_reg(reg) { - temp_var_data.no_use_set.insert(reg); - } - } + if let VarAlloc::Temp { + term_loc, + temp_var_data, + .. + } = &mut record.allocation + { + if cn_u == term_loc.chunk_num() + && u != var_gen_index + && !temp_var_data.uses_reg(reg) + { + temp_var_data.no_use_set.insert(reg); } - _ => {} } } } diff --git a/tests/scryer/issues.rs b/tests/scryer/issues.rs index 821bbbf30..f7a710076 100644 --- a/tests/scryer/issues.rs +++ b/tests/scryer/issues.rs @@ -69,7 +69,7 @@ fn handle_residual_goal() { #[test] fn occurs_check_flag() { run_top_level_test_with_args( - &["tests-pl/issue841-occurs-check.pl"], + ["tests-pl/issue841-occurs-check.pl"], "\ f(X, X).\n\ halt.\n\ @@ -102,14 +102,14 @@ fn occurs_check_flag2() { // issue #839 #[test] fn op3() { - run_top_level_test_with_args(&["tests-pl/issue839-op3.pl", "-g", "halt"], "", "") + run_top_level_test_with_args(["tests-pl/issue839-op3.pl", "-g", "halt"], "", "") } // issue #820 #[test] fn multiple_goals() { run_top_level_test_with_args( - &["-g", "test", "-g", "halt", "tests-pl/issue820-goals.pl"], + ["-g", "test", "-g", "halt", "tests-pl/issue820-goals.pl"], "", "helloworld\n", ); @@ -119,7 +119,7 @@ fn multiple_goals() { #[test] fn compound_goal() { run_top_level_test_with_args( - &["-g", "test,halt", "tests-pl/issue820-goals.pl"], + ["-g", "test,halt", "tests-pl/issue820-goals.pl"], "", "helloworld\n", ) diff --git a/tests/scryer/src_tests.rs b/tests/scryer/src_tests.rs index 05d24c9ee..ba68e5dd7 100644 --- a/tests/scryer/src_tests.rs +++ b/tests/scryer/src_tests.rs @@ -58,7 +58,7 @@ fn setup_call_cleanup_load() { #[test] fn setup_call_cleanup_process() { run_top_level_test_with_args( - &["src/tests/setup_call_cleanup.pl", "-f", "-g", "halt"], + ["src/tests/setup_call_cleanup.pl", "-f", "-g", "halt"], "", "1+21+31+2>A+B1+G1+2>41+2>B1+2>31+2>31+2>4ba", ); @@ -79,7 +79,7 @@ fn iso_conformity_tests() { #[test] fn dif_tests() { run_top_level_test_with_args( - &["src/tests/dif.pl", "-f", "-g", "main_quiet"], + ["src/tests/dif.pl", "-f", "-g", "main_quiet"], "", "All tests passed", ); @@ -88,7 +88,7 @@ fn dif_tests() { #[test] fn ground_tests() { run_top_level_test_with_args( - &["src/tests/ground.pl", "-f", "-g", "main_quiet"], + ["src/tests/ground.pl", "-f", "-g", "main_quiet"], "", "All tests passed", ); @@ -97,7 +97,7 @@ fn ground_tests() { #[test] fn term_variables_tests() { run_top_level_test_with_args( - &["src/tests/term_variables.pl", "-f", "-g", "main_quiet"], + ["src/tests/term_variables.pl", "-f", "-g", "main_quiet"], "", "All tests passed", ); @@ -106,7 +106,7 @@ fn term_variables_tests() { #[test] fn acyclic_term_tests() { run_top_level_test_with_args( - &["src/tests/acyclic_term.pl", "-f", "-g", "main_quiet"], + ["src/tests/acyclic_term.pl", "-f", "-g", "main_quiet"], "", "All tests passed", );