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

Emit unused_attributes for #[inline] on exported functions #138842

Merged
merged 1 commit into from
Mar 31, 2025

Conversation

Noratrieb
Copy link
Member

I saw someone post a code sample that contained these two attributes, which immediately made me suspicious.
My suspicions were confirmed when I did a small test and checked the compiler source code to confirm that in these cases, #[inline] is indeed ignored (because you can't exactly LocalCopyan unmangled symbol since that would lead to duplicate symbols, and doing a mix of an unmangled GloballyShared and mangled LocalCopy instantiation is too complicated for our current instatiation mode logic, which I don't want to change right now).

So instead, emit the usual unused attribute lint with a message saying that the attribute is ignored in this position.

I think this is not 100% true, since I expect LLVM inlinehint to still be applied to such a function, but that's not why people use this attribute, they use it for the LocalCopy instantiation mode, where it doesn't work.

r? saethlin as the instantiation guy

Procedurally, I think this should be fine to merge without any lang involvement, as this only does a very minor extension to an existing lint.

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Mar 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 22, 2025

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 22, 2025
@rust-log-analyzer

This comment has been minimized.

@Noratrieb
Copy link
Member Author

Noratrieb commented Mar 22, 2025

heh, good point test. we don't want to do this for #[inline(never)]. I'll fix it later (an initial review of the idea can be made before that)

@saethlin
Copy link
Member

I agree that warning on #[inline] and #[inline(always)] makes sense.

I'm somewhat idly curious to see how much this warning fires in crater. If the motivating example (and/or all we see in the ecosystem) was a mistake by a beginner or just someone not paying attention when writing a contrived example that seems fine, but if this was code that's been checked in for a while that's more concerning.

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Mar 23, 2025

As little miss attributes I thought I'd also have a look :3. This looks very reasonable to me :). I happen to have been looking at contains_extern_indicator, and I believe figured out the set is not always entirely complete but I'm working on that separately. It looks like this will catch all cases you're looking to catch. Neat!

After fixing never, r=me as well. I do agree with @saethlin that crater could be interesting but it doesn't look like it'd block merging this since its just a warning.

note: that last statement I'm not 100% sure about, I'm not sure what the precedent is for running crater on lint changes like this

@jieyouxu
Copy link
Member

jieyouxu commented Mar 23, 2025

Is this related to #65833?

EDIT: not quite, AFAICT #65294 (comment) is adjacent to this change.

@jieyouxu jieyouxu added the L-unused_attributes Lint: unused_attributes label Mar 23, 2025
@saethlin saethlin added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 24, 2025
I saw someone post a code sample that contained these two attributes,
which immediately made me suspicious.
My suspicions were confirmed when I did a small test and checked the
compiler source code to confirm that in these cases, `#[inline]` is
indeed ignored (because you can't exactly `LocalCopy`an unmangled symbol
since that would lead to duplicate symbols, and doing a mix of an
unmangled `GloballyShared` and mangled `LocalCopy` instantiation is too
complicated for our current instatiation mode logic, which I don't want
to change right now).

So instead, emit the usual unused attribute lint with a message saying
that the attribute is ignored in this position.

I think this is not 100% true, since I expect LLVM `inlinehint` to still
be applied to such a function, but that's not why people use this
attribute, they use it for the `LocalCopy` instantiation mode, where it
doesn't work.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea what happened here

@Noratrieb
Copy link
Member Author

i don't think a crater run here is really worth it. i don't think our action would change based on the results, even if it could be interesting to look at.

@saethlin
Copy link
Member

saethlin commented Mar 27, 2025

I approve of the idea of this PR, but it's mostly touching code I am not familiar with, so for implementation
r? jdonszelmann

@rustbot rustbot assigned jdonszelmann and unassigned saethlin Mar 27, 2025
@jdonszelmann
Copy link
Contributor

jdonszelmann commented Mar 27, 2025

I'll give it a more thorough review tomorrow then :)

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Mar 31, 2025

Alright, it wasn't quite tomorrow but I can't find anything wrong with this. @bors r=jdonszelmann,saethlin rollup

@bors
Copy link
Collaborator

bors commented Mar 31, 2025

📌 Commit 1aed58c has been approved by me,saethlin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 31, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 31, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#138176 (Prefer built-in sized impls (and only sized impls) for rigid types always)
 - rust-lang#138749 (Fix closure recovery for missing block when return type is specified)
 - rust-lang#138842 (Emit `unused_attributes` for `#[inline]` on exported functions)
 - rust-lang#139153 (Encode synthetic by-move coroutine body with a different `DefPathData`)
 - rust-lang#139157 (Remove mention of `exhaustive_patterns` from `never` docs)
 - rust-lang#139167 (Remove Amanieu from the libs review rotation)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ac05597 into rust-lang:master Mar 31, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Mar 31, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 31, 2025
Rollup merge of rust-lang#138842 - Noratrieb:inline-exported, r=me,saethlin

Emit `unused_attributes` for `#[inline]` on exported functions

I saw someone post a code sample that contained these two attributes, which immediately made me suspicious.
My suspicions were confirmed when I did a small test and checked the compiler source code to confirm that in these cases, `#[inline]` is indeed ignored (because you can't exactly `LocalCopy`an unmangled symbol since that would lead to duplicate symbols, and doing a mix of an unmangled `GloballyShared` and mangled `LocalCopy` instantiation is too complicated for our current instatiation mode logic, which I don't want to change right now).

So instead, emit the usual unused attribute lint with a message saying that the attribute is ignored in this position.

I think this is not 100% true, since I expect LLVM `inlinehint` to still be applied to such a function, but that's not why people use this attribute, they use it for the `LocalCopy` instantiation mode, where it doesn't work.

r? saethlin as the instantiation guy

Procedurally, I think this should be fine to merge without any lang involvement, as this only does a very minor extension to an existing lint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) L-unused_attributes Lint: unused_attributes S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants