diff options
author | Jeff Vander Stoep <jeffv@google.com> | 2023-03-07 03:55:21 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-03-07 03:55:21 +0000 |
commit | 6aed02773ec17fe5dcd3606f52451356e1f93b79 (patch) | |
tree | a930d3b84f8285e6210b2edf59de7382f0cf76d7 | |
parent | 1575776e467d34f62f073df15ac360e7b34e79dc (diff) | |
parent | 798e45b1ea4c4b5c6a1d51b0dc04823974bec621 (diff) | |
download | no-panic-6aed02773ec17fe5dcd3606f52451356e1f93b79.tar.gz |
Upgrade no-panic to 0.1.21 am: 1f1a6173ee am: b6cc1be463 am: 798e45b1ea
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/no-panic/+/2470804
Change-Id: I8da0283cfeb7f4232fd8db6e088c161b59a04ab0
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | .github/workflows/ci.yml | 9 | ||||
-rw-r--r-- | Android.bp | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | Cargo.toml.orig | 2 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | src/lib.rs | 60 | ||||
-rw-r--r-- | tests/test.rs | 52 |
8 files changed, 126 insertions, 11 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 3ae0a43..341a73b 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "67d07dbfd0a40f088de60cf0347f67fc373a2a44" + "sha1": "b5c6d7e1df7e423a9543ee9317f63339660743a6" }, "path_in_vcs": "" }
\ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f7af61..650b0e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,6 +3,7 @@ name: CI on: push: pull_request: + workflow_dispatch: schedule: [cron: "40 1 * * *"] permissions: @@ -30,6 +31,12 @@ jobs: - uses: dtolnay/rust-toolchain@master with: toolchain: ${{matrix.rust}} + - name: Enable deny(non_exhaustive_omitted_patterns) + run: echo RUSTFLAGS=$(RUSTFLAGS)\ --cfg=exhaustive >> $GITHUB_ENV + if: matrix.rust == 'nightly' + - name: Enable type layout randomization + run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV + if: matrix.rust == 'nightly' - run: cargo test xplat: @@ -49,6 +56,8 @@ jobs: steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@nightly + - name: Enable type layout randomization + run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV - run: cargo test msrv: @@ -41,7 +41,7 @@ rust_proc_macro { name: "libno_panic", crate_name: "no_panic", cargo_env_compat: true, - cargo_pkg_version: "0.1.19", + cargo_pkg_version: "0.1.21", srcs: ["src/lib.rs"], edition: "2018", rustlibs: [ @@ -13,7 +13,7 @@ edition = "2018" rust-version = "1.31" name = "no-panic" -version = "0.1.19" +version = "0.1.21" authors = ["David Tolnay <dtolnay@gmail.com>"] description = "Attribute macro to require that the compiler prove a function can't ever panic." documentation = "https://docs.rs/no-panic" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 3a7a13f..98bf79c 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "no-panic" -version = "0.1.19" +version = "0.1.21" authors = ["David Tolnay <dtolnay@gmail.com>"] description = "Attribute macro to require that the compiler prove a function can't ever panic." documentation = "https://docs.rs/no-panic" @@ -11,13 +11,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/no-panic/no-panic-0.1.19.crate" + value: "https://static.crates.io/crates/no-panic/no-panic-0.1.21.crate" } - version: "0.1.19" + version: "0.1.21" license_type: NOTICE last_upgrade_date { year: 2023 - month: 2 - day: 16 + month: 3 + day: 6 } } @@ -119,7 +119,13 @@ //! [Kixunil]: https://github.com/Kixunil //! [`dont_panic`]: https://github.com/Kixunil/dont_panic -#![allow(clippy::doc_markdown, clippy::missing_panics_doc)] +#![doc(html_root_url = "https://docs.rs/no-panic/0.1.21")] +#![allow( + clippy::doc_markdown, + clippy::match_same_arms, + clippy::missing_panics_doc +)] +#![cfg_attr(all(test, exhaustive), feature(non_exhaustive_omitted_patterns_lint))] extern crate proc_macro; @@ -127,7 +133,10 @@ use proc_macro::TokenStream; use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::quote; use syn::parse::{Error, Nothing, Result}; -use syn::{parse_quote, Attribute, FnArg, Ident, ItemFn, Pat, PatType, ReturnType}; +use syn::{ + parse_quote, Attribute, FnArg, GenericArgument, Ident, ItemFn, Pat, PatType, Path, + PathArguments, ReturnType, Token, Type, TypeInfer, TypeParamBound, +}; #[proc_macro_attribute] pub fn no_panic(args: TokenStream, input: TokenStream) -> TokenStream { @@ -155,6 +164,47 @@ fn parse(args: TokenStream2, input: TokenStream2) -> Result<ItemFn> { Ok(function) } +// Convert `Path<impl Trait>` to `Path<_>` +fn make_impl_trait_wild(ret: &mut Type) { + match ret { + Type::ImplTrait(impl_trait) => { + *ret = Type::Infer(TypeInfer { + underscore_token: Token![_](impl_trait.impl_token.span), + }); + } + Type::Array(ret) => make_impl_trait_wild(&mut ret.elem), + Type::Group(ret) => make_impl_trait_wild(&mut ret.elem), + Type::Paren(ret) => make_impl_trait_wild(&mut ret.elem), + Type::Path(ret) => make_impl_trait_wild_in_path(&mut ret.path), + Type::Ptr(ret) => make_impl_trait_wild(&mut ret.elem), + Type::Reference(ret) => make_impl_trait_wild(&mut ret.elem), + Type::Slice(ret) => make_impl_trait_wild(&mut ret.elem), + Type::TraitObject(ret) => { + for bound in &mut ret.bounds { + if let TypeParamBound::Trait(bound) = bound { + make_impl_trait_wild_in_path(&mut bound.path); + } + } + } + Type::Tuple(ret) => ret.elems.iter_mut().for_each(make_impl_trait_wild), + Type::BareFn(_) | Type::Infer(_) | Type::Macro(_) | Type::Never(_) | Type::Verbatim(_) => {} + #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))] + _ => {} + } +} + +fn make_impl_trait_wild_in_path(path: &mut Path) { + for segment in &mut path.segments { + if let PathArguments::AngleBracketed(bracketed) = &mut segment.arguments { + for arg in &mut bracketed.args { + if let GenericArgument::Type(arg) = arg { + make_impl_trait_wild(arg); + } + } + } + } +} + fn expand_no_panic(mut function: ItemFn) -> TokenStream2 { let mut move_self = None; let mut arg_pat = Vec::new(); @@ -197,7 +247,11 @@ fn expand_no_panic(mut function: ItemFn) -> TokenStream2 { let ret = match &function.sig.output { ReturnType::Default => quote!(-> ()), - output @ ReturnType::Type(..) => quote!(#output), + ReturnType::Type(arrow, output) => { + let mut output = output.clone(); + make_impl_trait_wild(&mut output); + quote!(#arrow #output) + } }; let stmts = function.block.stmts; let message = format!( diff --git a/tests/test.rs b/tests/test.rs index f6505ba..c65a08c 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -193,6 +193,58 @@ assert_no_panic! { fn main() {} } + + mod test_deref_coercion { + #[no_panic] + pub fn f(s: &str) -> &str { + &s + } + + fn main() {} + } + + mod test_return_impl_trait { + use std::io; + + #[no_panic] + pub fn f() -> io::Result<impl io::Write> { + Ok(Vec::new()) + } + + fn main() {} + } + + mod test_conditional_return { + #[no_panic] + pub fn f(i: i32) { + if i < 0 { + return; + } + } + + fn main() { + println!("{:?}", f(-1)); + } + } + + mod test_conditional_return_macro { + macro_rules! return_if_negative { + ($e:expr) => { + if $e < 0 { + return; + } + } + } + + #[no_panic] + pub fn f(i: i32) { + return_if_negative!(i); + } + + fn main() { + println!("{:?}", f(-1)); + } + } } assert_link_error! { |