Closed
Description
I am working on a proc-macro, where I would like to give the user the option, to decide, which attributes (and doc comments) should be forwarded.
For example
use shorthand::ShortHand;
#[derive(ShortHand)]
#[shorthand(enable(forward(doc)))]
/// This should be in the output
#[shorthand(disable(forward(doc)))]
/// This line should not.
#[shorthand(enable(forward(doc)))]
/// This should be in the output
struct Example {
value: String,
}
as you can see in the above case, the order of the attributes is very important.
Sadly the rust compiler (or the proc_macro
crate) does reorder the attributes, before passing them to the function as a TokenStream
.
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use shorthand::ShortHand;
/// This should be in the output
/// This line should not.
/// This should be in the output
#[shorthand(enable(forward(doc)))]
#[shorthand(disable(forward(doc)))]
#[shorthand(enable(forward(doc)))]
struct Example {
value: String,
}
#[allow(dead_code)]
impl Example {
#[inline(always)]
pub fn value(&self) -> &String {
&self.value
}
#[inline(always)]
pub fn set_value(&mut self, value: String) -> &mut Self {
self.value = value;
self
}
}
#[allow(dead_code)]
fn main() {}
#[main]
pub fn main() -> () {
extern crate test;
test::test_main_static(&[])
}
(this is the output of cargo expand
, the proc-macro gets a similar TokenStream
, where the order is not preserved either)
I think this should not happen and the order should be preserved! Are there any good reasons for reordering them?
related to #36211