@@ -175,9 +175,9 @@ pub fn gather_loans_in_static_initializer(bccx: &mut BorrowckCtxt, expr: &ast::E
175175 move_data : MoveData :: new ( )
176176 } ;
177177
178+ // FIXME #13005 This should also walk the
179+ // expression.
178180 match expr. node {
179- // Just visit the expression if the
180- // item is taking an address.
181181 ast:: ExprAddrOf ( ..) => {
182182 glcx. visit_expr ( expr, ( ) ) ;
183183 }
@@ -686,34 +686,45 @@ impl<'a> GatherLoanCtxt<'a> {
686686 -> Result < ( ) , ( ) > {
687687 //! Implements the A-* rules in doc.rs.
688688
689- match req_kind {
690- ty:: ImmBorrow => {
689+ match ( cmt. freely_aliasable ( bccx. tcx ) , req_kind) {
690+ ( None , _) => {
691+ /* Uniquely accessible path -- OK for `&` and `&mut` */
691692 Ok ( ( ) )
692693 }
693-
694- ty:: UniqueImmBorrow | ty:: MutBorrow => {
695- // Check for those cases where we cannot control
696- // the aliasing and make sure that we are not
697- // being asked to.
698- match cmt. freely_aliasable ( ) {
699- None => {
700- Ok ( ( ) )
694+ ( Some ( mc:: AliasableStatic ( safety) ) , ty:: ImmBorrow ) => {
695+ // Borrow of an immutable static item:
696+ match safety {
697+ mc:: InteriorUnsafe => {
698+ // If the static item contains an Unsafe<T>, it has interior mutability.
699+ // In such cases, we cannot permit it to be borrowed, because the
700+ // static item resides in immutable memory and mutating it would
701+ // cause segfaults.
702+ bccx. tcx . sess . span_err ( borrow_span,
703+ format ! ( "borrow of immutable static items with \
704+ unsafe interior is not allowed") ) ;
705+ Err ( ( ) )
701706 }
702- Some ( mc:: AliasableStaticMut ) => {
703- // This is nasty, but we ignore the
704- // aliasing rules if the data is based in
705- // a `static mut`, since those are always
706- // unsafe. At your own peril and all that.
707+ mc:: InteriorSafe => {
708+ // Immutable static can be borrowed, no problem.
707709 Ok ( ( ) )
708710 }
709- Some ( alias_cause) => {
710- bccx. report_aliasability_violation (
711+ }
712+ }
713+ ( Some ( mc:: AliasableStaticMut ( ..) ) , _) => {
714+ // Even touching a static mut is considered unsafe. We assume the
715+ // user knows what they're doing in these cases.
716+ Ok ( ( ) )
717+ }
718+ ( Some ( alias_cause) , ty:: UniqueImmBorrow ) |
719+ ( Some ( alias_cause) , ty:: MutBorrow ) => {
720+ bccx. report_aliasability_violation (
711721 borrow_span,
712722 BorrowViolation ( loan_cause) ,
713723 alias_cause) ;
714- Err ( ( ) )
715- }
716- }
724+ Err ( ( ) )
725+ }
726+ ( _, _) => {
727+ Ok ( ( ) )
717728 }
718729 }
719730 }
0 commit comments