Skip to content

Commit 3270930

Browse files
nikomatsakistmandry
andcommitted
add is_trait(DefId) helper to TyCtxt
Co-authored-by: Tyler Mandry <tmandry@gmail.com>
1 parent 7bbbed9 commit 3270930

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/librustc/ty/util.rs

+15
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,25 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
518518
result
519519
}
520520

521+
/// True if `def_id` refers to a closure (e.g., `|x| x * 2`). Note
522+
/// that closures have a def-id, but the closure *expression* also
523+
/// has a `HirId` that is located within the context where the
524+
/// closure appears (and, sadly, a corresponding `NodeId`, since
525+
/// those are not yet phased out). The parent of the closure's
526+
/// def-id will also be the context where it appears.
521527
pub fn is_closure(self, def_id: DefId) -> bool {
522528
self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr
523529
}
524530

531+
/// True if `def_id` refers to a trait (e.g., `trait Foo { ... }`).
532+
pub fn is_trait(self, def_id: DefId) -> bool {
533+
if let DefPathData::Trait(_) = self.def_key(def_id).disambiguated_data.data {
534+
true
535+
} else {
536+
false
537+
}
538+
}
539+
525540
/// True if this def-id refers to the implicit constructor for
526541
/// a tuple struct like `struct Foo(u32)`.
527542
pub fn is_struct_constructor(self, def_id: DefId) -> bool {

0 commit comments

Comments
 (0)