Skip to content

RFC: conditional attributes #12479

Closed
Closed
@huonw

Description

@huonw

Currently the only way to have attributes that apply conditionally is to duplicate the whole item (e.g. #[link()] extern metadata that change per platform, or wish to have dynamic/static linking toggled externally). It'd be nice to have be able to avoid this.

Proposal: add cfg_attr(predicate, attribute) that only applies attribute if predicate is satisfied. e.g.

#[cfg_attr(windows, link(name="mylib_win"))]
#[cfg_attr(not(windows), link(name="mylib"))]
extern {
     fn mylib_some_func();
     // ...
}

currently has to be

#[cfg(windows)]
#[link(name="mylib_win")]
extern {
     fn mylib_some_func();
     // ...
}

#[cfg(not(windows))]
#[link(name="mylib")]
extern {
     fn mylib_some_func();
     // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions