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

Add "Attributes on X" for various places attributes are allowed #355

Open
2 of 6 tasks
Havvy opened this issue Jun 10, 2018 · 7 comments
Open
2 of 6 tasks

Add "Attributes on X" for various places attributes are allowed #355

Havvy opened this issue Jun 10, 2018 · 7 comments
Assignees
Labels

Comments

@Havvy
Copy link
Contributor

Havvy commented Jun 10, 2018

Various places allow attributes. We document where on the attributes page already, but each individual page for those places should also have documentation about what they allow.

@Havvy Havvy self-assigned this Jun 10, 2018
@ehuss ehuss added the A-attributes Area: Attributes label Mar 11, 2019
@ehuss
Copy link
Contributor

ehuss commented Mar 16, 2019

@Centril had the idea of using a template on each attribute which would include a list of places where the attribute is valid (and the Syntax):


Syntax
MetaNameValueStr

Allowed on: any item


Once we have the information where every attribute is valid, it might be worth considering adding this.

I have concerns that many of the attributes locations are not validated, and are allowed everywhere even if they are ignored. Or the behavior of some attributes that are applicable in many places (like no_mangle) is not clear. I personally prefer to only document the locations where they are not ignored, but @Centril prefers to also include the ignored locations. Perhaps the best solution is to add deprecation warnings to the ignored locations, and pretend they are not valid. I'm not a big fan of documenting bugs since there are thousands of them, they change rapidly, and are rustc-specific.

Another example is link_section which is allowed everywhere, but (I think) only applies to functions and statics (ignored everywhere else).

@petrochenkov
Copy link
Contributor

I have concerns that many of the attributes locations are not validated

This is something that I personally intend to fix eventually, similarly to rust-lang/rust#57321 that did it for attribute inputs rather than locations.
If the rules as they exist now (random and accidental) are documented, it will be harder to do (at least ideologically).

@Centril
Copy link
Contributor

Centril commented Mar 17, 2019

If the rules as they exist now (random and accidental) are documented, it will be harder to do (at least ideologically).

@petrochenkov Yeah that's pretty fair... Maybe we can document them on an issue on this repo for now with an explicit caveat re. that they may change? You'll need to know what the current behaviors are and what we think they should be to fix them in any case? And when you are done with fixing we can move them into the reference?

@petrochenkov
Copy link
Contributor

I think it would be reasonable to list location in which the attribute should certainly be allowed, and keep silence about other locations.

@ehuss
Copy link
Contributor

ehuss commented Oct 30, 2020

Some more validation was added via rust-lang/rust#73461, which can help guide updating the docs on where each attribute can be placed.

@ehuss
Copy link
Contributor

ehuss commented Jan 13, 2021

More validation is being considered in rust-lang/rust#80920.

I noticed that no_mangle says it can go on any item. However, I think it only applies to functions and statics. It also should not be used with generic type or const parameters (lifetimes are ok). There are warnings for this (UNUSED_ATTRIBUTES, NO_MANGLE_CONST_ITEMS, NO_MANGLE_GENERIC_ITEMS).

@pazmank
Copy link

pazmank commented Nov 6, 2024

I noticed, that if link_section is the only attribute on a static, the section is created, like this:

// llvm-objdump output has `.example_section:`
#[link_section = ".example_section"]
pub static VAR1: u32 = 1;

But for a function, if link_section is the only attribute, the section is not created. For a function, a section specified in the link_section attribute is created only if the function also has an other attribute, like no_mangle, export_name or inline(never).

// llvm-objdump output does not have `.example_section:`
#[link_section = ".example_section"]
pub fn foo() {}

// llvm-objdump output has `.example_section:`
#[no_mangle]
#[link_section = ".example_section"]
pub fn foo() {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants