aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandroid-build-prod (mdb) <android-build-team-robot@google.com>2021-04-15 17:28:05 +0000
committerandroid-build-prod (mdb) <android-build-team-robot@google.com>2021-04-15 17:28:05 +0000
commit43e2b67ceb830ac4101c5f157532c491abad9a50 (patch)
treec33e9c5048109eae152e88eb76b4a978f2af98f2
parentae47280e989846edaae234a1f952fbf4faf4c088 (diff)
parentf8964bee559b08f21575a31653859c13cc2b0bfd (diff)
downloadconst_fn-simpleperf-release.tar.gz
Snap for 7286261 from f8964bee559b08f21575a31653859c13cc2b0bfd to simpleperf-releasesimpleperf-release
Change-Id: I42d3bccc2a2085c854b9e89a8fe074140b4bb17a
-rw-r--r--.cargo_vcs_info.json2
-rw-r--r--.editorconfig19
-rw-r--r--.gitattributes2
-rw-r--r--.github/CODEOWNERS1
-rw-r--r--.github/bors.toml2
-rw-r--r--.github/workflows/ci.yml113
-rw-r--r--Android.bp39
-rw-r--r--CHANGELOG.md71
-rw-r--r--Cargo.toml7
-rw-r--r--Cargo.toml.orig7
-rw-r--r--METADATA8
-rw-r--r--README.md46
-rw-r--r--TEST_MAPPING8
-rw-r--r--build.rs11
-rwxr-xr-xci/install-component.sh27
-rwxr-xr-xci/install-rust.sh12
-rw-r--r--out/version1
-rw-r--r--out/version.rs1
-rw-r--r--rustfmt.toml27
-rw-r--r--scripts/README.md3
-rw-r--r--scripts/check-minimal-versions.sh58
-rw-r--r--src/ast.rs58
-rw-r--r--src/error.rs3
-rw-r--r--src/lib.rs30
-rw-r--r--src/to_tokens.rs3
-rw-r--r--src/utils.rs5
26 files changed, 188 insertions, 376 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 3602ab1..f811224 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,5 @@
{
"git": {
- "sha1": "8684f7ec1965df5434d5eec38b340f10030fce49"
+ "sha1": "f25dd591fb4e4909ff08b54bb7067f411cf3882f"
}
}
diff --git a/.editorconfig b/.editorconfig
deleted file mode 100644
index 933a9a4..0000000
--- a/.editorconfig
+++ /dev/null
@@ -1,19 +0,0 @@
-# EditorConfig configuration
-# https://editorconfig.org
-
-# Top-most EditorConfig file
-root = true
-
-[*]
-end_of_line = lf
-insert_final_newline = true
-trim_trailing_whitespace = true
-charset = utf-8
-indent_style = space
-indent_size = 4
-
-[*.{json,yml,md}]
-indent_size = 2
-
-[*.sh]
-switch_case_indent = true
diff --git a/.gitattributes b/.gitattributes
deleted file mode 100644
index 7e4a4f0..0000000
--- a/.gitattributes
+++ /dev/null
@@ -1,2 +0,0 @@
-* text=auto eol=lf
-*.rs text eol=lf whitespace=tab-in-indent,trailing-space,tabwidth=4
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
deleted file mode 100644
index 2fdc28f..0000000
--- a/.github/CODEOWNERS
+++ /dev/null
@@ -1 +0,0 @@
-* @taiki-e
diff --git a/.github/bors.toml b/.github/bors.toml
deleted file mode 100644
index 1779788..0000000
--- a/.github/bors.toml
+++ /dev/null
@@ -1,2 +0,0 @@
-status = ["ci"]
-delete_merged_branches = true
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
deleted file mode 100644
index affe118..0000000
--- a/.github/workflows/ci.yml
+++ /dev/null
@@ -1,113 +0,0 @@
-name: CI
-
-on:
- pull_request:
- push:
- branches:
- - master
- - staging
- schedule:
- - cron: '0 1 * * *'
-
-env:
- RUSTFLAGS: -Dwarnings
- RUST_BACKTRACE: 1
-
-defaults:
- run:
- shell: bash
-
-jobs:
- test:
- name: test
- strategy:
- matrix:
- rust:
- # This is the minimum supported Rust version of this crate.
- # When updating this, the reminder to update the minimum supported Rust version in README.md.
- - 1.31.0
- - 1.33.0
- - 1.39.0
- - 1.46.0
- - stable
- - beta
- - nightly
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - name: Install Rust
- run: ci/install-rust.sh ${{ matrix.rust }}
- - if: matrix.rust == 'nightly'
- run: cargo install cargo-hack
- - run: cargo test --all
- - if: matrix.rust == 'nightly'
- run: bash scripts/check-minimal-versions.sh
-
- clippy:
- name: clippy
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - name: Install Rust and Clippy
- run: ci/install-component.sh clippy
- - run: cargo clippy --all --all-features --all-targets
-
- rustfmt:
- name: rustfmt
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - name: Install Rust and Rustfmt
- run: ci/install-component.sh rustfmt
- - run: cargo fmt --all -- --check
-
- rustdoc:
- name: rustdoc
- env:
- RUSTDOCFLAGS: -Dwarnings
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - name: Install Rust
- run: ci/install-rust.sh
- - run: cargo doc --no-deps --all --all-features
-
- shellcheck:
- name: shellcheck
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v2
- - run: shellcheck **/*.sh
-
- # These jobs don't actually test anything, but they're used to tell bors the
- # build completed, as there is no practical way to detect when a workflow is
- # successful listening to webhooks only.
- #
- # ALL THE PREVIOUS JOBS NEEDS TO BE ADDED TO THE `needs` SECTION OF THIS JOB!
-
- ci-success:
- name: ci
- if: github.event_name == 'push' && success()
- needs:
- - test
- - clippy
- - rustfmt
- - rustdoc
- - shellcheck
- runs-on: ubuntu-latest
- steps:
- - name: Mark the job as a success
- run: exit 0
- ci-failure:
- name: ci
- if: github.event_name == 'push' && !success()
- needs:
- - test
- - clippy
- - rustfmt
- - rustdoc
- - shellcheck
- runs-on: ubuntu-latest
- steps:
- - name: Mark the job as a failure
- run: exit 1
diff --git a/Android.bp b/Android.bp
index ebe674b..4870c29 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,10 +1,47 @@
// This file is generated by cargo2android.py --run --copy-out --dependencies.
+// Do not modify this file as changes will be overridden on upgrade.
+
+package {
+ default_applicable_licenses: ["external_rust_crates_const_fn_license"],
+}
+
+// Added automatically by a large-scale-change that took the approach of
+// 'apply every license found to every target'. While this makes sure we respect
+// every license restriction, it may not be entirely correct.
+//
+// e.g. GPL in an MIT project might only apply to the contrib/ directory.
+//
+// Please consider splitting the single license below into multiple licenses,
+// taking care not to lose any license_kind information, and overriding the
+// default license using the 'licenses: [...]' property on targets as needed.
+//
+// For unused files, consider creating a 'fileGroup' with "//visibility:private"
+// to attach the license to, and including a comment whether the files may be
+// used in the current project.
+//
+// large-scale-change included anything that looked like it might be a license
+// text as a license_text. e.g. LICENSE, NOTICE, COPYING etc.
+//
+// Please consider removing redundant or irrelevant files from 'license_text:'.
+// See: http://go/android-license-faq
+license {
+ name: "external_rust_crates_const_fn_license",
+ visibility: [":__subpackages__"],
+ license_kinds: [
+ "SPDX-license-identifier-Apache-2.0",
+ "SPDX-license-identifier-MIT",
+ ],
+ license_text: [
+ "LICENSE-APACHE",
+ "LICENSE-MIT",
+ ],
+}
genrule {
name: "copy_const_fn_build_out",
srcs: ["out/*"],
cmd: "cp $(in) $(genDir)",
- out: ["version.rs"],
+ out: ["version"],
}
rust_proc_macro {
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 00b86b9..74ff310 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,27 +4,51 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org).
+<!--
+Note: In this file, do not use the hard wrap in the middle of a sentence for compatibility with GitHub comment style markdown rendering.
+-->
+
## [Unreleased]
+## [0.4.6] - 2021-03-27
+
+- Fix compatibility issues with const generics.
+
+## [0.4.5] - 2021-01-05
+
+- Exclude unneeded files from crates.io.
+
+## [0.4.4] - 2020-11-02
+
+- [Accept `const_fn` attribute with no arguments and functions without `const` keyword.](https://github.com/taiki-e/const_fn/pull/34)
+ This allows `const_fn` to be used as an optional dependency.
+
+ ```rust
+ #[cfg_attr(feature = "...", const_fn::const_fn)]
+ pub fn func() {
+ /* ... */
+ }
+ ```
+
## [0.4.3] - 2020-11-02
-* [`const_fn` no longer fails to compile if unable to determine rustc version. Instead, it now displays a warning.](https://github.com/taiki-e/const_fn/pull/31)
+- [`const_fn` no longer fails to compile if unable to determine rustc version. Instead, it now displays a warning.](https://github.com/taiki-e/const_fn/pull/31)
-* [`const_fn` no longer relies on debug print format.](https://github.com/taiki-e/const_fn/pull/30)
+- [`const_fn` no longer relies on debug print format.](https://github.com/taiki-e/const_fn/pull/30)
## [0.4.2] - 2020-08-31
-* [Improve error messages when failed to parse version information.](https://github.com/taiki-e/const_fn/pull/26)
+- [Improve error messages when failed to parse version information.](https://github.com/taiki-e/const_fn/pull/26)
-* [Fix compile failure with cargo installed by yum.](https://github.com/taiki-e/const_fn/pull/26)
+- [Fix compile failure with cargo installed by yum.](https://github.com/taiki-e/const_fn/pull/26)
## [0.4.1] - 2020-08-25
-* [Fix compile failure with non-cargo build systems.](https://github.com/taiki-e/const_fn/pull/23)
+- [Fix compile failure with non-cargo build systems.](https://github.com/taiki-e/const_fn/pull/23)
## [0.4.0] - 2020-08-25
-* [Add support for version-based code generation.](https://github.com/taiki-e/const_fn/pull/17) The following conditions are available:
+- [Add support for version-based code generation.](https://github.com/taiki-e/const_fn/pull/17) The following conditions are available:
```rust
use const_fn::const_fn;
@@ -54,59 +78,64 @@ This project adheres to [Semantic Versioning](https://semver.org).
}
```
-* Improve compile time by removing proc-macro related dependencies ([#18](https://github.com/taiki-e/const_fn/pull/18), [#20](https://github.com/taiki-e/const_fn/pull/20)).
+- Improve compile time by removing proc-macro related dependencies ([#18](https://github.com/taiki-e/const_fn/pull/18), [#20](https://github.com/taiki-e/const_fn/pull/20)).
## [0.3.1] - 2019-12-09
-* Updated `syn-mid` to 0.5.
+- Updated `syn-mid` to 0.5.
## [0.3.0] - 2019-10-20
-* `#[const_fn]` attribute may only be used on const functions.
+- `#[const_fn]` attribute may only be used on const functions.
## [0.2.1] - 2019-08-15
-* Updated `proc-macro2`, `syn`, and `quote` to 1.0.
+- Updated `proc-macro2`, `syn`, and `quote` to 1.0.
-* Updated `syn-mid` to 0.4.
+- Updated `syn-mid` to 0.4.
## [0.2.0] - 2019-06-16
-* Transition to Rust 2018. With this change, the minimum required version will go up to Rust 1.31.
+- Transition to Rust 2018. With this change, the minimum required version will go up to Rust 1.31.
## [0.1.7] - 2019-02-18
-* Update syn-mid version to 0.3
+- Update syn-mid version to 0.3
## [0.1.6] - 2019-02-15
-* Reduce compilation time
+- Reduce compilation time
## [0.1.5] - 2019-02-15
-* Revert 0.1.4
+- Revert 0.1.4
+
+## [0.1.4] - 2019-02-15
-## [0.1.4] - 2019-02-15 - YANKED
+**Note: This release has been yanked.**
-* Reduce compilation time
+- Reduce compilation time
## [0.1.3] - 2019-01-06
-* Fix dependencies
+- Fix dependencies
## [0.1.2] - 2018-12-27
-* Improve error messages
+- Improve error messages
## [0.1.1] - 2018-12-27
-* Improve an error message
+- Improve an error message
## [0.1.0] - 2018-12-25
Initial release
-[Unreleased]: https://github.com/taiki-e/const_fn/compare/v0.4.3...HEAD
+[Unreleased]: https://github.com/taiki-e/const_fn/compare/v0.4.6...HEAD
+[0.4.6]: https://github.com/taiki-e/const_fn/compare/v0.4.5...v0.4.6
+[0.4.5]: https://github.com/taiki-e/const_fn/compare/v0.4.4...v0.4.5
+[0.4.4]: https://github.com/taiki-e/const_fn/compare/v0.4.3...v0.4.4
[0.4.3]: https://github.com/taiki-e/const_fn/compare/v0.4.2...v0.4.3
[0.4.2]: https://github.com/taiki-e/const_fn/compare/v0.4.1...v0.4.2
[0.4.1]: https://github.com/taiki-e/const_fn/compare/v0.4.0...v0.4.1
diff --git a/Cargo.toml b/Cargo.toml
index f27ba56..d61bbd9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,14 +13,13 @@
[package]
edition = "2018"
name = "const_fn"
-version = "0.4.3"
+version = "0.4.6"
authors = ["Taiki Endo <te316e89@gmail.com>"]
+exclude = ["/.*", "/tools"]
description = "An attribute for easy generation of const functions with conditional compilations.\n"
-homepage = "https://github.com/taiki-e/const_fn"
documentation = "https://docs.rs/const_fn"
-readme = "README.md"
keywords = ["macros", "attribute", "const", "static"]
-categories = ["rust-patterns", "no-std"]
+categories = ["no-std", "rust-patterns"]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/taiki-e/const_fn"
[package.metadata.docs.rs]
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 73c690a..02f2e8f 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,15 +1,14 @@
[package]
name = "const_fn"
-version = "0.4.3"
+version = "0.4.6"
authors = ["Taiki Endo <te316e89@gmail.com>"]
edition = "2018"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/taiki-e/const_fn"
-homepage = "https://github.com/taiki-e/const_fn"
documentation = "https://docs.rs/const_fn"
keywords = ["macros", "attribute", "const", "static"]
-categories = ["rust-patterns", "no-std"]
-readme = "README.md"
+categories = ["no-std", "rust-patterns"]
+exclude = ["/.*", "/tools"]
description = """
An attribute for easy generation of const functions with conditional compilations.
"""
diff --git a/METADATA b/METADATA
index 63bbaa8..85e3c25 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/const_fn/const_fn-0.4.3.crate"
+ value: "https://static.crates.io/crates/const_fn/const_fn-0.4.6.crate"
}
- version: "0.4.3"
+ version: "0.4.6"
license_type: NOTICE
last_upgrade_date {
- year: 2020
- month: 11
+ year: 2021
+ month: 4
day: 2
}
}
diff --git a/README.md b/README.md
index 325b789..57a1739 100644
--- a/README.md
+++ b/README.md
@@ -1,20 +1,13 @@
-# \#\[const\_fn\]
+# \#\[const_fn\]
-[![crates-badge]][crates-url]
-[![docs-badge]][docs-url]
-[![license-badge]][license]
-[![rustc-badge]][rustc-url]
+[![crates.io](https://img.shields.io/crates/v/const_fn?style=flat-square&logo=rust)](https://crates.io/crates/const_fn)
+[![docs.rs](https://img.shields.io/badge/docs.rs-const__fn-blue?style=flat-square)](https://docs.rs/const_fn)
+[![license](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue?style=flat-square)](#license)
+[![rustc](https://img.shields.io/badge/rustc-1.31+-blue?style=flat-square&logo=rust)](https://www.rust-lang.org)
+[![build status](https://img.shields.io/github/workflow/status/taiki-e/const_fn/CI/main?style=flat-square&logo=github)](https://github.com/taiki-e/const_fn/actions)
-[crates-badge]: https://img.shields.io/crates/v/const_fn.svg
-[crates-url]: https://crates.io/crates/const_fn
-[docs-badge]: https://docs.rs/const_fn/badge.svg
-[docs-url]: https://docs.rs/const_fn
-[license-badge]: https://img.shields.io/badge/license-Apache--2.0%20OR%20MIT-blue.svg
-[license]: #license
-[rustc-badge]: https://img.shields.io/badge/rustc-1.31+-lightgray.svg
-[rustc-url]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html
-
-An attribute for easy generation of const functions with conditional compilations.
+An attribute for easy generation of const functions with conditional
+compilations.
## Usage
@@ -25,7 +18,7 @@ Add this to your `Cargo.toml`:
const_fn = "0.4"
```
-The current const_fn requires Rust 1.31 or later.
+*Compiler support: requires rustc 1.31+*
## Examples
@@ -61,17 +54,18 @@ pub const fn feature() {
This crate is proc-macro, but is very lightweight, and has no dependencies.
-You can manually define declarative macros with similar functionality (see [`if_rust_version`](https://github.com/ogoffart/if_rust_version#examples)), or [you can define the same function twice with different cfg](https://github.com/crossbeam-rs/crossbeam/blob/0b6ea5f69fde8768c1cfac0d3601e0b4325d7997/crossbeam-epoch/src/atomic.rs#L340-L372). (Note: the former approach requires more macros to be defined depending on the number of version requirements, the latter approach requires more functions to be maintained manually)
+You can manually define declarative macros with similar functionality (see
+[`if_rust_version`](https://github.com/ogoffart/if_rust_version#examples)),
+or [you can define the same function twice with different cfg](https://github.com/crossbeam-rs/crossbeam/blob/0b6ea5f69fde8768c1cfac0d3601e0b4325d7997/crossbeam-epoch/src/atomic.rs#L340-L372).
+(Note: the former approach requires more macros to be defined depending on the
+number of version requirements, the latter approach requires more functions to
+be maintained manually)
## License
-Licensed under either of
-
-* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
-* MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
-
-at your option.
-
-### Contribution
+Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
+[MIT license](LICENSE-MIT) at your option.
-Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
+Unless you explicitly state otherwise, any contribution intentionally submitted
+for inclusion in the work by you, as defined in the Apache-2.0 license, shall
+be dual licensed as above, without any additional terms or conditions.
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..25a9cdc
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,8 @@
+// Generated by update_crate_tests.py for tests that depend on this crate.
+{
+ "presubmit": [
+ {
+ "name": "crossbeam-epoch_device_test_src_lib"
+ }
+ ]
+}
diff --git a/build.rs b/build.rs
index f9e80a5..56d771f 100644
--- a/build.rs
+++ b/build.rs
@@ -1,4 +1,4 @@
-#![forbid(unsafe_code)]
+#![forbid(unsafe_code)] // https://github.com/rust-secure-code/cargo-geiger/issues/116
#![warn(rust_2018_idioms, single_use_lifetimes)]
use std::{
@@ -12,7 +12,7 @@ use std::{
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
- let rustc = env::var_os("RUSTC").map_or_else(|| "rustc".into(), PathBuf::from);
+ let rustc: PathBuf = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()).into();
let version = match Version::from_rustc(&rustc) {
Ok(version) => version.print(),
Err(e) => {
@@ -25,9 +25,10 @@ fn main() {
}
};
- let out_dir = env::var_os("OUT_DIR").map(PathBuf::from).expect("OUT_DIR not set");
- let out_file = out_dir.join("version.rs");
- fs::write(out_file, version).expect("failed to write version.rs");
+ let out_dir: PathBuf = env::var_os("OUT_DIR").expect("OUT_DIR not set").into();
+ let out_file = &out_dir.join("version");
+ fs::write(out_file, version)
+ .unwrap_or_else(|e| panic!("failed to write {}: {}", out_file.display(), e));
// Mark as build script has been run successfully.
println!("cargo:rustc-cfg=const_fn_has_build_script");
diff --git a/ci/install-component.sh b/ci/install-component.sh
deleted file mode 100755
index dbba7b1..0000000
--- a/ci/install-component.sh
+++ /dev/null
@@ -1,27 +0,0 @@
-#!/bin/bash
-
-# Install nightly Rust with a given component.
-#
-# If the component is unavailable on the latest nightly,
-# use the latest toolchain with the component available.
-#
-# When using stable Rust, this script is basically unnecessary as almost components available.
-#
-# Refs: https://github.com/rust-lang/rustup-components-history#the-web-part
-
-set -euo pipefail
-
-package="${1:?}"
-target="${2:-x86_64-unknown-linux-gnu}"
-
-date=$(curl -sSf https://rust-lang.github.io/rustup-components-history/"${target}"/"${package}")
-
-# shellcheck disable=1090
-"$(cd "$(dirname "${0}")" && pwd)"/install-rust.sh nightly-"${date}"
-
-rustup component add "${package}"
-
-case "${package}" in
- rustfmt) "${package}" -V ;;
- *) cargo "${package}" -V ;;
-esac
diff --git a/ci/install-rust.sh b/ci/install-rust.sh
deleted file mode 100755
index 92c5877..0000000
--- a/ci/install-rust.sh
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/bash
-
-set -euo pipefail
-
-toolchain="${1:-nightly}"
-
-rustup toolchain install "${toolchain}" --no-self-update --profile minimal
-rustup default "${toolchain}"
-
-rustup -V
-rustc -V
-cargo -V
diff --git a/out/version b/out/version
new file mode 100644
index 0000000..649f525
--- /dev/null
+++ b/out/version
@@ -0,0 +1 @@
+Version { minor: 50, nightly: true }
diff --git a/out/version.rs b/out/version.rs
deleted file mode 100644
index be0b2d7..0000000
--- a/out/version.rs
+++ /dev/null
@@ -1 +0,0 @@
-Version { minor: 46, nightly: true }
diff --git a/rustfmt.toml b/rustfmt.toml
deleted file mode 100644
index 18c6d2a..0000000
--- a/rustfmt.toml
+++ /dev/null
@@ -1,27 +0,0 @@
-# Rustfmt configuration
-# https://github.com/rust-lang/rustfmt/blob/master/Configurations.md
-
-# This is required for bug-fixes, which technically can't be made to the stable
-# first version.
-# This is unstable (tracking issue: https://github.com/rust-lang/rustfmt/issues/3383).
-version = "Two"
-# This is unstable (tracking issue: https://github.com/rust-lang/rustfmt/issues/3391)
-error_on_line_overflow = true
-
-# Override the default formatting style.
-# See https://internals.rust-lang.org/t/running-rustfmt-on-rust-lang-rust-and-other-rust-lang-repositories/8732/81.
-use_small_heuristics = "Max"
-# See https://github.com/rust-dev-tools/fmt-rfcs/issues/149.
-# This is unstable (tracking issue: https://github.com/rust-lang/rustfmt/issues/3370)
-overflow_delimited_expr = true
-
-# Apply rustfmt to more places.
-# This is unstable (tracking issue: https://github.com/rust-lang/rustfmt/issues/3362).
-merge_imports = true
-# This is unstable (tracking issue: https://github.com/rust-lang/rustfmt/issues/3348).
-format_code_in_doc_comments = true
-
-# Set the default settings again to always apply the proper formatting without
-# being affected by the editor settings.
-edition = "2018"
-tab_spaces = 4
diff --git a/scripts/README.md b/scripts/README.md
deleted file mode 100644
index 671a9b1..0000000
--- a/scripts/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-This directory contains scripts used by the developers of this project.
-
-Some scripts in this directory are also used in CI, but scripts intended to be used only in CI and scripts not intended to be used directly by the developers are basically in the `ci` directory.
diff --git a/scripts/check-minimal-versions.sh b/scripts/check-minimal-versions.sh
deleted file mode 100644
index 7d16e93..0000000
--- a/scripts/check-minimal-versions.sh
+++ /dev/null
@@ -1,58 +0,0 @@
-#!/bin/bash
-
-# Check all public crates with minimal version dependencies.
-#
-# Usage:
-# bash scripts/check-minimal-versions.sh [check|test]
-#
-# Note:
-# - This script modifies Cargo.toml and Cargo.lock while running
-# - This script exits with 1 if there are any unstaged changes
-# - This script requires nightly Rust and cargo-hack
-#
-# Refs: https://github.com/rust-lang/cargo/issues/5657
-
-set -euo pipefail
-
-cd "$(cd "$(dirname "${0}")" && pwd)"/..
-
-# Decide Rust toolchain.
-# If the `CI` environment variable is not set to `true`, then nightly is used by default.
-if [[ "${1:-none}" == "+"* ]]; then
- toolchain="${1}"
- shift
-elif [[ "${CI:-false}" != "true" ]]; then
- cargo +nightly -V >/dev/null || exit 1
- toolchain="+nightly"
-fi
-# This script requires nightly Rust and cargo-hack
-if [[ "${toolchain:-+nightly}" != "+nightly"* ]] || ! cargo hack -V &>/dev/null; then
- echo "error: check-minimal-versions.sh requires nightly Rust and cargo-hack"
- exit 1
-fi
-
-subcmd="${1:-check}"
-case "${subcmd}" in
- check | test) ;;
- *)
- echo "error: invalid argument \`${1}\`"
- exit 1
- ;;
-esac
-
-# This script modifies Cargo.toml and Cargo.lock, so make sure there are no
-# unstaged changes.
-git diff --exit-code
-# Restore original Cargo.toml and Cargo.lock on exit.
-trap 'git checkout .' EXIT
-
-if [[ "${subcmd}" == "check" ]]; then
- # Remove dev-dependencies from Cargo.toml to prevent the next `cargo update`
- # from determining minimal versions based on dev-dependencies.
- cargo hack --remove-dev-deps --workspace
-fi
-
-# Update Cargo.lock to minimal version dependencies.
-cargo ${toolchain:-} update -Zminimal-versions
-# Run check for all public members of the workspace.
-cargo ${toolchain:-} hack "${subcmd}" --workspace --all-features --ignore-private -Zfeatures=all
diff --git a/src/ast.rs b/src/ast.rs
index b156493..88a2b96 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -1,16 +1,12 @@
-use proc_macro::{Delimiter, Literal, Span, TokenStream, TokenTree};
+use proc_macro::{Delimiter, Ident, Literal, Span, TokenStream, TokenTree};
-use crate::{
- iter::TokenIter,
- to_tokens::ToTokens,
- utils::{parse_as_empty, tt_span},
- Result,
-};
+use crate::{iter::TokenIter, to_tokens::ToTokens, utils::tt_span, Result};
pub(crate) struct Func {
- pub(crate) attrs: Vec<Attribute>,
- pub(crate) sig: Vec<TokenTree>,
- pub(crate) body: TokenTree,
+ attrs: Vec<Attribute>,
+ // [const] [async] [unsafe] [extern [<abi>]] fn
+ sig: Vec<TokenTree>,
+ body: TokenStream,
pub(crate) print_const: bool,
}
@@ -19,10 +15,9 @@ pub(crate) fn parse_input(input: TokenStream) -> Result<Func> {
let attrs = parse_attrs(&mut input)?;
let sig = parse_signature(&mut input);
- let body = input.next();
- parse_as_empty(input)?;
+ let body: TokenStream = input.collect();
- if body.is_none()
+ if body.is_empty()
|| !sig
.iter()
.any(|tt| if let TokenTree::Ident(i) = tt { i.to_string() == "fn" } else { false })
@@ -32,19 +27,8 @@ pub(crate) fn parse_input(input: TokenStream) -> Result<Func> {
"#[const_fn] attribute may only be used on functions"
));
}
- if !sig
- .iter()
- .any(|tt| if let TokenTree::Ident(i) = tt { i.to_string() == "const" } else { false })
- {
- let span = sig
- .iter()
- .position(|tt| if let TokenTree::Ident(i) = tt { i.to_string() == "fn" } else { false })
- .map(|i| sig[i].span())
- .unwrap();
- return Err(error!(span, "#[const_fn] attribute may only be used on const functions"));
- }
- Ok(Func { attrs, sig, body: body.unwrap(), print_const: true })
+ Ok(Func { attrs, sig, body, print_const: true })
}
impl ToTokens for Func {
@@ -66,10 +50,32 @@ impl ToTokens for Func {
fn parse_signature(input: &mut TokenIter) -> Vec<TokenTree> {
let mut sig = Vec::new();
+ let mut has_const = false;
loop {
match input.peek() {
- Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Brace => break,
None => break,
+ Some(TokenTree::Ident(i)) if !has_const => {
+ match &*i.to_string() {
+ "fn" => {
+ sig.push(TokenTree::Ident(Ident::new("const", i.span())));
+ sig.push(input.next().unwrap());
+ break;
+ }
+ "const" => {
+ has_const = true;
+ }
+ "async" | "unsafe" | "extern" => {
+ has_const = true;
+ sig.push(TokenTree::Ident(Ident::new("const", i.span())));
+ }
+ _ => {}
+ }
+ sig.push(input.next().unwrap());
+ }
+ Some(TokenTree::Ident(i)) if i.to_string() == "fn" => {
+ sig.push(input.next().unwrap());
+ break;
+ }
Some(_) => sig.push(input.next().unwrap()),
}
}
diff --git a/src/error.rs b/src/error.rs
index b572d5a..e4edb0e 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,6 +1,7 @@
-use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use std::iter::FromIterator;
+use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
+
pub(crate) struct Error {
span: Span,
msg: String,
diff --git a/src/lib.rs b/src/lib.rs
index 1f818f8..c023fb1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,5 @@
-//! An attribute for easy generation of const functions with conditional compilations.
+//! An attribute for easy generation of const functions with conditional
+//! compilations.
//!
//! # Examples
//!
@@ -39,23 +40,16 @@
//! You can manually define declarative macros with similar functionality (see [`if_rust_version`](https://github.com/ogoffart/if_rust_version#examples)), or [you can define the same function twice with different cfg](https://github.com/crossbeam-rs/crossbeam/blob/0b6ea5f69fde8768c1cfac0d3601e0b4325d7997/crossbeam-epoch/src/atomic.rs#L340-L372).
//! (Note: the former approach requires more macros to be defined depending on the number of version requirements, the latter approach requires more functions to be maintained manually)
-#![doc(html_root_url = "https://docs.rs/const_fn/0.4.3")]
#![doc(test(
no_crate_inject,
- attr(deny(warnings, rust_2018_idioms, single_use_lifetimes), allow(dead_code))
+ attr(
+ deny(warnings, rust_2018_idioms, single_use_lifetimes),
+ allow(dead_code, unused_variables)
+ )
))]
#![forbid(unsafe_code)]
#![warn(future_incompatible, rust_2018_idioms, single_use_lifetimes, unreachable_pub)]
#![warn(clippy::all, clippy::default_trait_access)]
-// mem::take, #[non_exhaustive], and Option::{as_deref, as_deref_mut} require Rust 1.40,
-// matches! requires Rust 1.42, str::{strip_prefix, strip_suffix} requires Rust 1.45
-#![allow(
- clippy::mem_replace_with_default,
- clippy::manual_non_exhaustive,
- clippy::option_as_ref_deref,
- clippy::match_like_matches_macro,
- clippy::manual_strip
-)]
// older compilers require explicit `extern crate`.
#[allow(unused_extern_crates)]
@@ -69,9 +63,10 @@ mod error;
mod iter;
mod to_tokens;
-use proc_macro::{Delimiter, TokenStream, TokenTree};
use std::str::FromStr;
+use proc_macro::{Delimiter, TokenStream, TokenTree};
+
use crate::{
ast::{Func, LitStr},
error::Error,
@@ -82,6 +77,7 @@ use crate::{
type Result<T, E = Error> = std::result::Result<T, E>;
/// An attribute for easy generation of const functions with conditional compilations.
+///
/// See crate level documentation for details.
#[proc_macro_attribute]
pub fn const_fn(args: TokenStream, input: TokenStream) -> TokenStream {
@@ -125,6 +121,7 @@ fn expand(arg: Arg, mut func: Func) -> TokenStream {
func.print_const = VERSION.nightly;
func.to_token_stream()
}
+ Arg::Always => func.to_token_stream(),
}
}
@@ -137,6 +134,8 @@ enum Arg {
Cfg(TokenStream),
// `const_fn(feature = "...")`
Feature(TokenStream),
+ // `const_fn`
+ Always,
}
fn parse_arg(tokens: TokenStream) -> Result<Arg> {
@@ -145,6 +144,7 @@ fn parse_arg(tokens: TokenStream) -> Result<Arg> {
let next = iter.next();
let next_span = tt_span(next.as_ref());
match next {
+ None => return Ok(Arg::Always),
Some(TokenTree::Ident(i)) => match &*i.to_string() {
"nightly" => {
parse_as_empty(iter)?;
@@ -188,7 +188,7 @@ fn parse_arg(tokens: TokenStream) -> Result<Arg> {
};
}
}
- _ => {}
+ Some(_) => {}
}
Err(error!(next_span, "expected one of: `nightly`, `cfg`, `feature`, string literal"))
@@ -228,7 +228,7 @@ struct Version {
}
#[cfg(const_fn_has_build_script)]
-const VERSION: Version = include!(concat!(env!("OUT_DIR"), "/version.rs"));
+const VERSION: Version = include!(concat!(env!("OUT_DIR"), "/version"));
// If build script has not run or unable to determine version, it is considered as Rust 1.0.
#[cfg(not(const_fn_has_build_script))]
const VERSION: Version = Version { minor: 0, nightly: false };
diff --git a/src/to_tokens.rs b/src/to_tokens.rs
index 771c7f6..2f44223 100644
--- a/src/to_tokens.rs
+++ b/src/to_tokens.rs
@@ -1,6 +1,7 @@
-use proc_macro::{Ident, Literal, TokenStream, TokenTree};
use std::iter;
+use proc_macro::{Ident, Literal, TokenStream, TokenTree};
+
pub(crate) trait ToTokens {
fn to_tokens(&self, tokens: &mut TokenStream);
diff --git a/src/utils.rs b/src/utils.rs
index e557ea5..30b04aa 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,6 +1,7 @@
-use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use std::iter::FromIterator;
+use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
+
use crate::Result;
macro_rules! error {
@@ -18,7 +19,7 @@ pub(crate) fn tt_span(tt: Option<&TokenTree>) -> Span {
pub(crate) fn parse_as_empty(mut tokens: impl Iterator<Item = TokenTree>) -> Result<()> {
match tokens.next() {
- Some(tt) => Err(error!(tt.span(), "unexpected token: {}", tt)),
+ Some(tt) => Err(error!(tt.span(), "unexpected token `{}`", tt)),
None => Ok(()),
}
}