@@ -1557,9 +1557,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1557
1557
// FIXME: We are currently creating two branches here in order to maintain
1558
1558
// consistency. But they should be merged as much as possible.
1559
1559
let fru_tys = if self . tcx . features ( ) . type_changing_struct_update {
1560
- let base_ty = self . check_expr ( base_expr) ;
1561
1560
match adt_ty. kind ( ) {
1562
1561
ty:: Adt ( adt, substs) if adt. is_struct ( ) => {
1562
+ // Make an ADT with fresh inference substitutions. This
1563
+ // will allow us to guide inference along so that, e.g.
1564
+ // ```
1565
+ // let x = MyStruct<'a, B, const C: usize> {
1566
+ // f: 1,
1567
+ // ..Default::default()
1568
+ // };
1569
+ // ```
1570
+ // will have the default base expression constrained to
1571
+ // `MyStruct<'_, _, _>`, as opposed to just `_`... This
1572
+ // will allow us to then do a subtyping relation on all
1573
+ // of the `remaining_fields` below, per the RFC.
1574
+ let fresh_substs = self . fresh_substs_for_item ( base_expr. span , adt. did ( ) ) ;
1575
+ let base_ty = self . check_expr_has_type_or_error (
1576
+ base_expr,
1577
+ self . tcx . mk_adt ( * adt, fresh_substs) ,
1578
+ |_| { } ,
1579
+ ) ;
1580
+ let base_ty = self . shallow_resolve ( base_ty) ;
1563
1581
match base_ty. kind ( ) {
1564
1582
ty:: Adt ( base_adt, base_subs) if adt == base_adt => {
1565
1583
variant
@@ -1585,7 +1603,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1585
1603
self . register_predicates ( obligations)
1586
1604
}
1587
1605
// FIXME: Need better diagnostics for `FieldMisMatch` error
1588
- Err ( _) => {
1606
+ Err ( type_error) => {
1607
+ debug ! ( "check_expr_struct_fields: {fru_ty} sub {target_ty} failed: {type_error:?}" ) ;
1589
1608
self . report_mismatched_types (
1590
1609
& cause,
1591
1610
target_ty,
@@ -1596,7 +1615,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1596
1615
}
1597
1616
}
1598
1617
}
1599
- fru_ty
1618
+ self . resolve_vars_if_possible ( fru_ty)
1600
1619
} )
1601
1620
. collect ( )
1602
1621
}
@@ -1613,6 +1632,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1613
1632
}
1614
1633
}
1615
1634
_ => {
1635
+ // Check the base_expr, regardless of a bad expected adt_ty, so we can get
1636
+ // type errors on that expression, too.
1637
+ self . check_expr ( base_expr) ;
1616
1638
self . tcx
1617
1639
. sess
1618
1640
. emit_err ( FunctionalRecordUpdateOnNonStruct { span : base_expr. span } ) ;
0 commit comments