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
This commit is contained in:
commit
d4532903b0
@ -1,4 +1,5 @@
|
||||
use std::borrow::Borrow;
|
||||
use std::fmt::Debug;
|
||||
use std::iter::FromIterator;
|
||||
use std::slice::Iter;
|
||||
use std::vec::IntoIter;
|
||||
@ -12,7 +13,8 @@
|
||||
|
||||
impl<K, V> VecMap<K, V>
|
||||
where
|
||||
K: PartialEq,
|
||||
K: Debug + PartialEq,
|
||||
V: Debug,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
VecMap(Default::default())
|
||||
@ -37,14 +39,31 @@ pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
|
||||
self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1)
|
||||
}
|
||||
|
||||
/// Returns the value corresponding to the supplied predicate filter.
|
||||
/// Returns the any value corresponding to the supplied predicate filter.
|
||||
///
|
||||
/// The supplied predicate will be applied to each (key, value) pair and it will return a
|
||||
/// reference to the values where the predicate returns `true`.
|
||||
pub fn get_by(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
|
||||
pub fn any_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
|
||||
self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1)
|
||||
}
|
||||
|
||||
/// Returns the value corresponding to the supplied predicate filter. It crashes if there's
|
||||
/// more than one matching element.
|
||||
///
|
||||
/// The supplied predicate will be applied to each (key, value) pair and it will return a
|
||||
/// reference to the value where the predicate returns `true`.
|
||||
pub fn get_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
|
||||
let mut filter = self.0.iter().filter(|kv| predicate(kv));
|
||||
let (_, value) = filter.next()?;
|
||||
// This should return just one element, otherwise it's a bug
|
||||
assert!(
|
||||
filter.next().is_none(),
|
||||
"Collection {:?} should have just one matching element",
|
||||
self
|
||||
);
|
||||
Some(value)
|
||||
}
|
||||
|
||||
/// Returns `true` if the map contains a value for the specified key.
|
||||
///
|
||||
/// The key may be any borrowed form of the map's key type,
|
||||
@ -131,7 +150,7 @@ fn into_iter(self) -> Self::IntoIter {
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: PartialEq, V> Extend<(K, V)> for VecMap<K, V> {
|
||||
impl<K: PartialEq + Debug, V: Debug> Extend<(K, V)> for VecMap<K, V> {
|
||||
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
|
||||
for (k, v) in iter {
|
||||
self.insert(k, v);
|
||||
|
@ -364,7 +364,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
||||
let concrete_ty = tcx
|
||||
.mir_borrowck(owner.expect_local())
|
||||
.concrete_opaque_types
|
||||
.get_by(|(key, _)| key.def_id == def_id.to_def_id())
|
||||
.get_value_matching(|(key, _)| key.def_id == def_id.to_def_id())
|
||||
.map(|concrete_ty| *concrete_ty)
|
||||
.unwrap_or_else(|| {
|
||||
tcx.sess.delay_span_bug(
|
||||
@ -512,8 +512,15 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
|
||||
|
||||
struct ConstraintLocator<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
|
||||
/// def_id of the opaque type whose defining uses are being checked
|
||||
def_id: DefId,
|
||||
// (first found type span, actual type)
|
||||
|
||||
/// as we walk the defining uses, we are checking that all of them
|
||||
/// define the same hidden type. This variable is set to `Some`
|
||||
/// with the first type that we find, and then later types are
|
||||
/// checked against it (we also carry the span of that first
|
||||
/// type).
|
||||
found: Option<(Span, Ty<'tcx>)>,
|
||||
}
|
||||
|
||||
@ -531,7 +538,7 @@ fn check(&mut self, def_id: LocalDefId) {
|
||||
.tcx
|
||||
.typeck(def_id)
|
||||
.concrete_opaque_types
|
||||
.get_by(|(key, _)| key.def_id == self.def_id)
|
||||
.any_value_matching(|(key, _)| key.def_id == self.def_id)
|
||||
.is_none()
|
||||
{
|
||||
debug!("no constraints in typeck results");
|
||||
|
Loading…
Reference in New Issue
Block a user