Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better visibility control #3505

Closed
jesse99 opened this issue Sep 15, 2012 · 11 comments
Closed

better visibility control #3505

jesse99 opened this issue Sep 15, 2012 · 11 comments
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) A-linkage Area: linking into static, shared libraries and binaries C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@jesse99
Copy link
Contributor

jesse99 commented Sep 15, 2012

It's very annoying that the only options for controlling visibility are public (to everyone) and private (to a module). C# has a nifty third option internal: (to an assembly).

If rust is moving towards using pub and priv to control visibility perhaps something like a crate keyword can be added so library authors have the freedom to structure their code as they like without being forced to make visible items which are properly of internal use.

@graydon
Copy link
Contributor

graydon commented Sep 19, 2012

We actually worked reasonably hard to make it possible to simulate this via pub and priv. The technique is to make sub-modules within your crate priv (which will, eventually, be the default for unqualified mods) and to then write some pub import directives in the outer crate-level module, to re-expose the portions you want to be visible-outside-the-crate. That's all we're going to do for now; if it proves awful in practice we can reopen this, but for now I'm closing it.

@graydon graydon closed this as completed Sep 19, 2012
@jesse99
Copy link
Contributor Author

jesse99 commented Sep 23, 2012

This has been a constant source of frustration for many months now (an example of how to structure a non-trivial library would be very helpful). The old model wasn't documented especially well and the new pub and priv stuff doesn't appear to be documented at all.

pub import doesn't compile with the latest rust and pub only seems to work with items so I can't make an identifier public in any case. I tried pub use foo::{bar}; which did compile but didn't seem to make bar visible from the module that did that.

@brson brson reopened this Sep 23, 2012
@brson
Copy link
Contributor

brson commented Sep 23, 2012

Reopening for @jesse99. I can certainly sympathize. Visibility and resolution rules have always been hazy, and even for 0.4 there's going to be a lot that isn't settled.

I will note that crate level visibility is similar to the issue that the rust test driver has calling private tests, and the test runner actually has a super-secret attribute to allow it to break visibility rules to do it.

@jesse99
Copy link
Contributor Author

jesse99 commented Oct 17, 2012

This seems to be working as of rust 0.4. However wasn't able to fully test it because of #3781.

@jesse99
Copy link
Contributor Author

jesse99 commented Nov 30, 2012

I've been able to use stuff like pub use blah::*; to control what is visible from the top level of my crate which is cool.

However I still don't know how to a write an item that is visible within a crate but not outside of it. I can create a private sub-module and the parent module can control what is exported from the sub-module but afaict it's exported to everyone.

Here's a motivating example. Fairly often I have some complex code with a very simple public API, often just a single function. I'd like to structure the code however I want. Maybe it's complex enough that I want to spread the implementation across multiple modules. Or maybe I want to split the code and unit tests into different modules. But as far as I know, I can't do any of this without making my implementation visible to everyone.

Perhaps I am missing something but that is highly unsatisfactory. I really don't want to make my implementation details visible to clients, but I also really don't want to be forced into writing gargantuan modules.

@graydon
Copy link
Contributor

graydon commented Dec 3, 2012

I apologize if I'm suggesting something you're already doing, but does this not do what you want?

pub mod proj {

    // This is the sole publicly-visible function: proj::entry_fn.
    pub use impl::entry_fn;

    mod impl {
        // All your implementation items are in here.
        pub use stuff::*;
        pub use things::*;
        pub mod stuff;
        pub mod things;

        pub fn entry_fn() { ... }
        pub fn other_fn1() { ... }
    }
}

@jesse99
Copy link
Contributor Author

jesse99 commented Dec 4, 2012

I don't think so. It comes down to logical versus physical organization. From a logical standpoint the above is fine. But it doesn't seem to address the physical organization of the code (i.e. the files).

@catamorphism
Copy link
Contributor

I'm not feeling like we're going to change this. Reopen if you have a concrete proposal for change.

@bblum
Copy link
Contributor

bblum commented Aug 9, 2013

Reopening. I would like the rule to be: "pub items in a non-pub mod are visible to the rest of the crate, but not visible to users outside the crate". This is necessary to finally seal the leakiness of the select trait abstraction, cf #5160.

Hmm well, it's not strictly necessary, as all of the stuff that refers to what I wish to keep private could just be concatenated into the same file. But having it that way would mandate having enormous files which could otherwise be split apart.

@bblum bblum reopened this Aug 9, 2013
@alexcrichton
Copy link
Member

@bblum, would the rules in #8215 be sufficient for your needs?

@bblum
Copy link
Contributor

bblum commented Aug 9, 2013

Yes, thank you.

@bblum bblum closed this as completed Aug 9, 2013
RalfJung pushed a commit to RalfJung/rust that referenced this issue Apr 25, 2024
CI: don't run cron-fail-notify when the job just got canceled

Doesn't seem right to prepare a PR in that case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) A-linkage Area: linking into static, shared libraries and binaries C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

6 participants