Along the lines of issue #1893, I wanted to propose a simple, hierarchical system for access control.

Conceptually, each item is assigned an access level describable as an enum:

    enum access {
        pub, priv(module_path)
    }

A `pub` item is accessible from outside the crate. A `priv(M)` item is accessible by module M and its submodules from within the crate.

The notation would look like:

fn foo(...) { ... } // Default: private to the root path, i.e., crate-private
    pub fn foo(...) { ... } // Public
    priv fn foo(...) { ... } // Default: private to current module
    priv(::) fn foo(...) { ... } // Explicit: crate-private
priv(metadata) fn foo(...) { ... } // Private to the metadata package of current root

If you write `priv(M)` before an item, then M must resolve to the current module or some supermodule of the current module (otherwise, the module would be defining an item which it itself does not have access to...or maybe this is ok, actually).

The reason for this change is that it makes it relatively easy to refactor. An example where this has come up for me is that while writing the astencode module, I needed access to non-exported items from rustc::metadata::{encode, tyencode}. I would have liked a way to easily express "the items are used from other parts of the metadata module, but not from the rest of the compiler". It can be expressed today but requires significant refactoring.

In general, it'd be nice to be able to take a given module M and split it into two submodules M.M0 and M.M1 such that M.M0 and M.M1 can easily access each other's content without exporting it to the rest of the world. This is kind of annoying today.


Niko

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to