]> git.lizzy.rs Git - rust.git/commitdiff
resolve: Add public entrypoint `traits_in_module`
authorJoshua Nelson <jyn514@gmail.com>
Sat, 8 Aug 2020 17:37:44 +0000 (13:37 -0400)
committerJoshua Nelson <jyn514@gmail.com>
Sat, 22 Aug 2020 04:22:42 +0000 (00:22 -0400)
- Consider the implicit prelude as well

src/librustc_resolve/lib.rs

index b8fe6724f89c7e6284bf639f11cebe460195900d..fdf094fae1e437b20d1a8972fd66358a5b91f217 100644 (file)
@@ -3125,6 +3125,31 @@ fn extern_prelude_get(
         })
     }
 
+    pub fn traits_in_scope(&mut self, module_id: DefId) -> Vec<TraitCandidate> {
+        let module = self.get_module(module_id);
+        module.ensure_traits(self);
+        let traits = module.traits.borrow();
+        let to_candidate =
+            |this: &mut Self, &(trait_name, binding): &(Ident, &NameBinding<'_>)| TraitCandidate {
+                def_id: binding.res().def_id(),
+                import_ids: this.find_transitive_imports(&binding.kind, trait_name),
+            };
+
+        let mut candidates: Vec<_> =
+            traits.as_ref().unwrap().iter().map(|x| to_candidate(self, x)).collect();
+
+        if let Some(prelude) = self.prelude {
+            if !module.no_implicit_prelude {
+                prelude.ensure_traits(self);
+                candidates.extend(
+                    prelude.traits.borrow().as_ref().unwrap().iter().map(|x| to_candidate(self, x)),
+                );
+            }
+        }
+
+        candidates
+    }
+
     /// Rustdoc uses this to resolve things in a recoverable way. `ResolutionError<'a>`
     /// isn't something that can be returned because it can't be made to live that long,
     /// and also it's a private type. Fortunately rustdoc doesn't need to know the error,