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 nested groups in imports #45846

Merged
merged 2 commits into from
Dec 1, 2017
Merged

Conversation

pietroalbini
Copy link
Member

@pietroalbini pietroalbini commented Nov 7, 2017

This PR adds support for nested groups in imports (rust-lang/rfcs#2128, tracking issue #44494).

r? @petrochenkov

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@pietroalbini
Copy link
Member Author

I know some tests are failing right now, but rustc compiles itself successfully and the actual implementation of the feature is ready.

cc @nikomatsakis @eddyb

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 8, 2017
}

pub type ViewPath = Spanned<ViewPath_>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you avoid Spanned here and add the span: Span field into ViewPath_ explicitly, and then rename ViewPath_ -> ViewPath and ViewPathKind_ -> ViewPathKind.

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 also a good opportunity to finally rename ViewPath (what does it even mean?) into something more reasonable, e.g. ImportTree or UseTree.

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct ViewPath_ {
pub kind: ViewPathKind_,
pub path: Path,
Copy link
Contributor

Choose a reason for hiding this comment

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

path -> prefix

/// `foo::bar::{a,b,c}`
ViewPathList(Path, Vec<PathListItem>)
pub enum ViewPathKind_ {
ViewPathSimple(Ident),
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use the "new-style" naming

enum ViewPathKind {
    Simple(Ident),
    Glob,
    ...
}

without pub use self::ViewPathKind_::*.

visitor.visit_path(&use_tree.prefix, id);

match use_tree.kind {
UseTreeKind::Simple(ref ident) => {
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've seen this ref ident in few places, ref is not necessary, Ident is small and Copy.

self.bump();
Ok(P(respan(lo.to(self.span), ViewPathGlob(prefix))))
// `use path::...;`
let mut parsed = self.parse_path(PathStyle::Mod)?;
Copy link
Contributor

@petrochenkov petrochenkov Nov 12, 2017

Choose a reason for hiding this comment

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

This will accept use :: ::a::b; :)
(And also use :: $path::{a::b}.)

You really have to parse the prefix as a whole (including the starting ::) if it's a path, and treat "non-path" prefixes Ø and :: specially.

}
} else {
// `foo::bar` or `foo::bar as baz`
Copy link
Contributor

Choose a reason for hiding this comment

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

Argh, deleted comment!

loop {
segments.push(self.parse_path_segment(style, enable_warning)?);

if self.is_import_coupler() || !self.eat(&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.

Could you keep is_import_coupler alive and used here (even if it's used only here).
It will likely be useful again for fixing https://github.com/rust-lang/rust/pull/45846/files#r150397110.

@bors
Copy link
Contributor

bors commented Nov 12, 2017

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

@pietroalbini pietroalbini force-pushed the use-nested-groups branch 2 times, most recently from 3c85490 to b074edb Compare November 12, 2017 11:29
} else {
let prefix = self.parse_path(PathStyle::Mod)?.default_to_global();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably like to get rid from default_to_global too, but it's better left for a separate PR.
default_to_global is also used in visibilities and is_global relies on that starting CrateRoot and now returns inconsistent results, and is_global is used in too many places that may require adjustments. I don't recommend dealing with these issues in this PR.

For now, let's keep default_to_global for the outer use tree and avoid it for nested trees.

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

Ping from triage @pietroalbini — it's been a week since we last heard from you! Will you be able to address the issues with this PR soon?

@petrochenkov petrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 18, 2017
@pietroalbini
Copy link
Member Author

@shepmaster all the review comments were already fixed, but in other files so github didn't hide them ;)
I plan to continue working on the missing parts of the PR (feature flag and tests) in the next few days.

if self.eat(&token::ModSep) {
prefix.segments.push(PathSegment::crate_root(self.prev_span));
} else if !nested {
prefix.segments.push(PathSegment::crate_root(syntax_pos::DUMMY_SP));
Copy link
Contributor

Choose a reason for hiding this comment

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

DUMMY_SP should not be used in anything that goes through name resolution - macro hygiene is span-based.
Actual span needs to be used even if it's zero length.

Copy link
Contributor

@petrochenkov petrochenkov Nov 19, 2017

Choose a reason for hiding this comment

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

(This may be the reason of ICE in the proc macro test, but I haven't checked.)

span: prefix.span.to(use_tree.prefix.span),
};

// Prefix the path with `::` if doesn't start with a crate root or segment keyword
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be removed, because default_to_global was restored.

.map(|seg| respan(seg.span, seg.identifier))
.collect();

// Prefix the module path with `::` if doesn't start with a crate root or segment keyword
Copy link
Contributor

Choose a reason for hiding this comment

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

default_to_global was restored so this isn't necessary too.

@petrochenkov petrochenkov removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 19, 2017
@bors
Copy link
Contributor

bors commented Nov 21, 2017

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

@pietroalbini pietroalbini force-pushed the use-nested-groups branch 2 times, most recently from 10ea27e to e46969b Compare November 21, 2017 18:02
@@ -243,6 +243,22 @@ impl<'a> Resolver<'a> {
}

for &(ref tree, id) in items {
// Ensure use keywords or absolute imports aren't used in a nested list
let subprefix = &tree.prefix.segments;
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 commit duplicates 90f5cfd, but only for import paths.

@bors
Copy link
Contributor

bors commented Nov 29, 2017

📌 Commit cb213ef has been approved by petrochenkov

@kennytm kennytm 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 29, 2017
@bors
Copy link
Contributor

bors commented Nov 30, 2017

⌛ Testing commit cb213ef5ba6f65e846091fa59c5523a8f37fcf36 with merge b48013cebad8ec4ebb4ce75b3db17096e24cf1d0...

@bors
Copy link
Contributor

bors commented Nov 30, 2017

💔 Test failed - status-travis

@petrochenkov
Copy link
Contributor

Of course, rustfmt is broken.
This can be fixed by setting rustfmt and rls to "Broken" in https://github.com/rust-lang/rust/blob/master/src/tools/toolstate.toml

It's also desirable to send a PR to rustfmt fixing the breakage.

@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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 30, 2017
This commit adds support for nested groups inside `use` declarations,
such as `use foo::{bar, sub::{baz::Foo, *}};`.
@pietroalbini
Copy link
Member Author

Ok, marked rustfmt and rls as broken. I'll look into sending a PR to rustfmt.

@bors r=petrochenkov

@bors
Copy link
Contributor

bors commented Nov 30, 2017

📌 Commit f7f6951 has been approved by petrochenkov

@kennytm kennytm 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 30, 2017
@bors
Copy link
Contributor

bors commented Dec 1, 2017

⌛ Testing commit f7f6951 with merge 804b15b...

bors added a commit that referenced this pull request Dec 1, 2017
Add nested groups in imports

This PR adds support for nested groups in imports (rust-lang/rfcs#2128, tracking issue #44494).

r? @petrochenkov
@bors
Copy link
Contributor

bors commented Dec 1, 2017

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

@bors bors merged commit f7f6951 into rust-lang:master Dec 1, 2017
@theduke
Copy link
Contributor

theduke commented Dec 1, 2017

@pietroalbini thanks for working on this, I've been waiting for the feature.

@pietroalbini pietroalbini deleted the use-nested-groups branch December 1, 2017 08:46
@pietroalbini
Copy link
Member Author

🎉 🎉 🎉 🎉

Thank you @eddyb, @petrochenkov and @nikomatsakis for helping me during the implementation!

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.

8 participants