From 064dc5463e84e09d9bbab8864df25f33080adbd0 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 8 Sep 2024 17:41:41 +0900 Subject: [PATCH] codegen: Favor assert/panic over error --- tools/codegen/Cargo.toml | 1 - tools/codegen/src/file.rs | 41 +++++++++++++++++++++------------------ tools/codegen/src/main.rs | 21 +++++++++----------- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/tools/codegen/Cargo.toml b/tools/codegen/Cargo.toml index c1982ef..a281ab5 100644 --- a/tools/codegen/Cargo.toml +++ b/tools/codegen/Cargo.toml @@ -3,7 +3,6 @@ name = "futures-async-stream-internal-codegen" edition = "2021" [dependencies] -anyhow = "1" fs-err = "2" globset = { version = "0.4", default-features = false } prettyplease = "0.2" diff --git a/tools/codegen/src/file.rs b/tools/codegen/src/file.rs index dbad092..2dc25d7 100644 --- a/tools/codegen/src/file.rs +++ b/tools/codegen/src/file.rs @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use std::{ + io, path::{Path, PathBuf}, process::Command, str, sync::LazyLock, }; -use anyhow::{bail, format_err, Context as _, Result}; use fs_err as fs; use proc_macro2::TokenStream; @@ -53,17 +53,18 @@ pub(crate) fn write( function_name: &str, path: impl AsRef, contents: TokenStream, -) -> Result<()> { - write_raw(function_name, path.as_ref(), format_tokens(contents)?) +) -> io::Result<()> { + write_raw(function_name, path.as_ref(), format_tokens(contents)) } -fn format_tokens(contents: TokenStream) -> Result> { +#[track_caller] +fn format_tokens(contents: TokenStream) -> Vec { let mut out = prettyplease::unparse( - &syn::parse2(contents.clone()).map_err(|e| format_err!("{e} in:\n---\n{contents}\n---"))?, + &syn::parse2(contents.clone()).unwrap_or_else(|e| panic!("{e} in:\n---\n{contents}\n---")), ) .into_bytes(); format_macros(&mut out); - Ok(out) + out } // Roughly format the code inside macro calls. @@ -129,7 +130,7 @@ pub(crate) fn write_raw( function_name: &str, path: &Path, contents: impl AsRef<[u8]>, -) -> Result<()> { +) -> io::Result<()> { static LINGUIST_GENERATED: LazyLock> = LazyLock::new(|| { let gitattributes = fs::read_to_string(workspace_root().join(".gitattributes")).unwrap(); let mut linguist_generated = vec![]; @@ -157,19 +158,21 @@ pub(crate) fn write_raw( Ok(()) } -pub(crate) fn git_ls_files(dir: &Path, filters: &[&str]) -> Result> { +#[track_caller] +pub(crate) fn git_ls_files(dir: &Path, filters: &[&str]) -> Vec<(String, PathBuf)> { let mut cmd = Command::new("git"); cmd.arg("ls-files").args(filters).current_dir(dir); - let output = cmd.output().with_context(|| format!("could not execute process `{cmd:?}`"))?; - if !output.status.success() { - bail!( - "process didn't exit successfully: `{cmd:?}`:\n\nSTDOUT:\n{0}\n{1}\n{0}\n\nSTDERR:\n{0}\n{2}\n{0}\n", - "-".repeat(60), - String::from_utf8_lossy(&output.stdout), - String::from_utf8_lossy(&output.stderr), - ); - } - Ok(str::from_utf8(&output.stdout)? + let output = + cmd.output().unwrap_or_else(|e| panic!("could not execute process `{cmd:?}`: {e}")); + assert!( + output.status.success(), + "process didn't exit successfully: `{cmd:?}`:\n\nSTDOUT:\n{0}\n{1}\n{0}\n\nSTDERR:\n{0}\n{2}\n{0}\n", + "-".repeat(60), + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr), + ); + str::from_utf8(&output.stdout) + .unwrap() .lines() .map(str::trim) .filter_map(|f| { @@ -182,5 +185,5 @@ pub(crate) fn git_ls_files(dir: &Path, filters: &[&str]) -> Result Result<()> { - gen_assert_impl()?; - Ok(()) +fn main() { + gen_assert_impl(); } -fn gen_assert_impl() -> Result<()> { +fn gen_assert_impl() { const NOT_SEND: &[&str] = &[]; const NOT_SYNC: &[&str] = &[]; const NOT_UNPIN: &[&str] = &[]; + // TODO: we can revert 477aa991a925fa5d6c5c11815cb4c6e16574a025 as fixed in https://github.com/rust-lang/rust/pull/125392 // const NOT_UNWIND_SAFE: &[&str] = &["future::ResumeTy"]; // const NOT_REF_UNWIND_SAFE: &[&str] = &["future::ResumeTy"]; let workspace_root = &workspace_root(); let out_dir = &workspace_root.join("src/gen"); - fs::create_dir_all(out_dir)?; + fs::create_dir_all(out_dir).unwrap(); - let files: BTreeSet = git_ls_files(&workspace_root.join("src"), &["*.rs"])? + let files: BTreeSet = git_ls_files(&workspace_root.join("src"), &["*.rs"]) .into_iter() .filter_map(|(file_name, path)| { // Assertions are only needed for the library's public APIs. @@ -48,8 +47,8 @@ fn gen_assert_impl() -> Result<()> { let mut visited_types = HashSet::new(); let mut use_generics_helpers = false; for f in &files { - let s = fs::read_to_string(f)?; - let mut ast = syn::parse_file(&s)?; + let s = fs::read_to_string(f).unwrap(); + let mut ast = syn::parse_file(&s).unwrap(); let module = if f.ends_with("lib.rs") { vec![] @@ -293,9 +292,7 @@ fn gen_assert_impl() -> Result<()> { #tokens }; }); - write(function_name!(), out_dir.join("assert_impl.rs"), out)?; - - Ok(()) + write(function_name!(), out_dir.join("assert_impl.rs"), out).unwrap(); } #[must_use]