Skip to content

Attributes are reordered before they are passed to a proc-macro #67839

Closed
@Luro02

Description

@Luro02

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

Metadata

Metadata

Assignees

Labels

A-attributesArea: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions