-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
#[used]
attribute on static items in extern
block has suboptimal error message
#126789
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Comments
aleksanderkrauze
added
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
labels
Jun 21, 2024
Yes. I think it makes sense. |
surechen
added a commit
to surechen/rust
that referenced
this issue
Jun 29, 2024
… `static` variable. fixes rust-lang#126789
surechen
added a commit
to surechen/rust
that referenced
this issue
Jun 29, 2024
… `static` variable. fixes rust-lang#126789
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this issue
Jun 29, 2024
Show `used attribute`'s kind for user when find it isn't applied to a `static` variable. For example : ```rust extern "C" { #[used] //~ ERROR attribute must be applied to a `static` variable static FOO: i32; // show the kind of this item to help user understand why the error is reported. } ``` fixes rust-lang#126789
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this issue
Jun 29, 2024
Show `used attribute`'s kind for user when find it isn't applied to a `static` variable. For example : ```rust extern "C" { #[used] //~ ERROR attribute must be applied to a `static` variable static FOO: i32; // show the kind of this item to help user understand why the error is reported. } ``` fixes rust-lang#126789
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this issue
Jun 30, 2024
Rollup merge of rust-lang#127118 - surechen:fix_126789, r=jieyouxu Show `used attribute`'s kind for user when find it isn't applied to a `static` variable. For example : ```rust extern "C" { #[used] //~ ERROR attribute must be applied to a `static` variable static FOO: i32; // show the kind of this item to help user understand why the error is reported. } ``` fixes rust-lang#126789
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Current output
Desired output
There should be at least a hint, or other kind of diagnostic message, explaining why
#[used]
attribute is invalid in this context.Rationale and extra context
#[used]
attribute cannot be applied to static items defined insideextern
blocks. However current compiler error, contrary to great Rust standards, does not explain why it is forbidden, or how to fix it. Moreover it can be confusing to newbie users. To the information "attribute must be applied to astatic
variable", one could say "But it is static. Look, it says it right there".I would expect a hint, or another diagnostic that would explain that the
#[used]
attribute is used on static items that are defined inside crate, andextern
blocks are used to declare an item, that exists elsewhere. Using this attribute insideextern
block means that user either does not understand how#[used]
attribute works, or howextern
blocks work and a hint pointing it out to them could be helpful.This would also be more inline with other errors related to incorrectly using static items inside
extern
blocks. For example assigning value to such static will result in following error message:Here compiler provides also notes, explaining why this code does not work and directs user to documentation that could be useful in this context.
Other cases
A similar case could be made for the
#[no_mangle]
attribute. Using it on static item inextern
block produces following warning (which will become error in the future):The Rust Reference says that
#[no_mangle]
implies#[used]
:Therefore using this attribute can be caused by the same fallacy - user not understanding that
extern
blocks refer to, well, external items, and not to those that are exported from their crate. Adding similar hint to this warning (and future error) could also help users making this error.PS. Actually the same argument could be made for other ABI attributes, like
#[link_section]
and#[export_name]
.Rust Version
Anything else?
No response
The text was updated successfully, but these errors were encountered: