]> git.lizzy.rs Git - rust.git/commitdiff
Don't treat a reference to a `static` as a reborrow
authorDylan MacKenzie <ecstaticmorse@gmail.com>
Wed, 27 Nov 2019 22:04:11 +0000 (14:04 -0800)
committerDylan MacKenzie <ecstaticmorse@gmail.com>
Thu, 28 Nov 2019 00:14:37 +0000 (16:14 -0800)
They now look the same in the MIR after #66587.

src/librustc_mir/transform/check_consts/validation.rs

index 4d8f9720af085ce86a2c671c64bfec5ef0c035da..bc13764f0a3ed19c5c86e38e40460d827f1aaecf 100644 (file)
@@ -705,6 +705,19 @@ fn place_as_reborrow(
                 return None;
             }
 
+            // A borrow of a `static` also looks like `&(*_1)` in the MIR, but `_1` is a `const`
+            // that points to the allocation for the static. Don't treat these as reborrows.
+            if let PlaceBase::Local(local) = place.base {
+                let decl = &body.local_decls[local];
+                if let LocalInfo::StaticRef { .. } = decl.local_info {
+                    return None;
+                }
+            }
+
+            // Ensure the type being derefed is a reference and not a raw pointer.
+            //
+            // This is sufficient to prevent an access to a `static mut` from being marked as a
+            // reborrow, even if the check above were to disappear.
             let inner_ty = Place::ty_from(&place.base, inner, body, tcx).ty;
             match inner_ty.kind {
                 ty::Ref(..) => Some(inner),