diff --git a/CHANGELOG.md b/CHANGELOG.md index 57591ff..ab8ed3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,60 @@ # Changelog +## [Unreleased] + +### Fixed + +- Recognizes `#[macro_export(local_inner_macros)]`. + +- Fixed a problem where `$crate`s are not replaced with `$crate::lib_name` in a certain case. + +- Fixed a problem where `--remove` option could not be used for CRLF code. + + With the above 3 fixtures, you can bundle [proconio](https://docs.rs/crate/proconio). + + ```console + ❯ cat <./src/main.rs + heredoc> #[macro_use] + heredoc> extern crate proconio as _; + heredoc> + heredoc> #[fastout] + heredoc> fn main() { + heredoc> input!(abs: [(u64, u64)]); + heredoc> for (a, b) in abs { + heredoc> println!("{}", a + b); + heredoc> } + heredoc> } + heredoc> EOF + ❯ cargo equip \ + > --resolve-cfgs \ + > --remove docs \ + > --minify libs \ + > --rustfmt \ + > --check \ + > -o ./bundled.rs + ❯ ./run-cargo-equip.bash -o ./bundled.rs + Running `/home/ryo/.cargo/bin/rustup run nightly cargo udeps --output json -p bundle-proconio --bin bundle-proconio` + Checking bundle-proconio v0.1.0 (/home/ryo/src/local/bundle-proconio) + Finished dev [unoptimized + debuginfo] target(s) in 0.45s + info: Loading save analysis from "/home/ryo/src/local/bundle-proconio/target/debug/deps/save-analysis/bundle_proconio-31a013a4acd96cad.json" + Running `/home/ryo/.cargo/bin/rustup run stable-x86_64-unknown-linux-gnu cargo check --message-format json -p 'bundle-proconio:0.1.0' --bin bundle-proconio` + Checking bundle-proconio v0.1.0 (/home/ryo/src/local/bundle-proconio) + Finished dev [unoptimized + debuginfo] target(s) in 0.44s + Spawning `/home/ryo/.cache/cargo-equip/rust-analyzer-2021-03-29 proc-macro` + Readied `#[derive_readable]` + Readied `#[fastout]` + Bundling the code + warning: found `crate` paths. replacing them with `crate::proconio` + Reading the license file of `lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)` + Reading the license file of `proconio 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)` + Checking cargo-equip-check-output-fs0en4z4r1d3gd3e v0.1.0 (/tmp/cargo-equip-check-output-fs0en4z4r1d3gd3e) + Finished dev [unoptimized + debuginfo] target(s) in 0.19s + ❯ stat -c %s ./bundled.rs + 18024 + ``` + + [Submission Info #46571 - Library Checker](https://judge.yosupo.jp/submission/46571) + ## [0.12.0] - 2021-05-01Z ### Added @@ -48,10 +103,6 @@ - cargo-equip won't error for unresolved `extern crate` items. -### Fixed - -- Recognizes `#[macro_export(local_inner_macros)]`. - ## [0.11.1] - 2021-03-30Z ### Added diff --git a/src/rust.rs b/src/rust.rs index b7dde3f..bbd0da5 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -8,6 +8,7 @@ use maplit::{btreemap, btreeset}; use proc_macro2::{LineColumn, Spacing, Span, TokenStream, TokenTree}; use quote::{quote, ToTokens}; use std::{ + borrow::Cow, collections::{BTreeMap, BTreeSet, VecDeque}, env, fs, mem, ops::Range, @@ -753,13 +754,13 @@ pub(crate) fn process_extern_crates_in_lib( pub(crate) fn modify_macros(code: &str, pseudo_extern_crate_name: &str) -> anyhow::Result { fn find_dollar_crates(token_stream: TokenStream, acc: &mut BTreeSet) { - for (i, (tt1, tt2)) in token_stream.into_iter().tuple_windows().enumerate() { - if i == 0 { - if let proc_macro2::TokenTree::Group(group) = &tt1 { - find_dollar_crates(group.stream(), acc); - } - } + let mut token_stream = token_stream.into_iter().peekable(); + + if let Some(proc_macro2::TokenTree::Group(group)) = token_stream.peek() { + find_dollar_crates(group.stream(), acc); + } + for (tt1, tt2) in token_stream.tuple_windows() { if let proc_macro2::TokenTree::Group(group) = &tt2 { find_dollar_crates(group.stream(), acc); } @@ -1248,6 +1249,12 @@ fn erase( code: &str, visit_file: fn(&mut [FixedBitSet], TokenStream) -> syn::Result<()>, ) -> anyhow::Result { + let code = &if code.contains("\r\n") { + Cow::from(code.replace("\r\n", "\n")) + } else { + Cow::from(code) + }; + let code = if code.starts_with("#!") { let (_, code) = code.split_at(code.find('\n').unwrap_or_else(|| code.len())); code