diff options
author | Joel Galenson <jgalenson@google.com> | 2021-06-09 20:22:44 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-06-09 20:22:44 +0000 |
commit | 8d280d0b1bf080b11eb1be3b950a13ac8ccf7024 (patch) | |
tree | bcbbfb99c192fde3b889a0f5eac132be76e06c71 | |
parent | 6a76904f4b9fc8d3ab46ca37a47e0d0de919f4d5 (diff) | |
parent | 40177b3a2a28b0f1b052f38691e663946c0cc926 (diff) | |
download | tokio-macros-8d280d0b1bf080b11eb1be3b950a13ac8ccf7024.tar.gz |
Upgrade rust/crates/tokio-macros to 1.2.0 am: 40177b3a2aandroid-s-beta-4android-s-beta-3android-s-beta-4
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/tokio-macros/+/1712792
Change-Id: I2f948f926398ea4625fcc6d8d7c34a9cb67d185b
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 27 | ||||
-rw-r--r-- | CHANGELOG.md | 10 | ||||
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | Cargo.toml.orig | 7 | ||||
-rw-r--r-- | LICENSE | 2 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | TEST_MAPPING | 119 | ||||
-rw-r--r-- | src/entry.rs | 134 | ||||
-rw-r--r-- | src/lib.rs | 5 |
10 files changed, 240 insertions, 80 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 482e2e5..6934779 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "d41882e2a18ea4459d8f3a3c7ba61c4c20b806b4" + "sha1": "3a659c47c33f0d8ccdebbdce320610dc8814fae0" } } @@ -1,4 +1,5 @@ // This file is generated by cargo2android.py --run --dependencies --tests. +// Do not modify this file as changes will be overridden on upgrade. package { default_applicable_licenses: ["external_rust_crates_tokio-macros_license"], @@ -52,22 +53,22 @@ rust_test_host { // bytes-1.0.1 "default,std" // cfg-if-1.0.0 // instant-0.1.9 -// libc-0.2.86 "align,default,std" -// lock_api-0.4.2 +// libc-0.2.94 "default,std" +// lock_api-0.4.4 // log-0.4.14 -// memchr-2.3.4 "default,std" -// mio-0.7.7 "default,net,os-ext,os-poll,os-util,tcp,udp,uds" +// memchr-2.4.0 "default,std" +// mio-0.7.11 "default,net,os-ext,os-poll,os-util,tcp,udp,uds" // num_cpus-1.13.0 -// once_cell-1.5.2 "alloc,default,std" +// once_cell-1.7.2 "alloc,default,race,std" // parking_lot-0.11.1 "default" -// parking_lot_core-0.8.2 -// pin-project-lite-0.2.4 -// proc-macro2-1.0.24 "default,proc-macro" -// quote-1.0.8 "default,proc-macro" +// parking_lot_core-0.8.3 +// pin-project-lite-0.2.6 +// proc-macro2-1.0.26 "default,proc-macro" +// quote-1.0.9 "default,proc-macro" // scopeguard-1.1.0 // signal-hook-registry-1.3.0 // smallvec-1.6.1 -// syn-1.0.60 "clone-impls,default,derive,full,parsing,printing,proc-macro,quote" -// tokio-1.2.0 "bytes,default,fs,full,io-std,io-util,libc,macros,memchr,mio,net,num_cpus,once_cell,parking_lot,process,rt,rt-multi-thread,signal,signal-hook-registry,sync,time,tokio-macros,winapi" -// tokio-macros-1.1.0 -// unicode-xid-0.2.1 "default" +// syn-1.0.72 "clone-impls,default,derive,full,parsing,printing,proc-macro,quote" +// tokio-1.6.0 "bytes,default,fs,full,io-std,io-util,libc,macros,memchr,mio,net,num_cpus,once_cell,parking_lot,process,rt,rt-multi-thread,signal,signal-hook-registry,sync,time,tokio-macros,winapi" +// tokio-macros-1.2.0 +// unicode-xid-0.2.2 "default" diff --git a/CHANGELOG.md b/CHANGELOG.md index 6760b17..6df34c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 1.2.0 (May 14, 2021) + +- macros: forward input arguments in `#[tokio::test]` ([#3691]) +- macros: improve diagnostics on type mismatch ([#3766]) +- macros: various error message improvements ([#3677]) + +[#3677]: https://github.com/tokio-rs/tokio/pull/3677 +[#3691]: https://github.com/tokio-rs/tokio/pull/3691 +[#3766]: https://github.com/tokio-rs/tokio/pull/3766 + # 1.1.0 (February 5, 2021) - add `start_paused` option to macros ([#3492]) @@ -13,11 +13,11 @@ [package] edition = "2018" name = "tokio-macros" -version = "1.1.0" +version = "1.2.0" authors = ["Tokio Contributors <team@tokio.rs>"] description = "Tokio's proc macros.\n" homepage = "https://tokio.rs" -documentation = "https://docs.rs/tokio-macros/1.1.0/tokio_macros" +documentation = "https://docs.rs/tokio-macros/1.2.0/tokio_macros" categories = ["asynchronous"] license = "MIT" repository = "https://github.com/tokio-rs/tokio" @@ -33,7 +33,7 @@ version = "1.0.7" version = "1" [dependencies.syn] -version = "1.0.3" +version = "1.0.56" features = ["full"] [dev-dependencies.tokio] version = "1.0.0" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index ea7b2de..875ceb6 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -2,18 +2,17 @@ name = "tokio-macros" # When releasing to crates.io: # - Remove path dependencies -# - Update html_root_url. # - Update doc url # - Cargo.toml # - Update CHANGELOG.md. # - Create "tokio-macros-1.0.x" git tag. -version = "1.1.0" +version = "1.2.0" edition = "2018" authors = ["Tokio Contributors <team@tokio.rs>"] license = "MIT" repository = "https://github.com/tokio-rs/tokio" homepage = "https://tokio.rs" -documentation = "https://docs.rs/tokio-macros/1.1.0/tokio_macros" +documentation = "https://docs.rs/tokio-macros/1.2.0/tokio_macros" description = """ Tokio's proc macros. """ @@ -27,7 +26,7 @@ proc-macro = true [dependencies] proc-macro2 = "1.0.7" quote = "1" -syn = { version = "1.0.3", features = ["full"] } +syn = { version = "1.0.56", features = ["full"] } [dev-dependencies] tokio = { version = "1.0.0", path = "../tokio", features = ["full"] } @@ -1,4 +1,4 @@ -Copyright (c) 2020 Tokio Contributors +Copyright (c) 2021 Tokio Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/tokio-macros/tokio-macros-1.1.0.crate" + value: "https://static.crates.io/crates/tokio-macros/tokio-macros-1.2.0.crate" } - version: "1.1.0" + version: "1.2.0" license_type: NOTICE last_upgrade_date { year: 2021 - month: 2 - day: 9 + month: 5 + day: 19 } } diff --git a/TEST_MAPPING b/TEST_MAPPING new file mode 100644 index 0000000..686fbda --- /dev/null +++ b/TEST_MAPPING @@ -0,0 +1,119 @@ +// Generated by update_crate_tests.py for tests that depend on this crate. +{ + "presubmit": [ + { + "name": "doh_unit_test" + }, + { + "name": "futures-util_device_test_src_lib" + }, + { + "name": "tokio-test_device_test_src_lib" + }, + { + "name": "tokio-test_device_test_tests_block_on" + }, + { + "name": "tokio-test_device_test_tests_io" + }, + { + "name": "tokio-test_device_test_tests_macros" + }, + { + "name": "tokio_device_test_tests_buffered" + }, + { + "name": "tokio_device_test_tests_io_async_read" + }, + { + "name": "tokio_device_test_tests_io_copy_bidirectional" + }, + { + "name": "tokio_device_test_tests_io_lines" + }, + { + "name": "tokio_device_test_tests_io_mem_stream" + }, + { + "name": "tokio_device_test_tests_io_read" + }, + { + "name": "tokio_device_test_tests_io_read_buf" + }, + { + "name": "tokio_device_test_tests_io_read_to_end" + }, + { + "name": "tokio_device_test_tests_io_take" + }, + { + "name": "tokio_device_test_tests_io_write" + }, + { + "name": "tokio_device_test_tests_io_write_all" + }, + { + "name": "tokio_device_test_tests_io_write_buf" + }, + { + "name": "tokio_device_test_tests_io_write_int" + }, + { + "name": "tokio_device_test_tests_macros_join" + }, + { + "name": "tokio_device_test_tests_no_rt" + }, + { + "name": "tokio_device_test_tests_rt_basic" + }, + { + "name": "tokio_device_test_tests_rt_threaded" + }, + { + "name": "tokio_device_test_tests_sync_barrier" + }, + { + "name": "tokio_device_test_tests_sync_broadcast" + }, + { + "name": "tokio_device_test_tests_sync_errors" + }, + { + "name": "tokio_device_test_tests_sync_mpsc" + }, + { + "name": "tokio_device_test_tests_sync_mutex_owned" + }, + { + "name": "tokio_device_test_tests_sync_rwlock" + }, + { + "name": "tokio_device_test_tests_sync_watch" + }, + { + "name": "tokio_device_test_tests_task_local" + }, + { + "name": "tokio_device_test_tests_task_local_set" + }, + { + "name": "tokio_device_test_tests_tcp_accept" + }, + { + "name": "tokio_device_test_tests_tcp_echo" + }, + { + "name": "tokio_device_test_tests_tcp_into_std" + }, + { + "name": "tokio_device_test_tests_tcp_shutdown" + }, + { + "name": "tokio_device_test_tests_time_rt" + }, + { + "name": "tokio_device_test_tests_uds_split" + } + ] +} diff --git a/src/entry.rs b/src/entry.rs index f82a329..ddc1958 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,7 +1,6 @@ use proc_macro::TokenStream; use proc_macro2::Span; -use quote::quote; -use syn::spanned::Spanned; +use quote::{quote, quote_spanned, ToTokens}; #[derive(Clone, Copy, PartialEq)] enum RuntimeFlavor { @@ -34,6 +33,7 @@ struct Configuration { flavor: Option<RuntimeFlavor>, worker_threads: Option<(usize, Span)>, start_paused: Option<(bool, Span)>, + is_test: bool, } impl Configuration { @@ -47,6 +47,7 @@ impl Configuration { flavor: None, worker_threads: None, start_paused: None, + is_test, } } @@ -92,16 +93,25 @@ impl Configuration { Ok(()) } + fn macro_name(&self) -> &'static str { + if self.is_test { + "tokio::test" + } else { + "tokio::main" + } + } + fn build(&self) -> Result<FinalConfig, syn::Error> { let flavor = self.flavor.unwrap_or(self.default_flavor); use RuntimeFlavor::*; let worker_threads = match (flavor, self.worker_threads) { (CurrentThread, Some((_, worker_threads_span))) => { - return Err(syn::Error::new( - worker_threads_span, - "The `worker_threads` option requires the `multi_thread` runtime flavor.", - )) + let msg = format!( + "The `worker_threads` option requires the `multi_thread` runtime flavor. Use `#[{}(flavor = \"multi_thread\")]`", + self.macro_name(), + ); + return Err(syn::Error::new(worker_threads_span, msg)); } (CurrentThread, None) => None, (Threaded, worker_threads) if self.rt_multi_thread_available => { @@ -119,10 +129,11 @@ impl Configuration { let start_paused = match (flavor, self.start_paused) { (Threaded, Some((_, start_paused_span))) => { - return Err(syn::Error::new( - start_paused_span, - "The `start_paused` option requires the `current_thread` runtime flavor.", - )); + let msg = format!( + "The `start_paused` option requires the `current_thread` runtime flavor. Use `#[{}(flavor = \"current_thread\")]`", + self.macro_name(), + ); + return Err(syn::Error::new(start_paused_span, msg)); } (CurrentThread, Some((start_paused, _))) => Some(start_paused), (_, None) => None, @@ -142,12 +153,12 @@ fn parse_int(int: syn::Lit, span: Span, field: &str) -> Result<usize, syn::Error Ok(value) => Ok(value), Err(e) => Err(syn::Error::new( span, - format!("Failed to parse {} as integer: {}", field, e), + format!("Failed to parse value of `{}` as integer: {}", field, e), )), }, _ => Err(syn::Error::new( span, - format!("Failed to parse {} as integer.", field), + format!("Failed to parse value of `{}` as integer.", field), )), } } @@ -158,7 +169,7 @@ fn parse_string(int: syn::Lit, span: Span, field: &str) -> Result<String, syn::E syn::Lit::Verbatim(s) => Ok(s.to_string()), _ => Err(syn::Error::new( span, - format!("Failed to parse {} as string.", field), + format!("Failed to parse value of `{}` as string.", field), )), } } @@ -168,7 +179,7 @@ fn parse_bool(bool: syn::Lit, span: Span, field: &str) -> Result<bool, syn::Erro syn::Lit::Bool(b) => Ok(b.value), _ => Err(syn::Error::new( span, - format!("Failed to parse {} as bool.", field), + format!("Failed to parse value of `{}` as bool.", field), )), } } @@ -179,24 +190,13 @@ fn parse_knobs( is_test: bool, rt_multi_thread: bool, ) -> Result<TokenStream, syn::Error> { - let sig = &mut input.sig; - let body = &input.block; - let attrs = &input.attrs; - let vis = input.vis; - - if sig.asyncness.is_none() { - let msg = "the async keyword is missing from the function declaration"; - return Err(syn::Error::new_spanned(sig.fn_token, msg)); + if input.sig.asyncness.take().is_none() { + let msg = "the `async` keyword is missing from the function declaration"; + return Err(syn::Error::new_spanned(input.sig.fn_token, msg)); } - sig.asyncness = None; - - let macro_name = if is_test { - "tokio::test" - } else { - "tokio::main" - }; let mut config = Configuration::new(is_test, rt_multi_thread); + let macro_name = config.macro_name(); for arg in args { match arg { @@ -208,20 +208,32 @@ fn parse_knobs( } match ident.unwrap().to_string().to_lowercase().as_str() { "worker_threads" => { - config.set_worker_threads(namevalue.lit.clone(), namevalue.span())?; + config.set_worker_threads( + namevalue.lit.clone(), + syn::spanned::Spanned::span(&namevalue.lit), + )?; } "flavor" => { - config.set_flavor(namevalue.lit.clone(), namevalue.span())?; + config.set_flavor( + namevalue.lit.clone(), + syn::spanned::Spanned::span(&namevalue.lit), + )?; } "start_paused" => { - config.set_start_paused(namevalue.lit.clone(), namevalue.span())?; + config.set_start_paused( + namevalue.lit.clone(), + syn::spanned::Spanned::span(&namevalue.lit), + )?; } "core_threads" => { let msg = "Attribute `core_threads` is renamed to `worker_threads`"; return Err(syn::Error::new_spanned(namevalue, msg)); } name => { - let msg = format!("Unknown attribute {} is specified; expected one of: `flavor`, `worker_threads`", name); + let msg = format!( + "Unknown attribute {} is specified; expected one of: `flavor`, `worker_threads`, `start_paused`", + name, + ); return Err(syn::Error::new_spanned(namevalue, msg)); } } @@ -266,11 +278,29 @@ fn parse_knobs( let config = config.build()?; + // If type mismatch occurs, the current rustc points to the last statement. + let (last_stmt_start_span, last_stmt_end_span) = { + let mut last_stmt = input + .block + .stmts + .last() + .map(ToTokens::into_token_stream) + .unwrap_or_default() + .into_iter(); + // `Span` on stable Rust has a limitation that only points to the first + // token, not the whole tokens. We can work around this limitation by + // using the first/last span of the tokens like + // `syn::Error::new_spanned` does. + let start = last_stmt.next().map_or_else(Span::call_site, |t| t.span()); + let end = last_stmt.last().map_or(start, |t| t.span()); + (start, end) + }; + let mut rt = match config.flavor { - RuntimeFlavor::CurrentThread => quote! { + RuntimeFlavor::CurrentThread => quote_spanned! {last_stmt_start_span=> tokio::runtime::Builder::new_current_thread() }, - RuntimeFlavor::Threaded => quote! { + RuntimeFlavor::Threaded => quote_spanned! {last_stmt_start_span=> tokio::runtime::Builder::new_multi_thread() }, }; @@ -281,26 +311,31 @@ fn parse_knobs( rt = quote! { #rt.start_paused(#v) }; } - let header = { - if is_test { - quote! { - #[::core::prelude::v1::test] - } - } else { - quote! {} + let header = if is_test { + quote! { + #[::core::prelude::v1::test] } + } else { + quote! {} }; - let result = quote! { - #header - #(#attrs)* - #vis #sig { + let body = &input.block; + let brace_token = input.block.brace_token; + input.block = syn::parse2(quote_spanned! {last_stmt_end_span=> + { #rt .enable_all() .build() .unwrap() .block_on(async #body) } + }) + .unwrap(); + input.block.brace_token = brace_token; + + let result = quote! { + #header + #input }; Ok(result.into()) @@ -334,12 +369,5 @@ pub(crate) fn test(args: TokenStream, item: TokenStream, rt_multi_thread: bool) } } - if !input.sig.inputs.is_empty() { - let msg = "the test function cannot accept arguments"; - return syn::Error::new_spanned(&input.sig.inputs, msg) - .to_compile_error() - .into(); - } - parse_knobs(input, args, true, rt_multi_thread).unwrap_or_else(|e| e.to_compile_error().into()) } @@ -1,4 +1,3 @@ -#![doc(html_root_url = "https://docs.rs/tokio-macros/1.1.0")] #![allow(clippy::needless_doctest_main)] #![warn( missing_debug_implementations, @@ -168,6 +167,8 @@ use proc_macro::TokenStream; /// } /// ``` /// +/// Note that `start_paused` requires the `test-util` feature to be enabled. +/// /// ### NOTE: /// /// If you rename the Tokio crate in your dependencies this macro will not work. @@ -258,6 +259,8 @@ pub fn main_rt(args: TokenStream, item: TokenStream) -> TokenStream { /// } /// ``` /// +/// Note that `start_paused` requires the `test-util` feature to be enabled. +/// /// ### NOTE: /// /// If you rename the Tokio crate in your dependencies this macro will not work. |