Skip to content

Commit

Permalink
Fix patterns in match_target
Browse files Browse the repository at this point in the history
  • Loading branch information
calebzulawski committed Dec 23, 2022
1 parent a68b5df commit 1855d95
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion multiversion-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multiversion-macros"
version = "0.7.0"
version = "0.7.1"
authors = ["Caleb Zulawski <caleb.zulawski@gmail.com>"]
license = "MIT OR Apache-2.0"
description = "Implementation crate for multiversion"
Expand Down
38 changes: 31 additions & 7 deletions multiversion-macros/src/match_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Target> {
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")),
}
}

Expand Down
4 changes: 2 additions & 2 deletions multiversion/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multiversion"
version = "0.7.0"
version = "0.7.1"
authors = ["Caleb Zulawski <caleb.zulawski@gmail.com>"]
license = "MIT OR Apache-2.0"
description = "Easy function multiversioning"
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions multiversion/tests/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down

0 comments on commit 1855d95

Please sign in to comment.