]> git.lizzy.rs Git - rust.git/commitdiff
add `is_trait(DefId)` helper to `TyCtxt`
authorNiko Matsakis <niko@alum.mit.edu>
Mon, 2 Jul 2018 14:34:19 +0000 (10:34 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Mon, 2 Jul 2018 14:38:37 +0000 (10:38 -0400)
Co-authored-by: Tyler Mandry <tmandry@gmail.com>
src/librustc/ty/util.rs

index 4e281231a4105ef258fbf451b68856e5e6967bef..f118d22c54d3fcff49c5f993f483b5d6e8a01c8c 100644 (file)
@@ -518,10 +518,25 @@ pub fn destructor_constraints(self, def: &'tcx ty::AdtDef)
         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 {