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

Separate DefIds for variants and their constructors #59382

Merged
merged 9 commits into from
Mar 25, 2019

Conversation

davidtwco
Copy link
Member

Part of #44109. Split off from #59376. See Zulip topic for previous discussion.

r? @petrochenkov

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 23, 2019
@bors

This comment has been minimized.

@petrochenkov
Copy link
Contributor

I reviewed the PR and had comments, but many of them required some experimentation, so I basically started tweaking the code on top of this PR.
I'll hopefully continue and finish making these changes tomorrow.

@petrochenkov petrochenkov changed the title RFC 2008: Refactoring Separated DefIds for variants and their constructors Mar 23, 2019
@petrochenkov petrochenkov changed the title Separated DefIds for variants and their constructors Separate DefIds for variants and their constructors Mar 23, 2019
@petrochenkov
Copy link
Contributor

petrochenkov commented Mar 23, 2019

Any uses of the is_struct/is_tuple/is_unit methods are suspicious (as you could notice, !is_struct was usually used as has_ctor).
(Exhaustive) matching should generally be used instead.
Some code in derive machinery still needs this, but that's because it's very legacy.

Getting from VariantDef to its AdtDef is an uncommon operation, so it's better to remove parent_did from it + the optional DefId introduced some complexity/confusion that wasn't useful in practice.

davidtwco and others added 2 commits March 24, 2019 12:10
This commit makes two changes - separating the `NodeId` that identifies
an enum variant from the `NodeId` that identifies the variant's
constructor; and no longer creating a `NodeId` for `Struct`-style enum
variants and structs.

Separation of the variant id and variant constructor id will allow the
rest of RFC 2008 to be implemented by lowering the visibility of the
variant's constructor without lowering the visbility of the variant
itself.

No longer creating a `NodeId` for `Struct`-style enum variants and
structs mostly simplifies logic as previously this `NodeId` wasn't used.
There were various cases where the `NodeId` wouldn't be used unless
there was an unit or tuple struct or enum variant but not all uses of
this `NodeId` had that condition, by removing this `NodeId`, this must
be explicitly dealt with. This change mostly applied cleanly, but there
were one or two cases in name resolution and one case in type check
where the existing logic required a id for `Struct`-style enum variants
and structs.
@petrochenkov petrochenkov force-pushed the rfc-2008-refactoring branch 2 times, most recently from 95cf143 to 3fe5b1d Compare March 24, 2019 14:21
@petrochenkov
Copy link
Contributor

I'll hopefully continue and finish making these changes tomorrow.

Done.

One remaining issue is that Node::Ctor should not contain CtorOf if possible.
CtorOf can be determined by looking at the node's parent in few places where it makes difference.
I tried to do that in petrochenkov@63edbb5, but it turned out that variant ctors and variants themselves are not always connected by the parent-child relation (variant is not a variant ctor's parent), and I didn't investigate how to fix that.

After the Node::Ctor is done, it will be better for CtorOf to live in def.rs together with CtorKind.
(And one more thing that annoys me unproportionally is that in Ctor(hir::CtorOf, DefId, CtorKind) the "secondary" thing (CtorOf) is placed before the "primary" thing (DefId), I'd personally change it to Ctor(DefId, CtorOf, CtorKind) or something.)

@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 Mar 24, 2019
This commit removes `CtorOf` from `Node::Ctor` as the parent of the
constructor can be determined by looking at the node's parent in the few
places where knowing this is necessary.
This commit moves the definition of `CtorOf` from `rustc::hir` to
`rustc::hir::def` and adds imports wherever it is used.
This commit moves the `DefId` field of `Def::Ctor` to be the first
field.
@davidtwco
Copy link
Member Author

Thanks for all the changes.

One remaining issue is that Node::Ctor should not contain CtorOf if possible.
CtorOf can be determined by looking at the node's parent in few places where it makes difference.
I tried to do that in petrochenkov/rust@63edbb5, but it turned out that variant ctors and variants themselves are not always connected by the parent-child relation (variant is not a variant ctor's parent), and I didn't investigate how to fix that.

I've fixed this. Your patch was pretty much there, but (the misleadingly named) get_parent function was returning a Node::Item as it finds the next parent that is an item, so I used get_parent_node and then it just worked.

After the Node::Ctor is done, it will be better for CtorOf to live in def.rs together with CtorKind.
(And one more thing that annoys me unproportionally is that in Ctor(hir::CtorOf, DefId, CtorKind) the "secondary" thing (CtorOf) is placed before the "primary" thing (DefId), I'd personally change it to Ctor(DefId, CtorOf, CtorKind) or something.)

Fixed both of these.

@davidtwco davidtwco 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 Mar 24, 2019
@petrochenkov
Copy link
Contributor

Thanks.
3.5 years in the making!
@bors r+

@bors
Copy link
Contributor

bors commented Mar 24, 2019

📌 Commit 23cae1d has been approved by petrochenkov

@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 Mar 24, 2019
@petrochenkov
Copy link
Contributor

@bors p=1
(The changes are somewhat conflict-prone and unblock other work.)

@bors
Copy link
Contributor

bors commented Mar 24, 2019

⌛ Testing commit 23cae1d with merge 3752b3d...

bors added a commit that referenced this pull request Mar 24, 2019
Separate `DefId`s for variants and their constructors

Part of #44109. Split off from #59376. See [Zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/rfc-2008/near/132663140) for previous discussion.

r? @petrochenkov
@bors
Copy link
Contributor

bors commented Mar 25, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: petrochenkov
Pushing 3752b3d to master...

flags: VariantFlags,
/// Recovered?
Copy link
Member

Choose a reason for hiding this comment

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

I think this is the most redundant doc-comment I have ever seen.^^ It explains literally nothing...

If you have a one-sentence description of what this field does, I can add it in #63346. Or maybe it is easier for you to make a PR yourself. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that this comment isn't particularly useful. This PR was a while ago, I assume I just wanted to add some comment on each field as I was changing the type - I'm afraid I don't know what the recovered field is used for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

5 participants