Skip to content

Commit cfb3109

Browse files
Enable potential_query_instability lint in rustc_hir_typeck.
Fix linting errors by using FxIndex(Map|Set) and Unord(Map|Set) as appropriate.
1 parent fe03b46 commit cfb3109

File tree

15 files changed

+175
-148
lines changed

15 files changed

+175
-148
lines changed

compiler/rustc_data_structures/src/unord.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::{
3131
///
3232
/// It's still possible to do the same thing with an `Fn` by using interior mutability,
3333
/// but the chance of doing it accidentally is reduced.
34+
#[derive(Clone)]
3435
pub struct UnordItems<T, I: Iterator<Item = T>>(I);
3536

3637
impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
@@ -194,6 +195,11 @@ impl<V: Eq + Hash> UnordSet<V> {
194195
Self { inner: Default::default() }
195196
}
196197

198+
#[inline]
199+
pub fn with_capacity(capacity: usize) -> Self {
200+
Self { inner: FxHashSet::with_capacity_and_hasher(capacity, Default::default()) }
201+
}
202+
197203
#[inline]
198204
pub fn len(&self) -> usize {
199205
self.inner.len()
@@ -258,9 +264,9 @@ impl<V: Eq + Hash> UnordSet<V> {
258264
#[inline]
259265
pub fn to_sorted_stable_ord(&self) -> Vec<V>
260266
where
261-
V: Ord + StableOrd + Copy,
267+
V: Ord + StableOrd + Clone,
262268
{
263-
let mut items: Vec<V> = self.inner.iter().copied().collect();
269+
let mut items: Vec<V> = self.inner.iter().cloned().collect();
264270
items.sort_unstable();
265271
items
266272
}
@@ -312,6 +318,12 @@ impl<V: Hash + Eq> From<FxHashSet<V>> for UnordSet<V> {
312318
}
313319
}
314320

321+
impl<V: Hash + Eq, I: Iterator<Item = V>> From<UnordItems<V, I>> for UnordSet<V> {
322+
fn from(value: UnordItems<V, I>) -> Self {
323+
UnordSet { inner: FxHashSet::from_iter(value.0) }
324+
}
325+
}
326+
315327
impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordSet<V> {
316328
#[inline]
317329
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
@@ -362,6 +374,11 @@ impl<K: Hash + Eq, V, I: Iterator<Item = (K, V)>> From<UnordItems<(K, V), I>> fo
362374
}
363375

364376
impl<K: Eq + Hash, V> UnordMap<K, V> {
377+
#[inline]
378+
pub fn with_capacity(capacity: usize) -> Self {
379+
Self { inner: FxHashMap::with_capacity_and_hasher(capacity, Default::default()) }
380+
}
381+
365382
#[inline]
366383
pub fn len(&self) -> usize {
367384
self.inner.len()

compiler/rustc_hir_typeck/src/fallback.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::FnCtxt;
22
use rustc_data_structures::{
3-
fx::{FxHashMap, FxHashSet},
43
graph::WithSuccessors,
54
graph::{iterate::DepthFirstSearch, vec_graph::VecGraph},
5+
unord::{UnordBag, UnordMap, UnordSet},
66
};
77
use rustc_middle::ty::{self, Ty};
88

@@ -83,7 +83,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
8383
fn fallback_if_possible(
8484
&self,
8585
ty: Ty<'tcx>,
86-
diverging_fallback: &FxHashMap<Ty<'tcx>, Ty<'tcx>>,
86+
diverging_fallback: &UnordMap<Ty<'tcx>, Ty<'tcx>>,
8787
) {
8888
// Careful: we do NOT shallow-resolve `ty`. We know that `ty`
8989
// is an unsolved variable, and we determine its fallback
@@ -193,7 +193,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
193193
fn calculate_diverging_fallback(
194194
&self,
195195
unsolved_variables: &[Ty<'tcx>],
196-
) -> FxHashMap<Ty<'tcx>, Ty<'tcx>> {
196+
) -> UnordMap<Ty<'tcx>, Ty<'tcx>> {
197197
debug!("calculate_diverging_fallback({:?})", unsolved_variables);
198198

199199
// Construct a coercion graph where an edge `A -> B` indicates
@@ -210,10 +210,10 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
210210
//
211211
// These variables are the ones that are targets for fallback to
212212
// either `!` or `()`.
213-
let diverging_roots: FxHashSet<ty::TyVid> = self
213+
let diverging_roots: UnordSet<ty::TyVid> = self
214214
.diverging_type_vars
215215
.borrow()
216-
.iter()
216+
.items()
217217
.map(|&ty| self.shallow_resolve(ty))
218218
.filter_map(|ty| ty.ty_vid())
219219
.map(|vid| self.root_var(vid))
@@ -284,23 +284,27 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
284284
// For each diverging variable, figure out whether it can
285285
// reach a member of N. If so, it falls back to `()`. Else
286286
// `!`.
287-
let mut diverging_fallback = FxHashMap::default();
288-
diverging_fallback.reserve(diverging_vids.len());
287+
let mut diverging_fallback = UnordMap::with_capacity(diverging_vids.len());
289288
for &diverging_vid in &diverging_vids {
290289
let diverging_ty = Ty::new_var(self.tcx, diverging_vid);
291290
let root_vid = self.root_var(diverging_vid);
292291
let can_reach_non_diverging = coercion_graph
293292
.depth_first_search(root_vid)
294293
.any(|n| roots_reachable_from_non_diverging.visited(n));
295294

296-
let mut found_infer_var_info = ty::InferVarInfo { self_in_trait: false, output: false };
295+
let infer_var_infos: UnordBag<_> = self
296+
.inh
297+
.infer_var_info
298+
.borrow()
299+
.items()
300+
.filter(|&(vid, _)| self.infcx.root_var(*vid) == root_vid)
301+
.map(|(_, info)| *info)
302+
.collect();
297303

298-
for (vid, info) in self.inh.infer_var_info.borrow().iter() {
299-
if self.infcx.root_var(*vid) == root_vid {
300-
found_infer_var_info.self_in_trait |= info.self_in_trait;
301-
found_infer_var_info.output |= info.output;
302-
}
303-
}
304+
let found_infer_var_info = ty::InferVarInfo {
305+
self_in_trait: infer_var_infos.items().any(|info| info.self_in_trait),
306+
output: infer_var_infos.items().any(|info| info.output),
307+
};
304308

305309
if found_infer_var_info.self_in_trait && found_infer_var_info.output {
306310
// This case falls back to () to ensure that the code pattern in

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir::{
66
intravisit::{self, Visitor},
77
Body, Expr, ExprKind, Guard, HirId, LoopIdError,
88
};
9-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9+
use rustc_data_structures::unord::{UnordMap, UnordSet};
1010
use rustc_hir as hir;
1111
use rustc_index::IndexVec;
1212
use rustc_infer::infer::InferCtxt;
@@ -28,7 +28,7 @@ pub(super) fn build_control_flow_graph<'tcx>(
2828
consumed_borrowed_places: ConsumedAndBorrowedPlaces,
2929
body: &'tcx Body<'tcx>,
3030
num_exprs: usize,
31-
) -> (DropRangesBuilder, FxHashSet<HirId>) {
31+
) -> (DropRangesBuilder, UnordSet<HirId>) {
3232
let mut drop_range_visitor = DropRangeVisitor::new(
3333
infcx,
3434
typeck_results,
@@ -528,7 +528,7 @@ impl DropRangesBuilder {
528528
hir: Map<'_>,
529529
num_exprs: usize,
530530
) -> Self {
531-
let mut tracked_value_map = FxHashMap::<_, TrackedValueIndex>::default();
531+
let mut tracked_value_map = UnordMap::<_, TrackedValueIndex>::default();
532532
let mut next = <_>::from(0u32);
533533
for value in tracked_values {
534534
for_each_consumable(hir, value, |value| {

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use self::record_consumed_borrow::find_consumed_and_borrowed;
1717
use crate::FnCtxt;
1818
use hir::def_id::DefId;
1919
use hir::{Body, HirId, HirIdMap, Node};
20-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
20+
use rustc_data_structures::unord::{UnordMap, UnordSet};
2121
use rustc_hir as hir;
2222
use rustc_index::bit_set::BitSet;
2323
use rustc_index::IndexVec;
@@ -63,7 +63,7 @@ pub fn compute_drop_ranges<'a, 'tcx>(
6363
// If drop range tracking is not enabled, skip all the analysis and produce an
6464
// empty set of DropRanges.
6565
DropRanges {
66-
tracked_value_map: FxHashMap::default(),
66+
tracked_value_map: UnordMap::default(),
6767
nodes: IndexVec::new(),
6868
borrowed_temporaries: None,
6969
}
@@ -182,9 +182,9 @@ impl TryFrom<&PlaceWithHirId<'_>> for TrackedValue {
182182
}
183183

184184
pub struct DropRanges {
185-
tracked_value_map: FxHashMap<TrackedValue, TrackedValueIndex>,
185+
tracked_value_map: UnordMap<TrackedValue, TrackedValueIndex>,
186186
nodes: IndexVec<PostOrderId, NodeInfo>,
187-
borrowed_temporaries: Option<FxHashSet<HirId>>,
187+
borrowed_temporaries: Option<UnordSet<HirId>>,
188188
}
189189

190190
impl DropRanges {
@@ -227,7 +227,7 @@ struct DropRangesBuilder {
227227
/// (see NodeInfo::drop_state). The hir_id_map field stores the mapping
228228
/// from HirIds to the HirIdIndex that is used to represent that value in
229229
/// bitvector.
230-
tracked_value_map: FxHashMap<TrackedValue, TrackedValueIndex>,
230+
tracked_value_map: UnordMap<TrackedValue, TrackedValueIndex>,
231231

232232
/// When building the control flow graph, we don't always know the
233233
/// post-order index of the target node at the point we encounter it.

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/record_consumed_borrow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
FnCtxt,
55
};
66
use hir::{def_id::DefId, Body, HirId, HirIdMap};
7-
use rustc_data_structures::fx::FxHashSet;
7+
use rustc_data_structures::{fx::FxIndexSet, unord::UnordSet};
88
use rustc_hir as hir;
99
use rustc_middle::ty::{ParamEnv, TyCtxt};
1010
use rustc_middle::{
@@ -30,13 +30,13 @@ pub(super) struct ConsumedAndBorrowedPlaces {
3030
///
3131
/// Note that this set excludes "partial drops" -- for example, a statement like `drop(x.y)` is
3232
/// not considered a drop of `x`, although it would be a drop of `x.y`.
33-
pub(super) consumed: HirIdMap<FxHashSet<TrackedValue>>,
33+
pub(super) consumed: HirIdMap<FxIndexSet<TrackedValue>>,
3434

3535
/// A set of hir-ids of values or variables that are borrowed at some point within the body.
36-
pub(super) borrowed: FxHashSet<TrackedValue>,
36+
pub(super) borrowed: UnordSet<TrackedValue>,
3737

3838
/// A set of hir-ids of values or variables that are borrowed at some point within the body.
39-
pub(super) borrowed_temporaries: FxHashSet<HirId>,
39+
pub(super) borrowed_temporaries: UnordSet<HirId>,
4040
}
4141

4242
/// Works with ExprUseVisitor to find interesting values for the drop range analysis.

compiler/rustc_hir_typeck/src/inherited.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::callee::DeferredCallResolution;
22

3-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3+
use rustc_data_structures::unord::{UnordMap, UnordSet};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::LocalDefId;
66
use rustc_hir::HirIdMap;
@@ -61,9 +61,9 @@ pub struct Inherited<'tcx> {
6161
/// Whenever we introduce an adjustment from `!` into a type variable,
6262
/// we record that type variable here. This is later used to inform
6363
/// fallback. See the `fallback` module for details.
64-
pub(super) diverging_type_vars: RefCell<FxHashSet<Ty<'tcx>>>,
64+
pub(super) diverging_type_vars: RefCell<UnordSet<Ty<'tcx>>>,
6565

66-
pub(super) infer_var_info: RefCell<FxHashMap<ty::TyVid, ty::InferVarInfo>>,
66+
pub(super) infer_var_info: RefCell<UnordMap<ty::TyVid, ty::InferVarInfo>>,
6767
}
6868

6969
impl<'tcx> Deref for Inherited<'tcx> {

compiler/rustc_hir_typeck/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#![feature(min_specialization)]
77
#![feature(control_flow_enum)]
88
#![feature(option_as_slice)]
9-
#![allow(rustc::potential_query_instability)]
109
#![recursion_limit = "256"]
1110

1211
#[macro_use]

compiler/rustc_hir_typeck/src/method/suggest.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::errors::NoAssociatedItem;
77
use crate::Expectation;
88
use crate::FnCtxt;
99
use rustc_ast::ast::Mutability;
10-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
10+
use rustc_data_structures::fx::FxIndexMap;
11+
use rustc_data_structures::fx::FxIndexSet;
12+
use rustc_data_structures::unord::UnordSet;
1113
use rustc_errors::StashKey;
1214
use rustc_errors::{
1315
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
@@ -31,6 +33,7 @@ use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
3133
use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths};
3234
use rustc_middle::ty::IsSuggestable;
3335
use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeVisitableExt};
36+
use rustc_span::def_id::DefIdSet;
3437
use rustc_span::symbol::{kw, sym, Ident};
3538
use rustc_span::Symbol;
3639
use rustc_span::{edit_distance, source_map, ExpnKind, FileName, MacroKind, Span};
@@ -536,11 +539,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
536539
));
537540
}
538541
} else if !unsatisfied_predicates.is_empty() {
539-
let mut type_params = FxHashMap::default();
542+
let mut type_params = FxIndexMap::default();
540543

541544
// Pick out the list of unimplemented traits on the receiver.
542545
// This is used for custom error messages with the `#[rustc_on_unimplemented]` attribute.
543-
let mut unimplemented_traits = FxHashMap::default();
546+
let mut unimplemented_traits = FxIndexMap::default();
544547
let mut unimplemented_traits_only = true;
545548
for (predicate, _parent_pred, cause) in unsatisfied_predicates {
546549
if let (ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)), Some(cause)) =
@@ -606,7 +609,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
606609
);
607610
type_params
608611
.entry(key)
609-
.or_insert_with(FxHashSet::default)
612+
.or_insert_with(UnordSet::default)
610613
.insert(obligation.to_owned());
611614
return true;
612615
}
@@ -680,8 +683,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
680683
};
681684

682685
// Find all the requirements that come from a local `impl` block.
683-
let mut skip_list: FxHashSet<_> = Default::default();
684-
let mut spanned_predicates = FxHashMap::default();
686+
let mut skip_list: UnordSet<_> = Default::default();
687+
let mut spanned_predicates = FxIndexMap::default();
685688
for (p, parent_p, cause) in unsatisfied_predicates {
686689
// Extract the predicate span and parent def id of the cause,
687690
// if we have one.
@@ -723,7 +726,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
723726
let span = self_ty.span.ctxt().outer_expn_data().call_site;
724727
let entry = spanned_predicates.entry(span);
725728
let entry = entry.or_insert_with(|| {
726-
(FxHashSet::default(), FxHashSet::default(), Vec::new())
729+
(FxIndexSet::default(), FxIndexSet::default(), Vec::new())
727730
});
728731
entry.0.insert(span);
729732
entry.1.insert((
@@ -771,7 +774,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
771774
skip_list.insert(p);
772775
let entry = spanned_predicates.entry(self_ty.span);
773776
let entry = entry.or_insert_with(|| {
774-
(FxHashSet::default(), FxHashSet::default(), Vec::new())
777+
(FxIndexSet::default(), FxIndexSet::default(), Vec::new())
775778
});
776779
entry.2.push(p);
777780
if cause_span != *item_span {
@@ -806,7 +809,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
806809
skip_list.insert(p);
807810
let entry = spanned_predicates.entry(ident.span);
808811
let entry = entry.or_insert_with(|| {
809-
(FxHashSet::default(), FxHashSet::default(), Vec::new())
812+
(FxIndexSet::default(), FxIndexSet::default(), Vec::new())
810813
});
811814
entry.0.insert(cause_span);
812815
entry.1.insert((ident.span, ""));
@@ -840,7 +843,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
840843
unsatisfied_bounds = true;
841844
}
842845

843-
let mut suggested_bounds = FxHashSet::default();
846+
let mut suggested_bounds = UnordSet::default();
844847
// The requirements that didn't have an `impl` span to show.
845848
let mut bound_list = unsatisfied_predicates
846849
.iter()
@@ -889,8 +892,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
889892
for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
890893
restrict_type_params = true;
891894
// #74886: Sort here so that the output is always the same.
892-
let mut obligations = obligations.into_iter().collect::<Vec<_>>();
893-
obligations.sort();
895+
let obligations = obligations.to_sorted_stable_ord();
894896
err.span_suggestion_verbose(
895897
span,
896898
format!(
@@ -2053,7 +2055,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20532055
ty::Adt(def, _) => Some(def.did()),
20542056
_ => None,
20552057
})
2056-
.collect::<FxHashSet<_>>();
2058+
.collect::<FxIndexSet<_>>();
20572059
let mut spans: MultiSpan = def_ids
20582060
.iter()
20592061
.filter_map(|def_id| {
@@ -2669,7 +2671,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26692671
Nothing,
26702672
}
26712673
let ast_generics = hir.get_generics(id.owner.def_id).unwrap();
2672-
let trait_def_ids: FxHashSet<DefId> = ast_generics
2674+
let trait_def_ids: DefIdSet = ast_generics
26732675
.bounds_for_param(def_id)
26742676
.flat_map(|bp| bp.bounds.iter())
26752677
.filter_map(|bound| bound.trait_ref()?.trait_def_id())

0 commit comments

Comments
 (0)