Skip to content

Commit 3587ff5

Browse files
committed
Don't complain re missing mut on attempt to partially initialize an uninitialized struct.
Under the semantics of rust-lang#54986 (our short term plan), the partial initialization itself will signal an error. We don't need to add noise to the output by also complaining about `mut`. (In particular, the user may well revise their code in a way that does not require `mut`.)
1 parent 57c4678 commit 3587ff5

File tree

1 file changed

+23
-9
lines changed
  • src/librustc_mir/borrow_check

1 file changed

+23
-9
lines changed

Diff for: src/librustc_mir/borrow_check/mod.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -1776,13 +1776,23 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17761776
location: Location,
17771777
) -> bool {
17781778
debug!(
1779-
"check_access_permissions({:?}, {:?}, {:?})",
1779+
"check_access_permissions({:?}, {:?}, is_local_mutation_allowed: {:?})",
17801780
place, kind, is_local_mutation_allowed
17811781
);
17821782

17831783
let error_access;
17841784
let the_place_err;
17851785

1786+
// rust-lang/rust#21232, #54986: during period where we reject
1787+
// partial initialization, do not complain about mutability
1788+
// errors except for actual mutation (as opposed to an attempt
1789+
// to do a partial initialization).
1790+
let previously_initialized = if let Some(local) = place.base_local() {
1791+
self.is_local_ever_initialized(local, flow_state).is_some()
1792+
} else {
1793+
true
1794+
};
1795+
17861796
match kind {
17871797
Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Unique))
17881798
| Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. }))
@@ -1875,14 +1885,18 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18751885
}
18761886

18771887
// at this point, we have set up the error reporting state.
1878-
self.report_mutability_error(
1879-
place,
1880-
span,
1881-
the_place_err,
1882-
error_access,
1883-
location,
1884-
);
1885-
return true;
1888+
if previously_initialized {
1889+
self.report_mutability_error(
1890+
place,
1891+
span,
1892+
the_place_err,
1893+
error_access,
1894+
location,
1895+
);
1896+
return true;
1897+
} else {
1898+
return false;
1899+
}
18861900
}
18871901

18881902
fn is_local_ever_initialized(&self,

0 commit comments

Comments
 (0)