forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#90035 - SparrowLii:rfc2528, r=jackh726
implement rfc-2528 type_changing-struct-update This PR implement rfc2528-type_changing-struct-update. The main change process is as follows: 1. Move the processing part of `base_expr` into `check_expr_struct_fields` to avoid returning `remaining_fields` (a relatively complex hash table) 2. Before performing the type consistency check(`check_expr_has_type_or_error`), if the `type_changing_struct_update` feature is set, enter a different processing flow, otherwise keep the original flow 3. In the case of the same structure definition, check each field in `remaining_fields`. If the field in `base_expr` is not the suptype of the field in `adt_ty`, an error(`FeildMisMatch`) will be reported. The MIR part does not need to be changed, because only the items contained in `remaining_fields` will be extracted from `base_expr` when MIR is generated. This means that fields with different types in `base_expr` will not be used Updates rust-lang#86618 cc `@nikomatsakis`
- Loading branch information
Showing
11 changed files
with
328 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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 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> { | ||
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 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 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 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 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.