Skip to content

Uplift the invalid_atomic_ordering lint from clippy to rustc#84039

Merged
bors merged 2 commits intorust-lang:masterfrom
jyn514:uplift-atomic-ordering
Aug 16, 2021
Merged

Uplift the invalid_atomic_ordering lint from clippy to rustc#84039
bors merged 2 commits intorust-lang:masterfrom
jyn514:uplift-atomic-ordering

Conversation

@jyn514
Copy link
Member

@jyn514 jyn514 commented Apr 9, 2021

This is mostly just a rebase of #79654; I've copy/pasted the text from that PR below.

r? @lcnr since you reviewed the last one, but feel free to reassign.


This is an implementation of rust-lang/compiler-team#390.

As mentioned, in general this turns an unconditional runtime panic into a (compile time) lint failure. It has no false positives, and the only false negatives I'm aware of are if Ordering isn't specified directly and is comes from an argument/constant/whatever.

As a result of it having no false positives, and the alternative always being strictly wrong, it's on as deny by default. This seems right.

In the zulip stream @joshtriplett suggested that lang team should FCP this before landing it. Perhaps libs team cares too?


Some notes on the code for reviewers / others below

Changes from clippy

The code is changed from the implementation in clippy in the following ways:

  1. Uses Symbols and rustc_diagnostic_items instead of string literals.
    • It's possible I should have just invoked Symbol::intern for some of these instead? Seems better to use symbol, but it did require adding several.
  2. The functions are moved to static methods inside the lint struct, as a way to namespace them.
    • There's a lot of other code in that file — which I picked as the location for this lint because @jyn514 told me that seemed reasonable.
  3. Supports unstable AtomicU128/AtomicI128.
    • I did this because it was almost easier to support them than not — not supporting them would have (ideally) required finding a way not to give them a rustc_diagnostic_item, which would have complicated an already big macro.
    • These don't have tests since I wasn't sure if/how I should make tests conditional on whether or not the target has the atomic... This is to a certain extent an issue of 64bit atomics too, but 128-bit atomics are much less common. Regardless, the existing tests should be more than thorough enough here.
  4. Minor changes like:
    • grammar tweaks ("loads cannot have Release and AcqRel ordering" => "loads cannot have Release or AcqRel ordering")
    • function renames (match_ordering_def_path => matches_ordering_def_path),
    • avoiding clippy-specific helper methods that don't exist in rustc_lint and didn't seem worth adding for this case (for example cx.struct_span_lint vs clippy's span_lint_and_help helper).

Potential issues

(This is just about the code in this PR, not conceptual issues with the lint or anything)

  1. I'm not sure if I should have used a diagnostic item for Ordering and its variants (I couldn't figure out how really, so if I should do this some pointers would be appreciated).

    • It seems possible that failing to do this might possibly mean there are more cases this lint would miss, but I don't really know how match_def_path works and if it has any pitfalls like that, so maybe not.
  2. I think I deprecated the lint in clippy (CC @flip1995 who asked to be notified about clippy changes in the future in this comment) but I'm not sure if I need to do anything else there.

    • I'm kind of hoping CI will catch if I missed anything, since x.py test src/tools/clippy fails with a lot of errors with and without my changes (and is probably a nonsense command regardless). Running cargo test from src/tools/clippy also fails with unrelated errors that seem like refactorings that didnt update clippy? So, honestly no clue.
  3. I wasn't sure if the description/example I gave good. Hopefully it is. The example is less thorough than the one from clippy here: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_atomic_ordering. Let me know if/how I should change it if it needs changing.

  4. It pulls in the if_chain crate. This crate was already used in clippy, and seems like it's used elsewhere in rustc, but I'm willing to rewrite it to not use this if needed (I'd prefer not to, all things being equal).

@jyn514 jyn514 added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-lang Relevant to the language team labels Apr 9, 2021
@rust-highfive
Copy link
Contributor

Some changes occurred in src/tools/clippy.

cc @rust-lang/clippy

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2021
@jyn514
Copy link
Member Author

jyn514 commented Apr 9, 2021

cc @thomcc

@jyn514
Copy link
Member Author

jyn514 commented Apr 9, 2021

Oh also the clippy deprecation will conflict with rust-lang/rust-clippy#7056; I expect there will be a subtree sync before fcp finishes though so it shouldn't be a big deal.

@rust-log-analyzer

This comment has been minimized.

@jyn514
Copy link
Member Author

jyn514 commented Apr 9, 2021

I am not sure why CI is failing :( I think it has something to do with Acquire being a Ctor instead of the variant itself, but I'm not sure what's going wrong. Some debugging in https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.

@thomcc
Copy link
Member

thomcc commented Apr 10, 2021

Hey, thanks for taking this over and carrying it over the finish line. I had struggled a few times with the rebase, since I'm mostly unfamiliar with rustc and clippy, and generally didn't have the time to dedicate to it at the moment.

Still, will be super thrilled to see it land, it will make atomic stuff way easier to teach, if nothing else!

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a growing pattern in Clippy that is now moving to rustc. Should we have a tcx.get_diagnostic_name(def_id)?

Or, alternatively...

if cx.tcx.is_diagnostic_item(sym::atomic_mod, cx.tcx.parent(did)) {
  if matches!(cx.tcx.item_name(did), sym::AtomicU8 | sym::AtomicBool | ..) {

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we should IMO

Copy link
Member Author

Choose a reason for hiding this comment

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

It already exists: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.get_diagnostic_item
It has a warning that it's slower than is_diagnostic_item (presumably because it invalids more of the incremental cache). Should I use it anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, I'm not sure what you're suggesting the new code should look like. Don't I need to check all the types in the array no matter what?

Copy link
Contributor

Choose a reason for hiding this comment

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

It already exists

That gets a DefId from a name. I'm suggesting the opposite.

My second suggestion is to make the parent module a diagnostic item instead of the types and then just check the name of the type.

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better short-circuiting to check the method name before the type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately that panics if the method name is the same but the type doesn't match, because it assumes the call has at least 2/3 args.

Copy link
Member Author

Choose a reason for hiding this comment

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

Err actually it panics with or without that change; let me look into it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@lcnr why was the UI test you suggested valid? Shouldn't inherent impls always take precedence over trait impls? nightly rustc accepts it, but I don't know why :/

Copy link
Contributor

Choose a reason for hiding this comment

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

Ooh bold move!

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer that we try to keep our external dependencies as small as possible and this doesn't seem like a necessary addition to me but we have no official policy saying that, so I don't think it should hold up this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

@wesleywiser if_chain is already in the source tree through clippy, this just adds it to rustc itself. I could rewrite it without if_chain, but it would be annoying and IMO a lot uglier.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's true but clippy isn't a dependency of rustc so any dependencies they pull in don't actually affect rustc. That's why you had to add it to the tidy allowed set of dependencies.

It's my personal preference and we already have quite a lot of external dependencies as that list showed so don't worry about it 🙂

@jyn514 jyn514 force-pushed the uplift-atomic-ordering branch 3 times, most recently from 40b4b00 to caa95ff Compare April 11, 2021 04:40
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@camsteffen camsteffen left a comment

Choose a reason for hiding this comment

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

A couple more ideas!

@jyn514 jyn514 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 Apr 14, 2021
@jyn514 jyn514 force-pushed the uplift-atomic-ordering branch from daf3ddf to 116d14b Compare April 14, 2021 20:03
@jyn514 jyn514 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 14, 2021
@lcnr
Copy link
Contributor

lcnr commented Apr 15, 2021

don't have a lot of capacity rn and this is out of cache for me

r? @estebank in case you have the time, otherwise I can probably get to this next weekend

@rust-highfive rust-highfive assigned estebank and unassigned lcnr Apr 15, 2021
@flip1995
Copy link
Member

Sync PR is up: #84427

@jyn514
Copy link
Member Author

jyn514 commented Apr 22, 2021

I think this is mostly done from an implementation standpoint. @rust-lang/lang does this need an FCP? If so, could someone start one?

@bors

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 24, 2021

⌛ Testing commit 768e85bbc5f8001b70bb13c37f0777b4ed232500 with merge f530f4975433fbc8a6bc2318fd508a50c0852372...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 24, 2021

💔 Test failed - checks-actions

@bors
Copy link
Collaborator

bors commented Jul 29, 2021

☔ The latest upstream changes (presumably #87579) made this pull request unmergeable. Please resolve the merge conflicts.

@jyn514
Copy link
Member Author

jyn514 commented Jul 29, 2021

@wesleywiser
Copy link
Member

I'm not sure. It seems like that should work as compiletest is getting invoked with the correct target flag. Maybe try adding // ignore-android?

@jyn514
Copy link
Member Author

jyn514 commented Jul 31, 2021

@bors r=wesleywiser rollup=never

@bors
Copy link
Collaborator

bors commented Jul 31, 2021

📌 Commit f1ee65e609789518247d9c8b35050f7941a2e9d4 has been approved by wesleywiser

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 31, 2021

⌛ Testing commit f1ee65e609789518247d9c8b35050f7941a2e9d4 with merge 677762189fe82d44347708c21a7eef0e65e5fad4...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 31, 2021

💔 Test failed - checks-actions

@camelid
Copy link
Member

camelid commented Aug 15, 2021

triage: @jyn514 it looks like some tests need to be blessed to update line numbers and then this can be re-approved.

@rustbot label: -S-waiting-on-review +S-waiting-on-author

@jyn514
Copy link
Member Author

jyn514 commented Aug 16, 2021

@bors r=wesleywiser rollup=never

@bors
Copy link
Collaborator

bors commented Aug 16, 2021

📌 Commit 95f4920a398d3900974a49259b8ee7fc4ec7190e has been approved by wesleywiser

@bors
Copy link
Collaborator

bors commented Aug 16, 2021

⌛ Testing commit 95f4920a398d3900974a49259b8ee7fc4ec7190e with merge 41d8c0eb647dd7026d752da6a7dcf871c589dffe...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Aug 16, 2021

💔 Test failed - checks-actions

@jyn514
Copy link
Member Author

jyn514 commented Aug 16, 2021

I give up. I have no idea what's wrong and I am not willing to put more time into it.

@jyn514
Copy link
Member Author

jyn514 commented Aug 16, 2021

Oh. For some reason there are like 12 files that all test the same thing, and I only added // ignore-android to one of them.

thomcc and others added 2 commits August 16, 2021 03:55
- Deprecate clippy::invalid_atomic_ordering
- Use rustc_diagnostic_item for the orderings in the invalid_atomic_ordering lint
- Reduce code duplication
- Give up on making enum variants diagnostic items and just look for
`Ordering` instead

  I ran into tons of trouble with this because apparently the change to
  store HIR attrs in a side table also gave the DefIds of the
  constructor instead of the variant itself. So I had to change
  `matches_ordering` to also check the grandparent of the defid as well.

- Rename `atomic_ordering_x` symbols to just the name of the variant
- Fix typos in checks - there were a few places that said "may not be
  Release" in the diagnostic but actually checked for SeqCst in the lint.
- Make constant items const
- Use fewer diagnostic items
- Only look at arguments after making sure the method matches

  This prevents an ICE when there aren't enough arguments.

- Ignore trait methods
- Only check Ctors instead of going through `qpath_res`

  The functions take values, so this couldn't ever be anything else.

- Add if_chain to allowed dependencies
- Fix grammar
- Remove unnecessary allow
@jyn514
Copy link
Member Author

jyn514 commented Aug 16, 2021

@bors r=wesleywiser

@bors
Copy link
Collaborator

bors commented Aug 16, 2021

📌 Commit 5522177 has been approved by wesleywiser

@bors
Copy link
Collaborator

bors commented Aug 16, 2021

⌛ Testing commit 5522177 with merge 92f3753...

@bors
Copy link
Collaborator

bors commented Aug 16, 2021

☀️ Test successful - checks-actions
Approved by: wesleywiser
Pushing 92f3753 to master...

@wesleywiser
Copy link
Member

Woohoo! Congrats @jyn514 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-clippy Relevant to the Clippy team. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.