-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
add modifier keyword spans to hir::Visibility; improve unreachable-pub, private-no-mangle lint suggestions #51866
add modifier keyword spans to hir::Visibility; improve unreachable-pub, private-no-mangle lint suggestions #51866
Conversation
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
5e8b1c8
to
4b68507
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
src/librustc/hir/mod.rs
Outdated
Restricted { path: P<Path>, id: NodeId }, | ||
Public(Span), | ||
Crate(Span, CrateSugar), | ||
Restricted { span: Span, path: P<Path>, id: NodeId }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you mirror what #47799 did with ast::Visibility
instead?
src/test/ui/lint/suggestions.stderr
Outdated
@@ -89,12 +89,16 @@ warning: static is marked #[no_mangle], but not exported | |||
| | |||
LL | #[no_mangle] pub static DAUNTLESS: bool = true; | |||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |||
| | |||
= help: check that enclosing modules are public |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hardly a realistic advice, making a module pub
requires changing the whole crate structure.
More local fix is to export the fn/static from the crate with pub use
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm from my side, though petrochenkov's question about the validity of the export suggestion may need handling
src/librustc_lint/builtin.rs
Outdated
hir::Visibility::Public(_) => None | ||
}; | ||
if let Some((replace_span, replacement)) = suggestion { | ||
err.span_suggestion(replace_span, "try making it public", replacement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mark this MaybeIncorrect?
(such changes likely need review, hence it's not MachineApplicable)
src/librustc/hir/mod.rs
Outdated
Inherited, | ||
} | ||
|
||
impl Visibility { | ||
pub fn is_pub(&self) -> bool { | ||
use self::Visibility::*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Please avoid enum imports and use Enum::Variant
, this really improves code searchability in rustc.
☔ The latest upstream changes (presumably #51762) made this pull request unmergeable. Please resolve the merge conflicts. |
Visibility spans were added to the AST in rust-lang#47799 (d6bdf29) as a `Spanned<_>`—which means that we need to choose a span even in the case of inherited visibility (what you get when there's no `pub` &c. keyword at all). That initial implementation's choice is pretty counterintuitive, which could matter if we want to use it as a site to suggest inserting a visibility modifier, &c. (The phrase "Schelling span" in the comment is meant in analogy to the game-theoretic concept of a "Schelling point", a value that is chosen simply because it's what one can expect to agree upon with other agents in the absence of explicit coördination.)
There are at least a couple (and plausibly even three) diagnostics that could use the spans of visibility modifiers in order to be reliably correct (rather than hacking and munging surrounding spans to try to infer where the visibility keyword must have been). We follow the naming convention established by the other `Spanned` HIR nodes: the "outer" type alias gets the "prime" node-type name, the "inner" enum gets the name suffixed with an underscore, and the variant names are prefixed with the prime name and `pub use` exported from here (from HIR). Thanks to veteran reviewer Vadim Petrochenkov for suggesting this uniform approach. (A previous draft, based on the reasoning that `Visibility::Inherited` should not have a span, tried to hack in a named `span` field on `Visibility::Restricted` and a positional field on `Public` and `Crate`. This was ... not so uniform.)
This is a true fix for rust-lang#50455, superior to the mere bandage offered in rust-lang#50476.
4b68507
to
52835b5
Compare
This is probably quite a lot less likely to come up in practice than the "inherited" (no visibility keyword) case, but now that we have visibility spans in the HIR, we can do this, and it presumably doesn't hurt to be exhaustive. (Who can say but that the attention to detail just might knock someone's socks off, someday, somewhere?) This is inspired by rust-lang#47383.
If the item is `pub`, one imagines users being confused as to why it's not reachable/exported; a code suggestion is beyond our local knowledge here, but we can at least offer a prose hint. (Thanks to Vadim Petrochenkov for shooting down the present author's original bad idea for the note text.) While we're here, use proper HELP expectations instead of ad hoc comments to communicate (and now, enforce) the expected suggestions in test/ui/lint/suggestions.rs.
April 2016's Issue rust-lang#33174 called out the E0446 diagnostics as confusing. While adding the name of the restricted type to the message (548e681) clarified matters somewhat, Esteban Küber pointed out that we could stand to place a secondary span on the restricted type. Here, we differentiate between crate-visible, truly private, and otherwise restricted types, and place a secondary span specifically on the visibility modifier of the restricted type's declaration (which we can do now that HIR visibilities have spans!). At long last, this resolves rust-lang#33174.
52835b5
to
c2d44b2
Compare
src/librustc/hir/mod.rs
Outdated
VisibilityPublic, | ||
VisibilityCrate(CrateSugar), | ||
VisibilityRestricted { path: P<Path>, id: NodeId }, | ||
VisibilityInherited, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention enum Something_ { SomethingVariant }
is "deprecated" and exists in HIR only because not all HIR structures were updated to the "modern" form enum SomethingKind { Variant }
like it was done in AST.
New structures should not use it, so it'd be better to rename Visibility_
to VisibilityKind
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
understood; filed #51968 for the other HIR structures
r=me after renaming |
It was pointed out in review that the glob-exported underscore-suffixed convention for `Spanned` HIR nodes is no longer preferred: see February 2016's rust-lang#31487 for AST's migration away from this style towards properly namespaced NodeKind enums. This concerns rust-lang#51968.
@petrochenkov changed to |
@bors r=petrochenkov |
📌 Commit 43a0a65 has been approved by |
…petrochenkov add modifier keyword spans to hir::Visibility; improve unreachable-pub, private-no-mangle lint suggestions #50455 pointed out that the unreachable-pub suggestion for brace-grouped `use`s was bogus; #50476 partially ameliorated this by marking the suggestion as `Applicability::MaybeIncorrect`, but this is the actual fix. Meanwhile, another application of having spans available in `hir::Visibility` is found in the private-no-mangle lints, where we can now issue a suggestion to use `pub` if the item has a more restricted visibility marker (this seems much less likely to come up in practice than not having any visibility keyword at all, but thoroughness is a virtue). While we're there, we can also add a helpful note if the item does have a `pub` (but triggered the lint presumably because enclosing modules were private). ![hir_vis](https://user-images.githubusercontent.com/1076988/42018064-ca830290-7a65-11e8-9c4c-48bc846f861f.png) r? @nrc cc @Manishearth
☀️ Test successful - status-appveyor, status-travis |
#50455 pointed out that the unreachable-pub suggestion for brace-grouped
use
s was bogus; #50476 partially ameliorated this by marking the suggestion asApplicability::MaybeIncorrect
, but this is the actual fix.Meanwhile, another application of having spans available in
hir::Visibility
is found in the private-no-mangle lints, where we can now issue a suggestion to usepub
if the item has a more restricted visibility marker (this seems much less likely to come up in practice than not having any visibility keyword at all, but thoroughness is a virtue). While we're there, we can also add a helpful note if the item does have apub
(but triggered the lint presumably because enclosing modules were private).r? @nrc
cc @Manishearth