@@ -329,12 +329,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
329329 "closure"
330330 } ;
331331
332- let ( desc_place, msg_place, msg_borrow) = self . describe_place_for_conflicting_borrow (
333- place, & issued_borrow. borrowed_place ,
334- ) ;
335- let via = |msg : String | if msg. is_empty ( ) { msg } else { format ! ( " (via `{}`)" , msg) } ;
336- let msg_place = via ( msg_place) ;
337- let msg_borrow = via ( msg_borrow) ;
332+ let ( desc_place, msg_place, msg_borrow, union_type_name) =
333+ self . describe_place_for_conflicting_borrow ( place, & issued_borrow. borrowed_place ) ;
338334
339335 let explanation = self . explain_why_borrow_contains_point ( context, issued_borrow, None ) ;
340336 let second_borrow_desc = if explanation. is_explained ( ) {
@@ -516,6 +512,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
516512 ) ;
517513 }
518514
515+ if union_type_name != "" {
516+ err. note ( & format ! (
517+ "`{}` is a field of the union `{}`, so it overlaps the field `{}`" ,
518+ msg_place, union_type_name, msg_borrow,
519+ ) ) ;
520+ }
521+
519522 explanation
520523 . add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, first_borrow_desc) ;
521524
@@ -549,7 +552,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
549552 & self ,
550553 first_borrowed_place : & Place < ' tcx > ,
551554 second_borrowed_place : & Place < ' tcx > ,
552- ) -> ( String , String , String ) {
555+ ) -> ( String , String , String , String ) {
553556 // Define a small closure that we can use to check if the type of a place
554557 // is a union.
555558 let is_union = |place : & Place < ' tcx > | -> bool {
@@ -600,7 +603,12 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
600603 . unwrap_or_else ( || "_" . to_owned ( ) ) ;
601604 let desc_second = self . describe_place ( second_borrowed_place)
602605 . unwrap_or_else ( || "_" . to_owned ( ) ) ;
603- return Some ( ( desc_base, desc_first, desc_second) ) ;
606+
607+ // Also compute the name of the union type, eg. `Foo` so we
608+ // can add a helpful note with it.
609+ let ty = base. ty ( self . mir , self . infcx . tcx ) . to_ty ( self . infcx . tcx ) ;
610+
611+ return Some ( ( desc_base, desc_first, desc_second, ty. to_string ( ) ) ) ;
604612 } ,
605613 _ => current = base,
606614 }
@@ -612,7 +620,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
612620 // only return the description of the first place.
613621 let desc_place = self . describe_place ( first_borrowed_place)
614622 . unwrap_or_else ( || "_" . to_owned ( ) ) ;
615- ( desc_place, "" . to_string ( ) , "" . to_string ( ) )
623+ ( desc_place, "" . to_string ( ) , "" . to_string ( ) , "" . to_string ( ) )
616624 } )
617625 }
618626
0 commit comments