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

Lower struct patterns and struct expressions with unnamed fields #121553

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

frank-king
Copy link
Contributor

@frank-king frank-king commented Feb 24, 2024

This implements #49804.

Goals:

  • pattern matching with unnamed fields
    • check field conflict/absent errors
    • lower to THIR
  • struct expressions with unnamed fields
    • check field conflict/absent errors
    • lower to THIR

Non-Goals (will be in the next PRs)

  • capturing generic params for the anonymous ADTs from the parent ADT
  • rustdoc support of unnamed fields and anonymous ADTs

r? @davidtwco

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 24, 2024
@frank-king
Copy link
Contributor Author

@rustbot author

@rustbot rustbot 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 Feb 24, 2024
@frank-king
Copy link
Contributor Author

@rustbot label F-unnamed_fields

@clubby789
Copy link
Contributor

cc #121757, which I believe isn't currently fixed by these changes

@davidtwco
Copy link
Member

cc #121757, which I believe isn't currently fixed by these changes

I've submitted a fix for this in #121975

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Some first pass review comments

&'tcx ty::FieldDef,
// The inner-most field type
Ty<'tcx>,
)> {
Copy link
Member

Choose a reason for hiding this comment

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

This should be a type.

match mentioned {
Some((idx, _need_recheck @ true)) => {
// Check the only mentioned field of the union again,
// really collecting the unamed fields.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// really collecting the unamed fields.
// really collecting the unnamed fields.


error: union patterns should have exactly one field
--> $DIR/union-fields-2.rs:17:9
|
LL | let U { a, b } = u;
| ^^^^^^^^^^
|
note: there are multiple fields from this union
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
note: there are multiple fields from this union
note: multiple fields from this union are matched but the union can only be one at a time

Something a bit more verbose like this might be better.

@@ -50,6 +50,11 @@ pub struct TypeckResults<'tcx> {
/// `_(2).field`.
nested_fields: ItemLocalMap<Vec<(Ty<'tcx>, FieldIdx)>>,
Copy link
Member

Choose a reason for hiding this comment

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

Can field_maps replace this?

return self.collect_unmentioned(
variant.fields.iter().skip(idx.index()).take(usize::MAX),
collect_unmentioned,
) || has_mentioned;
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 the implementation of this check is a bit more complex than it needs to be I'm not sure why you need to go into collect_unmentioned here rather than just add to unmentioned_fields from this loop.

Copy link
Member

Choose a reason for hiding this comment

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

Or rather, if there are unmentioned fields in one call to this function, then we could just queue up a UnionPatUnmentioned like you do for the other errors.

// In case of the multiple fields from the union are used,
// we do not collect those fields to report unmentioned errors.
self.check_struct_variant(nested_adt_def.non_enum_variant(), false)
.then_some((idx, /* need_recheck */ true))
Copy link
Member

Choose a reason for hiding this comment

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

Similarly, I'm not sure that it's that big of a deal if we report an error for unmentioned fields from two unnamed struct types of an unnamed union (instead of just one of them). It may not be worth the additional complexity this adds.

@bors
Copy link
Contributor

bors commented Mar 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-unnamed_fields `#![feature(unnamed_fields)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants