-
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? 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
Changes from 3 commits
7bde18a
f3679bc
1ab2616
926892d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,12 +33,15 @@ use rustc_hir::def_id::DefId; | |
use rustc_hir::{ExprKind, QPath}; | ||
use rustc_infer::infer; | ||
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; | ||
use rustc_infer::infer::InferOk; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase}; | ||
use rustc_middle::ty::error::TypeError::FieldMisMatch; | ||
use rustc_middle::ty::subst::SubstsRef; | ||
use rustc_middle::ty::Ty; | ||
use rustc_middle::ty::TypeFoldable; | ||
use rustc_middle::ty::{AdtKind, Visibility}; | ||
use rustc_session::parse::feature_err; | ||
use rustc_span::edition::LATEST_STABLE_EDITION; | ||
use rustc_span::hygiene::DesugaringKind; | ||
use rustc_span::lev_distance::find_best_match_for_name; | ||
|
@@ -1262,49 +1265,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
.emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() }); | ||
} | ||
|
||
let error_happened = self.check_expr_struct_fields( | ||
self.check_expr_struct_fields( | ||
adt_ty, | ||
expected, | ||
expr.hir_id, | ||
qpath.span(), | ||
variant, | ||
fields, | ||
base_expr.is_none(), | ||
base_expr, | ||
expr.span, | ||
); | ||
if let Some(base_expr) = base_expr { | ||
// If check_expr_struct_fields hit an error, do not attempt to populate | ||
// the fields with the base_expr. This could cause us to hit errors later | ||
// when certain fields are assumed to exist that in fact do not. | ||
if !error_happened { | ||
self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {}); | ||
match adt_ty.kind() { | ||
ty::Adt(adt, substs) if adt.is_struct() => { | ||
let fru_field_types = adt | ||
.non_enum_variant() | ||
.fields | ||
.iter() | ||
.map(|f| { | ||
self.normalize_associated_types_in( | ||
expr.span, | ||
f.ty(self.tcx, substs), | ||
) | ||
}) | ||
.collect(); | ||
|
||
self.typeck_results | ||
.borrow_mut() | ||
.fru_field_types_mut() | ||
.insert(expr.hir_id, fru_field_types); | ||
} | ||
_ => { | ||
self.tcx | ||
.sess | ||
.emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span }); | ||
} | ||
} | ||
} | ||
} | ||
self.require_type_is_sized(adt_ty, expr.span, traits::StructInitializerSized); | ||
adt_ty | ||
} | ||
|
@@ -1317,9 +1288,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
span: Span, | ||
variant: &'tcx ty::VariantDef, | ||
ast_fields: &'tcx [hir::ExprField<'tcx>], | ||
check_completeness: bool, | ||
base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>, | ||
expr_span: Span, | ||
) -> bool { | ||
) { | ||
let tcx = self.tcx; | ||
|
||
let adt_ty_hint = self | ||
|
@@ -1394,7 +1365,135 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
) | ||
.emit(); | ||
} | ||
} else if check_completeness && !error_happened && !remaining_fields.is_empty() { | ||
} | ||
|
||
// If check_expr_struct_fields hit an error, do not attempt to populate | ||
// the fields with the base_expr. This could cause us to hit errors later | ||
// when certain fields are assumed to exist that in fact do not. | ||
if error_happened { | ||
return; | ||
} | ||
|
||
if let Some(base_expr) = base_expr { | ||
jackh726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let expected = if self.tcx.features().type_changing_struct_update { | ||
NoExpectation | ||
} else { | ||
ExpectHasType(adt_ty) | ||
}; | ||
let mut ty = self.check_expr_with_expectation(base_expr, expected); | ||
|
||
let expected_ty = expected.to_option(&self).unwrap_or(adt_ty); | ||
// While we don't allow *arbitrary* coercions here, we *do* allow | ||
// coercions from ! to `expected`. | ||
if ty.is_never() { | ||
assert!( | ||
!self.typeck_results.borrow().adjustments().contains_key(base_expr.hir_id), | ||
"expression with never type wound up being adjusted" | ||
); | ||
let adj_ty = self.next_ty_var(TypeVariableOrigin { | ||
kind: TypeVariableOriginKind::AdjustmentType, | ||
span: base_expr.span, | ||
}); | ||
self.apply_adjustments( | ||
base_expr, | ||
vec![Adjustment { kind: Adjust::NeverToAny, target: adj_ty }], | ||
); | ||
ty = adj_ty; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, what is this needed for? If this is needed, can you add a test for it? |
||
let cause = self.misc(base_expr.span); | ||
let mut fru_tys = None; | ||
let mut err = None; | ||
let is_struct; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why define this here? |
||
|
||
if let ty::Adt(adt, substs) = expected_ty.kind() { | ||
match ty.kind() { | ||
ty::Adt(base_adt, base_subs) if adt == base_adt => { | ||
if self.tcx.features().type_changing_struct_update { | ||
let tys = variant | ||
.fields | ||
.iter() | ||
.map(|f| { | ||
let fru_ty = self.normalize_associated_types_in( | ||
expr_span, | ||
self.field_ty(base_expr.span, f, base_subs), | ||
); | ||
let ident = self.tcx.adjust_ident(f.ident, variant.def_id); | ||
if let Some(_) = remaining_fields.remove(&ident) { | ||
let target_ty = self.field_ty(base_expr.span, f, substs); | ||
match self.at(&cause, self.param_env).sup(target_ty, fru_ty) | ||
{ | ||
Ok(InferOk { obligations, value: () }) => { | ||
self.register_predicates(obligations) | ||
} | ||
// FIXME: Need better diagnostics for `FieldMisMatch` error | ||
Err(_) => { | ||
if err.is_none() { | ||
err = Some(self.report_mismatched_types( | ||
&cause, | ||
target_ty, | ||
fru_ty, | ||
FieldMisMatch( | ||
variant.ident.name, | ||
ident.name, | ||
), | ||
)) | ||
} | ||
} | ||
} | ||
} | ||
fru_ty | ||
}) | ||
.collect(); | ||
fru_tys = Some(tys); | ||
} else { | ||
err = self.demand_suptype_diag(base_expr.span, expected_ty, ty); | ||
if err.is_some() && self.tcx.sess.is_nightly_build() { | ||
feature_err( | ||
&self.tcx.sess.parse_sess, | ||
sym::type_changing_struct_update, | ||
base_expr.span, | ||
"type changing struct updating is experimental", | ||
) | ||
.emit(); | ||
} | ||
} | ||
} | ||
_ => { | ||
err = self.demand_suptype_diag(base_expr.span, expected_ty, ty); | ||
} | ||
} | ||
is_struct = adt.is_struct(); | ||
if is_struct && fru_tys.is_none() { | ||
fru_tys = Some( | ||
variant | ||
.fields | ||
.iter() | ||
.map(|f| { | ||
self.normalize_associated_types_in( | ||
expr_span, | ||
f.ty(self.tcx, substs), | ||
) | ||
}) | ||
.collect(), | ||
) | ||
} | ||
} else { | ||
err = self.demand_suptype_diag(base_expr.span, expected_ty, ty); | ||
is_struct = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just emit the error here? Or, better yet, any way we can just return early and do away with the nesting? |
||
} | ||
if let Some(mut err) = err { | ||
let expr = base_expr.peel_drop_temps(); | ||
self.suggest_deref_ref_or_into(&mut err, expr, expected_ty, ty, None); | ||
err.emit(); | ||
} | ||
if let Some(fru_tys) = fru_tys { | ||
self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr_id, fru_tys); | ||
} | ||
if !is_struct { | ||
let e = FunctionalRecordUpdateOnNonStruct { span: base_expr.span }; | ||
self.tcx.sess.emit_err(e); | ||
} | ||
} else if kind_name != "union" && !remaining_fields.is_empty() { | ||
let inaccessible_remaining_fields = remaining_fields.iter().any(|(_, (_, field))| { | ||
!field.vis.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx) | ||
}); | ||
|
@@ -1405,8 +1504,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
self.report_missing_fields(adt_ty, span, remaining_fields); | ||
} | ||
} | ||
|
||
error_happened | ||
} | ||
|
||
fn check_struct_fields_on_error( | ||
|
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 | ||
}; | ||
} | ||
``` |
This file was deleted.
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`. |
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() {} |
Uh oh!
There was an error while loading. Please reload this page.