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 up`unreachable-pub` lint (as authorized by RFC 2126) #45569
Conversation
rust-highfive
assigned
petrochenkov
Oct 27, 2017
zackmdavis
referenced this pull request
Oct 27, 2017
Merged
regenerate libcore/char_private.rs #45571
kennytm
added
the
S-waiting-on-review
label
Oct 27, 2017
petrochenkov
reviewed
Oct 27, 2017
| pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, | ||
| name: &str) | ||
| -> DebugStruct<'a, 'b> { | ||
| pub(crate) fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 27, 2017
Contributor
I'd avoid changing libcore until the crate sugar for pub(crate) is available in bootstrap compiler.
petrochenkov
reviewed
Oct 27, 2017
|
|
||
| impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnexportedPub { | ||
| fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { | ||
| if !cx.access_levels.is_exported(item.id) && item.vis == hir::Visibility::Public { |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 27, 2017
Contributor
As I mentioned on gitter, this should use is_reachable instead of is_exported.
With is_exported this lint will give false positives on anything that is leaked through any other means than use (type aliases, return types, field types). All these things still need to be pub to be usable from other crates.
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 28, 2017
Contributor
Test to add:
mod m {
pub struct S; // The lint should not fire on this struct
}
pub fn f() -> m::S { m::S }
petrochenkov
reviewed
Oct 28, 2017
| pub struct UnexportedPub; | ||
|
|
||
| declare_lint! { | ||
| UNEXPORTED_PUB, |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 28, 2017
Contributor
I'd actually rename the lint into unreachable_pub (+ update all the comments, etc).
petrochenkov
reviewed
Oct 28, 2017
| if !cx.access_levels.is_exported(item.id) && item.vis == hir::Visibility::Public { | ||
| let mut err = cx.struct_span_lint(UNEXPORTED_PUB, item.span, "unexported `pub` item"); | ||
| err.help("consider exporting it for use by other crates"); | ||
| // `pub` is three chars at start of declaration |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 28, 2017
Contributor
This is a bold claim!
pub can be passed to an item through a $vis matcher, for example.
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 28, 2017
Contributor
Could you add a test for this case? Something like
mod m {
macro_rules! m {
($visibility: vis) => { $visibility struct S; }
}
m!(pub);
}
petrochenkov
reviewed
Oct 28, 2017
| fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { | ||
| if !cx.access_levels.is_exported(item.id) && item.vis == hir::Visibility::Public { | ||
| let mut err = cx.struct_span_lint(UNEXPORTED_PUB, item.span, "unexported `pub` item"); | ||
| err.help("consider exporting it for use by other crates"); |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 28, 2017
Contributor
Looking at the tests, "or consider exporting it for use by other crates" would look better.
petrochenkov
reviewed
Oct 28, 2017
| } | ||
|
|
||
| impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnexportedPub { | ||
| fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 28, 2017
Contributor
check_item doesn't cover pub on impl items, foreign items and struct fields.
petrochenkov
reviewed
Oct 28, 2017
| impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnexportedPub { | ||
| fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { | ||
| if !cx.access_levels.is_exported(item.id) && item.vis == hir::Visibility::Public { | ||
| let mut err = cx.struct_span_lint(UNEXPORTED_PUB, item.span, "unexported `pub` item"); |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 28, 2017
Contributor
def_span will probably look better here (see an example of use in #44847)
zackmdavis
force-pushed the
zackmdavis:unexported_pub_lint
branch
from
2e7a731
to
7edd04d
Oct 30, 2017
This comment has been minimized.
This comment has been minimized.
|
updated (force-push) |
zackmdavis
changed the title
`unexported-pub` lint (as authorized by RFC 2126)
`unreachable-pub` lint (as authorized by RFC 2126)
Oct 30, 2017
estebank
reviewed
Oct 31, 2017
|
Could you do a squash of your commits while you're at it? |
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| #![feature(crate_visibility_modifier)] |
This comment has been minimized.
This comment has been minimized.
estebank
Oct 31, 2017
Contributor
Could you add a test without this feature enabled to make sure there're no regressions going forward with the pub(crate) suggestion?
This comment has been minimized.
This comment has been minimized.
|
r=me with @estebank's comment (#45569 (comment)) addressed |
kennytm
added
S-waiting-on-author
and removed
S-waiting-on-review
labels
Nov 1, 2017
zackmdavis
force-pushed the
zackmdavis:unexported_pub_lint
branch
from
7edd04d
to
085ec6d
Nov 3, 2017
This comment has been minimized.
This comment has been minimized.
|
(added non- |
This comment has been minimized.
This comment has been minimized.
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Nov 3, 2017
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@bors retry |
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Nov 3, 2017
This comment has been minimized.
This comment has been minimized.
|
|
zackmdavis commentedOct 27, 2017
•
edited
To whom it may concern:
RFC 2126 commissions the creation of a lint for
pubitems that are not visible from crate root (#45521). We understand (but seek confirmation from more knowledgable compiler elders) that this can be implemented by linting HIR items that are notcx.access_levels.is_exportedcx.access_levels.is_reachablebut have avis(-ibility) field ofhir::Visibility::Public.The lint, tentatively called
unexported-pubunreachable-pub(with the understanding that much could be written on the merits of various names, as it is said of the colors of bicycle-sheds), suggestscrateas a replacement forpubif thecrate_visibility_modifierfeature is enabled (see #45388), andpub(crate)otherwise. We also use help messaging to suggest the other potential fix of exporting the item; feedback is desired as to whether this may be confusing or could be worded better.As a preview of what respecting the proposed lint would look like (and to generate confirmatory evidence that this implementation doesn't issue false positives),
we take its suggestions for(save one, which is deferred to another pull request because it brings up an unrelated technical matter). I remain your obedient servant.libcorer? @petrochenkov