From 1855d9501e0112022cb369291155c13796911966 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Fri, 23 Dec 2022 14:20:46 -0500 Subject: [PATCH] Fix patterns in match_target --- CHANGELOG.md | 7 ++++- multiversion-macros/Cargo.toml | 2 +- multiversion-macros/src/match_target.rs | 38 ++++++++++++++++++++----- multiversion/Cargo.toml | 4 +-- multiversion/tests/cfg.rs | 1 + 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf139cf..7e140f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.7.1] - 2022-12-23 +### Fixed +- Fixed handling patterns in `match_target`. + ## [0.7.0] - 2022-12-09 ### Changed - The `multiversion` macro has been overhauled. Now uses a single attribute macro, rather than helper attributes. @@ -93,7 +97,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial multiversion implementation -[Unreleased]: https://github.com/calebzulawski/multiversion/compare/0.7.0...HEAD +[Unreleased]: https://github.com/calebzulawski/multiversion/compare/0.7.1...HEAD +[0.7.1]: https://github.com/calebzulawski/multiversion/compare/0.7.0...0.7.1 [0.7.0]: https://github.com/calebzulawski/multiversion/compare/0.6.1...0.7.0 [0.6.1]: https://github.com/calebzulawski/multiversion/compare/0.6.0...0.6.1 [0.6.0]: https://github.com/calebzulawski/multiversion/compare/0.5.1...0.6.0 diff --git a/multiversion-macros/Cargo.toml b/multiversion-macros/Cargo.toml index 49af9d9..5b33823 100644 --- a/multiversion-macros/Cargo.toml +++ b/multiversion-macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "multiversion-macros" -version = "0.7.0" +version = "0.7.1" authors = ["Caleb Zulawski "] license = "MIT OR Apache-2.0" description = "Implementation crate for multiversion" diff --git a/multiversion-macros/src/match_target.rs b/multiversion-macros/src/match_target.rs index 30a2407..e86f4d9 100644 --- a/multiversion-macros/src/match_target.rs +++ b/multiversion-macros/src/match_target.rs @@ -5,7 +5,7 @@ use syn::{ parse::{Parse, ParseStream}, parse_quote, spanned::Spanned, - Error, Expr, LitStr, Result, + Error, Expr, ExprLit, Lit, LitStr, Pat, Result, }; pub struct MatchTarget { @@ -29,13 +29,37 @@ impl Parse for MatchTarget { if let Some(guard) = arm.guard { return Err(Error::new(guard.0.span(), "unexpected guard")); } - if pat == parse_quote!(_) { - default_target = Some(*arm.body); - if !input.is_empty() { - return Err(Error::new(input.span(), "unreachable targets")); + + fn parse_target(e: &Expr) -> Result { + if let Expr::Lit(ExprLit { + lit: Lit::Str(s), .. + }) = e + { + Target::parse(s) + } else { + Err(Error::new(e.span(), "expected a string literal")) + } + } + match pat { + Pat::Lit(lit) => { + arms.push((parse_target(&lit.expr)?, *arm.body)); + } + Pat::Or(or) => { + for case in or.cases.iter() { + if let Pat::Lit(lit) = case { + arms.push((parse_target(&lit.expr)?, *arm.body.clone())); + } else { + return Err(Error::new(case.span(), "expected a string literal")); + } + } + } + Pat::Wild(_) => { + default_target = Some(*arm.body); + if !input.is_empty() { + return Err(Error::new(input.span(), "unreachable targets")); + } } - } else { - arms.push((parse_quote!(#pat), *arm.body)); + _ => return Err(Error::new(pat.span(), "expected string literal")), } } diff --git a/multiversion/Cargo.toml b/multiversion/Cargo.toml index 1b1c1b8..7aa9ab7 100644 --- a/multiversion/Cargo.toml +++ b/multiversion/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "multiversion" -version = "0.7.0" +version = "0.7.1" authors = ["Caleb Zulawski "] license = "MIT OR Apache-2.0" description = "Easy function multiversioning" @@ -23,7 +23,7 @@ default = ["std"] std = ["multiversion-macros/std"] [dependencies] -multiversion-macros = { version = "0.7.0", path = "../multiversion-macros", default-features = false } +multiversion-macros = { version = "0.7.1", path = "../multiversion-macros", default-features = false } target-features = "0.1" [dev-dependencies] diff --git a/multiversion/tests/cfg.rs b/multiversion/tests/cfg.rs index 73e44c5..829b46b 100644 --- a/multiversion/tests/cfg.rs +++ b/multiversion/tests/cfg.rs @@ -68,6 +68,7 @@ fn match_target() { fn foo() { let match_avx = match_target! { "x86_64+avx" => true, + "aarch64+neon" | "x86_64+sse" => false, _ => false, };