Rollup merge of #98013 - compiler-errors:guide-inference-2, r=lcnr

Subtype FRU fields first in `type_changing_struct_update`

So this fixes a subtle bug that `type_changing_struct_update` introduced, where it'll no longer coerce the base expr correctly. I actually think this code is easier to understand now, too.

r? `@lcnr` since you reviewed the last one
This commit is contained in:
Dylan DPC 2022-06-17 12:21:49 +02:00 committed by GitHub
commit 4793397f11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 81 deletions

View File

@ -1561,73 +1561,70 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// FIXME: We are currently creating two branches here in order to maintain // FIXME: We are currently creating two branches here in order to maintain
// consistency. But they should be merged as much as possible. // consistency. But they should be merged as much as possible.
let fru_tys = if self.tcx.features().type_changing_struct_update { let fru_tys = if self.tcx.features().type_changing_struct_update {
if let ty::Adt(adt, substs) = adt_ty.kind() && adt.is_struct() { if adt.is_struct() {
// Make an ADT with fresh inference substitutions. This // Make some fresh substitutions for our ADT type.
// will allow us to guide inference along so that, e.g.
// ```
// let x = MyStruct<'a, B, const C: usize> {
// f: 1,
// ..Default::default()
// };
// ```
// will have the default base expression constrained to
// `MyStruct<'_, _, _>`, as opposed to just `_`... This
// will allow us to then do a subtyping relation on all
// of the `remaining_fields` below, per the RFC.
let fresh_substs = self.fresh_substs_for_item(base_expr.span, adt.did()); let fresh_substs = self.fresh_substs_for_item(base_expr.span, adt.did());
let fresh_base_ty = self.tcx.mk_adt(*adt, fresh_substs); // We do subtyping on the FRU fields first, so we can
let base_ty = self.check_expr_has_type_or_error( // learn exactly what types we expect the base expr
base_expr, // needs constrained to be compatible with the struct
fresh_base_ty, // type we expect from the expectation value.
|_| { let fru_tys = variant
error_happened = true; .fields
}, .iter()
); .map(|f| {
let base_ty = self.shallow_resolve(base_ty); let fru_ty = self.normalize_associated_types_in(
if let ty::Adt(base_adt, base_substs) = base_ty.kind() && adt == base_adt { expr_span,
variant self.field_ty(base_expr.span, f, fresh_substs),
.fields );
.iter() let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
.map(|f| { if let Some(_) = remaining_fields.remove(&ident) {
let fru_ty = self.normalize_associated_types_in( let target_ty = self.field_ty(base_expr.span, f, substs);
expr_span, let cause = self.misc(base_expr.span);
self.field_ty(base_expr.span, f, base_substs), match self.at(&cause, self.param_env).sup(target_ty, fru_ty) {
); Ok(InferOk { obligations, value: () }) => {
let ident = self self.register_predicates(obligations)
.tcx }
.adjust_ident(f.ident(self.tcx), variant.def_id); Err(_) => {
if let Some(_) = remaining_fields.remove(&ident) { // This should never happen, since we're just subtyping the
let target_ty = // remaining_fields, but it's fine to emit this, I guess.
self.field_ty(base_expr.span, f, substs); self.report_mismatched_types(
let cause = self.misc(base_expr.span); &cause,
match self target_ty,
.at(&cause, self.param_env) fru_ty,
.sup(target_ty, fru_ty) FieldMisMatch(variant.name, ident.name),
{ )
Ok(InferOk { obligations, value: () }) => { .emit();
self.register_predicates(obligations)
}
// FIXME: Need better diagnostics for `FieldMisMatch` error
Err(_) => {
self.report_mismatched_types(
&cause,
target_ty,
fru_ty,
FieldMisMatch(variant.name, ident.name),
)
.emit();
}
} }
} }
self.resolve_vars_if_possible(fru_ty) }
}) self.resolve_vars_if_possible(fru_ty)
.collect() })
} else { .collect();
if !error_happened && !base_ty.references_error() { // The use of fresh substs that we have subtyped against
span_bug!(base_expr.span, "expected an error to have been reported in `check_expr_has_type_or_error`"); // our base ADT type's fields allows us to guide inference
} // along so that, e.g.
return; // ```
} // MyStruct<'a, F1, F2, const C: usize> {
// f: F1,
// // Other fields that reference `'a`, `F2`, and `C`
// }
//
// let x = MyStruct {
// f: 1usize,
// ..other_struct
// };
// ```
// will have the `other_struct` expression constrained to
// `MyStruct<'a, _, F2, C>`, as opposed to just `_`...
// This is important to allow coercions to happen in
// `other_struct` itself. See `coerce-in-base-expr.rs`.
let fresh_base_ty = self.tcx.mk_adt(*adt, fresh_substs);
self.check_expr_has_type_or_error(
base_expr,
self.resolve_vars_if_possible(fresh_base_ty),
|_| {},
);
fru_tys
} else { } else {
// Check the base_expr, regardless of a bad expected adt_ty, so we can get // Check the base_expr, regardless of a bad expected adt_ty, so we can get
// type errors on that expression, too. // type errors on that expression, too.

View File

@ -0,0 +1,28 @@
// check-pass
#![feature(type_changing_struct_update)]
#![allow(incomplete_features)]
use std::any::Any;
struct Foo<A, B: ?Sized, C: ?Sized> {
a: A,
b: Box<B>,
c: Box<C>,
}
struct B;
struct C;
fn main() {
let y = Foo::<usize, dyn Any, dyn Any> {
a: 0,
b: Box::new(B),
..Foo {
a: 0,
b: Box::new(B),
// C needs to be told to coerce to `Box<dyn Any>`
c: Box::new(C),
}
};
}

View File

@ -50,7 +50,6 @@ fn fail_update() {
let m3 = Machine::<i32, i32> { let m3 = Machine::<i32, i32> {
..m1 ..m1
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| ERROR mismatched types [E0308]
}; };
} }

View File

@ -2,29 +2,20 @@ error[E0308]: mismatched types
--> $DIR/type-generic-update.rs:46:11 --> $DIR/type-generic-update.rs:46:11
| |
LL | ..m1 LL | ..m1
| ^^ field type mismatch: Machine.state | ^^ expected `i32`, found `f64`
| |
= note: expected type `i32` = note: expected struct `Machine<'_, i32, _>`
found type `f64` found struct `Machine<'_, f64, _>`
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/type-generic-update.rs:51:11 --> $DIR/type-generic-update.rs:51:11
| |
LL | ..m1 LL | ..m1
| ^^ field type mismatch: Machine.state | ^^ expected `i32`, found `f64`
| |
= note: expected type `i32` = note: expected struct `Machine<'_, i32, i32>`
found type `f64` found struct `Machine<'_, f64, f64>`
error[E0308]: mismatched types error: aborting due to 2 previous errors
--> $DIR/type-generic-update.rs:51:11
|
LL | ..m1
| ^^ field type mismatch: Machine.message
|
= note: expected type `i32`
found type `f64`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0308`.