2016-08-04 14:20:01 -05:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
// no-prefer-dynamic
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
#![crate_type = "proc-macro"]
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 09:50:39 -05:00
|
|
|
#![feature(proc_macro, proc_macro_non_items)]
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
extern crate proc_macro;
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
use proc_macro::*;
|
2016-08-04 14:20:01 -05:00
|
|
|
|
2017-03-17 18:41:09 -05:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn cond(input: TokenStream) -> TokenStream {
|
2017-01-17 21:27:09 -06:00
|
|
|
let mut conds = Vec::new();
|
2017-03-17 18:41:09 -05:00
|
|
|
let mut input = input.into_iter().peekable();
|
2017-01-17 21:27:09 -06:00
|
|
|
while let Some(tree) = input.next() {
|
2018-04-02 10:19:32 -05:00
|
|
|
let cond = match tree {
|
|
|
|
TokenTree::Group(tt) => tt.stream(),
|
2017-01-17 21:27:09 -06:00
|
|
|
_ => panic!("Invalid input"),
|
|
|
|
};
|
2017-03-17 18:41:09 -05:00
|
|
|
let mut cond_trees = cond.clone().into_iter();
|
|
|
|
let test = cond_trees.next().expect("Unexpected empty condition in `cond!`");
|
|
|
|
let rhs = cond_trees.collect::<TokenStream>();
|
2017-01-17 21:27:09 -06:00
|
|
|
if rhs.is_empty() {
|
|
|
|
panic!("Invalid macro usage in cond: {}", cond);
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
let is_else = match test {
|
2018-04-09 16:49:25 -05:00
|
|
|
TokenTree::Term(word) => &*word.to_string() == "else",
|
2017-01-17 21:27:09 -06:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
conds.push(if is_else || input.peek().is_none() {
|
2017-03-14 17:04:46 -05:00
|
|
|
quote!({ $rhs })
|
2017-01-17 21:27:09 -06:00
|
|
|
} else {
|
2017-03-14 17:04:46 -05:00
|
|
|
quote!(if $test { $rhs } else)
|
2017-01-17 21:27:09 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
conds.into_iter().flat_map(|x| x.into_iter()).collect()
|
2016-08-04 14:20:01 -05:00
|
|
|
}
|