Repository navigation
Support underscore as associated const name in inherent impls - #158970
Conversation
This comment has been minimized.
This comment has been minimized.
|
This PR changes rustc_public cc @oli-obk, @celinval, @ouz-a HIR ty lowering was modified cc @fmease
cc @rust-lang/clippy These commits modify Please ensure that if you've changed the output:
cc @obi1kenobi |
|
r? @jackh726 rustbot has assigned @jackh726. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
e803c41 to
c664e8b
Compare
This comment has been minimized.
This comment has been minimized.
| /// Returns named associated items in definition order. | ||
| pub fn named_items(&self) -> impl '_ + Iterator<Item = &ty::AssocItem> { | ||
| self.items.iter().filter_map(|(name, v)| name.is_some().then(|| v)) | ||
| } |
There was a problem hiding this comment.
I have a slight issue with adding this function.
The comment on in_definition_order has a comment about new code; this function doesn't have that comment. It's each to accidentally rely on definition order with realizing. So - at the very least that comment needs to be added here too.
Second, it makes me slightly worried that someone might use this when they should have been iterating over all items.
There was a problem hiding this comment.
Would a rename to named_items_in_defnition_order make sense here?
| } | ||
| (ty::AssocKind::Const { name, .. }, ty::AssocContainer::Trait) => { | ||
| (ty::AssocKind::Const { .. }, ty::AssocContainer::Trait) => { | ||
| let name = this.item.name(); |
There was a problem hiding this comment.
I would rather use name.expect(...) than this (here and elsewhere).
There was a problem hiding this comment.
Here this.item.name() already does opt_name().expect(...), with the same error message. Would that be enough? Or were you suggesting we shouldn't change the AssocKind::Const representation from { name: Symbol, ... } -> { data: AssocConstData, ... }?
There was a problem hiding this comment.
Sorry, let me say more:
By calling this.item.name() we are now introducing a panic path that was there before - and one that's hidden through indirection at that.
Looking at this with fresh eyes, I actually think that we should have separate match arms and be explicit about what we're doing, rather than rely on a panic in AssocItem::name.
There was a problem hiding this comment.
Sorry for the delay >_<. I do think you have a point, and I've done what you suggested
| explicit_implied_const_bounds: Table<DefIndex, LazyArray<(ty::PolyTraitRef<'static>, Span)>>, | ||
| inherent_impls: Table<DefIndex, LazyArray<DefIndex>>, | ||
| opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>, | ||
| is_anon_assoc_const: Table<DefIndex, bool>, |
There was a problem hiding this comment.
:/ is this really necessary? Why not use opt_item_name in decoder?
There was a problem hiding this comment.
I was thinking more about the perf issue(?) Intuitively bool should be cheaper than Symbol? But I just found that Symbol is interned so Option<Symbol> only takes up 4 bytes when reading the doc. So yeah I suppose that doesn't matter that much, so let's go with it :P
| } | ||
| } | ||
| DefKind::AssocFn | DefKind::AssocConst { .. } | DefKind::AssocTy => { | ||
| // As with free constants named `_`, associated constants named `_` are always live. |
There was a problem hiding this comment.
"live" -> "dead"?
| let kind = match impl_item.kind { | ||
| hir::ImplItemKind::Const(_, rhs) => { | ||
| ty::AssocKind::Const { name, is_type_const: matches!(rhs, ConstItemRhs::TypeConst(_)) } | ||
| let data = if impl_item.is_anon_const() { |
There was a problem hiding this comment.
This can just look at name directly? Is hir::ImplItem::is_anon_const used elsewhere? There are extra checks there that aren't meaningful because we already know that is a Const impl item, right?
There was a problem hiding this comment.
I suppose there is the impl_kind check?
There was a problem hiding this comment.
I think that very much depends on if the ImplItemImplKind (or say ty::AssocContainer) says it's an inherent impl. We could drop the anon const check if the context here is definitely an inherent impl, but I don't think that's guaranteed since this associated_item_from_impl_item() function can accept any kind of impl item in any context, and then return the corresponding ty::AssocItem, no? Or am I missing something?
| impl Public { | ||
| const _: Private = Private; | ||
| } | ||
|
|
||
| impl Private { | ||
| pub const _: Private = Private; | ||
| pub const _: () = {}; | ||
| } |
There was a problem hiding this comment.
Curious why these don't trigger the dead code lint?
| const _: () = panic!(); | ||
| const _: () = assert!(false); | ||
| const _: [(); { | ||
| { | ||
| assert!(std::mem::size_of::<Self>() == 0) | ||
| }; | ||
| 0 | ||
| }] = []; | ||
| const _: [(); { | ||
| { | ||
| assert!(true) | ||
| }; | ||
| 0 | ||
| }] = []; |
There was a problem hiding this comment.
I would expect these all to trigger dead code lints?
There was a problem hiding this comment.
As I said in the PR description, I intentionally kept the behavior of dead code lints on both underscore and underscore-prefixed associated consts as is. Currently if you write const _foo: () = {};, it won't trigger the dead code lint: playground.
I did this since one could argue that "I already prefixed/named it with underscore, why did rustc still warn me it's dead code?". I'd personally propose a new lint to teach users that they won't be evaluated if they're never accessed, rather than teaching them through the dead code lint. I kinda tend to agree with rust-lang/rfcs#3527 (comment) to some extent.
So at the very least, it's debatable. Let's just keep it as is for now and discuss this later.
This comment has been minimized.
This comment has been minimized.
c664e8b to
82ef566
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
82ef566 to
5c63523
Compare
…,fmease Support underscore as associated const name in inherent impls This PR doesn't change the current behavior of the `dead_code` lint for unused associated consts with underscore-prefixed names and treats associated consts named `_` consistently. There might still be some places that should've been changed to use `named_items_in_definition_order()` instead of `in_definition_order()`, though I'm not quite sure. [The RFC](https://github.com/dtolnay/rfcs/blob/assocunderscore/text/0000-associated-const-underscore.md#reference-level-explanation) does not propose const underscore inside traits or trait impls, so this PR only implements support for const underscore in inherent impls. Unresolved questions: - Whether to lint `impl Private { pub const _: Private = Private; }`, where the visibility qualifier here has no effect. It seems like there wasn't any disagreement on linting this, though what's proposed in the RFC currently diverges from that: [RFC: Associated const underscore](https://github.com/dtolnay/rfcs/blob/assocunderscore/text/0000-associated-const-underscore.md#reference-level-explanation). Tracking: - rust-lang#158944
…uwer Rollup of 8 pull requests Successful merges: - #158970 (Support underscore as associated const name in inherent impls) - #165192 (BPF: Define max/min atomic width per CPU version) - #165611 (Avoid terminator: None in MIR building) - #165749 (Improve duplicate attribute diagnostics for active built-in attributes) - #165762 (Fix `x bench library/std`) - #165726 ([rustdoc-frontend] Properly clear search state when sidebar link is clicked) - #165728 (Disable no_threads compiler errors for Trusty targets) - #165754 (rustdoc: Rename `--show-coverage` CLI flag into `--coverage`)
|
⌛ Testing commit 5baf366 with merge d43c99d... Workflow: https://github.com/rust-lang/rust/actions/runs/42897069548 |
Support underscore as associated const name in inherent impls This PR doesn't change the current behavior of the `dead_code` lint for unused associated consts with underscore-prefixed names and treats associated consts named `_` consistently. There might still be some places that should've been changed to use `named_items_in_definition_order()` instead of `in_definition_order()`, though I'm not quite sure. [The RFC](https://github.com/dtolnay/rfcs/blob/assocunderscore/text/0000-associated-const-underscore.md#reference-level-explanation) does not propose const underscore inside traits or trait impls, so this PR only implements support for const underscore in inherent impls. Unresolved questions: - Whether to lint `impl Private { pub const _: Private = Private; }`, where the visibility qualifier here has no effect. It seems like there wasn't any disagreement on linting this, though what's proposed in the RFC currently diverges from that: [RFC: Associated const underscore](https://github.com/dtolnay/rfcs/blob/assocunderscore/text/0000-associated-const-underscore.md#reference-level-explanation). Tracking: - #158944
|
💔 I suspect this PR failed tests as part of a rollup After fixing the problem, consider running a try job for the failed job before re-approving. Link to failure: #165775 (comment) |
|
This pull request was unapproved. This PR was contained in a rollup (#165775), which was unapproved. Auto build was cancelled due to unapproval. Cancelled workflows: |
This comment has been minimized.
This comment has been minimized.
Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
|
The check failed because #164217 had been merged right before the run, which added a new field to |
5baf366 to
5b3196f
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
I don't think we need to bother running a try job here so I'm going to r= this outright (fingers crossed) @bors r=jackh726,fmease |
…uwer Rollup of 8 pull requests Successful merges: - #165522 (Add relnotes for 1.100) - #158970 (Support underscore as associated const name in inherent impls) - #164582 (Properly feature gate doc(rust_logo)) - #165680 (Update call to llvm::codegen::InitTargetOptionsFromCodeGenFlags) - #165768 (Bail out ill-formed aliases when assembling trait object bounds candidates) - #165776 (docs: Fix a typo in rustc-dev-guide [NFC]) - #165797 (Disable flaky `tests/debuginfo/basic-stepping.rs` on aarch64 msvc) - #165798 (Add a regression test for fixed next-solver ICE)
Rollup merge of #158970 - makai410:us-assoc-cnst, r=jackh726,fmease Support underscore as associated const name in inherent impls This PR doesn't change the current behavior of the `dead_code` lint for unused associated consts with underscore-prefixed names and treats associated consts named `_` consistently. There might still be some places that should've been changed to use `named_items_in_definition_order()` instead of `in_definition_order()`, though I'm not quite sure. [The RFC](https://github.com/dtolnay/rfcs/blob/assocunderscore/text/0000-associated-const-underscore.md#reference-level-explanation) does not propose const underscore inside traits or trait impls, so this PR only implements support for const underscore in inherent impls. Unresolved questions: - Whether to lint `impl Private { pub const _: Private = Private; }`, where the visibility qualifier here has no effect. It seems like there wasn't any disagreement on linting this, though what's proposed in the RFC currently diverges from that: [RFC: Associated const underscore](https://github.com/dtolnay/rfcs/blob/assocunderscore/text/0000-associated-const-underscore.md#reference-level-explanation). Tracking: - #158944
Upstream rustc replaced `ty::AssocKind::Const { name }` with
`ty::AssocKind::Const { data: AssocConstData }` in rust-lang/rust#158970
(commit `5b3196fcd42db0cca7df744bf0efc84b4c0d8f37`) to support anonymous
associated constants (`const _: ...` and `pub const _: ...`).
Use `assoc_item.opt_name()` instead of destructuring `name` on
`ty::AssocKind::Const` so `cc_bindings_from_rs` builds against both old and
new toolchains, and skip nameless anonymous associated constants in
`generate_cpp_enum` and `generate_associated_item` (matching module-level
underscore constants).
PiperOrigin-RevId: 1019241616
Upstream rustc replaced `ty::AssocKind::Const { name }` with
`ty::AssocKind::Const { data: AssocConstData }` in rust-lang/rust#158970
(commit `5b3196fcd42db0cca7df744bf0efc84b4c0d8f37`) to support anonymous
associated constants (`const _: ...` and `pub const _: ...`).
Use `assoc_item.opt_name()` instead of destructuring `name` on
`ty::AssocKind::Const` so `cc_bindings_from_rs` builds against both old and
new toolchains, and skip nameless anonymous associated constants in
`generate_cpp_enum` and `generate_associated_item` (matching module-level
underscore constants).
PiperOrigin-RevId: 1019451675
View all comments
This PR doesn't change the current behavior of the
dead_codelint for unused associated consts with underscore-prefixed names and treats associated consts named_consistently.There might still be some places that should've been changed to use
named_items_in_definition_order()instead ofin_definition_order(), though I'm not quite sure.The RFC does not propose const underscore inside traits or trait impls, so this PR only implements support for const underscore in inherent impls.
Unresolved questions:
impl Private { pub const _: Private = Private; }, where the visibility qualifier here has no effect. It seems like there wasn't any disagreement on linting this, though what's proposed in the RFC currently diverges from that: RFC: Associated const underscore.Tracking: