Skip to content

Add reference annotations for unions - #164654

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
traviscross:TC/add-reference-annotations_unions
Oct 23, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
traviscross:TC/add-reference-annotations_unions

Conversation

@traviscross

@traviscross traviscross commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Let's link the relevant UI tests to the Reference's rules for unions.

@traviscross traviscross added the T-lang-docs Relevant to the lang-docs team. label Oct 20, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 20, 2026
@traviscross

Copy link
Copy Markdown
Contributor Author

(As work within the scope of the lang-docs team, this is outside of the scope of the rust-forge#1040 policy; nonetheless, I'll note this work was created with the assistance of AI tools and that this has been prearranged with all reviewers.)

r? @traviscross (placeholder for @PLeVasseur to review while we fix permissions)

@rust-log-analyzer

This comment has been minimized.

@DanielEScherzer DanielEScherzer left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

mostly looks good to me, one rule addition I'm confused by

View changes since this review

@@ -1,3 +1,4 @@
//@ reference: items.union.common-storage

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This doesn't seem to be testing the common storage so much as something like expr.mut.valid-places if I'm reading this correctly (for the second error) and items.union.init.intro for the initialization

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Test was added in #53258 to "add tests for assigning fields without initializing var", though the "This should never be allowed" was in the original form of the test, src/test/ui/nll/reassignment_immutable_fields_overlapping.rs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. The Reference identifiers are meant to match the intent of the test, and the intent of this test, as described in the comment, is:

// This should never be allowed -- `foo.a` and `foo.b` are
// overlapping, so since `x` is not `mut` we should not permit
// reassignment.

Similarly, the test function is unsafe fn overlapping_fields(). So I think this Reference rule is correct, in the sense of matching the intent of the test.

But, similar to you, it's not immediately clear to me how the test itself matches its stated intent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I looked into this. What it's testing here is subtle, and it's made more subtle by the fact that the test doesn't annotate the error messages. Note that if we use mut on the binding, it's still an error:

union U { a: u8, b: u8 }
let mut x: U;
x.a = 1; //~ ERROR partially assigned binding `x` isn't fully initialized
x.b = 2;

(I.e., we have to fully initialize x either in the let or via an assignment to x.)

Without the mut, we get:

union U { a: u8, b: u8 }
let x: U;
x.a = 1; //~ ERROR partially assigned binding `x` isn't fully initialized
x.b = 2; //~ ERROR cannot assign to `x.b`, as `x` is not declared as mutable

What matters here is that the two error messages are different. Due to common storage, any write to the union marks the entire union as initialized. So when we get to the assignment to x.b, we fall through and notice that the binding isn't mut. If this were a struct, that wouldn't happen.

That's pretty subtle and compiler-specific, though, so I'll remove this annotation. Thanks for flagging this.

@PLeVasseur PLeVasseur left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's link the relevant UI tests to the Reference's rules for unions.
@traviscross
traviscross force-pushed the TC/add-reference-annotations_unions branch from b330897 to 6d7d075 Compare October 20, 2026 22:29
@traviscross

Copy link
Copy Markdown
Contributor Author

Thanks @PLeVasseur, @DanielEScherzer.

@bors r=PLeVasseur,traviscross rollup

@rust-bors

rust-bors Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 6d7d075 has been approved by PLeVasseur,traviscross

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Oct 21, 2026
@traviscross

Copy link
Copy Markdown
Contributor Author

@bors r-

I'm thinking about moving the Reference annotations to the bottom to minimize the churn on .stderr files as these annotations change. I'll evaluate that and then may update this PR accordingly.

@rust-bors rust-bors Bot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 21, 2026
@rust-bors

rust-bors Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

View changes since this unapproval

@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Oct 21, 2026
@traviscross
traviscross force-pushed the TC/add-reference-annotations_unions branch from 6d7d075 to 205d6ac Compare October 22, 2026 04:40
@traviscross traviscross added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Oct 22, 2026
@traviscross

Copy link
Copy Markdown
Contributor Author

Though it's less usual for directives to be at the bottom, the Reference identifiers are special. These are going to change more often than the other directives. Since we don't anonymize line numbers on --> lines pointing to tests, changes to these annotations would otherwise create a lot of churn, and it seems worth avoiding that. In the series of commits I'm working on for adding these annotations, putting them at the bottom dramatically reduces this churn. So let's do that.

I've moved the annotations to the bottom. They're otherwise the same.

@bors r=PLeVasseur,traviscross rollup

@rust-bors

rust-bors Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 205d6ac has been approved by PLeVasseur,traviscross

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 22, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 22, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #164458 (Avoid stdlib's `impl Hash for Path` for stable hashing)
 - #161790 (update to illumos sysroot 20210501)
 - #163087 (rmake: Add helpers for iterating symbols and objects)
 - #163586 (refactor Windows remove_dir_all)
 - #163983 (std: use `SOCK_NONBLOCK` in `connect_timeout`)
 - #164307 (Rename `PostTypeckUntilBorrowck` -> `PostTypeckThroughBorrowck`)
 - #164425 (Declare `SyntaxContext` through `rustc_index::newtype_index!`)
 - #156841 (Avoid cloning CFG-related analysis data.)
 - #164654 (Add reference annotations for unions)
 - #164717 (Allow `llm-assisted` label to be set without write access)
 - #164735 (Remove docs mentioning instability of wasm64)
 - #164737 (Update LLVM PassBuilder constructor calls to pass a context)

Failed merges:

 - #164636 (Remove `record_*` macros from the metadata encoder)
rust-bors Bot pushed a commit that referenced this pull request Oct 23, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #164458 (Avoid stdlib's `impl Hash for Path` for stable hashing)
 - #161790 (update to illumos sysroot 20210501)
 - #163087 (rmake: Add helpers for iterating symbols and objects)
 - #163586 (refactor Windows remove_dir_all)
 - #163983 (std: use `SOCK_NONBLOCK` in `connect_timeout`)
 - #164307 (Rename `PostTypeckUntilBorrowck` -> `PostTypeckThroughBorrowck`)
 - #164425 (Declare `SyntaxContext` through `rustc_index::newtype_index!`)
 - #156841 (Avoid cloning CFG-related analysis data.)
 - #164654 (Add reference annotations for unions)
 - #164717 (Allow `llm-assisted` label to be set without write access)
 - #164735 (Remove docs mentioning instability of wasm64)
 - #164737 (Update LLVM PassBuilder constructor calls to pass a context)

Failed merges:

 - #164636 (Remove `record_*` macros from the metadata encoder)
rust-bors Bot pushed a commit that referenced this pull request Oct 23, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #164458 (Avoid stdlib's `impl Hash for Path` for stable hashing)
 - #161790 (update to illumos sysroot 20210501)
 - #163087 (rmake: Add helpers for iterating symbols and objects)
 - #163586 (refactor Windows remove_dir_all)
 - #163983 (std: use `SOCK_NONBLOCK` in `connect_timeout`)
 - #164307 (Rename `PostTypeckUntilBorrowck` -> `PostTypeckThroughBorrowck`)
 - #164425 (Declare `SyntaxContext` through `rustc_index::newtype_index!`)
 - #156841 (Avoid cloning CFG-related analysis data.)
 - #164654 (Add reference annotations for unions)
 - #164717 (Allow `llm-assisted` label to be set without write access)
 - #164735 (Remove docs mentioning instability of wasm64)
 - #164737 (Update LLVM PassBuilder constructor calls to pass a context)

Failed merges:

 - #164636 (Remove `record_*` macros from the metadata encoder)
@rust-bors
rust-bors Bot merged commit 209922e into rust-lang:main Oct 23, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 23, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 23, 2026
Rollup merge of #164654 - traviscross:TC/add-reference-annotations_unions, r=PLeVasseur,traviscross

Add reference annotations for unions

Let's link the relevant UI tests to the Reference's rules for unions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang-docs Relevant to the lang-docs team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants