Skip to content

Commit e2daccc

Browse files
committed
Add a convenience function
1 parent 84a43f0 commit e2daccc

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,7 @@ fn check_opaque_meets_bounds<'tcx>(
397397
) {
398398
let defining_use_anchor = match *origin {
399399
hir::OpaqueTyOrigin::FnReturn(did) | hir::OpaqueTyOrigin::AsyncFn(did) => did,
400-
hir::OpaqueTyOrigin::TyAlias { .. } => {
401-
let mut def_id = def_id;
402-
// Find the surrounding item (type alias or assoc type)
403-
while let DefKind::OpaqueTy = tcx.def_kind(def_id) {
404-
def_id = tcx.local_parent(def_id);
405-
}
406-
def_id
407-
}
400+
hir::OpaqueTyOrigin::TyAlias { .. } => tcx.impl_trait_parent(def_id),
408401
};
409402
let param_env = tcx.param_env(defining_use_anchor);
410403

compiler/rustc_middle/src/ty/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,18 @@ impl<'tcx> TyCtxt<'tcx> {
24762476
}
24772477
}
24782478

2479+
/// Returns the `DefId` of the item within which the `impl Trait` is declared.
2480+
/// For type-alias-impl-trait this is the `type` alias.
2481+
/// For impl-trait-in-assoc-type this is the assoc type.
2482+
/// For return-position-impl-trait this is the function.
2483+
pub fn impl_trait_parent(self, mut def_id: LocalDefId) -> LocalDefId {
2484+
// Find the surrounding item (type alias or assoc type)
2485+
while let DefKind::OpaqueTy = self.def_kind(def_id) {
2486+
def_id = self.local_parent(def_id);
2487+
}
2488+
def_id
2489+
}
2490+
24792491
pub fn impl_method_has_trait_impl_trait_tys(self, def_id: DefId) -> bool {
24802492
if self.def_kind(def_id) != DefKind::AssocFn {
24812493
return false;

0 commit comments

Comments
 (0)