-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add nested groups in imports #45846
Conversation
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. |
I know some tests are failing right now, but rustc compiles itself successfully and the actual implementation of the feature is ready. |
ff1eb91
to
73011db
Compare
src/libsyntax/ast.rs
Outdated
} | ||
|
||
pub type ViewPath = Spanned<ViewPath_>; |
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 avoid Spanned
here and add the span: Span
field into ViewPath_
explicitly, and then rename ViewPath_
-> ViewPath
and ViewPathKind_
-> ViewPathKind
.
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.
It's also a good opportunity to finally rename ViewPath
(what does it even mean?) into something more reasonable, e.g. ImportTree
or UseTree
.
src/libsyntax/ast.rs
Outdated
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] | ||
pub struct ViewPath_ { | ||
pub kind: ViewPathKind_, | ||
pub path: Path, |
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.
path
-> prefix
src/libsyntax/ast.rs
Outdated
/// `foo::bar::{a,b,c}` | ||
ViewPathList(Path, Vec<PathListItem>) | ||
pub enum ViewPathKind_ { | ||
ViewPathSimple(Ident), |
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 use the "new-style" naming
enum ViewPathKind {
Simple(Ident),
Glob,
...
}
without pub use self::ViewPathKind_::*
.
0b88045
to
b67f99f
Compare
src/libsyntax/visit.rs
Outdated
visitor.visit_path(&use_tree.prefix, id); | ||
|
||
match use_tree.kind { | ||
UseTreeKind::Simple(ref ident) => { |
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: 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)?; |
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 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` |
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.
Argh, deleted comment!
src/libsyntax/parse/parser.rs
Outdated
loop { | ||
segments.push(self.parse_path_segment(style, enable_warning)?); | ||
|
||
if self.is_import_coupler() || !self.eat(&token::ModSep) { |
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 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.
☔ The latest upstream changes (presumably #45848) made this pull request unmergeable. Please resolve the merge conflicts. |
3c85490
to
b074edb
Compare
src/libsyntax/parse/parser.rs
Outdated
} else { | ||
let prefix = self.parse_path(PathStyle::Mod)?.default_to_global(); |
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.
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.
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? |
@shepmaster all the review comments were already fixed, but in other files so github didn't hide them ;) |
src/libsyntax/parse/parser.rs
Outdated
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)); |
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.
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.
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 may be the reason of ICE in the proc macro test, but I haven't checked.)
src/librustc_resolve/lib.rs
Outdated
span: prefix.span.to(use_tree.prefix.span), | ||
}; | ||
|
||
// Prefix the path with `::` if doesn't start with a crate root or segment keyword |
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 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 |
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.
default_to_global
was restored so this isn't necessary too.
☔ The latest upstream changes (presumably #45771) made this pull request unmergeable. Please resolve the merge conflicts. |
10ea27e
to
e46969b
Compare
@@ -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; |
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.
Looks like this commit duplicates 90f5cfd, but only for import paths.
📌 Commit cb213ef has been approved by |
⌛ Testing commit cb213ef5ba6f65e846091fa59c5523a8f37fcf36 with merge b48013cebad8ec4ebb4ce75b3db17096e24cf1d0... |
💔 Test failed - status-travis |
Of course, rustfmt is broken. It's also desirable to send a PR to rustfmt fixing the breakage. |
This commit adds support for nested groups inside `use` declarations, such as `use foo::{bar, sub::{baz::Foo, *}};`.
cb213ef
to
f7f6951
Compare
Ok, marked rustfmt and rls as broken. I'll look into sending a PR to rustfmt. @bors r=petrochenkov |
📌 Commit f7f6951 has been approved by |
Add nested groups in imports This PR adds support for nested groups in imports (rust-lang/rfcs#2128, tracking issue #44494). r? @petrochenkov
☀️ Test successful - status-appveyor, status-travis |
@pietroalbini thanks for working on this, I've been waiting for the feature. |
🎉 🎉 🎉 🎉 Thank you @eddyb, @petrochenkov and @nikomatsakis for helping me during the implementation! |
syntax: Make imports in AST closer to the source and cleanup their parsing This is a continuation of #45846 in some sense.
This PR adds support for nested groups in imports (rust-lang/rfcs#2128, tracking issue #44494).
r? @petrochenkov