Skip to content

Refactor global paths - #38232

Merged
bors merged 2 commits into
rust-lang:masterfrom
jseyfried:refactor_global_paths
Dec 23, 2016
Merged

Refactor global paths#38232
bors merged 2 commits into
rust-lang:masterfrom
jseyfried:refactor_global_paths

Conversation

@jseyfried

@jseyfriedjseyfried commented Dec 8, 2016

Copy link
Copy Markdown
Contributor

This PR removes the field global: bool from ast::Path and hir::Path, instead representing a global path ::foo::bar as {{root}}::foo::bar, where {{root}} is a virtual keyword keywords::CrateRoot.

Also, fixes#38016.

r? @nrc

@jseyfried

jseyfried commented Dec 8, 2016

Copy link
Copy Markdown
ContributorAuthor

This is groundwork for hygiene 2.0, specifically hygienic global paths between crates.

Since we will need the CrateRoot ident's ctxt to hygienically resolve semantically global paths, use foo; must parse into use {{root}}::foo; (use foo; and use ::foo; now parse into the same AST).

Alternatively, we could instead support hygienic global paths between crates by adding a field ctxt: SyntaxContext to today's Path that we would mark in the Marker fold.

@jseyfried
jseyfriedforce-pushed the refactor_global_paths branch 2 times, most recently from 156a9b9 to 74b8d67CompareDecember 8, 2016 09:24
@jseyfried

Copy link
Copy Markdown
ContributorAuthor

cc @eddyb@petrochenkov

@jseyfried
jseyfriedforce-pushed the refactor_global_paths branch 3 times, most recently from bcd5a06 to 47eac3fCompareDecember 8, 2016 23:21
@petrochenkov

Copy link
Copy Markdown
Contributor

I wanted to review this PR, but forgot, sorry.

Is the intent to eliminate $crate and just use ::path in macros (2.0)?
I looked through the diff and this refactoring doesn't seem to be especially useful for that goal, neither it removes significant amount of code.
Does this affect results of -Z hir-stats noticeably?
Maybe Option<Marker> could be used instead (None for lexical paths and Some(..) for global)?

@jseyfriedjseyfried reopened this Dec 12, 2016
@jseyfried

jseyfried commented Dec 12, 2016

Copy link
Copy Markdown
ContributorAuthor

@petrochenkov

Is the intent to eliminate $crate and just use ::path in macros (2.0)?

Yeah, global paths should be hygienic by default for macros 2.0, so we won't need $crate.

this refactoring doesn't seem to be especially useful for that goal

This refactoring will allow us to use nearly the same logic to resolve {{root}} hygienically as we currently use to resolve $crate, which I think is nice aesthetically. That being said, it would also be simple to resolve hygienically if Path instead had a field SyntaxContext or Option<SyntaxContext>.

Does this affect results of -Z hir-stats noticeably?

I haven't measured it, but I don't expect this to have a significant effect on hir stats. I could instead leave hir paths unchanged and just refactor ast paths -- IIRC refactoring hir paths only caused fallout in the lowerer and pretty printer.

ast::Path is part of the largest variant in a couple of ast enums though, and this refactoring did slightly improve memory usage and performance during parsing and expansion. The perf improvement should be more pronounced after the "Optimize ast::PathSegment" commit from #38171 lands -- I'll measure more thoroughly then.

@bors

bors commented Dec 15, 2016

Copy link
Copy Markdown
Collaborator

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

@jseyfried
jseyfriedforce-pushed the refactor_global_paths branch 3 times, most recently from ae2f1a8 to 550b4e1CompareDecember 15, 2016 08:55
nrc
nrc approved these changes Dec 20, 2016

@nrcnrc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

r=me with the minor refactoring

Comment threadsrc/libsyntax/print/pprust.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I would like an is_global method on paths, rather than having checks like this (path.segments[0].identifier.name == keywords::CrateRoot.name()) scattered around the code. It will also make it easier for tools using the AST.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Good point -- will do.

Comment threadsrc/libsyntax/print/pprust.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps also for segments

@jseyfried
jseyfriedforce-pushed the refactor_global_paths branch 2 times, most recently from e9b84d0 to 0bd6eabCompareDecember 20, 2016 21:28
@bors

bors commented Dec 21, 2016

Copy link
Copy Markdown
Collaborator

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

@jseyfried

Copy link
Copy Markdown
ContributorAuthor

@nrc I added a commit to fix #38016 by pretty-printing $crate::path as ::path (as discussed on IRC).

@nrc

nrc commented Dec 21, 2016

Copy link
Copy Markdown
Member

r = me, but you have test failures now

@jseyfried

Copy link
Copy Markdown
ContributorAuthor

@bors r=nrc

@bors

bors commented Dec 22, 2016

Copy link
Copy Markdown
Collaborator

📌 Commit 8a1acb2 has been approved by nrc

@bors

bors commented Dec 22, 2016

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 8a1acb2 with merge b86cb75...

@bors

bors commented Dec 22, 2016

Copy link
Copy Markdown
Collaborator

💔 Test failed - auto-mac-32-opt

@jseyfried

Copy link
Copy Markdown
ContributorAuthor

@bors retry

@bors

bors commented Dec 23, 2016

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 8a1acb2 with merge 82611a0...

bors added a commit that referenced this pull request Dec 23, 2016
Refactor global paths
This PR removes the field `global: bool` from `ast::Path` and `hir::Path`, instead representing a global path `::foo::bar` as `{{root}}::foo::bar`, where `{{root}}` is a virtual keyword `keywords::CrateRoot`.
Also, fixes#38016.
r? @nrc
Sign up for freeto 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.

Eliminate $crate in --pretty=expanded

4 participants

@jseyfried@petrochenkov@bors@nrc