@@ -1506,20 +1506,35 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1506
1506
}
1507
1507
1508
1508
fn specialized_description ( & self , place : & Place < ' tcx > ) -> Option < String > {
1509
- if let Some ( name ) = self . describe_place ( place) {
1510
- Some ( format ! ( "`&`-reference `{}`" , name ) )
1509
+ if let Some ( _name ) = self . describe_place ( place) {
1510
+ Some ( format ! ( "data in a `&` reference" ) )
1511
1511
} else {
1512
1512
None
1513
1513
}
1514
1514
}
1515
1515
1516
- fn get_main_error_message ( & self , place : & Place < ' tcx > ) -> String {
1516
+ fn get_default_err_msg ( & self , place : & Place < ' tcx > ) -> String {
1517
1517
match self . describe_place ( place) {
1518
1518
Some ( name) => format ! ( "immutable item `{}`" , name) ,
1519
1519
None => "immutable item" . to_owned ( ) ,
1520
1520
}
1521
1521
}
1522
1522
1523
+ fn get_secondary_err_msg ( & self , place : & Place < ' tcx > ) -> String {
1524
+ match self . specialized_description ( place) {
1525
+ Some ( _) => format ! ( "data in a `&` reference" ) ,
1526
+ None => self . get_default_err_msg ( place)
1527
+ }
1528
+ }
1529
+
1530
+ fn get_primary_err_msg ( & self , place : & Place < ' tcx > ) -> String {
1531
+ if let Some ( name) = self . describe_place ( place) {
1532
+ format ! ( "`{}` is a `&` reference, so the data it refers to cannot be written" , name)
1533
+ } else {
1534
+ format ! ( "cannot assign through `&`-reference" )
1535
+ }
1536
+ }
1537
+
1523
1538
/// Check the permissions for the given place and read or write kind
1524
1539
///
1525
1540
/// Returns true if an error is reported, false otherwise.
@@ -1546,7 +1561,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1546
1561
self . is_mutable ( place, is_local_mutation_allowed)
1547
1562
{
1548
1563
error_reported = true ;
1549
- let item_msg = self . get_main_error_message ( place) ;
1564
+ let item_msg = self . get_default_err_msg ( place) ;
1550
1565
let mut err = self . tcx
1551
1566
. cannot_borrow_path_as_mutable ( span, & item_msg, Origin :: Mir ) ;
1552
1567
err. span_label ( span, "cannot borrow as mutable" ) ;
@@ -1576,18 +1591,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1576
1591
let locations = self . mir . find_assignments ( local) ;
1577
1592
if locations. len ( ) > 0 {
1578
1593
let item_msg = if error_reported {
1579
- match self . specialized_description ( base) {
1580
- Some ( msg) => msg,
1581
- None => self . get_main_error_message ( place)
1582
- }
1594
+ self . get_secondary_err_msg ( base)
1583
1595
} else {
1584
- self . get_main_error_message ( place)
1596
+ self . get_default_err_msg ( place)
1585
1597
} ;
1598
+
1586
1599
err_info = Some ( (
1587
1600
self . mir . source_info ( locations[ 0 ] ) . span ,
1588
1601
"consider changing this to be a \
1589
1602
mutable reference: `&mut`", item_msg,
1590
- "cannot assign through `&`-reference" ) ) ;
1603
+ self . get_primary_err_msg ( base ) ) ) ;
1591
1604
}
1592
1605
} ,
1593
1606
_ => { } ,
@@ -1597,15 +1610,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1597
1610
}
1598
1611
1599
1612
if let Some ( ( err_help_span, err_help_stmt, item_msg, sec_span) ) = err_info {
1600
- let mut err = self . tcx . cannot_assign ( span, & item_msg, Origin :: Mir , true ) ;
1613
+ let mut err = self . tcx . cannot_assign ( span, & item_msg, Origin :: Mir ) ;
1601
1614
err. span_suggestion ( err_help_span, err_help_stmt, format ! ( "" ) ) ;
1602
1615
if place != place_err {
1603
1616
err. span_label ( span, sec_span) ;
1604
1617
}
1605
1618
err. emit ( )
1606
1619
} else {
1607
- let item_msg_ = self . get_main_error_message ( place) ;
1608
- let mut err = self . tcx . cannot_assign ( span, & item_msg_, Origin :: Mir , false ) ;
1620
+ let item_msg_ = self . get_default_err_msg ( place) ;
1621
+ let mut err = self . tcx . cannot_assign ( span, & item_msg_, Origin :: Mir ) ;
1609
1622
err. span_label ( span, "cannot mutate" ) ;
1610
1623
if place != place_err {
1611
1624
if let Some ( name) = self . describe_place ( place_err) {
0 commit comments