@@ -329,12 +329,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
329
329
"closure"
330
330
} ;
331
331
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 ) ;
338
334
339
335
let explanation = self . explain_why_borrow_contains_point ( context, issued_borrow, None ) ;
340
336
let second_borrow_desc = if explanation. is_explained ( ) {
@@ -516,6 +512,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
516
512
) ;
517
513
}
518
514
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
+
519
522
explanation
520
523
. add_explanation_to_diagnostic ( self . infcx . tcx , self . mir , & mut err, first_borrow_desc) ;
521
524
@@ -549,7 +552,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
549
552
& self ,
550
553
first_borrowed_place : & Place < ' tcx > ,
551
554
second_borrowed_place : & Place < ' tcx > ,
552
- ) -> ( String , String , String ) {
555
+ ) -> ( String , String , String , String ) {
553
556
// Define a small closure that we can use to check if the type of a place
554
557
// is a union.
555
558
let is_union = |place : & Place < ' tcx > | -> bool {
@@ -600,7 +603,12 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
600
603
. unwrap_or_else ( || "_" . to_owned ( ) ) ;
601
604
let desc_second = self . describe_place ( second_borrowed_place)
602
605
. 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 ( ) ) ) ;
604
612
} ,
605
613
_ => current = base,
606
614
}
@@ -612,7 +620,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
612
620
// only return the description of the first place.
613
621
let desc_place = self . describe_place ( first_borrowed_place)
614
622
. unwrap_or_else ( || "_" . to_owned ( ) ) ;
615
- ( desc_place, "" . to_string ( ) , "" . to_string ( ) )
623
+ ( desc_place, "" . to_string ( ) , "" . to_string ( ) , "" . to_string ( ) )
616
624
} )
617
625
}
618
626
0 commit comments