Skip to content

Audit and handle remaining cases of rustc::potential_query_instability lint (iterating HashMaps) #84447

Open

Description

@Aaron1011

View all comments

The iteration order of a HashMap depends on the Hash value of the keys. In rustc, the Hash value of a key can change between compilation sessions, even if the HashStable value remains the same (e.g. DefId, Symbol, etc.). This means that iterating over a HashMap can cause a query to produce different results given the same (as determined by HashStable) input. This can be quite subtle, as demonstrated by #82272 (comment)

To eliminate this source of issues, we should make an internal rustc-only lint for iterating over a HashMap in any way. This will need to cover explicit method calls (e.g. .keys() and .values()), as well as any usage of the IntoIterator impl for HashMap.

Note that we don't want to ban the usage of HashMaps with unstable keys - as long as they're only used for lookup (and not iteration), it's much more efficient than repeatedly converting to the stable form of a key (e.g. DefId -> DefPathHash or Symbol -> String).

Activity

  1. self-assigned this
    on Apr 22, 2021
  2. added
    A-incr-compArea: Incremental compilation
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 22, 2021
  3. bjorn3 commented on Apr 23, 2021

    @bjorn3
    Member

    Note that we don't want to ban the usage of HashMaps with unstable keys - as long as they're only used for lookup (and not iteration),

    #64131 added the StableMap wrapper for FxHashMap that doesn't expose the iteration order.

  4. jyn514 commented on Apr 23, 2021

    @jyn514
    Member
  5. cjgillot commented on Jul 5, 2022

    @cjgillot
    Contributor

    The lint exists since #89558, and has allowed on all compiler crates as rustc::potential_query_instability.
    We now need to stop allowing it in the compiler.

    Steps, for each crate except rustc_data_structures:

    • remove the allow(rustc::potential_query_instability) in lib.rs;
    • replace the FxHash{Map,Set} by FxIndex{Map,Set} where necessary.

    Please reach out on zulip if there are any questions.

  6. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    E-help-wantedCall for participation: Help is requested to fix this issue.
    on Jul 5, 2022
  7. fee1-dead commented on Jul 7, 2022

    @fee1-dead
    Member

    @rustbot claim

    @rustbot release-assignment

  8. NiklasJonsson commented on Jul 8, 2022

    @NiklasJonsson
    Contributor

    It seems like no one is assigned to this and I'd like to give it a try so assigning myself.

    @rustbot claim

  9. 126 remaining items

  10. Caylub commented on Jan 24, 2026

    @Caylub

    Poking this since it still unresolved. What work still needs be done to finish this? @ismailarilik @Aaron1011?

    Reading through this issue, and the PR's / merges above, and probing around with grep, my current understanding is that there are still some areas with #[allow(...)], without justifying comments.

    Specifically, these are the files with local allows missing justifying comments

    • compiler/rustc_type_ir/src/search_graph/mod.rs (7 occurrences)
      • Line 250
      • Line 504
      • Line 512
      • Line 713
      • Line 904
      • Line 962
      • Line 1146
    • compiler/rustc_mir_dataflow/src/value_analysis.rs (3 occurrences)
      • Line 70
      • Line 128
      • Line 584
    • compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs (2 occurrences)
      • Line 437
      • Line 445
    • compiler/rustc_lint/src/early/diagnostics/check_cfg.rs (1 occurrence)
      • Line 118
    • compiler/rustc_interface/src/interface.rs (1 occurrence)
      • Line 307
    • compiler/rustc_expand/src/mbe/macro_rules.rs (1 occurrence)
      • Line 625
    • compiler/rustc_codegen_ssa/src/target_features.rs (1 occurrence)
      • Line 193

    [Updated, the list above, assuming github links to comments count as valid justifications]

    Is adding justifying comments (or converting to FxIndexMap where necessary) all that remains for this to be resolved?

  11. added 3 commits that reference this issue on Mar 10, 2026
  12. added a commit that references this issue on Mar 11, 2026
  13. ismailarilik commented on May 23, 2026

    @ismailarilik
    Contributor

    @rustbot release-assignment

  14. JinRudy commented on Jun 3, 2026

    @JinRudy
    Contributor

    @rustbot claim

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

Metadata

Metadata

Assignees

Labels

A-incr-compArea: Incremental compilationC-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-help-wantedCall for participation: Help is requested to fix this issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions