Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Minver guard #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ name = "rustc"
proc-macro = true

[dependencies]
lazy_static = "1"
proc-macro2 = "0.4"
quote = "0.6.8"
syn = { version = "0.15.25", features = ["full"] }
12 changes: 12 additions & 0 deletions src/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::cmp::Ordering;
use syn::parse::{Error, Parse, ParseStream, Result};
use syn::{LitFloat, LitInt, Token};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord)]
pub enum Bound {
Nightly(Date),
Stable(Release),
Expand Down Expand Up @@ -82,3 +83,14 @@ impl PartialOrd<Bound> for Version {
}
}
}

impl PartialOrd<Bound> for Bound {
fn partial_cmp(&self, rhs: &Bound) -> Option<Ordering> {
Some(match (self, rhs) {
(Bound::Nightly(ref date1), Bound::Nightly(ref date2)) => date1.cmp(date2),
(Bound::Nightly(_), Bound::Stable(_)) => Ordering::Greater,
(Bound::Stable(_), Bound::Nightly(_)) => Ordering::Less,
(Bound::Stable(ref release1), Bound::Stable(ref release2)) => release1.cmp(release2),
})
}
}
157 changes: 141 additions & 16 deletions src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::bound::{Bound, Release};
use crate::date::Date;
use crate::time;
use crate::version::{Channel, Version};
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{parenthesized, token, Token};
use std::sync::RwLock;

pub enum Expr {
Stable,
Expand All @@ -16,34 +18,143 @@ pub enum Expr {
Not(Box<Expr>),
Any(Vec<Expr>),
All(Vec<Expr>),
MinVer(Bound),
}

pub enum ExprError {
}

impl ExprError {
fn nightly_date(mindate: Date, date: Date) -> Self {
unimplemented!()
}

fn nightly_channel(channel: Channel) -> Self {
unimplemented!()
}

fn nightly_release(release: Release) -> Self {
unimplemented!()
}

fn release(minrel: Release, release: Release) -> Self {
unimplemented!()
}

fn bad_bound(minver: Bound, bound: Bound) -> Self {
unimplemented!()
}

fn duplicate_minver(minver: Bound, new_minver: Bound) -> Self {
unimplemented!()
}
}

pub type ExprResult<T> = std::result::Result<T, ExprError>;

lazy_static! {
static ref MINVER: RwLock<Option<Bound>> = RwLock::new(None);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you are planning to rely on invocation order and sharing data through a static in the proc macro's process. Be aware that the compiler makes no guarantee about what order macro invocations run in or whether different macro calls all run in the same process, so I don't plan to accept an implementation like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that might be a problem, but documentation seems to be thin. Is there a place to store stateful information like this? I assume so since I'm not sure how/where clippy would store warning state information, but maybe that's a plugin thing and not available to proc macros?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know of an established place to store information across proc macro calls without putting it in the generated code. Clippy is not implemented as a proc macro.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if it does go into the generated code, how would a proc macro access it anyways? Maybe this isn't possible with a proc macro today?

}

fn check_minver_channel(minver: Option<Bound>, channel: Channel) -> ExprResult<()> {
match minver {
Some(Bound::Nightly(mindate)) => {
match channel {
Channel::Nightly(checkdate) => if checkdate < mindate {
Err(ExprError::nightly_date(mindate, checkdate))
} else {
Ok(())
},
Channel::Stable | Channel::Beta => Err(ExprError::nightly_channel(channel)),
_ => Ok(()),
}
},
_ => Ok(()),
}
}

fn check_minver_bound(minver: Option<Bound>, bound: &Bound) -> ExprResult<()> {
match minver {
Some(minver) => if bound < &minver {
Err(ExprError::bad_bound(minver, *bound))
} else {
Ok(())
},
None => Ok(()),
}
}

fn check_minver_release(minver: Option<Bound>, release: &Release) -> ExprResult<()> {
match minver {
Some(Bound::Nightly(_)) => Err(ExprError::nightly_release(*release)),
Some(Bound::Stable(minrel)) => if release < &minrel {
Err(ExprError::release(minrel, *release))
} else {
Ok(())
},
None => Ok(()),
}
}

impl Expr {
pub fn eval(&self, rustc: Version) -> bool {
pub fn eval(&self, rustc: Version) -> ExprResult<bool> {
use self::Expr::*;

match self {
Stable => rustc.channel == Channel::Stable,
Beta => rustc.channel == Channel::Beta,
Nightly => match rustc.channel {
Channel::Nightly(_) | Channel::Dev => true,
Channel::Stable | Channel::Beta => false,
let minver = {
let bound = MINVER.read().expect("minver lock poisoned");
*bound
};

Ok(match self {
Stable => {
check_minver_channel(minver, Channel::Stable)?;
rustc.channel == Channel::Stable
},
Beta => {
check_minver_channel(minver, Channel::Beta)?;
rustc.channel == Channel::Beta
},
Nightly => {
check_minver_channel(minver, Channel::Nightly(time::today()))?;
match rustc.channel {
Channel::Nightly(_) | Channel::Dev => true,
Channel::Stable | Channel::Beta => false,
}
},
Date(date) => {
check_minver_channel(minver, Channel::Nightly(*date))?;
match rustc.channel {
Channel::Nightly(rustc) => rustc == *date,
Channel::Stable | Channel::Beta | Channel::Dev => false,
}
},
Date(date) => match rustc.channel {
Channel::Nightly(rustc) => rustc == *date,
Channel::Stable | Channel::Beta | Channel::Dev => false,
Since(bound) => {
check_minver_bound(minver, bound)?;
rustc >= *bound
},
Before(bound) => {
check_minver_bound(minver, bound)?;
rustc < *bound
},
Since(bound) => rustc >= *bound,
Before(bound) => rustc < *bound,
Release(release) => {
check_minver_release(minver, release)?;
rustc.channel == Channel::Stable
&& rustc.minor == release.minor
&& release.patch.map_or(true, |patch| rustc.patch == patch)
}
Not(expr) => !expr.eval(rustc),
Any(exprs) => exprs.iter().any(|e| e.eval(rustc)),
All(exprs) => exprs.iter().all(|e| e.eval(rustc)),
}
Not(expr) => !expr.eval(rustc)?,
Any(exprs) => exprs.iter().map(|e| e.eval(rustc)).collect::<ExprResult<Vec<_>>>()?.into_iter().any(|b| b),
All(exprs) => exprs.iter().map(|e| e.eval(rustc)).collect::<ExprResult<Vec<_>>>()?.into_iter().all(|b| b),
MinVer(bound) => {
if let Some(minver) = minver {
Err(ExprError::duplicate_minver(minver, *bound))?
} else {
let mut new_minver = MINVER.write().expect("minver lock poisoned");
*new_minver = Some(*bound);
true
}
},
})
}
}

Expand All @@ -58,6 +169,7 @@ mod keyword {
syn::custom_keyword!(not);
syn::custom_keyword!(any);
syn::custom_keyword!(all);
syn::custom_keyword!(minver);
}

impl Parse for Expr {
Expand All @@ -79,6 +191,8 @@ impl Parse for Expr {
Self::parse_any(input)
} else if lookahead.peek(keyword::all) {
Self::parse_all(input)
} else if lookahead.peek(keyword::minver) {
Self::parse_minver(input)
} else {
Err(lookahead.error())
}
Expand Down Expand Up @@ -174,4 +288,15 @@ impl Expr {

Ok(Expr::All(exprs.into_iter().collect()))
}

fn parse_minver(input: ParseStream) -> Result<Self> {
input.parse::<keyword::minver>()?;

let paren;
parenthesized!(paren in input);
let bound: Bound = paren.parse()?;
paren.parse::<Option<Token![,]>>()?;

Ok(Expr::MinVer(bound))
}
}
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
//!
//! <br>

#[macro_use] extern crate lazy_static;
extern crate proc_macro;

mod attr;
Expand Down Expand Up @@ -194,6 +195,11 @@ pub fn all(args: TokenStream, input: TokenStream) -> TokenStream {
cfg("all", args, input)
}

#[proc_macro_attribute]
pub fn minver(args: TokenStream, input: TokenStream) -> TokenStream {
cfg("minver", args, input)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this would be only usable as a global #![attr] at the crate level. Is it possible to restrict that?

}

fn cfg(top: &str, args: TokenStream, input: TokenStream) -> TokenStream {
match try_cfg(top, args, input) {
Ok(tokens) => tokens,
Expand All @@ -213,10 +219,10 @@ fn try_cfg(top: &str, args: TokenStream, input: TokenStream) -> Result<TokenStre
let expr: Expr = syn::parse2(full_args)?;
let version = rustc::version()?;

if expr.eval(version) {
Ok(input)
} else {
Ok(TokenStream::new())
match expr.eval(version) {
Ok(true) => Ok(input),
Ok(false) => Ok(TokenStream::new()),
Err(err) => unimplemented!(),
}
}

Expand All @@ -233,9 +239,11 @@ pub fn attr(args: TokenStream, input: TokenStream) -> TokenStream {
fn try_attr(args: attr::Args, input: TokenStream) -> Result<TokenStream> {
let version = rustc::version()?;

if !args.condition.eval(version) {
return Ok(input);
}
match args.condition.eval(version) {
Ok(false) => return Ok(input),
Err(err) => unimplemented!(),
_ => (),
};

match args.then {
Then::Const(const_token) => {
Expand Down