diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index 43300482c1..785fa409c9 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -23,7 +23,7 @@ jobs: - name: Set up Rust uses: dtolnay/rust-toolchain@master with: - toolchain: "1.71.1" + toolchain: stable - name: Install doxygen and dependencies run: | sudo apt-get update diff --git a/.github/workflows/rust-bindings.yml b/.github/workflows/rust-bindings.yml index 85e766adb8..f2b9d45431 100644 --- a/.github/workflows/rust-bindings.yml +++ b/.github/workflows/rust-bindings.yml @@ -20,19 +20,21 @@ jobs: name: cargo:test strategy: fail-fast: false - runs-on: ubuntu-latest + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: head + ruby-version: 3.4 bundler-cache: true - name: Set up Rust uses: dtolnay/rust-toolchain@master with: - toolchain: "1.71.1" - targets: wasm32-wasi + toolchain: stable + targets: wasm32-wasip1 - uses: actions/cache@v4 with: path: | @@ -45,7 +47,9 @@ jobs: ${{ runner.os }}-cargo - name: Run tests run: bundle exec rake cargo:test + - name: Run examples + if: ${{ matrix.os != 'windows-latest' }} run: bundle exec rake cargo:examples lint: @@ -58,12 +62,12 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: head + ruby-version: 3.4 bundler-cache: true - name: Set up Rust uses: dtolnay/rust-toolchain@master with: - toolchain: "1.71.1" + toolchain: stable components: clippy, rustfmt - uses: actions/cache@v4 with: @@ -92,13 +96,13 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: head + ruby-version: 3.4 bundler-cache: true - name: rake cargo:build run: bundle exec rake cargo:build - uses: dtolnay/rust-toolchain@master with: - toolchain: nightly-2023-10-24 + toolchain: nightly target: "x86_64-unknown-linux-gnu" components: "rust-src" - name: Test with sanitizer @@ -124,7 +128,7 @@ jobs: # uses: dtolnay/rust-toolchain@master # with: # toolchain: "1.71.1" - # targets: wasm32-wasi + # targets: wasm32-wasip1 # - uses: actions/cache@v4 # with: # path: | diff --git a/rakelib/cargo.rake b/rakelib/cargo.rake index 8f5d5266a9..c52cebba2b 100644 --- a/rakelib/cargo.rake +++ b/rakelib/cargo.rake @@ -97,7 +97,7 @@ namespace :cargo do CRATES.each do |crate| Dir.chdir("rust/#{crate}") do - sh("cargo +nightly-2023-10-24 test -Zbuild-std --target=#{current_target} -- --nocapture") + sh("cargo +nightly test -Zbuild-std --target=#{current_target} -- --nocapture") end end ensure diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 25499d37f8..e51c0878e9 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" @@ -13,17 +13,15 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.66.1" +version = "0.72.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" +checksum = "4f72209734318d0b619a5e0f5129918b848c416e122a3c4ce054e03cb87b726f" dependencies = [ "bitflags", "cexpr", "clang-sys", - "lazy_static", - "lazycell", + "itertools", "log", - "peeking_take_while", "prettyplease", "proc-macro2", "quote", @@ -31,7 +29,6 @@ dependencies = [ "rustc-hash", "shlex", "syn", - "which", ] [[package]] @@ -87,16 +84,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" -[[package]] -name = "errno" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "glob" version = "0.3.1" @@ -109,15 +96,6 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" -[[package]] -name = "home" -version = "0.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" -dependencies = [ - "windows-sys 0.48.0", -] - [[package]] name = "indexmap" version = "2.1.0" @@ -129,22 +107,19 @@ dependencies = [ ] [[package]] -name = "itoa" -version = "1.0.9" +name = "itertools" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] [[package]] -name = "lazycell" -version = "1.3.0" +name = "itoa" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "libc" @@ -162,12 +137,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "linux-raw-sys" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" - [[package]] name = "log" version = "0.4.20" @@ -196,18 +165,6 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "once_cell" -version = "1.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" - -[[package]] -name = "peeking_take_while" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" - [[package]] name = "prettyplease" version = "0.2.15" @@ -220,9 +177,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.70" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] @@ -267,7 +224,7 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "ruby-prism" -version = "1.3.0" +version = "1.4.0" dependencies = [ "ruby-prism-sys", "serde", @@ -276,7 +233,7 @@ dependencies = [ [[package]] name = "ruby-prism-sys" -version = "1.3.0" +version = "1.4.0" dependencies = [ "bindgen", "cc", @@ -284,22 +241,9 @@ dependencies = [ [[package]] name = "rustc-hash" -version = "1.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - -[[package]] -name = "rustix" -version = "0.38.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9470c4bf8246c8daf25f9598dca807fb6510347b1e1cfa55749113850c79d88a" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.52.0", -] +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" [[package]] name = "ryu" @@ -369,18 +313,6 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b" -[[package]] -name = "which" -version = "4.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" -dependencies = [ - "either", - "home", - "once_cell", - "rustix", -] - [[package]] name = "winapi" version = "0.3.9" @@ -402,135 +334,3 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets 0.52.0", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", -] - -[[package]] -name = "windows-targets" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" -dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/rust/ruby-prism-sys/Cargo.toml b/rust/ruby-prism-sys/Cargo.toml index 223788fe6a..ae76e11a7e 100644 --- a/rust/ruby-prism-sys/Cargo.toml +++ b/rust/ruby-prism-sys/Cargo.toml @@ -22,7 +22,7 @@ build = "build/main.rs" include = ["src/", "build/", "Cargo.toml", "Cargo.lock", "README.md", "vendor"] [build-dependencies] -bindgen = "0.66" +bindgen = "0.72" cc = { version = "1.0", optional = true } [features] diff --git a/rust/ruby-prism-sys/build/main.rs b/rust/ruby-prism-sys/build/main.rs index 82f75c3e47..798d06d8ff 100644 --- a/rust/ruby-prism-sys/build/main.rs +++ b/rust/ruby-prism-sys/build/main.rs @@ -108,7 +108,7 @@ fn generate_bindings(ruby_include_path: &Path) -> bindgen::Bindings { .impl_debug(true) .layout_tests(true) .merge_extern_blocks(true) - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) .parse_callbacks(Box::new(Callbacks)) .prepend_enum_name(false) .size_t_is_usize(true) diff --git a/rust/ruby-prism-sys/build/vendored.rs b/rust/ruby-prism-sys/build/vendored.rs index 670df42d50..da01fa6a69 100644 --- a/rust/ruby-prism-sys/build/vendored.rs +++ b/rust/ruby-prism-sys/build/vendored.rs @@ -114,7 +114,7 @@ fn source_files>(root_dir: P) -> Vec { if Path::new(&path) .extension() - .map_or(false, |ext| ext.eq_ignore_ascii_case("c")) + .is_some_and(|ext| ext.eq_ignore_ascii_case("c")) { files.push(path); } diff --git a/rust/ruby-prism-sys/src/lib.rs b/rust/ruby-prism-sys/src/lib.rs index ecaf83a8f4..e18fced44e 100644 --- a/rust/ruby-prism-sys/src/lib.rs +++ b/rust/ruby-prism-sys/src/lib.rs @@ -4,7 +4,6 @@ //! #![deny(unused_extern_crates)] #![warn( - box_pointers, clippy::all, clippy::nursery, clippy::pedantic, @@ -25,6 +24,8 @@ #[allow(non_camel_case_types)] #[allow(non_snake_case)] #[allow(non_upper_case_globals)] +#[allow(unused_qualifications)] +#[allow(clippy::missing_const_for_fn)] mod bindings { // In `build.rs`, we use `bindgen` to generate bindings based on C headers // and `libprism`. Here is where we pull in those bindings and make diff --git a/rust/ruby-prism-sys/tests/pack_tests.rs b/rust/ruby-prism-sys/tests/pack_tests.rs index c729aa6cdc..63bc1cb1ea 100644 --- a/rust/ruby-prism-sys/tests/pack_tests.rs +++ b/rust/ruby-prism-sys/tests/pack_tests.rs @@ -30,7 +30,7 @@ fn pack_parse_test() { endian_out.as_mut_ptr(), size_out.as_mut_ptr(), length_type_out.as_mut_ptr(), - &mut length_out, + &raw mut length_out, encoding_out.as_mut_ptr(), ); diff --git a/rust/ruby-prism-sys/tests/utils_tests.rs b/rust/ruby-prism-sys/tests/utils_tests.rs index ea1b8bc4a2..8ab12d620c 100644 --- a/rust/ruby-prism-sys/tests/utils_tests.rs +++ b/rust/ruby-prism-sys/tests/utils_tests.rs @@ -66,13 +66,13 @@ mod string { let mut s = make_string(PM_STRING_SHARED); unsafe { - let len = pm_string_length(&s.pm_string); + let len = pm_string_length(&raw const s.pm_string); assert_eq!(len, 16); - let result_start = pm_string_source(&s.pm_string); + let result_start = pm_string_source(&raw const s.pm_string); assert_eq!(s.start_ptr(), result_start); - pm_string_free(&mut s.pm_string); + pm_string_free(&raw mut s.pm_string); } } @@ -81,10 +81,10 @@ mod string { let s = make_string(PM_STRING_OWNED); unsafe { - let result_len = pm_string_length(&s.pm_string); + let result_len = pm_string_length(&raw const s.pm_string); assert_eq!(result_len, 16); - let result_start = pm_string_source(&s.pm_string); + let result_start = pm_string_source(&raw const s.pm_string); assert_eq!(s.pm_string.source, result_start); // Don't drop the pm_string--we don't own it anymore! @@ -96,13 +96,13 @@ mod string { let mut s = make_string(PM_STRING_CONSTANT); unsafe { - let result_len = pm_string_length(&s.pm_string); + let result_len = pm_string_length(&raw const s.pm_string); assert_eq!(result_len, 16); - let result_start = pm_string_source(&s.pm_string); + let result_start = pm_string_source(&raw const s.pm_string); assert_eq!(s.pm_string.source, result_start); - pm_string_free(&mut s.pm_string); + pm_string_free(&raw mut s.pm_string); } } @@ -111,10 +111,10 @@ mod string { let s = make_string(PM_STRING_MAPPED); unsafe { - let result_len = pm_string_length(&s.pm_string); + let result_len = pm_string_length(&raw const s.pm_string); assert_eq!(result_len, 16); - let result_start = pm_string_source(&s.pm_string); + let result_start = pm_string_source(&raw const s.pm_string); assert_eq!(s.pm_string.source, result_start); } } diff --git a/rust/ruby-prism/build.rs b/rust/ruby-prism/build.rs index a43a159c5b..e1dd0b4271 100644 --- a/rust/ruby-prism/build.rs +++ b/rust/ruby-prism/build.rs @@ -247,14 +247,14 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box {}<'pr> {{", node.name)?; writeln!(file, " /// Converts this node to a generic node.")?; writeln!(file, " #[must_use]")?; - writeln!(file, " pub fn as_node(&self) -> Node<'pr> {{")?; + writeln!(file, " pub const fn as_node(&self) -> Node<'pr> {{")?; writeln!(file, " Node::{} {{ parser: self.parser, pointer: self.pointer, marker: PhantomData }}", node.name)?; writeln!(file, " }}")?; writeln!(file)?; writeln!(file, " /// Returns the location of this node.")?; writeln!(file, " #[must_use]")?; writeln!(file, " pub fn location(&self) -> Location<'pr> {{")?; - writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &mut (*self.pointer).base.location }};")?; + writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &raw mut (*self.pointer).base.location }};")?; writeln!(file, " Location::new(self.parser, unsafe {{ &(*pointer) }})")?; writeln!(file, " }}")?; writeln!(file)?; @@ -326,7 +326,7 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box { writeln!(file, " pub fn {}(&self) -> NodeList<'pr> {{", field.name)?; - writeln!(file, " let pointer: *mut pm_node_list = unsafe {{ &mut (*self.pointer).{} }};", field.name)?; + writeln!(file, " let pointer: *mut pm_node_list = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?; writeln!(file, " NodeList {{ parser: self.parser, pointer: unsafe {{ NonNull::new_unchecked(pointer) }}, marker: PhantomData }}")?; writeln!(file, " }}")?; }, @@ -359,19 +359,19 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box { writeln!(file, " pub fn {}(&self) -> ConstantList<'pr> {{", field.name)?; - writeln!(file, " let pointer: *mut pm_constant_id_list_t = unsafe {{ &mut (*self.pointer).{} }};", field.name)?; + writeln!(file, " let pointer: *mut pm_constant_id_list_t = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?; writeln!(file, " ConstantList {{ parser: self.parser, pointer: unsafe {{ NonNull::new_unchecked(pointer) }}, marker: PhantomData }}")?; writeln!(file, " }}")?; }, NodeFieldType::Location => { writeln!(file, " pub fn {}(&self) -> Location<'pr> {{", field.name)?; - writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &mut (*self.pointer).{} }};", field.name)?; + writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?; writeln!(file, " Location::new(self.parser, unsafe {{ &(*pointer) }})")?; writeln!(file, " }}")?; }, NodeFieldType::OptionalLocation => { writeln!(file, " pub fn {}(&self) -> Option> {{", field.name)?; - writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &mut (*self.pointer).{} }};", field.name)?; + writeln!(file, " let pointer: *mut pm_location_t = unsafe {{ &raw mut (*self.pointer).{} }};", field.name)?; writeln!(file, " let start = unsafe {{ (*pointer).start }};")?; writeln!(file, " if start.is_null() {{")?; writeln!(file, " None")?; @@ -392,7 +392,7 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box { writeln!(file, " pub fn {}(&self) -> Integer<'pr> {{", field.name)?; - writeln!(file, " Integer::new(unsafe {{ &(*self.pointer).{} }})", field.name)?; + writeln!(file, " Integer::new(unsafe {{ &raw const(*self.pointer).{} }})", field.name)?; writeln!(file, " }}")?; }, NodeFieldType::Double => { @@ -523,7 +523,7 @@ fn write_visit(file: &mut File, config: &Config) -> Result<(), Box { - writeln!(file, " for node in node.{}().iter() {{", field.name)?; + writeln!(file, " for node in &node.{}() {{", field.name)?; writeln!(file, " visitor.visit(&node);")?; writeln!(file, " }}")?; }, @@ -533,7 +533,7 @@ fn write_visit(file: &mut File, config: &Config) -> Result<(), Box(_visitor: &mut V, _node: &{}<'pr>)", struct_name(&node.name), node.name)?; + writeln!(file, "pub const fn visit{}<'pr, V>(_visitor: &mut V, _node: &{}<'pr>)", struct_name(&node.name), node.name)?; writeln!(file, "where")?; writeln!(file, " V: Visit<'pr> + ?Sized,")?; writeln!(file, "{{}}")?; @@ -552,14 +552,14 @@ fn write_bindings(config: &Config) -> Result<(), Box> { write!( file, - r#" + r" use std::marker::PhantomData; use std::ptr::NonNull; #[allow(clippy::wildcard_imports)] use ruby_prism_sys::*; use crate::{{ConstantId, ConstantList, Integer, Location, NodeList}}; -"# +" )?; for node in &config.nodes { @@ -578,7 +578,7 @@ use crate::{{ConstantId, ConstantList, Integer, Location, NodeList}}; writeln!(file, "pub enum Node<'pr> {{")?; for node in &config.nodes { - writeln!(file, " /// The {} node", node.name)?; + writeln!(file, " /// The `{}` node", node.name)?; writeln!(file, " {} {{", node.name)?; writeln!(file, " /// The pointer to the associated parser this node came from.")?; writeln!(file, " parser: NonNull,")?; @@ -596,7 +596,7 @@ use crate::{{ConstantId, ConstantList, Integer, Location, NodeList}}; writeln!( file, - r#" + r" impl<'pr> Node<'pr> {{ /// Creates a new node from the given pointer. /// @@ -607,7 +607,7 @@ impl<'pr> Node<'pr> {{ #[allow(clippy::not_unsafe_ptr_arg_deref)] pub(crate) fn new(parser: NonNull, node: *mut pm_node_t) -> Self {{ match unsafe {{ (*node).type_ }} {{ -"# +" )?; for node in &config.nodes { @@ -633,7 +633,7 @@ impl<'pr> Node<'pr> {{ for node in &config.nodes { writeln!(file, " /// Returns the node as a `{}`.", node.name)?; writeln!(file, " #[must_use]")?; - writeln!(file, " pub fn as{}(&self) -> Option<{}<'pr>> {{", struct_name(&node.name), node.name)?; + writeln!(file, " pub const fn as{}(&self) -> Option<{}<'pr>> {{", struct_name(&node.name), node.name)?; writeln!(file, " match *self {{")?; writeln!(file, " Self::{} {{ parser, pointer, marker }} => Some({} {{ parser, pointer, marker }}),", node.name, node.name)?; writeln!(file, " _ => None")?; diff --git a/rust/ruby-prism/examples/wasm/main.rs b/rust/ruby-prism/examples/wasm/main.rs index bfb8e0023f..8a49f634a0 100644 --- a/rust/ruby-prism/examples/wasm/main.rs +++ b/rust/ruby-prism/examples/wasm/main.rs @@ -7,7 +7,7 @@ fn main() { let comments_count = result.comments().count(); let warnings_count = result.warnings().count(); let errors_count = result.errors().count(); - println!(" comments: {}", comments_count); - println!(" warnings: {}", warnings_count); - println!(" errors: {}", errors_count); + println!(" comments: {comments_count}"); + println!(" warnings: {warnings_count}"); + println!(" errors: {errors_count}"); } diff --git a/rust/ruby-prism/examples/wasm/run.sh b/rust/ruby-prism/examples/wasm/run.sh index bd55ce69e0..f1f141df47 100755 --- a/rust/ruby-prism/examples/wasm/run.sh +++ b/rust/ruby-prism/examples/wasm/run.sh @@ -7,7 +7,7 @@ WASI_SDK_VERSION="$WASI_SDK_VERSION_MAJOR.0" TMPDIR="$(pwd)/tmp" WASI_SDK_PATH="${TMPDIR}/wasi-sdk-${WASI_SDK_VERSION}" WASI_SDK_TAR="${TMPDIR}/wasi-sdk.tar.gz" -WASM_BUILD_DIR="./target/wasm32-wasi/debug/examples" +WASM_BUILD_DIR="./target/wasm32-wasip1/debug/examples" WASM_FILE="${WASM_BUILD_DIR}/wasm.wasm" export WASI_SDK_PATH @@ -45,7 +45,7 @@ download_wasi_sdk() { } build_wasm() { - cargo build --target=wasm32-wasi --example wasm >&2 + cargo build --target=wasm32-wasip1 --example wasm >&2 echo "WASM built to ${WASM_FILE}" >&2 } diff --git a/rust/ruby-prism/src/lib.rs b/rust/ruby-prism/src/lib.rs index 22054de8ba..43b1be6409 100644 --- a/rust/ruby-prism/src/lib.rs +++ b/rust/ruby-prism/src/lib.rs @@ -31,6 +31,8 @@ pub struct Location<'pr> { impl<'pr> Location<'pr> { /// Returns a byte slice for the range. + /// # Panics + /// Panics if the end offset is not greater than the start offset. #[must_use] pub fn as_slice(&self) -> &'pr [u8] { unsafe { @@ -41,7 +43,7 @@ impl<'pr> Location<'pr> { /// Return a Location from the given `pm_location_t`. #[must_use] - pub(crate) const fn new(parser: NonNull, loc: &'pr pm_location_t) -> Location<'pr> { + pub(crate) const fn new(parser: NonNull, loc: &'pr pm_location_t) -> Self { Location { parser, start: loc.start, @@ -54,7 +56,7 @@ impl<'pr> Location<'pr> { /// Returns None if both locations did not originate from the same parser, /// or if self starts after other. #[must_use] - pub fn join(&self, other: &Location<'pr>) -> Option> { + pub fn join(&self, other: &Self) -> Option { if self.parser != other.parser || self.start > other.start { None } else { @@ -68,6 +70,8 @@ impl<'pr> Location<'pr> { } /// Return the start offset from the beginning of the parsed source. + /// # Panics + /// Panics if the start offset is not greater than the parser's start. #[must_use] pub fn start_offset(&self) -> usize { unsafe { @@ -77,6 +81,8 @@ impl<'pr> Location<'pr> { } /// Return the end offset from the beginning of the parsed source. + /// # Panics + /// Panics if the end offset is not greater than the parser's start. #[must_use] pub fn end_offset(&self) -> usize { unsafe { @@ -135,7 +141,7 @@ pub struct NodeList<'pr> { impl<'pr> NodeList<'pr> { /// Returns an iterator over the nodes. #[must_use] - pub fn iter(&self) -> NodeListIter<'pr> { + pub const fn iter(&self) -> NodeListIter<'pr> { NodeListIter { parser: self.parser, pointer: self.pointer, @@ -145,6 +151,14 @@ impl<'pr> NodeList<'pr> { } } +impl<'pr> IntoIterator for &NodeList<'pr> { + type Item = Node<'pr>; + type IntoIter = NodeListIter<'pr>; + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + impl std::fmt::Debug for NodeList<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.iter().collect::>()) @@ -159,7 +173,7 @@ pub struct ConstantId<'pr> { } impl<'pr> ConstantId<'pr> { - fn new(parser: NonNull, id: pm_constant_id_t) -> Self { + const fn new(parser: NonNull, id: pm_constant_id_t) -> Self { ConstantId { parser, id, marker: PhantomData } } @@ -221,7 +235,7 @@ pub struct ConstantList<'pr> { impl<'pr> ConstantList<'pr> { /// Returns an iterator over the constants in the list. #[must_use] - pub fn iter(&self) -> ConstantListIter<'pr> { + pub const fn iter(&self) -> ConstantListIter<'pr> { ConstantListIter { parser: self.parser, pointer: self.pointer, @@ -231,6 +245,14 @@ impl<'pr> ConstantList<'pr> { } } +impl<'pr> IntoIterator for &ConstantList<'pr> { + type Item = ConstantId<'pr>; + type IntoIter = ConstantListIter<'pr>; + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + impl std::fmt::Debug for ConstantList<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.iter().collect::>()) @@ -246,15 +268,15 @@ pub struct Integer<'pr> { marker: PhantomData<&'pr mut pm_constant_id_t>, } -impl<'pr> Integer<'pr> { - fn new(pointer: *const pm_integer_t) -> Self { +impl Integer<'_> { + const fn new(pointer: *const pm_integer_t) -> Self { Integer { pointer, marker: PhantomData } } /// Returns the sign and the u32 digits representation of the integer, /// ordered least significant digit first. #[must_use] - pub fn to_u32_digits(&self) -> (bool, &[u32]) { + pub const fn to_u32_digits(&self) -> (bool, &[u32]) { let negative = unsafe { (*self.pointer).negative }; let length = unsafe { (*self.pointer).length }; let values = unsafe { (*self.pointer).values }; @@ -294,7 +316,7 @@ impl TryInto for Integer<'_> { /// A diagnostic message that came back from the parser. #[derive(Debug)] pub struct Diagnostic<'pr> { - diagnostic: NonNull, + diag: NonNull, parser: NonNull, marker: PhantomData<&'pr pm_diagnostic_t>, } @@ -309,22 +331,22 @@ impl<'pr> Diagnostic<'pr> { #[must_use] pub fn message(&self) -> &str { unsafe { - let message: *mut c_char = self.diagnostic.as_ref().message.cast_mut(); + let message: *mut c_char = self.diag.as_ref().message.cast_mut(); CStr::from_ptr(message).to_str().expect("prism allows only UTF-8 for diagnostics.") } } /// The location of the diagnostic in the source. #[must_use] - pub fn location(&self) -> Location<'pr> { - Location::new(self.parser, unsafe { &self.diagnostic.as_ref().location }) + pub const fn location(&self) -> Location<'pr> { + Location::new(self.parser, unsafe { &self.diag.as_ref().location }) } } /// A comment that was found during parsing. #[derive(Debug)] pub struct Comment<'pr> { - comment: NonNull, + content: NonNull, parser: NonNull, marker: PhantomData<&'pr pm_comment_t>, } @@ -341,8 +363,8 @@ impl<'pr> Comment<'pr> { /// The location of the comment in the source. #[must_use] - pub fn location(&self) -> Location<'pr> { - Location::new(self.parser, unsafe { &self.comment.as_ref().location }) + pub const fn location(&self) -> Location<'pr> { + Location::new(self.parser, unsafe { &self.content.as_ref().location }) } } @@ -353,10 +375,10 @@ pub struct MagicComment<'pr> { marker: PhantomData<&'pr pm_magic_comment_t>, } -impl<'pr> MagicComment<'pr> { +impl MagicComment<'_> { /// Returns the text of the comment's key. #[must_use] - pub fn key(&self) -> &[u8] { + pub const fn key(&self) -> &[u8] { unsafe { let start = self.comment.as_ref().key_start; let len = self.comment.as_ref().key_length as usize; @@ -366,7 +388,7 @@ impl<'pr> MagicComment<'pr> { /// Returns the text of the comment's value. #[must_use] - pub fn value(&self) -> &[u8] { + pub const fn value(&self) -> &[u8] { unsafe { let start = self.comment.as_ref().value_start; let len = self.comment.as_ref().value_length as usize; @@ -388,7 +410,11 @@ impl<'pr> Iterator for Diagnostics<'pr> { fn next(&mut self) -> Option { if let Some(diagnostic) = NonNull::new(self.diagnostic) { - let current = Diagnostic { diagnostic, parser: self.parser, marker: PhantomData }; + let current = Diagnostic { + diag: diagnostic, + parser: self.parser, + marker: PhantomData, + }; self.diagnostic = unsafe { diagnostic.as_ref().node.next.cast::() }; Some(current) } else { @@ -410,7 +436,11 @@ impl<'pr> Iterator for Comments<'pr> { fn next(&mut self) -> Option { if let Some(comment) = NonNull::new(self.comment) { - let current = Comment { comment, parser: self.parser, marker: PhantomData }; + let current = Comment { + content: comment, + parser: self.parser, + marker: PhantomData, + }; self.comment = unsafe { comment.as_ref().node.next.cast::() }; Some(current) } else { @@ -549,7 +579,7 @@ impl<'pr> ParseResult<'pr> { } } -impl<'pr> Drop for ParseResult<'pr> { +impl Drop for ParseResult<'_> { fn drop(&mut self) { unsafe { pm_node_destroy(self.parser.as_ptr(), self.node.as_ptr()); @@ -755,12 +785,12 @@ mod tests { #[test] fn optional_loc_test() { - let source = r#" + let source = r" module Example x = call_func(3, 4) y = x.call_func 5, 6 end -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -829,9 +859,9 @@ end #[test] fn call_flags_test() { - let source = r#" + let source = r" x -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -839,9 +869,9 @@ x let call = call.as_call_node().unwrap(); assert!(call.is_variable_call()); - let source = r#" + let source = r" x&.foo -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -852,9 +882,9 @@ x&.foo #[test] fn integer_flags_test() { - let source = r#" + let source = r" 0b1 -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -865,9 +895,9 @@ x&.foo assert!(!i.is_octal()); assert!(!i.is_hexadecimal()); - let source = r#" + let source = r" 1 -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -878,9 +908,9 @@ x&.foo assert!(!i.is_octal()); assert!(!i.is_hexadecimal()); - let source = r#" + let source = r" 0o1 -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -891,9 +921,9 @@ x&.foo assert!(i.is_octal()); assert!(!i.is_hexadecimal()); - let source = r#" + let source = r" 0x1 -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -907,9 +937,9 @@ x&.foo #[test] fn range_flags_test() { - let source = r#" + let source = r" 0..1 -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -917,9 +947,9 @@ x&.foo let range = range.as_range_node().unwrap(); assert!(!range.is_exclude_end()); - let source = r#" + let source = r" 0...1 -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -931,9 +961,9 @@ x&.foo #[allow(clippy::too_many_lines, clippy::cognitive_complexity)] #[test] fn regex_flags_test() { - let source = r#" + let source = r" /a/i -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -948,9 +978,9 @@ x&.foo assert!(!regex.is_utf_8()); assert!(!regex.is_once()); - let source = r#" + let source = r" /a/x -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -965,9 +995,9 @@ x&.foo assert!(!regex.is_utf_8()); assert!(!regex.is_once()); - let source = r#" + let source = r" /a/m -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -982,9 +1012,9 @@ x&.foo assert!(!regex.is_utf_8()); assert!(!regex.is_once()); - let source = r#" + let source = r" /a/e -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -999,9 +1029,9 @@ x&.foo assert!(!regex.is_utf_8()); assert!(!regex.is_once()); - let source = r#" + let source = r" /a/n -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -1016,9 +1046,9 @@ x&.foo assert!(!regex.is_utf_8()); assert!(!regex.is_once()); - let source = r#" + let source = r" /a/s -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -1033,9 +1063,9 @@ x&.foo assert!(!regex.is_utf_8()); assert!(!regex.is_once()); - let source = r#" + let source = r" /a/u -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -1050,9 +1080,9 @@ x&.foo assert!(regex.is_utf_8()); assert!(!regex.is_once()); - let source = r#" + let source = r" /a/o -"#; +"; let result = parse(source.as_ref()); let node = result.node(); @@ -1085,7 +1115,7 @@ x&.foo counts: NodeCounts, } - impl<'pr> Visit<'pr> for CountingVisitor { + impl Visit<'_> for CountingVisitor { fn visit_branch_node_enter(&mut self, _node: Node<'_>) { self.counts.pre_parent += 1; } @@ -1103,12 +1133,12 @@ x&.foo } } - let source = r#" + let source = r" module Example x = call_func(3, 4) y = x.call_func 5, 6 end -"#; +"; let result = parse(source.as_ref()); let node = result.node(); let mut visitor = CountingVisitor::default(); @@ -1144,12 +1174,12 @@ end } } - let source = r#" + let source = r" module Example x = call_func(3, 4) y = x.call_func 5, 6 end -"#; +"; let result = parse(source.as_ref()); let node = result.node(); let mut visitor = StackingNodeVisitor::default();