Simplify code by using VecMap::get_by

This commit is contained in:
Santiago Pastorino 2021-06-08 09:41:26 -03:00
parent dd56ec653c
commit 1278f3f295
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
2 changed files with 65 additions and 73 deletions

View file

@ -1291,9 +1291,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}; };
let concrete_ty = match concrete_opaque_types let concrete_ty = match concrete_opaque_types
.iter() .get_by(|(key, _)| key.def_id == opaque_type_key.def_id)
.find(|(key, _)| key.def_id == opaque_type_key.def_id)
.map(|(_, ty)| ty)
{ {
None => { None => {
if !concrete_is_opaque { if !concrete_is_opaque {

View file

@ -1,5 +1,4 @@
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::vec_map::VecMap;
use rustc_errors::{Applicability, ErrorReported, StashKey}; use rustc_errors::{Applicability, ErrorReported, StashKey};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
@ -10,7 +9,7 @@ use rustc_hir::{HirId, Node};
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts}; use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
use rustc_middle::ty::util::IntTypeExt; use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{self, DefIdTree, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable}; use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable};
use rustc_span::symbol::Ident; use rustc_span::symbol::Ident;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
@ -347,36 +346,36 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
} }
// Opaque types desugared from `impl Trait`. // Opaque types desugared from `impl Trait`.
ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: Some(owner), .. }) => { ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: Some(owner), .. }) => {
let concrete_ty = find_concrete_ty_from_def_id( let concrete_ty = tcx
&tcx.mir_borrowck(owner.expect_local()).concrete_opaque_types, .mir_borrowck(owner.expect_local())
def_id.to_def_id(), .concrete_opaque_types
) .get_by(|(key, _)| key.def_id == def_id.to_def_id())
.map(|&(_, concrete_ty)| concrete_ty) .map(|concrete_ty| *concrete_ty)
.unwrap_or_else(|| { .unwrap_or_else(|| {
tcx.sess.delay_span_bug( tcx.sess.delay_span_bug(
DUMMY_SP, DUMMY_SP,
&format!( &format!(
"owner {:?} has no opaque type for {:?} in its typeck results", "owner {:?} has no opaque type for {:?} in its typeck results",
owner, def_id, owner, def_id,
), ),
); );
if let Some(ErrorReported) = if let Some(ErrorReported) =
tcx.typeck(owner.expect_local()).tainted_by_errors tcx.typeck(owner.expect_local()).tainted_by_errors
{ {
// Some error in the // Some error in the
// owner fn prevented us from populating // owner fn prevented us from populating
// the `concrete_opaque_types` table. // the `concrete_opaque_types` table.
tcx.ty_error() tcx.ty_error()
} else { } else {
// We failed to resolve the opaque type or it // We failed to resolve the opaque type or it
// resolves to itself. Return the non-revealed // resolves to itself. Return the non-revealed
// type, which should result in E0720. // type, which should result in E0720.
tcx.mk_opaque( tcx.mk_opaque(
def_id.to_def_id(), def_id.to_def_id(),
InternalSubsts::identity_for_item(tcx, def_id.to_def_id()), InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
) )
} }
}); });
debug!("concrete_ty = {:?}", concrete_ty); debug!("concrete_ty = {:?}", concrete_ty);
concrete_ty concrete_ty
} }
@ -516,11 +515,12 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
} }
// Calling `mir_borrowck` can lead to cycle errors through // Calling `mir_borrowck` can lead to cycle errors through
// const-checking, avoid calling it if we don't have to. // const-checking, avoid calling it if we don't have to.
if find_concrete_ty_from_def_id( if self
&self.tcx.typeck(def_id).concrete_opaque_types, .tcx
self.def_id, .typeck(def_id)
) .concrete_opaque_types
.is_none() .get_by(|(key, _)| key.def_id == self.def_id)
.is_none()
{ {
debug!( debug!(
"find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`", "find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`",
@ -531,7 +531,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
// Use borrowck to get the type with unerased regions. // Use borrowck to get the type with unerased regions.
let concrete_opaque_types = &self.tcx.mir_borrowck(def_id).concrete_opaque_types; let concrete_opaque_types = &self.tcx.mir_borrowck(def_id).concrete_opaque_types;
if let Some((opaque_type_key, concrete_type)) = if let Some((opaque_type_key, concrete_type)) =
find_concrete_ty_from_def_id(concrete_opaque_types, self.def_id) concrete_opaque_types.iter().find(|(key, _)| key.def_id == self.def_id)
{ {
debug!( debug!(
"find_opaque_ty_constraints: found constraint for `{:?}` at `{:?}`: {:?}", "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
let opaque_ty_def_id = opaque_ty_id.to_def_id(); let opaque_ty_def_id = opaque_ty_id.to_def_id();
let owner_typeck_results = tcx.typeck(scope_def_id); let owner_typeck_results = tcx.typeck(scope_def_id);
let concrete_ty = let concrete_ty = owner_typeck_results
find_concrete_ty_from_def_id(&owner_typeck_results.concrete_opaque_types, opaque_ty_def_id) .concrete_opaque_types
.map(|&(_, concrete_ty)| concrete_ty) .get_by(|(key, _)| key.def_id == opaque_ty_def_id)
.unwrap_or_else(|| { .map(|concrete_ty| *concrete_ty)
tcx.sess.delay_span_bug( .unwrap_or_else(|| {
DUMMY_SP, tcx.sess.delay_span_bug(
&format!( DUMMY_SP,
"owner {:?} has no opaque type for {:?} in its typeck results", &format!(
scope_def_id, opaque_ty_id "owner {:?} has no opaque type for {:?} in its typeck results",
), scope_def_id, opaque_ty_id
); ),
if let Some(ErrorReported) = owner_typeck_results.tainted_by_errors { );
// Some error in the owner fn prevented us from populating the if let Some(ErrorReported) = owner_typeck_results.tainted_by_errors {
// `concrete_opaque_types` table. // Some error in the owner fn prevented us from populating the
tcx.ty_error() // `concrete_opaque_types` table.
} else { tcx.ty_error()
// We failed to resolve the opaque type or it resolves to } else {
// itself. Return the non-revealed type, which should result in // We failed to resolve the opaque type or it resolves to
// E0720. // itself. Return the non-revealed type, which should result in
tcx.mk_opaque( // E0720.
opaque_ty_def_id, tcx.mk_opaque(
InternalSubsts::identity_for_item(tcx, opaque_ty_def_id), opaque_ty_def_id,
) InternalSubsts::identity_for_item(tcx, opaque_ty_def_id),
} )
}); }
});
debug!("concrete_ty = {:?}", concrete_ty); debug!("concrete_ty = {:?}", concrete_ty);
if concrete_ty.has_erased_regions() { if concrete_ty.has_erased_regions() {
// FIXME(impl_trait_in_bindings) Handle this case. // FIXME(impl_trait_in_bindings) Handle this case.
@ -803,10 +804,3 @@ fn check_feature_inherent_assoc_ty(tcx: TyCtxt<'_>, span: Span) {
.emit(); .emit();
} }
} }
fn find_concrete_ty_from_def_id<'tcx>(
concrete_opaque_types: &'tcx VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
def_id: DefId,
) -> Option<&'tcx (OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
concrete_opaque_types.iter().find(|(opaque_type_key, _)| opaque_type_key.def_id == def_id)
}