-
Notifications
You must be signed in to change notification settings - Fork 13.4k
implement rfc-2528 type_changing-struct-update #90035
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7bde18a
implement type-changing-struct-update
SparrowLii f3679bc
move the processing part of `base_expr` into `check_expr_struct_fields`
SparrowLii 1ab2616
Add feature trigger and correct `is_struct` check
SparrowLii 926892d
Add feature trigger and enable is_struct check
SparrowLii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/doc/unstable-book/src/language-features/type-changing-struct-update.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# `type_changing_struct_update` | ||
|
||
The tracking issue for this feature is: [#86555] | ||
|
||
[#86555]: https://github.com/rust-lang/rust/issues/86555 | ||
|
||
------------------------ | ||
|
||
This implements [RFC2528]. When turned on, you can create instances of the same struct | ||
that have different generic type or lifetime parameters. | ||
|
||
[RFC2528]: https://github.com/rust-lang/rfcs/blob/master/text/2528-type-changing-struct-update-syntax.md | ||
|
||
```rust | ||
#![allow(unused_variables, dead_code)] | ||
#![feature(type_changing_struct_update)] | ||
|
||
fn main () { | ||
struct Foo<T, U> { | ||
jackh726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
field1: T, | ||
field2: U, | ||
} | ||
|
||
let base: Foo<String, i32> = Foo { | ||
field1: String::from("hello"), | ||
field2: 1234, | ||
}; | ||
let updated: Foo<f64, i32> = Foo { | ||
field1: 3.14, | ||
..base | ||
}; | ||
} | ||
``` |
12 changes: 0 additions & 12 deletions
12
src/test/ui/feature-gates/feature-gate-type_changing_struct_update.stderr
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/test/ui/rfcs/rfc-2528-type-changing-struct-update/feature-gate.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error[E0658]: type changing struct updating is experimental | ||
--> $DIR/feature-gate.rs:22:11 | ||
| | ||
LL | ..m1 | ||
| ^^ | ||
| | ||
= note: see issue #86555 <https://github.com/rust-lang/rust/issues/86555> for more information | ||
= help: add `#![feature(type_changing_struct_update)]` to the crate attributes to enable | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/feature-gate.rs:22:11 | ||
| | ||
LL | ..m1 | ||
| ^^ expected struct `State2`, found struct `State1` | ||
| | ||
= note: expected struct `Machine<State2>` | ||
found struct `Machine<State1>` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0308, E0658. | ||
For more information about an error, try `rustc --explain E0308`. |
43 changes: 43 additions & 0 deletions
43
src/test/ui/rfcs/rfc-2528-type-changing-struct-update/lifetime-update.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#![feature(type_changing_struct_update)] | ||
#![allow(incomplete_features)] | ||
|
||
#[derive(Clone)] | ||
struct Machine<'a, S> { | ||
state: S, | ||
lt_str: &'a str, | ||
common_field: i32, | ||
} | ||
|
||
#[derive(Clone)] | ||
struct State1; | ||
#[derive(Clone)] | ||
struct State2; | ||
|
||
fn update_to_state2() { | ||
let s = String::from("hello"); | ||
let m1: Machine<State1> = Machine { | ||
state: State1, | ||
lt_str: &s, | ||
//~^ ERROR `s` does not live long enough [E0597] | ||
// FIXME: The error here actually comes from line 34. The | ||
// span of the error message should be corrected to line 34 | ||
common_field: 2, | ||
}; | ||
// update lifetime | ||
let m3: Machine<'static, State1> = Machine { | ||
lt_str: "hello, too", | ||
..m1.clone() | ||
}; | ||
// update lifetime and type | ||
let m4: Machine<'static, State2> = Machine { | ||
state: State2, | ||
lt_str: "hello, again", | ||
..m1.clone() | ||
}; | ||
// updating to `static should fail. | ||
let m2: Machine<'static, State1> = Machine { | ||
..m1 | ||
}; | ||
} | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
src/test/ui/rfcs/rfc-2528-type-changing-struct-update/lifetime-update.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0597]: `s` does not live long enough | ||
--> $DIR/lifetime-update.rs:20:17 | ||
| | ||
LL | lt_str: &s, | ||
| ^^ borrowed value does not live long enough | ||
... | ||
LL | let m2: Machine<'static, State1> = Machine { | ||
| ------------------------ type annotation requires that `s` is borrowed for `'static` | ||
... | ||
LL | } | ||
| - `s` dropped here while still borrowed | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0597`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.