Skip to content

Commit 1a50725

Browse files
committed
refactor is_param_bound
1 parent c9fcbda commit 1a50725

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,22 @@ pub struct WhereBoundPredicate<'hir> {
647647
pub bounds: GenericBounds<'hir>,
648648
}
649649

650+
impl WhereBoundPredicate<'hir> {
651+
/// Returns `true` if `param_def_id` matches the `bounded_ty` of this predicate.
652+
pub fn is_param_bound(&self, param_def_id: DefId) -> bool {
653+
let path = match self.bounded_ty.kind {
654+
TyKind::Path(QPath::Resolved(None, path)) => path,
655+
_ => return false,
656+
};
657+
match path.res {
658+
Res::Def(DefKind::TyParam, def_id) | Res::SelfTy(Some(def_id), None) => {
659+
def_id == param_def_id
660+
}
661+
_ => false,
662+
}
663+
}
664+
}
665+
650666
/// A lifetime predicate (e.g., `'a: 'b + 'c`).
651667
#[derive(Debug, HashStable_Generic)]
652668
pub struct WhereRegionPredicate<'hir> {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::infer::{self, InferCtxt, TyCtxtInferExt};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
1616
use rustc_hir as hir;
17-
use rustc_hir::def::{DefKind, Res};
1817
use rustc_hir::def_id::DefId;
1918
use rustc_hir::intravisit::Visitor;
2019
use rustc_hir::GenericParam;
@@ -2017,13 +2016,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
20172016
hir::WherePredicate::BoundPredicate(bp) => Some(bp),
20182017
_ => None,
20192018
})
2020-
.flat_map(|bp| match bp.bounded_ty.kind {
2021-
hir::TyKind::Path(hir::QPath::Resolved(
2022-
None,
2023-
&hir::Path { res: Res::Def(DefKind::TyParam, def_id), .. },
2024-
)) if def_id == param_def_id => bp.bounds,
2025-
_ => &[][..],
2026-
})
2019+
.filter(|bp| bp.is_param_bound(param_def_id))
2020+
.flat_map(|bp| bp.bounds)
20272021
.any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
20282022
if explicitly_sized {
20292023
return;

compiler/rustc_typeck/src/collect.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_data_structures::captures::Captures;
2828
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
2929
use rustc_errors::{struct_span_err, Applicability};
3030
use rustc_hir as hir;
31-
use rustc_hir::def::{CtorKind, DefKind, Res};
31+
use rustc_hir::def::{CtorKind, DefKind};
3232
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
3333
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
3434
use rustc_hir::weak_lang_items;
@@ -668,6 +668,7 @@ impl ItemCtxt<'tcx> {
668668
})
669669
.flat_map(|b| predicates_from_bound(self, ty, b));
670670

671+
let param_def_id = self.tcx.hir().local_def_id(param_id).to_def_id();
671672
let from_where_clauses = ast_generics
672673
.where_clause
673674
.predicates
@@ -677,7 +678,7 @@ impl ItemCtxt<'tcx> {
677678
_ => None,
678679
})
679680
.flat_map(|bp| {
680-
let bt = if is_param(self.tcx, bp.bounded_ty, param_id) {
681+
let bt = if bp.is_param_bound(param_def_id) {
681682
Some(ty)
682683
} else if !only_self_bounds.0 {
683684
Some(self.to_ty(bp.bounded_ty))
@@ -714,23 +715,6 @@ impl ItemCtxt<'tcx> {
714715
}
715716
}
716717

717-
/// Tests whether this is the AST for a reference to the type
718-
/// parameter with ID `param_id`. We use this so as to avoid running
719-
/// `ast_ty_to_ty`, because we want to avoid triggering an all-out
720-
/// conversion of the type to avoid inducing unnecessary cycles.
721-
fn is_param(tcx: TyCtxt<'_>, ast_ty: &hir::Ty<'_>, param_id: hir::HirId) -> bool {
722-
if let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = ast_ty.kind {
723-
match path.res {
724-
Res::SelfTy(Some(def_id), None) | Res::Def(DefKind::TyParam, def_id) => {
725-
def_id == tcx.hir().local_def_id(param_id).to_def_id()
726-
}
727-
_ => false,
728-
}
729-
} else {
730-
false
731-
}
732-
}
733-
734718
fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
735719
let it = tcx.hir().item(item_id);
736720
debug!("convert: item {} with id {}", it.ident, it.hir_id());

0 commit comments

Comments
 (0)