attr: Allow #[used] attribute and its argument variant. - #4837
Open
CohenArthur wants to merge 2 commits into
Open
CohenArthur wants to merge 2 commits into
CohenArthur wants to merge 2 commits into
Conversation
CohenArthur
requested review from
P-E-P and
powerboat9
and removed request for
powerboat9
September 3, 2026 03:52
powerboat9
reviewed
Sep 3, 2026
Comment on lines
+524
to
+526
| // FIXME: We need to enable this when the compatibility mode is >= 1.60 I guess? | ||
| FEATURE_ACTIVE ("used_with_arg", USED_WITH_ARG, "1.60.0", ISSUE_SOME (93798), | ||
| EDITION_NONE) |
Collaborator
There was a problem hiding this comment.
This should go in rust-feature-defs-rfl.h for now
Collaborator
It might be, could you add a comment around where that's happening which mentions the |
CohenArthur
force-pushed
the
recognize-used-attr
branch
from
September 4, 2026 13:46
772c286 to
8d7e5e5
Compare
CohenArthur
force-pushed
the
recognize-used-attr
branch
2 times, most recently
from
September 16, 2026 02:58
c985dc1 to
36c5afe
Compare
Member
Author
|
I ended up implementing all three forms of the attribute including the linker one, so we don't need to do anything particular. this last commit may break the CI on older platforms though |
CohenArthur
force-pushed
the
recognize-used-attr
branch
2 times, most recently
from
September 16, 2026 15:29
31b188a to
01c3963
Compare
This commit allows recognizing both forms of the `#[used]` attribute: The basic one, which forbids any argument, and the one gated behind `feature(used_with_arg)`, which allows specifying whether the compiler or linker should be responsible for marking the static item as used. This does not do anything in the backend for actually marking the item as used even if the compiler decides it should not be used. This is a first step towards handling it as the kernel requires it. gcc/rust/ChangeLog: * checks/errors/feature/rust-feature-defs-rfl.h (FEATURE_ACTIVE): Add `used_with_arg` feature. * checks/errors/feature/rust-feature-gate.cc (FeatureGate::check_used_attribute): New function. (FeatureGate::visit): Implement feature gating visits for static items. * checks/errors/feature/rust-feature-gate.h: Declare them. * checks/errors/rust-builtin-attribute-checker.cc (used): New handler. (check_valid_attribute_for_item): Handle `used` attribute for static items. * util/rust-attribute-values.h: Add new attribute value for `used`. * util/rust-attributes.cc: Likewise. * backend/rust-compile-item.cc (CompileItem::visit): Add note about implementing the `#[used]` attribute properly in our backend. gcc/testsuite/ChangeLog: * rust/compile/used-attr1.rs: New test. * rust/compile/used-attr2.rs: New test. * rust/compile/used-attr3.rs: New test.
We now support the full implementation of the #[used] attribute, which can be used to conserve a static variable even if the compiler deems it unused. This commit also implements #[used(linker)], which instructs the linker to not garbage collect the static even if it seems unused. This behavior is equivalent to the `used` and `retain` attributes in C. gcc/rust/ChangeLog: * backend/rust-compile-item.cc (handle_used_attr): New function. (CompileItem::visit): Call it. gcc/testsuite/ChangeLog: * rust/compile/used-attr4.rs: New test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4836, but I think it's missing some stuff in the backend to properly handle the attribute. We always mark
StaticItems withTREE_USED, not sure if that is enough?This commit allows recognizing both forms of the
#[used]attribute: The basic one, which forbids any argument, and the one gated behindfeature(used_with_arg), which allows specifying whether the compiler or linker should be responsible for marking the static item as used.This does not do anything in the backend for actually marking the item as used even if the compiler decides it should not be used. This is a first step towards handling it as the kernel requires it.
gcc/rust/ChangeLog:
gcc/testsuite/ChangeLog:
backend: Add proper handling for #[used] attribute.
We now support the full implementation of the #[used] attribute, which
can be used to conserve a static variable even if the compiler deems it
unused. This commit also implements #[used(linker)], which instructs the
linker to not garbage collect the static even if it seems unused.
This behavior is equivalent to the
usedandretainattributes in C.gcc/rust/ChangeLog:
gcc/testsuite/ChangeLog: