Skip to content

Commit 1278f3f

Browse files
committed
Simplify code by using VecMap::get_by
1 parent dd56ec6 commit 1278f3f

File tree

2 files changed

+65
-73
lines changed
  • compiler

2 files changed

+65
-73
lines changed

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1291,9 +1291,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12911291
};
12921292

12931293
let concrete_ty = match concrete_opaque_types
1294-
.iter()
1295-
.find(|(key, _)| key.def_id == opaque_type_key.def_id)
1296-
.map(|(_, ty)| ty)
1294+
.get_by(|(key, _)| key.def_id == opaque_type_key.def_id)
12971295
{
12981296
None => {
12991297
if !concrete_is_opaque {

compiler/rustc_typeck/src/collect/type_of.rs

+64-70
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_data_structures::vec_map::VecMap;
32
use rustc_errors::{Applicability, ErrorReported, StashKey};
43
use rustc_hir as hir;
54
use rustc_hir::def::{DefKind, Res};
@@ -10,7 +9,7 @@ use rustc_hir::{HirId, Node};
109
use rustc_middle::hir::map::Map;
1110
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
1211
use rustc_middle::ty::util::IntTypeExt;
13-
use rustc_middle::ty::{self, DefIdTree, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
12+
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable};
1413
use rustc_span::symbol::Ident;
1514
use rustc_span::{Span, DUMMY_SP};
1615

@@ -347,36 +346,36 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
347346
}
348347
// Opaque types desugared from `impl Trait`.
349348
ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: Some(owner), .. }) => {
350-
let concrete_ty = find_concrete_ty_from_def_id(
351-
&tcx.mir_borrowck(owner.expect_local()).concrete_opaque_types,
352-
def_id.to_def_id(),
353-
)
354-
.map(|&(_, concrete_ty)| concrete_ty)
355-
.unwrap_or_else(|| {
356-
tcx.sess.delay_span_bug(
357-
DUMMY_SP,
358-
&format!(
359-
"owner {:?} has no opaque type for {:?} in its typeck results",
360-
owner, def_id,
361-
),
362-
);
363-
if let Some(ErrorReported) =
364-
tcx.typeck(owner.expect_local()).tainted_by_errors
365-
{
366-
// Some error in the
367-
// owner fn prevented us from populating
368-
// the `concrete_opaque_types` table.
369-
tcx.ty_error()
370-
} else {
371-
// We failed to resolve the opaque type or it
372-
// resolves to itself. Return the non-revealed
373-
// type, which should result in E0720.
374-
tcx.mk_opaque(
375-
def_id.to_def_id(),
376-
InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
377-
)
378-
}
379-
});
349+
let concrete_ty = tcx
350+
.mir_borrowck(owner.expect_local())
351+
.concrete_opaque_types
352+
.get_by(|(key, _)| key.def_id == def_id.to_def_id())
353+
.map(|concrete_ty| *concrete_ty)
354+
.unwrap_or_else(|| {
355+
tcx.sess.delay_span_bug(
356+
DUMMY_SP,
357+
&format!(
358+
"owner {:?} has no opaque type for {:?} in its typeck results",
359+
owner, def_id,
360+
),
361+
);
362+
if let Some(ErrorReported) =
363+
tcx.typeck(owner.expect_local()).tainted_by_errors
364+
{
365+
// Some error in the
366+
// owner fn prevented us from populating
367+
// the `concrete_opaque_types` table.
368+
tcx.ty_error()
369+
} else {
370+
// We failed to resolve the opaque type or it
371+
// resolves to itself. Return the non-revealed
372+
// type, which should result in E0720.
373+
tcx.mk_opaque(
374+
def_id.to_def_id(),
375+
InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
376+
)
377+
}
378+
});
380379
debug!("concrete_ty = {:?}", concrete_ty);
381380
concrete_ty
382381
}
@@ -516,11 +515,12 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
516515
}
517516
// Calling `mir_borrowck` can lead to cycle errors through
518517
// const-checking, avoid calling it if we don't have to.
519-
if find_concrete_ty_from_def_id(
520-
&self.tcx.typeck(def_id).concrete_opaque_types,
521-
self.def_id,
522-
)
523-
.is_none()
518+
if self
519+
.tcx
520+
.typeck(def_id)
521+
.concrete_opaque_types
522+
.get_by(|(key, _)| key.def_id == self.def_id)
523+
.is_none()
524524
{
525525
debug!(
526526
"find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`",
@@ -531,7 +531,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
531531
// Use borrowck to get the type with unerased regions.
532532
let concrete_opaque_types = &self.tcx.mir_borrowck(def_id).concrete_opaque_types;
533533
if let Some((opaque_type_key, concrete_type)) =
534-
find_concrete_ty_from_def_id(concrete_opaque_types, self.def_id)
534+
concrete_opaque_types.iter().find(|(key, _)| key.def_id == self.def_id)
535535
{
536536
debug!(
537537
"find_opaque_ty_constraints: found constraint for `{:?}` at `{:?}`: {:?}",
@@ -705,31 +705,32 @@ fn let_position_impl_trait_type(tcx: TyCtxt<'_>, opaque_ty_id: LocalDefId) -> Ty
705705
let opaque_ty_def_id = opaque_ty_id.to_def_id();
706706

707707
let owner_typeck_results = tcx.typeck(scope_def_id);
708-
let concrete_ty =
709-
find_concrete_ty_from_def_id(&owner_typeck_results.concrete_opaque_types, opaque_ty_def_id)
710-
.map(|&(_, concrete_ty)| concrete_ty)
711-
.unwrap_or_else(|| {
712-
tcx.sess.delay_span_bug(
713-
DUMMY_SP,
714-
&format!(
715-
"owner {:?} has no opaque type for {:?} in its typeck results",
716-
scope_def_id, opaque_ty_id
717-
),
718-
);
719-
if let Some(ErrorReported) = owner_typeck_results.tainted_by_errors {
720-
// Some error in the owner fn prevented us from populating the
721-
// `concrete_opaque_types` table.
722-
tcx.ty_error()
723-
} else {
724-
// We failed to resolve the opaque type or it resolves to
725-
// itself. Return the non-revealed type, which should result in
726-
// E0720.
727-
tcx.mk_opaque(
728-
opaque_ty_def_id,
729-
InternalSubsts::identity_for_item(tcx, opaque_ty_def_id),
730-
)
731-
}
732-
});
708+
let concrete_ty = owner_typeck_results
709+
.concrete_opaque_types
710+
.get_by(|(key, _)| key.def_id == opaque_ty_def_id)
711+
.map(|concrete_ty| *concrete_ty)
712+
.unwrap_or_else(|| {
713+
tcx.sess.delay_span_bug(
714+
DUMMY_SP,
715+
&format!(
716+
"owner {:?} has no opaque type for {:?} in its typeck results",
717+
scope_def_id, opaque_ty_id
718+
),
719+
);
720+
if let Some(ErrorReported) = owner_typeck_results.tainted_by_errors {
721+
// Some error in the owner fn prevented us from populating the
722+
// `concrete_opaque_types` table.
723+
tcx.ty_error()
724+
} else {
725+
// We failed to resolve the opaque type or it resolves to
726+
// itself. Return the non-revealed type, which should result in
727+
// E0720.
728+
tcx.mk_opaque(
729+
opaque_ty_def_id,
730+
InternalSubsts::identity_for_item(tcx, opaque_ty_def_id),
731+
)
732+
}
733+
});
733734
debug!("concrete_ty = {:?}", concrete_ty);
734735
if concrete_ty.has_erased_regions() {
735736
// FIXME(impl_trait_in_bindings) Handle this case.
@@ -803,10 +804,3 @@ fn check_feature_inherent_assoc_ty(tcx: TyCtxt<'_>, span: Span) {
803804
.emit();
804805
}
805806
}
806-
807-
fn find_concrete_ty_from_def_id<'tcx>(
808-
concrete_opaque_types: &'tcx VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
809-
def_id: DefId,
810-
) -> Option<&'tcx (OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
811-
concrete_opaque_types.iter().find(|(opaque_type_key, _)| opaque_type_key.def_id == def_id)
812-
}

0 commit comments

Comments
 (0)