Skip to content

Separate DefIds for variants and their constructors - #59382

Merged
bors merged 9 commits into
rust-lang:masterfrom
davidtwco:rfc-2008-refactoring
Mar 25, 2019
Merged

Separate DefIds for variants and their constructors#59382
bors merged 9 commits into
rust-lang:masterfrom
davidtwco:rfc-2008-refactoring

Conversation

@davidtwco

Copy link
Copy Markdown
Member

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

r? @petrochenkov

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

This comment has been minimized.

@petrochenkov

Copy link
Copy Markdown
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.

@petrochenkovpetrochenkov changed the title RFC 2008: RefactoringSeparated DefIds for variants and their constructorsMar 23, 2019
@petrochenkovpetrochenkov changed the title Separated DefIds for variants and their constructorsSeparate DefIds for variants and their constructorsMar 23, 2019
@petrochenkov

petrochenkov commented Mar 23, 2019

Copy link
Copy Markdown
Contributor

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.

davidtwcoand 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
petrochenkovforce-pushed the rfc-2008-refactoring branch 2 times, most recently from 95cf143 to 3fe5b1dCompareMarch 24, 2019 14:21
@petrochenkov

Copy link
Copy Markdown
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.)

@petrochenkovpetrochenkov 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
Copy Markdown
MemberAuthor

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.

@davidtwcodavidtwco 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
Copy Markdown
Contributor

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

@bors

bors commented Mar 24, 2019

Copy link
Copy Markdown
Collaborator

📌 Commit 23cae1d has been approved by petrochenkov

@borsbors 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
Copy Markdown
Contributor

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

@bors

bors commented Mar 24, 2019

Copy link
Copy Markdown
Collaborator

⌛ 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

bors commented Mar 25, 2019

Copy link
Copy Markdown
Collaborator

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

pub ctor_kind: CtorKind,
/// Flags of the variant (e.g. is field list non-exhaustive)?
flags: VariantFlags,
/// Recovered?

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 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
Copy Markdown
MemberAuthor

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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.S-waiting-on-borsStatus: 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

@davidtwco@bors@petrochenkov@RalfJung@rust-highfive