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

Add diagnostic for incorrect pub (restriction) #40627

Merged
merged 1 commit into from
Mar 23, 2017

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Mar 18, 2017

Given the following statement

pub (a) fn afn() {}

Provide the following diagnostic:

error: incorrect restriction in `pub`
  --> file.rs:15:1
   |
15 | pub (a) fn afn() {}
   |     ^^^
   |
   = help: some valid visibility restrictions are:
           `pub(crate)`: visible only on the current crate
           `pub(super)`: visible only in the current module's parent
           `pub(in path::to::module)`: visible only on the specified path
help: to make this visible only to module `a`, add `in` before the path:
   | pub (in a) fn afn() {}

Follow up to #40340, fix #40599, cc #32409.

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@estebank
Copy link
Contributor Author

estebank commented Mar 18, 2017

cc @pnkfelix @jonathandturner

@@ -5102,6 +5103,29 @@ impl<'a> Parser<'a> {
let vis = Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
self.expect(&token::CloseDelim(token::Paren))?; // `)`
return Ok(vis)
} else if self.look_ahead(0, |t| *t == token::Token::OpenDelim(token::Paren)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is always true, we have already checked for ( in the outer condition.

@petrochenkov
Copy link
Contributor

Looks like struct S(pub ()); and struct S(pub (u8)), valid item declarations, are not treated correctly.

@estebank estebank force-pushed the pub-restricted branch 3 times, most recently from 19fd6b3 to f682709 Compare March 19, 2017 20:20
@sophiajt
Copy link
Contributor

It's pretty chatty, but I admit it also looks very helpful. I'm good with it as a message.

@pnkfelix
Copy link
Member

regarding the chattiness, do we really need to keep these two lines:

    `pub`: visible on every module that imports this module
    ...
    `pub(self)` or ``: visible only in the current module

It seems like these cases are obvious enough, if one assumes that someone using parentheses after pub is looking for a non-trivial restriction, and removing them takes the help message from 6 lines to 4 lines...

@estebank
Copy link
Contributor Author

@pnkfelix that's a reasonable. I'll modify it tonight.

@nikomatsakis
Copy link
Contributor

If we're not going to make it an exhaustive listing, then I would change the heading text from "valid restrictions are" to "some example restrictions are" or something like that.

@nikomatsakis
Copy link
Contributor

The code looks good to me, modulo tweaking the message. I agree it's chatty but also that it seems helpful.

@@ -5066,18 +5066,25 @@ impl<'a> Parser<'a> {
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
let attrs = self.parse_outer_attributes()?;
let lo = self.span.lo;
let vis = self.parse_visibility()?;
let vis = self.parse_visibility(true)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is false too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that reminds me. Seems like we should have some tests exercising these paths (i.e., tests that would fail with this being true)...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and done.

@@ -5009,7 +5009,7 @@ impl<'a> Parser<'a> {
|p| {
let attrs = p.parse_outer_attributes()?;
let lo = p.span.lo;
let mut vis = p.parse_visibility()?;
let mut vis = p.parse_visibility(true)?;
let ty_is_interpolated =
p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
let mut ty = p.parse_ty()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not fully on-topic, but could you remove all this cruft remaining from the old visibility syntax, I've missed it somehow.
It should be "parse_visibility" -> "parse_ty" -> "return the result" now, no extra magic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@estebank
Copy link
Contributor Author

Current output:

@bors
Copy link
Contributor

bors commented Mar 22, 2017

☔ The latest upstream changes (presumably #40043) made this pull request unmergeable. Please resolve the merge conflicts.

@estebank estebank force-pushed the pub-restricted branch 2 times, most recently from 4d89d2e to c93ccb5 Compare March 22, 2017 05:49
@petrochenkov
Copy link
Contributor

LGTM, but couple of tests fail after rebase.

Given the following statement

```rust
pub (a) fn afn() {}
```

Provide the following diagnostic:

```rust
error: incorrect restriction in `pub`
  --> file.rs:15:1
   |
15 | pub (a) fn afn() {}
   | ^^^^^^^
   |
   = help: some valid visibility restrictions are:
           `pub(crate)`: visible only on the current crate
           `pub(super)`: visible only in the current module's parent
           `pub(in path::to::module)`: visible only on the specified path
help: to make this visible only to module `a`, add `in` before the path:
   | pub (in a) fn afn() {}
```

Remove cruft from old `pub(path)` syntax.
@estebank
Copy link
Contributor Author

@petrochenkov fixed.

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Mar 23, 2017

📌 Commit 769b95d has been approved by petrochenkov

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Mar 23, 2017
…nkov

Add diagnostic for incorrect `pub (restriction)`

Given the following statement

```rust
pub (a) fn afn() {}
```

Provide the following diagnostic:

```rust
error: incorrect restriction in `pub`
  --> file.rs:15:1
   |
15 | pub (a) fn afn() {}
   |     ^^^
   |
   = help: some valid visibility restrictions are:
           `pub(crate)`: visible only on the current crate
           `pub(super)`: visible only in the current module's parent
           `pub(in path::to::module)`: visible only on the specified path
help: to make this visible only to module `a`, add `in` before the path:
   | pub (in a) fn afn() {}
```

Follow up to rust-lang#40340, fix rust-lang#40599, cc rust-lang#32409.
bors added a commit that referenced this pull request Mar 23, 2017
Rollup of 5 pull requests

- Successful merges: #40612, #40627, #40668, #40715, #40753
- Failed merges:
@bors bors merged commit 769b95d into rust-lang:master Mar 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Subpar error message on pub (ident) fn foo
7 participants