Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upresolve: Privacy rules for re-exports can be too restrictive #31783
Comments
This comment has been minimized.
This comment has been minimized.
|
Well, I have more interesting examples though, in which |
This comment has been minimized.
This comment has been minimized.
|
I agree that the practical impact is very small, but I do think it is inconsistent to be able to re-export a I think of I'm definitely interested in the examples -- not sure what properties means in this context. |
This comment has been minimized.
This comment has been minimized.
In this case
I think this is going to change when proper glob shadowing is implemented to support cases like
but there are some details, I'll elaborate later. |
This comment has been minimized.
This comment has been minimized.
Good point. To be fair though, it would be an unused import warning if a glob pattern didn't match anything. It actually might be a good idea to not allow an empty glob import -- I don't think anyone would do that intentionally.
mod foo {
pub fn f() {}
fn g() {}
}Right now |
This comment has been minimized.
This comment has been minimized.
|
There's also precedent here -- this compiles: mod foo {
mod bar { pub fn f() {} }
pub fn bar() {}
}
// This effectively imports `foo` in the value namespace. I say "effectively" because
// trying to use the imported name `foo` in the type namespace is a privacy error,
// not an unresolved name error.
use foo::bar;
fn main() {
bar();
// bar::f(); // If this line were uncommented, the import of `foo` would be a privacy error.
} |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
Theoretically, an import with Note: this currently compiles, but I'm not sure it should
it allows to observe one initial step of the bifurcation mentioned above
|
This comment has been minimized.
This comment has been minimized.
|
Interesting. I think that example should definitely not compile. |
This comment has been minimized.
This comment has been minimized.
This seems to be already done in globs - |
This comment has been minimized.
This comment has been minimized.
After some experimenting, I think there are only two basic scenarios, and there's a simple scheme covering both concrete imports
Isolated
If
Isolated
If This scheme can be straightforwardly extended to support Isolated
The visibilities |
This comment has been minimized.
This comment has been minimized.
|
Interesting, I like these semantics.
|
This comment has been minimized.
This comment has been minimized.
|
After #31726, it will be straightforward to implement the setup you describe (i.e. private imports are proper items, globs can import private items, unused ambiguous glob-imported names are OK, multiple imports of the same item are OK, items and named imports can shadow globs). The current plan seems to be for the RFC proposing these changes to also propose a macros 2.0 system with new hygiene rules and the ability to import and use macros like any other item (changes that I support). However, I think the new import semantics should get their own RFC before (or beside) macros 2.0 since the new import semantics are much easier to implement, backwards incompatible (theoretically -- hopefully not in practice), more immediately useful (imo), and already are partially implemented by accident. What do you think? |
This comment has been minimized.
This comment has been minimized.
Sure, if you are already working on it, then why wait for macros 2.0, @nrc can review the RFC and ensure it's compatible with the new macro system. |
petrochenkov
referenced
this issue
in jseyfried/rust
Feb 23, 2016
This comment has been minimized.
This comment has been minimized.
|
What I don't like in this #31783 (comment) scheme (and in the current scheme as well) is that resolution depends on privacy. I.e. to determine the import set and well-formedness for (1)
we need to resolve paths Can we come up with a scheme not using privacy information in any way in |
This comment has been minimized.
This comment has been minimized.
// If the type `root::bar` is `root::foo::bar -> root`, then `pub(root::bar)` is public and the glob is shadowed.
// If the type `root::bar` is `root::foo`, then `pub(root::bar)` is private to `foo` and the glob is not shadowed.
mod root {
mod glob_imported {
pub use root::foo as bar;
}
mod foo {
pub fn bar() {}
pub(root::bar) use root as bar;
}
use root::glob_imported::*;
use root::foo::bar;
}
// The same thing happens here, except that the bindings are not stable in either
// configuration instead of being stable in both configurations.
mod root {
mod glob_imported {
pub use root as bar;
}
mod foo {
pub fn bar() {}
pub(root::bar) use root::foo as bar;
}
use root::glob_imported::*;
use root::foo::bar;
} |
This comment has been minimized.
This comment has been minimized.
|
It would be doable to implement such a scheme, but it would add quite a bit of complexity. |
This comment has been minimized.
This comment has been minimized.
|
Maybe we could only allow It seems excessive to allow all paths since all we need to express is a De Bruijn index -- the paths |
This comment has been minimized.
This comment has been minimized.
|
I expected shadowing to precede privacy checks. But it still leaves "glob vs glob" conflicts. And they need to be resolved based on privacy, otherwise using two globs in one module could lead to unpredictable conflicts due to implementation details. Yeah, it seems like restricting paths in |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Thinking about this some more, I strongly prefer your semantics over my alternative. Under your semantics, the following would compile today, right? pub fn foo() {}
mod foo { pub fn f() {} }
mod bar {
pub use foo; // This publicly imports `foo` in the value namespace and privately imports `foo` in the type namespace.
fn g() { foo::f() }
}
fn main() {
bar::foo();
} |
This comment has been minimized.
This comment has been minimized.
Yep. Edit: And with |
jseyfried
referenced this issue
Apr 11, 2016
Merged
Implement `pub(restricted)` privacy (RFC 1422) #32875
bors
added a commit
that referenced
this issue
Apr 16, 2016
This comment has been minimized.
This comment has been minimized.
|
This was fixed by stabilizing RFC 1560. |
jseyfried commentedFeb 20, 2016
•
edited