@@ -344,7 +344,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
344344 visitor. visit_ty ( ty) ;
345345 }
346346
347- fn check_mut_borrow ( & mut self , local : Local , kind : hir:: BorrowKind ) {
347+ fn check_mut_borrow ( & mut self , place : & Place < ' _ > , kind : hir:: BorrowKind ) {
348348 match self . const_kind ( ) {
349349 // In a const fn all borrows are transient or point to the places given via
350350 // references in the arguments (so we already checked them with
@@ -355,10 +355,19 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
355355 // to mutable memory.
356356 hir:: ConstContext :: ConstFn => self . check_op ( ops:: TransientMutBorrow ( kind) ) ,
357357 _ => {
358+ // For indirect places, we are not creating a new permanent borrow, it's
359+ // just as transient as the already existing one. For reborrowing references
360+ // this is handled at the top of `visit_rvalue`, but for raw pointers we handle it
361+ // here. Pointers/references to `static mut` also end up here.
358362 // Locals with StorageDead do not live beyond the evaluation and can
359363 // thus safely be borrowed without being able to be leaked to the final
360364 // value of the constant.
361- if self . local_has_storage_dead ( local) {
365+ // Note: This is only sound if every local that has a `StorageDead` has a
366+ // `StorageDead` in every control flow path leading to a `return` terminator.
367+ // The good news is that interning will detect if any unexpected mutable
368+ // pointer slips through.
369+ if place. is_indirect_first_projection ( ) || self . local_has_storage_dead ( place. local )
370+ {
362371 self . check_op ( ops:: TransientMutBorrow ( kind) ) ;
363372 } else {
364373 self . check_op ( ops:: MutBorrow ( kind) ) ;
@@ -460,7 +469,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
460469
461470 if !is_allowed {
462471 self . check_mut_borrow (
463- place. local ,
472+ place,
464473 if matches ! ( rvalue, Rvalue :: Ref ( ..) ) {
465474 hir:: BorrowKind :: Ref
466475 } else {
@@ -478,7 +487,13 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
478487 place. as_ref ( ) ,
479488 ) ;
480489
481- if borrowed_place_has_mut_interior {
490+ // If the place is indirect, this is basically a reborrow. We have a reborrow
491+ // special case above, but for raw pointers and pointers/references to `static`,
492+ // `place_as_reborrow` does not recognize them as such, so we end up here. This
493+ // should probably be considered a `TransientCellBorrow` (we consider the equivalent
494+ // mutable case a `TransientMutBorrow`), but such reborrows got accidentally
495+ // stabilized already and it is too much of a breaking change to take back.
496+ if borrowed_place_has_mut_interior && !place. is_indirect_first_projection ( ) {
482497 match self . const_kind ( ) {
483498 // In a const fn all borrows are transient or point to the places given via
484499 // references in the arguments (so we already checked them with
@@ -495,6 +510,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
495510 // final value.
496511 // Note: This is only sound if every local that has a `StorageDead` has a
497512 // `StorageDead` in every control flow path leading to a `return` terminator.
513+ // The good news is that interning will detect if any unexpected mutable
514+ // pointer slips through.
498515 if self . local_has_storage_dead ( place. local ) {
499516 self . check_op ( ops:: TransientCellBorrow ) ;
500517 } else {
@@ -619,6 +636,11 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
619636 if base_ty. is_unsafe_ptr ( ) {
620637 if place_ref. projection . is_empty ( ) {
621638 let decl = & self . body . local_decls [ place_ref. local ] ;
639+ // FIXME: Why is this only checked for unsafe pointers?
640+ // That will check `static mut` but not `static`.
641+ // Crucially it then skips the other checks below!
642+ // Probably, this can be removed entirely once `MutDeref`
643+ // and `RawMutPtrDeref` are stable.
622644 if let LocalInfo :: StaticRef { def_id, .. } = * decl. local_info ( ) {
623645 let span = decl. source_info . span ;
624646 self . check_static ( def_id, span) ;
@@ -946,6 +968,12 @@ fn place_as_reborrow<'tcx>(
946968) -> Option < PlaceRef < ' tcx > > {
947969 match place. as_ref ( ) . last_projection ( ) {
948970 Some ( ( place_base, ProjectionElem :: Deref ) ) => {
971+ // FIXME: why do statics and raw pointers get excluded here? This makes
972+ // some code involving mutable pointers unstable, but it is unclear
973+ // why that code is treated differently from mutable references.
974+ // Once TransientMutBorrow and TransientCellBorrow are stable,
975+ // this can probably be cleaned up without any behavioral changes.
976+
949977 // A borrow of a `static` also looks like `&(*_1)` in the MIR, but `_1` is a `const`
950978 // that points to the allocation for the static. Don't treat these as reborrows.
951979 if body. local_decls [ place_base. local ] . is_ref_to_static ( ) {
0 commit comments