Description
As part of #44660, we plan to support crate
as a visibility modifier equivalent to pub(crate)
. Given that pub(crate)
exists, this should be relatively straight-forward.
Visibilities are stored in the AST type Visibility
. We will want to extend the Crate
variant to include one additional field, indicating whether the sugar crate
or the expanded form pub(crate)
was used. We can add an enum like
enum CrateSugar {
/// User wrote `pub(crate)`
PubCrate,
/// User wrote `crate`
JustCrate,
}
and add this field to the Crate
variant, so that it looks like Crate(Span, CrateSugar)
. Actually, at the same time we can remove the Span
field, which appears to be unused, so it would just become Crate(CrateSugar)
. Doing this simultaneously is sorta bad but will save you some editing, since all existing uses look like Crate(_)
, and 99% of them want to ignore the CrateSugar
field anyway. =)
The one user that does NOT want to ignore CrateSugar
is the pretty printer. We just want to change this line to something like:
ast::Visibility::Crate(CrateSugar::PubCrate) => self.word_nbsp("pub(crate)"),
ast::Visibility::Crate(CrateSugar::JustCrate) => self.word_nbsp("crate"),
Naturally, we also will want to alter the parser itself, in particular the parse_visibility
function. Currently, if it doesn't see the word pub
, it just returns. We need to extend this to look for crate
and -- if it finds it -- to return the new visibility (with sugar JustCrate
). We also need to modify the existing pub(crate)
code to return PubCrate
for the sugar.
(Note: Please limit discussion on this issue strictly to implementation concerns relative to this particular change. Policy discussion as to whether or not to make this change -- or whether to make other changes to the module system -- belong in #44660.)