From 327093007add511cdcca4032dda13f7a342f29ea Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 2 Jul 2018 10:34:19 -0400 Subject: [PATCH] add `is_trait(DefId)` helper to `TyCtxt` Co-authored-by: Tyler Mandry --- src/librustc/ty/util.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 4e281231a410..f118d22c54d3 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -518,10 +518,25 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { result } + /// True if `def_id` refers to a closure (e.g., `|x| x * 2`). Note + /// that closures have a def-id, but the closure *expression* also + /// has a `HirId` that is located within the context where the + /// closure appears (and, sadly, a corresponding `NodeId`, since + /// those are not yet phased out). The parent of the closure's + /// def-id will also be the context where it appears. pub fn is_closure(self, def_id: DefId) -> bool { self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr } + /// True if `def_id` refers to a trait (e.g., `trait Foo { ... }`). + pub fn is_trait(self, def_id: DefId) -> bool { + if let DefPathData::Trait(_) = self.def_key(def_id).disambiguated_data.data { + true + } else { + false + } + } + /// True if this def-id refers to the implicit constructor for /// a tuple struct like `struct Foo(u32)`. pub fn is_struct_constructor(self, def_id: DefId) -> bool {