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

Support ::crate in paths #45771

Merged
merged 2 commits into from
Nov 21, 2017
Merged

Support ::crate in paths #45771

merged 2 commits into from
Nov 21, 2017

Conversation

petrochenkov
Copy link
Contributor

@petrochenkov petrochenkov commented Nov 4, 2017

cc #45477
Fixes #45229

@petrochenkov
Copy link
Contributor Author

r? @nikomatsakis

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(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 Nov 5, 2017
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

I left various requests around the tests, but my biggest concern is that crate::foo::bar seems to parse in contexts where I would not expect it to.

@@ -97,9 +97,12 @@ impl Path {
}

pub fn default_to_global(mut self) -> Path {
Copy link
Contributor

Choose a reason for hiding this comment

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

Pre-existing, but I think a comment on this function would be great, just showing the transformations it does (and maybe explaining the reason for its existence).

!self.is_union_item() {
!self.is_union_item() &&
!(self.token.is_keyword(keywords::Crate) &&
self.look_ahead(1, |t| t != &token::ModSep)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I feel like some comments would be nice here too. In fact, I personally am a bit confused about what's going on here. =) Lots of nested nots!

// Parse any path `foo::bar`. But be careful of various corner cases involving contextual keywords:
//
// - `union Foo { .. }`, a union item. This gets parsed below. Note that `union::foo` is a path.
// - Note that `crate::foo` *is* a path, but `crate` followed by any other token is not. (??)

I'm not entirely clear I guess on what is happening. =)

// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please collect the tests for this feature in some kind of directory named after the rfc? I would suggest {compile-fail,run-pass,ui}/rfc2126-crate-paths.

pub struct Z;
pub struct S1(crate (::m::Z)); // OK
pub struct S2(::crate ::m::Z); // OK
pub struct S3(crate ::m::Z); //~ ERROR undeclared type or module `crate`
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, this error seems to indicate that it is parsing a path crate::m::Z which then fails to resolve... is that correct? If so, that seems sort of wrong, right? Today at least you get an error, and if we were going to favor any interpretation here, I would expect us to treat crate as a visibility modifier (wasn't that the whole point of accepting ::crate only?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, crate ::m::Z is parsed as a single path, I explained this choice in #45477 (comment).
FWIW, it's also the most conservative solution (since crate::anything::else is an error).

wasn't that the whole point of accepting ::crate only?

I thought it was for teachability / symmetry with ::other_crate::x::y (#44660 (comment)).


pub fn g() {
use ::super::main; //~ ERROR global paths cannot start with `super`
use ::super::main; //~ ERROR unresolved import `super`
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the change to the error message here? Seems less good now.


#![feature(crate_in_paths)]

use crate::m::f;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also test this from another module besides the root module?

}

fn main() {
f();
Copy link
Contributor

Choose a reason for hiding this comment

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

Not that I think it's really needed, bu this test doesn't demonstrate that things resolved correctly in any way. Perhaps have f and g return values, and we can assert that you get the right thing back?

@kennytm kennytm 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 Nov 7, 2017
@shepmaster
Copy link
Member

Hi from triage @petrochenkov — looks like you've got some grade-A feedback; will you be able to act on it soon?

@petrochenkov
Copy link
Contributor Author

will you be able to act on it soon?

I'll address the comments tomorrow.

@petrochenkov
Copy link
Contributor Author

Updated.

@bors
Copy link
Contributor

bors commented Nov 12, 2017

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

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

Looks nice. Two more questions. =)

pub struct Z;
pub struct S1(crate (::m::Z)); // OK
pub struct S2(::crate ::m::Z); // OK
pub struct S3(crate ::m::Z); //~ ERROR `crate` can only be used in absolute paths
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still a bit surprised that this doesn't work out with crate being a visibility modifier. What is the reason for it not to parse that way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I answered this earlier (#45771 (comment)), but GitHub has hidden the message.


pub fn g() {
use ::super::main; //~ ERROR global paths cannot start with `super`
use ::super::main; //~ ERROR unresolved import `super`
//~^ NOTE global paths cannot start with `super`
Copy link
Contributor

Choose a reason for hiding this comment

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

The notes are definitely an improvement -- but "unresolved import super" still feels misleading to me. Among other things, super is a keyword and hence could not possibly be an import, right? Can we readily change from adding a note to altering the main message?

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.

@bors
Copy link
Contributor

bors commented Nov 20, 2017

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

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 20, 2017

@petrochenkov

Regarding the precedence of crate, I don't agree with your conclusion at the moment, but I'd rather not get stuck on it. Let's land the PR as is. I expect us to be revisiting various aspects of the syntax here in any case.

@nikomatsakis
Copy link
Contributor

r=me post rebase

@petrochenkov
Copy link
Contributor Author

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Nov 20, 2017

📌 Commit 90f5cfd has been approved by nikomatsakis

@petrochenkov
Copy link
Contributor Author

@nikomatsakis

I expect us to be revisiting various aspects of the syntax here in any case.

FWIW, I still hope this never ends up on stable.

@petrochenkov petrochenkov 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 20, 2017
@bors
Copy link
Contributor

bors commented Nov 21, 2017

⌛ Testing commit 90f5cfd with merge f28df20...

bors added a commit that referenced this pull request Nov 21, 2017
@bors
Copy link
Contributor

bors commented Nov 21, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing f28df20 to master...

@bors bors merged commit 90f5cfd into rust-lang:master Nov 21, 2017
bors added a commit that referenced this pull request Jan 7, 2018
Support `extern` in paths

Implement the primary alternative to #46613 + #45771, achieving the same effect without requiring changes to other imports.
Both need to be experimentally evaluated before making further progress.

The PR also adds docs for all these related features into the unstable book.

cc #44660
r? @nikomatsakis
@petrochenkov petrochenkov deleted the crate branch June 5, 2019 15:57
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.

6 participants