Repository navigation
Add reference annotations for unions - #164654
rust-bors[bot] merged 1 commit into
Conversation
|
(As work within the scope of the lang-docs team, this is outside of the scope of the r? @traviscross (placeholder for @PLeVasseur to review while we fix permissions) |
This comment has been minimized.
This comment has been minimized.
| @@ -1,3 +1,4 @@ | |||
| //@ reference: items.union.common-storage | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 mutableWhat 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.
Let's link the relevant UI tests to the Reference's rules for unions.
b330897 to
6d7d075
Compare
|
Thanks @PLeVasseur, @DanielEScherzer. @bors r=PLeVasseur,traviscross rollup |
|
@bors r- I'm thinking about moving the Reference annotations to the bottom to minimize the churn on |
|
This pull request was unapproved. |
6d7d075 to
205d6ac
Compare
|
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 I've moved the annotations to the bottom. They're otherwise the same. @bors r=PLeVasseur,traviscross rollup |
…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)
…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)
…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)
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.
Let's link the relevant UI tests to the Reference's rules for unions.