Skip to content

Commit

Permalink
Make iteration order of shallow_lint_levels_on query stable
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Dec 21, 2023
1 parent 45c93e3 commit 62fc1e7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
SubdiagnosticMessage, Substitution, SubstitutionPart, SuggestionStyle,
};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::unord::UnordMap;
use rustc_error_messages::fluent_value_from_str_list_sep_by_and;
use rustc_error_messages::FluentValue;
use rustc_lint_defs::{Applicability, LintExpectationId};
Expand Down Expand Up @@ -280,7 +281,7 @@ impl Diagnostic {

pub(crate) fn update_unstable_expectation_id(
&mut self,
unstable_to_stable: &FxHashMap<LintExpectationId, LintExpectationId>,
unstable_to_stable: &UnordMap<LintExpectationId, LintExpectationId>,
) {
if let Level::Expect(expectation_id) | Level::Warning(Some(expectation_id)) =
&mut self.level
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ extern crate self as rustc_errors;

pub use emitter::ColorConfig;

use rustc_data_structures::unord::UnordMap;
use rustc_lint_defs::LintExpectationId;
use Level::*;

use emitter::{is_case_difference, DynEmitter, Emitter, EmitterWriter};
use registry::Registry;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_data_structures::AtomicRef;
Expand Down Expand Up @@ -1367,7 +1368,7 @@ impl DiagCtxt {

pub fn update_unstable_expectation_id(
&self,
unstable_to_stable: &FxHashMap<LintExpectationId, LintExpectationId>,
unstable_to_stable: &UnordMap<LintExpectationId, LintExpectationId>,
) {
let mut inner = self.inner.borrow_mut();
let diags = std::mem::take(&mut inner.unstable_expect_diagnostics);
Expand Down
36 changes: 18 additions & 18 deletions compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};
use rustc_ast as ast;
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{DecorateLint, DiagnosticBuilder, DiagnosticMessage, MultiSpan};
use rustc_feature::{Features, GateIssue};
use rustc_hir as hir;
Expand Down Expand Up @@ -73,7 +73,7 @@ rustc_index::newtype_index! {
struct LintSet {
// -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
// flag.
specs: FxHashMap<LintId, LevelAndSource>,
specs: UnordMap<LintId, LevelAndSource>,
parent: LintStackIndex,
}

Expand All @@ -86,7 +86,7 @@ impl LintLevelSets {
&self,
lint: &'static Lint,
idx: LintStackIndex,
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
aux: Option<&UnordMap<LintId, LevelAndSource>>,
sess: &Session,
) -> LevelAndSource {
let lint = LintId::of(lint);
Expand All @@ -101,7 +101,7 @@ impl LintLevelSets {
&self,
id: LintId,
mut idx: LintStackIndex,
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
aux: Option<&UnordMap<LintId, LevelAndSource>>,
) -> (Option<Level>, LintLevelSource) {
if let Some(specs) = aux {
if let Some(&(level, src)) = specs.get(&id) {
Expand Down Expand Up @@ -132,8 +132,8 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
cur: hir::CRATE_HIR_ID,
specs: ShallowLintLevelMap::default(),
expectations: Vec::new(),
unstable_to_stable_ids: FxHashMap::default(),
empty: FxHashMap::default(),
unstable_to_stable_ids: Default::default(),
empty: Default::default(),
},
warn_about_weird_lints: false,
store,
Expand Down Expand Up @@ -161,7 +161,7 @@ fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLe
tcx,
cur: owner.into(),
specs: ShallowLintLevelMap::default(),
empty: FxHashMap::default(),
empty: Default::default(),
attrs,
},
warn_about_weird_lints: false,
Expand Down Expand Up @@ -209,14 +209,14 @@ pub struct TopDown {
}

pub trait LintLevelsProvider {
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource>;
fn current_specs(&self) -> &UnordMap<LintId, LevelAndSource>;
fn insert(&mut self, id: LintId, lvl: LevelAndSource);
fn get_lint_level(&self, lint: &'static Lint, sess: &Session) -> LevelAndSource;
fn push_expectation(&mut self, _id: LintExpectationId, _expectation: LintExpectation) {}
}

impl LintLevelsProvider for TopDown {
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
fn current_specs(&self) -> &UnordMap<LintId, LevelAndSource> {
&self.sets.list[self.cur].specs
}

Expand All @@ -234,12 +234,12 @@ struct LintLevelQueryMap<'tcx> {
cur: HirId,
specs: ShallowLintLevelMap,
/// Empty hash map to simplify code.
empty: FxHashMap<LintId, LevelAndSource>,
empty: UnordMap<LintId, LevelAndSource>,
attrs: &'tcx hir::AttributeMap<'tcx>,
}

impl LintLevelsProvider for LintLevelQueryMap<'_> {
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
fn current_specs(&self) -> &UnordMap<LintId, LevelAndSource> {
self.specs.specs.get(&self.cur.local_id).unwrap_or(&self.empty)
}
fn insert(&mut self, id: LintId, lvl: LevelAndSource) {
Expand All @@ -257,13 +257,13 @@ struct QueryMapExpectationsWrapper<'tcx> {
/// Level map for `cur`.
specs: ShallowLintLevelMap,
expectations: Vec<(LintExpectationId, LintExpectation)>,
unstable_to_stable_ids: FxHashMap<LintExpectationId, LintExpectationId>,
unstable_to_stable_ids: UnordMap<LintExpectationId, LintExpectationId>,
/// Empty hash map to simplify code.
empty: FxHashMap<LintId, LevelAndSource>,
empty: UnordMap<LintId, LevelAndSource>,
}

impl LintLevelsProvider for QueryMapExpectationsWrapper<'_> {
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
fn current_specs(&self) -> &UnordMap<LintId, LevelAndSource> {
self.specs.specs.get(&self.cur.local_id).unwrap_or(&self.empty)
}
fn insert(&mut self, id: LintId, lvl: LevelAndSource) {
Expand Down Expand Up @@ -486,7 +486,7 @@ impl<'s> LintLevelsBuilder<'s, TopDown> {
.provider
.sets
.list
.push(LintSet { specs: FxHashMap::default(), parent: COMMAND_LINE });
.push(LintSet { specs: Default::default(), parent: COMMAND_LINE });
self.add_command_line();
}

Expand All @@ -512,7 +512,7 @@ impl<'s> LintLevelsBuilder<'s, TopDown> {
) -> BuilderPush {
let prev = self.provider.cur;
self.provider.cur =
self.provider.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev });
self.provider.sets.list.push(LintSet { specs: Default::default(), parent: prev });

self.add(attrs, is_crate_node, source_hir_id);

Expand Down Expand Up @@ -547,7 +547,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
self.features
}

fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
fn current_specs(&self) -> &UnordMap<LintId, LevelAndSource> {
self.provider.current_specs()
}

Expand Down Expand Up @@ -1028,7 +1028,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
}

if !is_crate_node {
for (id, &(level, ref src)) in self.current_specs().iter() {
for (id, &(level, ref src)) in self.current_specs().to_sorted_stable_ord() {
if !id.lint.crate_level_only {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cmp;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, MultiSpan};
use rustc_hir::{HirId, ItemLocalId};
use rustc_session::lint::{
Expand Down Expand Up @@ -61,7 +61,7 @@ pub type LevelAndSource = (Level, LintLevelSource);
/// by the attributes for *a single HirId*.
#[derive(Default, Debug, HashStable)]
pub struct ShallowLintLevelMap {
pub specs: SortedMap<ItemLocalId, FxHashMap<LintId, LevelAndSource>>,
pub specs: SortedMap<ItemLocalId, UnordMap<LintId, LevelAndSource>>,
}

/// From an initial level and source, verify the effect of special annotations:
Expand Down

0 comments on commit 62fc1e7

Please sign in to comment.