Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
[target-spec] fix handling of unknown predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshowers committed Aug 31, 2022
1 parent d1dc1b5 commit c489ddd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions target-spec/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

# Changelog

## [1.1.0] - 2022-08-30

### Fixed

- Unknown predicates now evaluate to false, matching Cargo's behavior.
- As a result, `Error::UnknownPredicate` is no longer in use and has been deprecated.

## [1.0.2] - 2022-05-29

### Changed
Expand Down Expand Up @@ -161,6 +168,7 @@ This was mistakenly published and was yanked.
## [0.1.0] - 2020-03-20
- Initial release.

[1.1.0]: https://github.com/facebookincubator/cargo-guppy/releases/tag/target-spec-1.1.0
[1.0.2]: https://github.com/facebookincubator/cargo-guppy/releases/tag/target-spec-1.0.2
[1.0.1]: https://github.com/facebookincubator/cargo-guppy/releases/tag/target-spec-1.0.1
[1.0.0]: https://github.com/facebookincubator/cargo-guppy/releases/tag/target-spec-1.0.0
Expand Down
2 changes: 1 addition & 1 deletion target-spec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "target-spec"
version = "1.0.2"
version = "1.1.0"
description = "Evaluate Cargo.toml target specifications"
documentation = "https://docs.rs/target-spec"
repository = "https://github.com/facebookincubator/cargo-guppy"
Expand Down
5 changes: 5 additions & 0 deletions target-spec/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub enum Error {
/// The provided platform triple was unknown.
UnknownPlatformTriple(TripleParseError),
/// The provided `cfg()` expression parsed correctly, but it had an unknown predicate.
///
/// This is no longer used, but is kept for backwards compatibility.
#[deprecated(since = "1.1.0", note = "this variant is no longer in use")]
UnknownPredicate(String),
}

Expand All @@ -27,6 +30,7 @@ impl fmt::Display for Error {
Error::UnknownPlatformTriple(_) => {
write!(f, "unknown platform triple")
}
#[allow(deprecated)]
Error::UnknownPredicate(pred) => {
write!(f, "cfg() expression has unknown predicate: {}", pred)
}
Expand All @@ -40,6 +44,7 @@ impl error::Error for Error {
Error::InvalidExpression(err) => Some(err),
Error::UnknownTargetTriple(err) => Some(err),
Error::UnknownPlatformTriple(err) => Some(err),
#[allow(deprecated)]
Error::UnknownPredicate(_) => None,
}
}
Expand Down
48 changes: 29 additions & 19 deletions target-spec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ impl TargetExpression {
pub fn new(input: &str) -> Result<Self, Error> {
let expr = Expression::parse(input)
.map_err(|err| Error::InvalidExpression(ExpressionParseError::new(err)))?;
Self::verify_expr(expr)
Ok(Self {
inner: Arc::new(expr),
})
}

/// Returns the string that was parsed into `self`.
Expand Down Expand Up @@ -127,25 +129,12 @@ impl TargetExpression {
Some(platform.has_flag(flag))
}
Predicate::KeyValue { .. } => {
unreachable!("these predicates are disallowed in TargetExpression::verify_expr")
// This is always interpreted by Cargo as false.
Some(false)
}
}
})
}

/// Verify this `cfg()` expression.
fn verify_expr(expr: Expression) -> Result<Self, Error> {
// Error out on unknown key-value pairs. Everything else is recognized (though
// DebugAssertions/ProcMacro etc always returns false, and flags return false by default).
for pred in expr.predicates() {
if let Predicate::KeyValue { key, .. } = pred {
return Err(Error::UnknownPredicate(key.to_string()));
}
}
Ok(Self {
inner: Arc::new(expr),
})
}
}

impl FromStr for TargetExpression {
Expand Down Expand Up @@ -237,9 +226,30 @@ mod tests {

#[test]
fn test_unknown_predicate() {
let err =
TargetSpec::new("cfg(bogus_key = \"bogus_value\")").expect_err("unknown predicate");
assert_eq!(err, Error::UnknownPredicate("bogus_key".to_string()));
let expr = match TargetSpec::new("cfg(bogus_key = \"bogus_value\")")
.expect("unknown predicate should parse")
{
TargetSpec::Triple(triple) => {
panic!("expected spec, got triple: {:?}", triple)
}
TargetSpec::Expression(expr) => expr,
};
assert_eq!(
expr.inner.predicates().collect::<Vec<_>>(),
vec![Predicate::KeyValue {
key: "bogus_key",
val: "bogus_value"
}],
);

let platform = Platform::current().unwrap();
// This should always evaluate to false.
assert_eq!(expr.eval(&platform), Some(false));

let expr = TargetSpec::new("cfg(not(bogus_key = \"bogus_value\"))")
.expect("unknown predicate should parse");
// This is a cfg(not()), so it should always evaluate to true.
assert_eq!(expr.eval(&platform), Some(true));
}

#[test]
Expand Down

0 comments on commit c489ddd

Please sign in to comment.