From cfb310939b9098cf5fd211c2c176eb0b2b2498fa Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Tue, 4 Jul 2023 16:22:01 +0200 Subject: Enable potential_query_instability lint in rustc_hir_typeck. Fix linting errors by using FxIndex(Map|Set) and Unord(Map|Set) as appropriate. --- compiler/rustc_data_structures/src/unord.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_data_structures') diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs index 2b21815b687..ec779854f36 100644 --- a/compiler/rustc_data_structures/src/unord.rs +++ b/compiler/rustc_data_structures/src/unord.rs @@ -31,6 +31,7 @@ use crate::{ /// /// It's still possible to do the same thing with an `Fn` by using interior mutability, /// but the chance of doing it accidentally is reduced. +#[derive(Clone)] pub struct UnordItems>(I); impl> UnordItems { @@ -194,6 +195,11 @@ impl UnordSet { Self { inner: Default::default() } } + #[inline] + pub fn with_capacity(capacity: usize) -> Self { + Self { inner: FxHashSet::with_capacity_and_hasher(capacity, Default::default()) } + } + #[inline] pub fn len(&self) -> usize { self.inner.len() @@ -258,9 +264,9 @@ impl UnordSet { #[inline] pub fn to_sorted_stable_ord(&self) -> Vec where - V: Ord + StableOrd + Copy, + V: Ord + StableOrd + Clone, { - let mut items: Vec = self.inner.iter().copied().collect(); + let mut items: Vec = self.inner.iter().cloned().collect(); items.sort_unstable(); items } @@ -312,6 +318,12 @@ impl From> for UnordSet { } } +impl> From> for UnordSet { + fn from(value: UnordItems) -> Self { + UnordSet { inner: FxHashSet::from_iter(value.0) } + } +} + impl> HashStable for UnordSet { #[inline] fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { @@ -362,6 +374,11 @@ impl> From> fo } impl UnordMap { + #[inline] + pub fn with_capacity(capacity: usize) -> Self { + Self { inner: FxHashMap::with_capacity_and_hasher(capacity, Default::default()) } + } + #[inline] pub fn len(&self) -> usize { self.inner.len() -- cgit 1.4.1-3-g733a5 From 457b787a524e87a11d715ec26a24e68c8cf272d9 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Mon, 10 Jul 2023 13:02:52 +0200 Subject: Introduce ExtentUnord trait for collections that can safely consume UnordItems. --- compiler/rustc_data_structures/src/unord.rs | 52 +++++++++++++--------- compiler/rustc_hir_analysis/src/check_unused.rs | 2 +- compiler/rustc_hir_typeck/src/upvar.rs | 2 +- compiler/rustc_hir_typeck/src/writeback.rs | 3 +- compiler/rustc_middle/src/ty/typeck_results.rs | 4 +- compiler/rustc_mir_transform/src/check_unsafety.rs | 2 +- 6 files changed, 39 insertions(+), 26 deletions(-) (limited to 'compiler/rustc_data_structures') diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs index ec779854f36..47c56eba7ad 100644 --- a/compiler/rustc_data_structures/src/unord.rs +++ b/compiler/rustc_data_structures/src/unord.rs @@ -168,6 +168,14 @@ impl> UnordItems { } } +/// A marker trait specifying that `Self` can consume `UnordItems<_>` without +/// exposing any internal ordering. +/// +/// Note: right now this is just a marker trait. It could be extended to contain +/// some useful, common methods though, like `len`, `clear`, or the various +/// kinds of `to_sorted`. +trait UnordCollection {} + /// This is a set collection type that tries very hard to not expose /// any internal iteration. This is a useful property when trying to /// uphold the determinism invariants imposed by the query system. @@ -182,6 +190,8 @@ pub struct UnordSet { inner: FxHashSet, } +impl UnordCollection for UnordSet {} + impl Default for UnordSet { #[inline] fn default() -> Self { @@ -285,16 +295,28 @@ impl UnordSet { to_sorted_vec(hcx, self.inner.into_iter(), cache_sort_key, |x| x) } - // We can safely extend this UnordSet from a set of unordered values because that - // won't expose the internal ordering anywhere. #[inline] - pub fn extend_unord>(&mut self, items: UnordItems) { - self.inner.extend(items.0) + pub fn clear(&mut self) { + self.inner.clear(); } +} + +pub trait ExtendUnord { + /// Extend this unord collection with the given `UnordItems`. + /// This method is called `extend_unord` instead of just `extend` so it + /// does not conflict with `Extend::extend`. Otherwise there would be many + /// places where the two methods would have to be explicitly disambiguated + /// via UFCS. + fn extend_unord>(&mut self, items: UnordItems); +} +// Note: it is important that `C` implements `UnordCollection` in addition to +// `Extend`, otherwise this impl would leak the internal iteration order of +// `items`, e.g. when calling `some_vec.extend_unord(some_unord_items)`. +impl + UnordCollection, T> ExtendUnord for C { #[inline] - pub fn clear(&mut self) { - self.inner.clear(); + fn extend_unord>(&mut self, items: UnordItems) { + self.extend(items.0) } } @@ -345,6 +367,8 @@ pub struct UnordMap { inner: FxHashMap, } +impl UnordCollection for UnordMap {} + impl Default for UnordMap { #[inline] fn default() -> Self { @@ -445,13 +469,6 @@ impl UnordMap { UnordItems(self.inner.into_iter()) } - // We can safely extend this UnordMap from a set of unordered values because that - // won't expose the internal ordering anywhere. - #[inline] - pub fn extend>(&mut self, items: UnordItems<(K, V), I>) { - self.inner.extend(items.0) - } - /// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`). /// /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or @@ -571,15 +588,10 @@ impl UnordBag { pub fn into_items(self) -> UnordItems> { UnordItems(self.inner.into_iter()) } - - // We can safely extend this UnordSet from a set of unordered values because that - // won't expose the internal ordering anywhere. - #[inline] - pub fn extend>(&mut self, items: UnordItems) { - self.inner.extend(items.0) - } } +impl UnordCollection for UnordBag {} + impl Extend for UnordBag { fn extend>(&mut self, iter: I) { self.inner.extend(iter) diff --git a/compiler/rustc_hir_analysis/src/check_unused.rs b/compiler/rustc_hir_analysis/src/check_unused.rs index 3471d5f1e77..5318e637fc7 100644 --- a/compiler/rustc_hir_analysis/src/check_unused.rs +++ b/compiler/rustc_hir_analysis/src/check_unused.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::unord::UnordSet; +use rustc_data_structures::unord::{ExtendUnord, UnordSet}; use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::TyCtxt; diff --git a/compiler/rustc_hir_typeck/src/upvar.rs b/compiler/rustc_hir_typeck/src/upvar.rs index 14c073707e6..a3838275dc5 100644 --- a/compiler/rustc_hir_typeck/src/upvar.rs +++ b/compiler/rustc_hir_typeck/src/upvar.rs @@ -33,7 +33,7 @@ use super::FnCtxt; use crate::expr_use_visitor as euv; -use rustc_data_structures::unord::UnordSet; +use rustc_data_structures::unord::{ExtendUnord, UnordSet}; use rustc_errors::{Applicability, MultiSpan}; use rustc_hir as hir; use rustc_hir::def_id::LocalDefId; diff --git a/compiler/rustc_hir_typeck/src/writeback.rs b/compiler/rustc_hir_typeck/src/writeback.rs index eef2b835cbb..fab6090ff19 100644 --- a/compiler/rustc_hir_typeck/src/writeback.rs +++ b/compiler/rustc_hir_typeck/src/writeback.rs @@ -4,6 +4,7 @@ use crate::FnCtxt; use hir::def_id::LocalDefId; +use rustc_data_structures::unord::ExtendUnord; use rustc_errors::{ErrorGuaranteed, StashKey}; use rustc_hir as hir; use rustc_hir::intravisit::{self, Visitor}; @@ -526,7 +527,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { let fcx_typeck_results = self.fcx.typeck_results.borrow(); assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner); - self.typeck_results.user_provided_sigs.extend( + self.typeck_results.user_provided_sigs.extend_unord( fcx_typeck_results.user_provided_sigs.items().map(|(&def_id, c_sig)| { if cfg!(debug_assertions) && c_sig.has_infer() { span_bug!( diff --git a/compiler/rustc_middle/src/ty/typeck_results.rs b/compiler/rustc_middle/src/ty/typeck_results.rs index 3a63e28d97e..9c25c01b056 100644 --- a/compiler/rustc_middle/src/ty/typeck_results.rs +++ b/compiler/rustc_middle/src/ty/typeck_results.rs @@ -9,7 +9,7 @@ use crate::{ }; use rustc_data_structures::{ fx::FxIndexMap, - unord::{UnordItems, UnordSet}, + unord::{ExtendUnord, UnordItems, UnordSet}, }; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; @@ -635,7 +635,7 @@ impl<'a, V> LocalTableInContextMut<'a, V> { &mut self, items: UnordItems<(hir::HirId, V), impl Iterator>, ) { - self.data.extend(items.map(|(id, value)| { + self.data.extend_unord(items.map(|(id, value)| { validate_hir_id_for_typeck_results(self.hir_owner, id); (id.local_id, value) })) diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index 70812761e88..58e9786ec1a 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::unord::{UnordItems, UnordSet}; +use rustc_data_structures::unord::{ExtendUnord, UnordItems, UnordSet}; use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; -- cgit 1.4.1-3-g733a5