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

Visibilities of "private" items from macros 1.2 #50130

Closed
petrochenkov opened this issue Apr 21, 2018 · 1 comment
Closed

Visibilities of "private" items from macros 1.2 #50130

petrochenkov opened this issue Apr 21, 2018 · 1 comment
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@petrochenkov
Copy link
Contributor

Procedural macros generate tokens, but sometimes the information is kept not in tokens, but rather in the lack of them.
Look at the struct S in the macro below, it doesn't have a pub annotation, so it's private and "private" is desugared into pub(self) where self is the current module, but "current module" depends on the location from which we resolve self.

mod m {
    pub macro mac() {
        struct S;
    }
}

m::mac!();

fn main() {}

If self is resolved from the macro def-site, then the current module is m, if self is resolved from the macro call-site, then the current module is crate root.

The question is what kind of self is implicitly generated for private items from macros.


Unfortunately, as far as I know, the only viable answer right now is self is the module in which the item will end up after all the macro expansions (i.e. "transitive call-site").

There are some alternatives potentially (#40847 (comment)), but if self is resolved from def site, then we'll get "visibilities can only be restricted to ancestor modules" errors and also conservative type privacy errors on every use of the generated struct.
These issues are probably resolvable, but they require nontrivial amount of design and implementation work and we can't delay stabilization of items-without-pub defined by macros until then.

@petrochenkov petrochenkov added the A-macros-2.0 Area: Declarative macros 2.0 (#39412) label Apr 21, 2018
@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 25, 2018
@petrochenkov
Copy link
Contributor Author

Macros 1.2 were stabilized with "implicit private" visibilities behaving as described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants