Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bump MSRV to 1.58.0
  • Loading branch information
Jake-Shadle committed Sep 27, 2022
commit ca150eefe71f1a4cef18260093a820db23e647e6
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ authors = [
"Embark <[email protected]>",
"Jake Shadle <[email protected]>",
]
edition = "2018"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
rust-version = "1.58.0"
documentation = "https://docs.rs/cfg-expr"
homepage = "https://github.com/EmbarkStudios/cfg-expr"
keywords = ["cargo", "rustc", "cfg"]
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
msrv = "1.52"
msrv = "1.58.0"
avoid-breaking-exported-api = false
18 changes: 9 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{error::Error, fmt};

/// An error related to parsing of a cfg expression
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct ParseError {
/// The string that was parsed
pub original: String,
Expand All @@ -13,7 +13,7 @@ pub struct ParseError {
}

/// The particular reason for a `ParseError`
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Reason {
/// not() takes exactly 1 predicate, unlike all() and any()
InvalidNot(usize),
Expand Down Expand Up @@ -54,18 +54,18 @@ impl fmt::Display for ParseError {
// Mismatched parens/quotes have a slightly different output
// than the other errors
match &self.reason {
r @ Reason::UnclosedParens | r @ Reason::UnclosedQuotes => {
f.write_fmt(format_args!("- {}", r))
r @ (Reason::UnclosedParens | Reason::UnclosedQuotes) => {
f.write_fmt(format_args!("- {r}"))
}
r @ Reason::UnopenedParens | r @ Reason::UnopenedQuotes => {
f.write_fmt(format_args!("^ {}", r))
r @ (Reason::UnopenedParens | Reason::UnopenedQuotes) => {
f.write_fmt(format_args!("^ {r}"))
}
other => {
for _ in self.span.start..self.span.end {
f.write_str("^")?;
}

f.write_fmt(format_args!(" {}", other))
f.write_fmt(format_args!(" {other}"))
}
}
}
Expand All @@ -91,7 +91,7 @@ impl fmt::Display for Reason {
f.write_str("expected one of ")?;

for (i, exp) in expected.iter().enumerate() {
f.write_fmt(format_args!("{}`{}`", if i > 0 { ", " } else { "" }, exp))?;
f.write_fmt(format_args!("{}`{exp}`", if i > 0 { ", " } else { "" }))?;
}
f.write_str(" here")
} else if !expected.is_empty() {
Expand All @@ -100,7 +100,7 @@ impl fmt::Display for Reason {
f.write_str("the term was not expected here")
}
}
InvalidNot(np) => f.write_fmt(format_args!("not() takes 1 predicate, found {}", np)),
InvalidNot(np) => f.write_fmt(format_args!("not() takes 1 predicate, found {np}")),
InvalidInteger => f.write_str("invalid integer"),
MultipleRootPredicates => f.write_str("multiple root predicates"),
InvalidHasAtomic => f.write_str("expected integer or \"ptr\""),
Expand Down
14 changes: 8 additions & 6 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum Func {
use crate::targets as targ;

/// All predicates that pertains to a target, except for `target_feature`
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum TargetPredicate {
/// [target_arch](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch)
Arch(targ::Arch),
Expand Down Expand Up @@ -136,13 +136,15 @@ impl TargetMatcher for target_lexicon::Triple {
OperatingSystem::Redox => env == &targ::Env::relibc,
OperatingSystem::VxWorks => env == &targ::Env::gnu,
OperatingSystem::Freebsd => match self.architecture {
Architecture::Arm(ArmArchitecture::Armv6)
| Architecture::Arm(ArmArchitecture::Armv7) => env == &targ::Env::gnueabihf,
Architecture::Arm(ArmArchitecture::Armv6 | ArmArchitecture::Armv7) => {
env == &targ::Env::gnueabihf
}
_ => env.0.is_empty(),
},
OperatingSystem::Netbsd => match self.architecture {
Architecture::Arm(ArmArchitecture::Armv6)
| Architecture::Arm(ArmArchitecture::Armv7) => env == &targ::Env::eabihf,
Architecture::Arm(ArmArchitecture::Armv6 | ArmArchitecture::Armv7) => {
env == &targ::Env::eabihf
}
_ => env.0.is_empty(),
},
OperatingSystem::None_
Expand Down Expand Up @@ -372,7 +374,7 @@ pub(crate) struct InnerTarget {
}

/// A single predicate in a `cfg()` expression
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Predicate<'a> {
/// A target predicate, with the `target_` prefix
Target(TargetPredicate),
Expand Down
156 changes: 80 additions & 76 deletions src/expr/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Expression {
($span:expr) => {{
let expected: &[&str] = match last_token {
None => &["<key>", "all", "any", "not"],
Some(Token::All) | Some(Token::Any) | Some(Token::Not) => &["("],
Some(Token::All | Token::Any | Token::Not) => &["("],
Some(Token::CloseParen) => &[")", ","],
Some(Token::Comma) => &[")", "<key>"],
Some(Token::Equals) => &["\""],
Expand All @@ -242,29 +242,32 @@ impl Expression {
'outer: for lt in lexer {
let lt = lt?;
match &lt.token {
Token::Key(k) => match last_token {
None | Some(Token::OpenParen) | Some(Token::Comma) => {
Token::Key(k) => {
if matches!(last_token, None | Some(Token::OpenParen | Token::Comma)) {
pred_key = Some((k, lt.span.clone()));
} else {
token_err!(lt.span)
}
_ => token_err!(lt.span),
},
Token::Value(v) => match last_token {
Some(Token::Equals) => {
}
Token::Value(v) => {
if matches!(last_token, Some(Token::Equals)) {
// We only record the span for keys and values
// so that the expression doesn't need a lifetime
// but in the value case we need to strip off
// the quotes so that the proper raw string is
// provided to callers when evaluating the expression
pred_val = Some((v, lt.span.start + 1..lt.span.end - 1));
} else {
token_err!(lt.span)
}
_ => token_err!(lt.span),
},
Token::Equals => match last_token {
Some(Token::Key(_)) => {}
_ => token_err!(lt.span),
},
Token::All | Token::Any | Token::Not => match last_token {
None | Some(Token::OpenParen) | Some(Token::Comma) => {
}
Token::Equals => {
if !matches!(last_token, Some(Token::Key(_))) {
token_err!(lt.span)
}
}
Token::All | Token::Any | Token::Not => {
if matches!(last_token, None | Some(Token::OpenParen | Token::Comma)) {
let new_fn = match lt.token {
// the 0 is a dummy value -- it will be substituted for the real
// number of predicates in the `CloseParen` branch below.
Expand All @@ -285,23 +288,26 @@ impl Expression {
predicates: SmallVec::new(),
nest_level: 0,
});
} else {
token_err!(lt.span)
}
_ => token_err!(lt.span),
},
Token::OpenParen => match last_token {
Some(Token::All) | Some(Token::Any) | Some(Token::Not) => {
}
Token::OpenParen => {
if matches!(last_token, Some(Token::All | Token::Any | Token::Not)) {
if let Some(ref mut fs) = func_stack.last_mut() {
fs.parens_index = lt.span.start;
}
}
_ => token_err!(lt.span),
},
Token::CloseParen => match last_token {
None | Some(Token::All) | Some(Token::Any) | Some(Token::Not)
| Some(Token::Equals) => {
} else {
token_err!(lt.span)
}
_ => {
}
Token::CloseParen => {
if matches!(
last_token,
None | Some(Token::All | Token::Any | Token::Not | Token::Equals)
) {
token_err!(lt.span)
} else {
if let Some(top) = func_stack.pop() {
let key = pred_key.take();
let val = pred_val.take();
Expand Down Expand Up @@ -352,15 +358,16 @@ impl Expression {
reason: Reason::UnopenedParens,
});
}
},
Token::Comma => match last_token {
None
| Some(Token::OpenParen)
| Some(Token::All)
| Some(Token::Any)
| Some(Token::Not)
| Some(Token::Equals) => token_err!(lt.span),
_ => {
}
Token::Comma => {
if matches!(
last_token,
None | Some(
Token::OpenParen | Token::All | Token::Any | Token::Not | Token::Equals
)
) {
token_err!(lt.span)
} else {
let key = pred_key.take();
let val = pred_val.take();

Expand All @@ -378,7 +385,7 @@ impl Expression {
_ => {}
}
}
},
}
}

last_token = Some(lt.token);
Expand All @@ -393,49 +400,46 @@ impl Expression {
}

// If we still have functions on the stack, it means we have an unclosed parens
match func_stack.pop() {
Some(top) => {
if top.parens_index != 0 {
Err(ParseError {
original: original.to_owned(),
span: top.parens_index..original.len(),
reason: Reason::UnclosedParens,
})
} else {
Err(ParseError {
original: original.to_owned(),
span: top.span,
reason: Reason::Unexpected(&["("]),
})
}
if let Some(top) = func_stack.pop() {
if top.parens_index != 0 {
Err(ParseError {
original: original.to_owned(),
span: top.parens_index..original.len(),
reason: Reason::UnclosedParens,
})
} else {
Err(ParseError {
original: original.to_owned(),
span: top.span,
reason: Reason::Unexpected(&["("]),
})
}
None => {
let key = pred_key.take();
let val = pred_val.take();
} else {
let key = pred_key.take();
let val = pred_val.take();

if let Some(key) = key {
root_predicate_count += 1;
expr_queue.push(ExprNode::Predicate(parse_predicate(key, val)?));
}
if let Some(key) = key {
root_predicate_count += 1;
expr_queue.push(ExprNode::Predicate(parse_predicate(key, val)?));
}

if expr_queue.is_empty() {
Err(ParseError {
original: original.to_owned(),
span: 0..original.len(),
reason: Reason::Empty,
})
} else if root_predicate_count > 1 {
Err(ParseError {
original: original.to_owned(),
span: 0..original.len(),
reason: Reason::MultipleRootPredicates,
})
} else {
Ok(Expression {
original: original.to_owned(),
expr: expr_queue,
})
}
if expr_queue.is_empty() {
Err(ParseError {
original: original.to_owned(),
span: 0..original.len(),
reason: Reason::Empty,
})
} else if root_predicate_count > 1 {
Err(ParseError {
original: original.to_owned(),
span: 0..original.len(),
reason: Reason::MultipleRootPredicates,
})
} else {
Ok(Expression {
original: original.to_owned(),
expr: expr_queue,
})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub fn get_builtin_target_by_triple(triple: &str) -> Option<&'static TargetInfo>
/// versions.
///
/// ```
/// assert_eq!("1.63.0", cfg_expr::targets::rustc_version());
/// assert_eq!("1.64.0", cfg_expr::targets::rustc_version());
/// ```
pub fn rustc_version() -> &'static str {
builtins::RUSTC_VERSION
Expand Down
6 changes: 1 addition & 5 deletions tests/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ fn very_specific() {
} else {
"".to_owned()
},
target
.env
.as_ref()
.map(|e| e.as_str())
.unwrap_or_else(|| ""),
target.env.as_ref().map_or("", |e| e.as_str()),
);

let specific = Expression::parse(&expr).unwrap();
Expand Down