]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/privacy.rs
handle errors based on parse_sess
[rust.git] / src / librustc / middle / privacy.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A pass that checks to make sure private fields and methods aren't used
12 //! outside their scopes. This pass will also generate a set of exported items
13 //! which are available for use externally when compiled as a library.
14
15 use util::nodemap::{DefIdSet, FxHashMap};
16
17 use std::hash::Hash;
18 use std::fmt;
19 use syntax::ast::NodeId;
20
21 // Accessibility levels, sorted in ascending order
22 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
23 pub enum AccessLevel {
24     // Superset of Reachable used to mark impl Trait items.
25     ReachableFromImplTrait,
26     // Exported items + items participating in various kinds of public interfaces,
27     // but not directly nameable. For example, if function `fn f() -> T {...}` is
28     // public, then type `T` is reachable. Its values can be obtained by other crates
29     // even if the type itself is not nameable.
30     Reachable,
31     // Public items + items accessible to other crates with help of `pub use` re-exports
32     Exported,
33     // Items accessible to other crates directly, without help of re-exports
34     Public,
35 }
36
37 // Accessibility levels for reachable HIR nodes
38 #[derive(Clone)]
39 pub struct AccessLevels<Id = NodeId> {
40     pub map: FxHashMap<Id, AccessLevel>
41 }
42
43 impl<Id: Hash + Eq> AccessLevels<Id> {
44     pub fn is_reachable(&self, id: Id) -> bool {
45         self.map.get(&id) >= Some(&AccessLevel::Reachable)
46     }
47     pub fn is_exported(&self, id: Id) -> bool {
48         self.map.get(&id) >= Some(&AccessLevel::Exported)
49     }
50     pub fn is_public(&self, id: Id) -> bool {
51         self.map.get(&id) >= Some(&AccessLevel::Public)
52     }
53 }
54
55 impl<Id: Hash + Eq> Default for AccessLevels<Id> {
56     fn default() -> Self {
57         AccessLevels { map: Default::default() }
58     }
59 }
60
61 impl<Id: Hash + Eq + fmt::Debug> fmt::Debug for AccessLevels<Id> {
62     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63         fmt::Debug::fmt(&self.map, f)
64     }
65 }
66
67 /// A set containing all exported definitions from external crates.
68 /// The set does not contain any entries from local crates.
69 pub type ExternalExports = DefIdSet;