refactor is_param_bound

This commit is contained in:
Taylor Yu 2021-06-18 19:24:22 -05:00
parent c9fcbda389
commit 1a50725a4d
3 changed files with 21 additions and 27 deletions

View File

@ -647,6 +647,22 @@ pub struct WhereBoundPredicate<'hir> {
pub bounds: GenericBounds<'hir>, pub bounds: GenericBounds<'hir>,
} }
impl WhereBoundPredicate<'hir> {
/// Returns `true` if `param_def_id` matches the `bounded_ty` of this predicate.
pub fn is_param_bound(&self, param_def_id: DefId) -> bool {
let path = match self.bounded_ty.kind {
TyKind::Path(QPath::Resolved(None, path)) => path,
_ => return false,
};
match path.res {
Res::Def(DefKind::TyParam, def_id) | Res::SelfTy(Some(def_id), None) => {
def_id == param_def_id
}
_ => false,
}
}
}
/// A lifetime predicate (e.g., `'a: 'b + 'c`). /// A lifetime predicate (e.g., `'a: 'b + 'c`).
#[derive(Debug, HashStable_Generic)] #[derive(Debug, HashStable_Generic)]
pub struct WhereRegionPredicate<'hir> { pub struct WhereRegionPredicate<'hir> {

View File

@ -14,7 +14,6 @@ use crate::infer::{self, InferCtxt, TyCtxtInferExt};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorReported}; use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
use rustc_hir::GenericParam; use rustc_hir::GenericParam;
@ -2017,13 +2016,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
hir::WherePredicate::BoundPredicate(bp) => Some(bp), hir::WherePredicate::BoundPredicate(bp) => Some(bp),
_ => None, _ => None,
}) })
.flat_map(|bp| match bp.bounded_ty.kind { .filter(|bp| bp.is_param_bound(param_def_id))
hir::TyKind::Path(hir::QPath::Resolved( .flat_map(|bp| bp.bounds)
None,
&hir::Path { res: Res::Def(DefKind::TyParam, def_id), .. },
)) if def_id == param_def_id => bp.bounds,
_ => &[][..],
})
.any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait); .any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
if explicitly_sized { if explicitly_sized {
return; return;

View File

@ -28,7 +28,7 @@ use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_errors::{struct_span_err, Applicability}; use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res}; use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::weak_lang_items; use rustc_hir::weak_lang_items;
@ -668,6 +668,7 @@ impl ItemCtxt<'tcx> {
}) })
.flat_map(|b| predicates_from_bound(self, ty, b)); .flat_map(|b| predicates_from_bound(self, ty, b));
let param_def_id = self.tcx.hir().local_def_id(param_id).to_def_id();
let from_where_clauses = ast_generics let from_where_clauses = ast_generics
.where_clause .where_clause
.predicates .predicates
@ -677,7 +678,7 @@ impl ItemCtxt<'tcx> {
_ => None, _ => None,
}) })
.flat_map(|bp| { .flat_map(|bp| {
let bt = if is_param(self.tcx, bp.bounded_ty, param_id) { let bt = if bp.is_param_bound(param_def_id) {
Some(ty) Some(ty)
} else if !only_self_bounds.0 { } else if !only_self_bounds.0 {
Some(self.to_ty(bp.bounded_ty)) Some(self.to_ty(bp.bounded_ty))
@ -714,23 +715,6 @@ impl ItemCtxt<'tcx> {
} }
} }
/// Tests whether this is the AST for a reference to the type
/// parameter with ID `param_id`. We use this so as to avoid running
/// `ast_ty_to_ty`, because we want to avoid triggering an all-out
/// conversion of the type to avoid inducing unnecessary cycles.
fn is_param(tcx: TyCtxt<'_>, ast_ty: &hir::Ty<'_>, param_id: hir::HirId) -> bool {
if let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = ast_ty.kind {
match path.res {
Res::SelfTy(Some(def_id), None) | Res::Def(DefKind::TyParam, def_id) => {
def_id == tcx.hir().local_def_id(param_id).to_def_id()
}
_ => false,
}
} else {
false
}
}
fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) { fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
let it = tcx.hir().item(item_id); let it = tcx.hir().item(item_id);
debug!("convert: item {} with id {}", it.ident, it.hir_id()); debug!("convert: item {} with id {}", it.ident, it.hir_id());