Skip to content

iwyu: map std::hash to <functional> - #35073

Closed
Sjors wants to merge 1 commit into
bitcoin:masterfrom
Sjors:2026/04/iwyu
Closed

Sjors wants to merge 1 commit into
bitcoin:masterfrom
Sjors:2026/04/iwyu

Conversation

@Sjors

@Sjors Sjors commented Apr 14, 2026 •

Copy link
Copy Markdown
Member

IWYU incorrectly suggests <variant> for std::hash, as observed in #33922.

On Ubuntu with include-what-you-use 0.26 based clang version 22.1.1,
it incorrectly suggested <string_view>.

On macOS with IWYU 0.26 based on clang version 22.1.2, it correctly suggests <functional>.

Fix this by adding the mapping.

Tracked upstream: include-what-you-use/include-what-you-use#2007

@DrahtBot

DrahtBot commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept NACK maflcko

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@fanquake

Copy link
Copy Markdown
Member

Can you link to the relevant upstream issue (or open one).

@Sjors

Sjors commented Apr 14, 2026

Copy link
Copy Markdown
Member Author

@fanquake I can't find an upstream issue for this specific case. IIUC include-what-you-use/include-what-you-use#785 would prevent this in the most general way?

@@ -5,4 +5,6 @@
{ "include": [ "<mmintrin.h>", "private", "<immintrin.h>", "public" ] },
{ "include": [ "<smmintrin.h>", "private", "<immintrin.h>", "public" ] },
{ "include": [ "<tmmintrin.h>", "private", "<immintrin.h>", "public" ] },

{ "symbol": ["std::hash", "private", "<functional>", "public"] },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems fine, but this is equally wrong.

Any header is fine, see:

https://en.cppreference.com/w/cpp/utility/hash.html

I think iwyu should just tolerate any of them

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How do I specify "any header"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure if this is possible without fixing up iwyu.

In any case, I wanted to say that I don't see the point of this pull request. What is the exact harm you are trying to fix? What is the risk?

Right now, all you are doing is swapping one perfectly fine thing (for the compiler) with another one. Both are equally confusing and "wrong" for a reader, so I don't see the point for this churn here. Why does it need to change?

@hebasto hebasto Apr 16, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the context of #33922, we can simply redeclare the primary template:

namespace std {
// Redeclaration of the primary template.
template <class T>
struct hash; // IWYU pragma: keep

/** Disable default std::hash for CTransactionRef to prevent accidentally
 *  comparing by pointer. Use CTransactionRefSaltedHash or provide a custom
 *  hasher. */
template <>
struct hash<CTransactionRef> {
    size_t operator()(const CTransactionRef&) const = delete;
};
} // namespace std

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What is the exact harm you are trying to fix?

CI spuriously fails for #33922.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the exact harm you are trying to fix?

CI spuriously fails for #33922.

Given how iwyu works, it is a harmless and arguably valid suggestion. Recall that the goal of using iwyu is to avoid missing includes, which have lead to compile failures in the past. Adding an include that already exists implicitly is harmless. And fwiw, CI did pass on commit 10acf818ed70d4cd2af864278a35a6b64de0bb16.

Working around that in the wrong way will just achieve the opposite. E.g. a no_include pragma may lead to the exact compile failures we are trying to avoid, down the line.

@Sjors
Sjors marked this pull request as draft April 14, 2026 21:03
@Sjors

Sjors commented Apr 14, 2026

Copy link
Copy Markdown
Member Author

This might be a clang vs libc++ thing, because I can't reproduce the CI failure locally: include-what-you-use 0.26 based on clang version 22.1.2

@fanquake

Copy link
Copy Markdown
Member

cc @hebasto

@Sjors
Sjors marked this pull request as ready for review April 15, 2026 13:31
@Sjors

Sjors commented Apr 15, 2026

Copy link
Copy Markdown
Member Author

I was able to reproduce a similar, but not identical, result on Ubuntu (see PR description), where it suggested <string_view> instead of <variant>. Since I can't reproduce the issue on macOS, the issue seems specific to libstdc++.

@Sjors
Sjors marked this pull request as draft April 15, 2026 13:34
IWYU incorrectly suggested <variant> for std::hash in bitcoin#33922.
On Ubuntu with include-what-you-use 0.26 based clang version 22.1.1,
it incorrectly suggested <string_view>.

On macOS with IWYU 0.26 based on clang version 22.1.2, it correctly
suggests <functional>.

Fix this by adding the mapping.
@Sjors
Sjors marked this pull request as ready for review April 15, 2026 13:35
@Sjors

Sjors commented Apr 15, 2026 •

Copy link
Copy Markdown
Member Author

(accidentally pushed part of #33922, it should be good for review now)

Tracking upstream: include-what-you-use/include-what-you-use#2007

{ "include": [ "<tmmintrin.h>", "private", "<immintrin.h>", "public" ] },

# IWYU's std_symbol_map.inc maps std::hash<T> specializations to their
# correct headers, but with libstdc++ IWYU still suggests a wrong

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use libc++, if libstdc++ yields the wrong result?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not sure if it's worth switching based on this N=1 example.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To give some background why I suggested it (but I haven't confirmed any of this): libc++ has the option -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES. Further, assuming that the "wrong" header was just picked because it was the first one present anyway (ref include-what-you-use/include-what-you-use#2007 (comment)), it seems that D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES may improve the end result minimally. Though, I haven't checked any of this.

@hebasto

hebasto commented Apr 15, 2026

Copy link
Copy Markdown
Member

@Sjors

Sjors commented Apr 15, 2026

Copy link
Copy Markdown
Member Author

@hebasto so should I use <memory> instead of <functional>? Or is there just no way to get a universal rule and do we somehow disable the linter for this case?

@hebasto

hebasto commented Apr 16, 2026

Copy link
Copy Markdown
Member

@hebasto so should I use <memory> instead of <functional>? Or is there just no way to get a universal rule and do we somehow disable the linter for this case?

I would suggest the following approach in #33922:

// IWYU erroneously suggests including <variant> instead of <memory>
// for std::hash<CTransactionRef>.
// See https://github.com/include-what-you-use/include-what-you-use/issues/2007.
// IWYU pragma: no_include <variant>

@maflcko

maflcko commented Apr 16, 2026

Copy link
Copy Markdown
Member
// IWYU pragma: no_include <variant>

I disagree. This patch would be harmful. If std::variant is used any time in the future, it would be missing the include and lead to compile failures.

As per my last comment (#35073 (comment)), I don't understand why this pull requests exists and what it is trying to achieve. There is no real bug and any attempted fix is just going to add more harm and risks.

NACK

@purpleKarrot

Copy link
Copy Markdown
Contributor

What exactly is the root problem here? Is it that IWYU does not recognize std::hash<CTransactionRef> as a template specialization for std::shared_ptr and therefore does not map it to <memory>? Would it help to add a mapping for that type?

  { "symbol": ["std::hash<CTransactionRef>", "private", "<memory>", "public"] },

@hebasto

hebasto commented Apr 16, 2026

Copy link
Copy Markdown
Member

What exactly is the root problem here? Is it that IWYU does not recognize std::hash<CTransactionRef> as a template specialization for std::shared_ptr and therefore does not map it to <memory>? Would it help to add a mapping for that type?

  { "symbol": ["std::hash<CTransactionRef>", "private", "<memory>", "public"] },

See include-what-you-use/include-what-you-use#2007 (comment).

@Sjors

Sjors commented Apr 17, 2026

Copy link
Copy Markdown
Member Author

@hebasto's workaround from #35073 (comment) doesn't work unfortunately, see (iwyu ci log).

If I keep the <functional> include, it wants it removed. When I remove it, demands <variant> again.

Happy to close this is it's incorrect, but I don't think we should be enforcing buggy linters project-wide. This sort of spurious CI failure is impossible to debug without deep compiler / build system knowledge.

@Sjors

Sjors commented Apr 17, 2026 •

Copy link
Copy Markdown
Member Author

Actually @hebasto suggested two different workarounds.

  1. redeclare the primary template: this doesn't work, per my previous comment
  2. IWYU pragma: no_include <variant>: I'm trying that now

@maflcko

maflcko commented Apr 17, 2026

Copy link
Copy Markdown
Member

redeclare the primary template: this doesn't work, per my previous comment

You pushed the wrong thing. You can use the copy-paste icon in the upper right on GitHub to properly copy-paste it.

@Sjors

Sjors commented Apr 17, 2026

Copy link
Copy Markdown
Member Author

Workaround (2) worked: ci log

It's the most narrow fix, so closing this.

@Sjors Sjors closed this Apr 17, 2026
@Sjors

Sjors commented Apr 17, 2026

Copy link
Copy Markdown
Member Author

@maflcko for (1) I pushed this:

template <class T>
struct hash; // IWYU pragma: keep

Which is from: #35073 (comment)

Here's the commit: 4673836

Here's the CI failure: https://github.com/bitcoin/bitcoin/actions/runs/24566460651/job/71827949758

@maflcko

maflcko commented Apr 17, 2026

Copy link
Copy Markdown
Member

@maflcko for (1) I pushed this:

Yes, that is wrong, because it is missing the namespace. You'll have to copy-paste the full suggestion.

@Sjors

Sjors commented Apr 17, 2026

Copy link
Copy Markdown
Member Author

That didn't work either, but b89942d29f1e71429070b2d1140d2792eb33bf47 does.

@Sjors

Sjors commented Apr 17, 2026 •

Copy link
Copy Markdown
Member Author

Ended up with a less verbose approach in #35101.

achow101 added a commit that referenced this pull request Jun 10, 2026
a9301cf refactor: disable default std::hash for CTransactionRef (Sjors Provoost)
47d68cd ci: backport iwyu PR 2013 std::hash mapping (Sjors Provoost)

Pull request description:

  While working on #33922 I initially forgot to add `CTransactionRefComp` to the `std::unordered_map<CTransactionRef` defined there. This PR turns that into a compiler error. See #33922 (comment)

  This change triggers a false positive IWYU error, and an inconsistent one at that: our CI wants `<variant>`, while a manual build on Ubuntu (version 0.26 with clang version 22.1.1) wants `<string_view>`.

  Various workarounds were discussed in:
  - include-what-you-use/include-what-you-use#2007
  - #35073
  - #33922 (comment)

  Addressed by back-porting:
  - include-what-you-use/include-what-you-use#2013

ACKs for top commit:
  achow101:
    ACK a9301cf
  vasild:
    ACK a9301cf
  w0xlt:
    lgtm ACK a9301cf

Tree-SHA512: 11b3f8698e66457a14d1c16f55cac3ee17a572e9c1c98f5aa9c8a8f1e8928246b1676f7544f2819bc16ec4dcf025585b9cbd60ab3f20839d3d9bcc65ee9e7c0f
rustaceanrob pushed a commit to 2140-dev/bitcoin that referenced this pull request Jun 12, 2026
…tionRef

a9301cf refactor: disable default std::hash for CTransactionRef (Sjors Provoost)
47d68cd ci: backport iwyu PR 2013 std::hash mapping (Sjors Provoost)

Pull request description:

  While working on bitcoin#33922 I initially forgot to add `CTransactionRefComp` to the `std::unordered_map<CTransactionRef` defined there. This PR turns that into a compiler error. See bitcoin#33922 (comment)

  This change triggers a false positive IWYU error, and an inconsistent one at that: our CI wants `<variant>`, while a manual build on Ubuntu (version 0.26 with clang version 22.1.1) wants `<string_view>`.

  Various workarounds were discussed in:
  - include-what-you-use/include-what-you-use#2007
  - bitcoin#35073
  - bitcoin#33922 (comment)

  Addressed by back-porting:
  - include-what-you-use/include-what-you-use#2013

ACKs for top commit:
  achow101:
    ACK a9301cf
  vasild:
    ACK a9301cf
  w0xlt:
    lgtm ACK a9301cf

Tree-SHA512: 11b3f8698e66457a14d1c16f55cac3ee17a572e9c1c98f5aa9c8a8f1e8928246b1676f7544f2819bc16ec4dcf025585b9cbd60ab3f20839d3d9bcc65ee9e7c0f
(cherry picked from commit 3bbc3c6)
Kino1994 pushed a commit to Kino1994/bitcoin-full-history that referenced this pull request Jun 28, 2026
…CTransactionRef

c20bfce refactor: disable default std::hash for CTransactionRef (Sjors Provoost)
645f544 ci: backport iwyu PR 2013 std::hash mapping (Sjors Provoost)

Pull request description:

  While working on #33922 I initially forgot to add `CTransactionRefComp` to the `std::unordered_map<CTransactionRef` defined there. This PR turns that into a compiler error. See bitcoin/bitcoin#33922 (comment)

  This change triggers a false positive IWYU error, and an inconsistent one at that: our CI wants `<variant>`, while a manual build on Ubuntu (version 0.26 with clang version 22.1.1) wants `<string_view>`.

  Various workarounds were discussed in:
  - include-what-you-use/include-what-you-use#2007
  - bitcoin/bitcoin#35073
  - bitcoin/bitcoin#33922 (comment)

  Addressed by back-porting:
  - include-what-you-use/include-what-you-use#2013

ACKs for top commit:
  achow101:
    ACK c20bfce
  vasild:
    ACK c20bfce
  w0xlt:
    lgtm ACK c20bfce

Tree-SHA512: 11b3f8698e66457a14d1c16f55cac3ee17a572e9c1c98f5aa9c8a8f1e8928246b1676f7544f2819bc16ec4dcf025585b9cbd60ab3f20839d3d9bcc65ee9e7c0f
Kino1994 pushed a commit to Kino1994/bitcoin-full-history that referenced this pull request Aug 19, 2026
…CTransactionRef

a9301cfa0730d1d08c9e982f990dfc8aa0b3a27a refactor: disable default std::hash for CTransactionRef (Sjors Provoost)
47d68cd981fb2985acca0b4d2b8d79dbdccb136d ci: backport iwyu PR 2013 std::hash mapping (Sjors Provoost)

Pull request description:

  While working on #33922 I initially forgot to add `CTransactionRefComp` to the `std::unordered_map<CTransactionRef` defined there. This PR turns that into a compiler error. See bitcoin/bitcoin#33922 (comment)

  This change triggers a false positive IWYU error, and an inconsistent one at that: our CI wants `<variant>`, while a manual build on Ubuntu (version 0.26 with clang version 22.1.1) wants `<string_view>`.

  Various workarounds were discussed in:
  - include-what-you-use/include-what-you-use#2007
  - bitcoin/bitcoin#35073
  - bitcoin/bitcoin#33922 (comment)

  Addressed by back-porting:
  - include-what-you-use/include-what-you-use#2013

ACKs for top commit:
  achow101:
    ACK a9301cfa0730d1d08c9e982f990dfc8aa0b3a27a
  vasild:
    ACK a9301cfa0730d1d08c9e982f990dfc8aa0b3a27a
  w0xlt:
    lgtm ACK a9301cfa0730d1d08c9e982f990dfc8aa0b3a27a

Tree-SHA512: 11b3f8698e66457a14d1c16f55cac3ee17a572e9c1c98f5aa9c8a8f1e8928246b1676f7544f2819bc16ec4dcf025585b9cbd60ab3f20839d3d9bcc65ee9e7c0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants