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

Fix span of visibility #47799

Merged
merged 6 commits into from
Feb 23, 2018
Merged

Conversation

topecongiro
Copy link
Contributor

This PR

  1. adds a closing parenthesis to the span of Visibility::Crate (e.g. pub(crate)). The current span only covers pub(crate.
  2. adds a span field to Visibility::Restricted. This span covers the entire visibility expression (e.g. pub (in self)). Currently all we can have is a span for Path.

This PR is motivated by the bug found in rustfmt (rust-lang/rustfmt#2398).

The first change is a strict improvement IMHO. The second change may not be desirable, as it adds a field which is currently not used by the compiler.

@rust-highfive
Copy link
Collaborator

r? @michaelwoerister

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

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 27, 2018
@petrochenkov
Copy link
Contributor

IIRC, the whole visibility span is used in couple of places, but it's retrieved from codemap when necessary rather than precomputed.
I'd rather add a span field to Visibility itself, but it's a larger refactoring.

r=me without the second commit.
@bors delegate+

@bors
Copy link
Contributor

bors commented Jan 27, 2018

✌️ @topecongiro can now approve this pull request

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 27, 2018
@topecongiro
Copy link
Contributor Author

@petrochenkov Thank you for your review!

If adding a span filed to Visibility itself is ok, I would rather do that in this PR. I will see if I can do it.

@topecongiro
Copy link
Contributor Author

Or, rather than adding a span field to Visibility, use Spanned? E.g.

pub type Visibility = Spanned<VisibilityKind>;

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum VisibilityKind {
    Public,
    Crate(CrateSugar),
    Restricted { path: P<Path>, id: NodeId },
    Inherited,
}

@petrochenkov
Copy link
Contributor

@topecongiro
The rule of thumb is that Spanned is used with enums because we cannot add fields to them directly and additional span field is used with structs, so Spanned would be okay in this case.

@@ -97,7 +97,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
]);
let mut items = vec![
f.cx.item_extern_crate(f.span, f.alloc),
f.cx.item_use_simple(f.span, Visibility::Inherited, super_path),
f.cx.item_use_simple(f.span, dummy_spanned(VisibilityKind::Inherited), super_path),
Copy link
Contributor

Choose a reason for hiding this comment

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

Dummy spans should not be used, unless completely unavoidable.
In most cases during expansion spans for generated code can be inherited from something. In this case item.span is used for the path and for the extern crate item, it can be used for visibility as well.

Copy link
Contributor

@petrochenkov petrochenkov Jan 29, 2018

Choose a reason for hiding this comment

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

Dummy spans should not be used, unless completely unavoidable.

It's probably not so dire for visibilities because they don't have hygiene, but it's still desirable to use some valid spans when possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@petrochenkov Thank you for your advice!

Is it acceptable to use 'empty' span like the following, rather than item.span? I think it makes easier to handle comments in rustfmt and reflects the syntactic structure of source code more accurately (as Inherited visibility has no keyword).

Span { hi: item.span.lo(), ..item.span }

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that would be even better.
(I was concerned about item.span.lo pointing to the location before item attributes, but it looks like this is not the case.)

@topecongiro
Copy link
Contributor Author

Updated and rebased against the master to resolve conflicts.

@petrochenkov
Copy link
Contributor

r=me after removing unintended submodule updates and fixing tidy.
@bors delegate+

@bors
Copy link
Contributor

bors commented Feb 4, 2018

✌️ @topecongiro can now approve this pull request

@topecongiro
Copy link
Contributor Author

@bors r+

@bors
Copy link
Contributor

bors commented Feb 4, 2018

📌 Commit 7d51391 has been approved by topecongiro

@topecongiro
Copy link
Contributor Author

@bors r-

Had to fix tidy.

@topecongiro topecongiro force-pushed the fix-span-of-visibility branch 2 times, most recently from 4bdf937 to 2e17f62 Compare February 5, 2018 03:59
@petrochenkov
Copy link
Contributor

Travis fails, unit tests in libsyntax need updating.

@pietroalbini
Copy link
Member

Hi @topecongiro, we haven't heard from you on this in a while! There are some broken tests (#47799 (comment)), could you update them so the PR can be merged?

@topecongiro
Copy link
Contributor Author

Updated broken tests. I am sorry that this took a long time to get fixed.

@pietroalbini pietroalbini added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 14, 2018
@bors bors added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 19, 2018
@petrochenkov
Copy link
Contributor

Timeout on i686-pc-windows-msvc again! Strange, but let's try again.
@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 19, 2018
@bors
Copy link
Contributor

bors commented Feb 19, 2018

⌛ Testing commit 8e9fa57 with merge b2642ba62d41570201a9d85e7f4df133590e5fed...

Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 20, 2018
@Manishearth Manishearth reopened this Feb 20, 2018
@Manishearth
Copy link
Member

@bors clean

(included in the rollup)

@bors
Copy link
Contributor

bors commented Feb 20, 2018

💥 Test timed out

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 20, 2018
@Manishearth
Copy link
Member

@bors r=petrochenkov retry clean

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 20, 2018
@bors
Copy link
Contributor

bors commented Feb 20, 2018

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Feb 20, 2018

📌 Commit 8e9fa57 has been approved by petrochenkov

@bors
Copy link
Contributor

bors commented Feb 23, 2018

⌛ Testing commit 8e9fa57 with merge 063deba...

bors added a commit that referenced this pull request Feb 23, 2018
…nkov

Fix span of visibility

This PR

1. adds a closing parenthesis to the span of `Visibility::Crate` (e.g. `pub(crate)`). The current span only covers `pub(crate`.
2. adds a `span` field to `Visibility::Restricted`. This span covers the entire visibility expression (e.g. `pub (in self)`). Currently all we can have is a span for `Path`.

This PR is motivated by the bug found in rustfmt (rust-lang/rustfmt#2398).

The first change is a strict improvement IMHO. The second change may not be desirable, as it adds a field which is currently not used by the compiler.
@bors
Copy link
Contributor

bors commented Feb 23, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing 063deba to master...

@bors bors merged commit 8e9fa57 into rust-lang:master Feb 23, 2018
kennytm-githubbot added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Feb 23, 2018
Tested on commit rust-lang/rust@063deba.

💔 clippy-driver on windows: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk).
💔 clippy-driver on linux: test-pass → build-fail (cc @Manishearth @llogiq @mcarton @oli-obk).
@topecongiro topecongiro deleted the fix-span-of-visibility branch February 23, 2018 14:19
@kennytm
Copy link
Member

kennytm commented Feb 23, 2018

Clippy is broken by this PR, cc @Manishearth @llogiq @mcarton @oli-obk.

[01:21:49]    Compiling clippy_lints v0.0.186 (file:///checkout/src/tools/clippy/clippy_lints)
[01:21:56] error[E0599]: no associated item named `Public` found for type `syntax::codemap::Spanned<syntax::ast::VisibilityKind>` in the current scope
[01:21:56]    --> tools/clippy/clippy_lints/src/enum_variants.rs:263:36
[01:21:56]     |
[01:21:56] 263 |                     if item.vis == Visibility::Public {
[01:21:56]     |                                    ^^^^^^^^^^^^^^^^^^ associated item not found in `syntax::codemap::Spanned<syntax::ast::VisibilityKind>`
[01:21:56] 
[01:21:56] error[E0599]: no associated item named `Public` found for type `syntax::codemap::Spanned<syntax::ast::VisibilityKind>` in the current scope
[01:21:56]    --> tools/clippy/clippy_lints/src/enum_variants.rs:288:17
[01:21:56]     |
[01:21:56] 288 |                 Visibility::Public => PUB_ENUM_VARIANT_NAMES,
[01:21:56]     |                 ^^^^^^^^^^^^^^^^^^ associated item not found in `syntax::codemap::Spanned<syntax::ast::VisibilityKind>`
[01:21:56] 
[01:21:56] error[E0599]: no associated item named `Public` found for type `syntax::codemap::Spanned<syntax::ast::VisibilityKind>` in the current scope
[01:21:56]    --> tools/clippy/clippy_lints/src/enum_variants.rs:263:36
[01:21:56]     |
[01:21:56] 263 |                     if item.vis == Visibility::Public {
[01:21:56]     |                                    ^^^^^^^^^^^^^^^^^^ associated item not found in `syntax::codemap::Spanned<syntax::ast::VisibilityKind>`
[01:21:56] 
[01:21:56] error[E0599]: no associated item named `Public` found for type `syntax::codemap::Spanned<syntax::ast::VisibilityKind>` in the current scope
[01:21:56]    --> tools/clippy/clippy_lints/src/enum_variants.rs:288:17
[01:21:56]     |
[01:21:56] 288 |                 Visibility::Public => PUB_ENUM_VARIANT_NAMES,
[01:21:56]     |                 ^^^^^^^^^^^^^^^^^^ associated item not found in `syntax::codemap::Spanned<syntax::ast::VisibilityKind>`
[01:21:56] 
[01:21:59] error: aborting due to 2 previous errors
[01:21:59] 
[01:21:59] error: Could not compile `clippy_lints`.
[01:21:59] warning: build failed, waiting for other jobs to finish...
[01:21:59] error: aborting due to 2 previous errors

zackmdavis added a commit to zackmdavis/rust that referenced this pull request Jul 1, 2018
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.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants