Skip to content

Commit d453290

Browse files
authored
Rollup merge of #86410 - spastorino:get_value_matching, r=oli-obk
VecMap::get_value_matching should return just one element r? `@nikomatsakis` Related to #86465 and #87287
2 parents 0443424 + c79df85 commit d453290

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

compiler/rustc_data_structures/src/vec_map.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Borrow;
2+
use std::fmt::Debug;
23
use std::iter::FromIterator;
34
use std::slice::Iter;
45
use std::vec::IntoIter;
@@ -12,7 +13,8 @@ pub struct VecMap<K, V>(Vec<(K, V)>);
1213

1314
impl<K, V> VecMap<K, V>
1415
where
15-
K: PartialEq,
16+
K: Debug + PartialEq,
17+
V: Debug,
1618
{
1719
pub fn new() -> Self {
1820
VecMap(Default::default())
@@ -37,14 +39,31 @@ where
3739
self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1)
3840
}
3941

40-
/// Returns the value corresponding to the supplied predicate filter.
42+
/// Returns the any value corresponding to the supplied predicate filter.
4143
///
4244
/// The supplied predicate will be applied to each (key, value) pair and it will return a
4345
/// reference to the values where the predicate returns `true`.
44-
pub fn get_by(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
46+
pub fn any_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
4547
self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1)
4648
}
4749

50+
/// Returns the value corresponding to the supplied predicate filter. It crashes if there's
51+
/// more than one matching element.
52+
///
53+
/// The supplied predicate will be applied to each (key, value) pair and it will return a
54+
/// reference to the value where the predicate returns `true`.
55+
pub fn get_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
56+
let mut filter = self.0.iter().filter(|kv| predicate(kv));
57+
let (_, value) = filter.next()?;
58+
// This should return just one element, otherwise it's a bug
59+
assert!(
60+
filter.next().is_none(),
61+
"Collection {:?} should have just one matching element",
62+
self
63+
);
64+
Some(value)
65+
}
66+
4867
/// Returns `true` if the map contains a value for the specified key.
4968
///
5069
/// The key may be any borrowed form of the map's key type,
@@ -131,7 +150,7 @@ impl<K, V> IntoIterator for VecMap<K, V> {
131150
}
132151
}
133152

134-
impl<K: PartialEq, V> Extend<(K, V)> for VecMap<K, V> {
153+
impl<K: PartialEq + Debug, V: Debug> Extend<(K, V)> for VecMap<K, V> {
135154
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
136155
for (k, v) in iter {
137156
self.insert(k, v);

compiler/rustc_typeck/src/collect/type_of.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
364364
let concrete_ty = tcx
365365
.mir_borrowck(owner.expect_local())
366366
.concrete_opaque_types
367-
.get_by(|(key, _)| key.def_id == def_id.to_def_id())
367+
.get_value_matching(|(key, _)| key.def_id == def_id.to_def_id())
368368
.map(|concrete_ty| *concrete_ty)
369369
.unwrap_or_else(|| {
370370
tcx.sess.delay_span_bug(
@@ -512,8 +512,15 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
512512

513513
struct ConstraintLocator<'tcx> {
514514
tcx: TyCtxt<'tcx>,
515+
516+
/// def_id of the opaque type whose defining uses are being checked
515517
def_id: DefId,
516-
// (first found type span, actual type)
518+
519+
/// as we walk the defining uses, we are checking that all of them
520+
/// define the same hidden type. This variable is set to `Some`
521+
/// with the first type that we find, and then later types are
522+
/// checked against it (we also carry the span of that first
523+
/// type).
517524
found: Option<(Span, Ty<'tcx>)>,
518525
}
519526

@@ -531,7 +538,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
531538
.tcx
532539
.typeck(def_id)
533540
.concrete_opaque_types
534-
.get_by(|(key, _)| key.def_id == self.def_id)
541+
.any_value_matching(|(key, _)| key.def_id == self.def_id)
535542
.is_none()
536543
{
537544
debug!("no constraints in typeck results");

0 commit comments

Comments
 (0)