]> git.lizzy.rs Git - rust.git/commitdiff
Introduce language items for `Arc` and `Rc`.
authorDavid Wood <david@davidtw.co>
Sun, 30 Sep 2018 19:19:55 +0000 (21:19 +0200)
committerDavid Wood <david@davidtw.co>
Mon, 1 Oct 2018 11:50:21 +0000 (13:50 +0200)
This commit introduces language items for `Arc` and `Rc` so that types
can later be checked to be `Arc` or `Rc` in the NLL borrow checker. The
`lang` attribute is currently limited to `stage1` as it requires a
compiler built with knowledge of the expected language items.

src/doc/unstable-book/src/language-features/lang-items.md
src/liballoc/rc.rs
src/liballoc/sync.rs
src/librustc/middle/lang_items.rs
src/librustc/ty/mod.rs
src/librustc/ty/sty.rs

index bac619fd4a30d2c17e50d45415d55d1ef42600d6..b16739b4743a9ebb0070527216419f6388da4fb3 100644 (file)
@@ -311,3 +311,5 @@ the source code.
   - `freeze`: `libcore/marker.rs`
   - `debug_trait`: `libcore/fmt/mod.rs`
   - `non_zero`: `libcore/nonzero.rs`
+  - `arc`: `liballoc/sync.rs`
+  - `rc`: `liballoc/rc.rs`
index 9d951691a34e76b4847498e18649ee5cb0746159..915b8e7787e998d0d12fb19147172675cda8a236 100644 (file)
@@ -282,6 +282,7 @@ struct RcBox<T: ?Sized> {
 /// type `T`.
 ///
 /// [get_mut]: #method.get_mut
+#[cfg_attr(all(not(stage0), not(test)), lang = "rc")]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct Rc<T: ?Sized> {
     ptr: NonNull<RcBox<T>>,
index 116393bdad195b54375dcba6e8266af802bc4386..9e245fbd7bbe563595a72054a70f69ed72a5dffe 100644 (file)
 /// counting in general.
 ///
 /// [rc_examples]: ../../std/rc/index.html#examples
+#[cfg_attr(all(not(stage0), not(test)), lang = "arc")]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct Arc<T: ?Sized> {
     ptr: NonNull<ArcInner<T>>,
index bfde4e4a3aed8abb8d94d85aa8381f15908f1633..ef70a4cc45d56e0c95082d90bc88d6db1631d000 100644 (file)
@@ -362,6 +362,9 @@ pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {
     AlignOffsetLangItem,             "align_offset",            align_offset_fn;
 
     TerminationTraitLangItem,        "termination",             termination;
+
+    Arc,                             "arc",                     arc;
+    Rc,                              "rc",                      rc;
 }
 
 impl<'a, 'tcx, 'gcx> TyCtxt<'a, 'tcx, 'gcx> {
index b2281691bd66089da9269d309bc5583a0a63121a..633551aed18a73155f0e63a23fe2ec53fef9851b 100644 (file)
@@ -1694,9 +1694,13 @@ pub struct AdtFlags: u32 {
         const IS_FUNDAMENTAL      = 1 << 2;
         const IS_UNION            = 1 << 3;
         const IS_BOX              = 1 << 4;
+        /// Indicates whether the type is an `Arc`.
+        const IS_ARC              = 1 << 5;
+        /// Indicates whether the type is an `Rc`.
+        const IS_RC               = 1 << 6;
         /// Indicates whether the variant list of this ADT is `#[non_exhaustive]`.
         /// (i.e., this flag is never set unless this ADT is an enum).
-        const IS_VARIANT_LIST_NON_EXHAUSTIVE   = 1 << 5;
+        const IS_VARIANT_LIST_NON_EXHAUSTIVE   = 1 << 7;
     }
 }
 
@@ -2016,6 +2020,12 @@ fn new(tcx: TyCtxt<'_, '_, '_>,
         if Some(did) == tcx.lang_items().owned_box() {
             flags = flags | AdtFlags::IS_BOX;
         }
+        if Some(did) == tcx.lang_items().arc() {
+            flags = flags | AdtFlags::IS_ARC;
+        }
+        if Some(did) == tcx.lang_items().rc() {
+            flags = flags | AdtFlags::IS_RC;
+        }
         if kind == AdtKind::Enum && tcx.has_attr(did, "non_exhaustive") {
             debug!("found non-exhaustive variant list for {:?}", did);
             flags = flags | AdtFlags::IS_VARIANT_LIST_NON_EXHAUSTIVE;
@@ -2094,6 +2104,16 @@ pub fn is_phantom_data(&self) -> bool {
         self.flags.intersects(AdtFlags::IS_PHANTOM_DATA)
     }
 
+    /// Returns `true` if this is `Arc<T>`.
+    pub fn is_arc(&self) -> bool {
+        self.flags.intersects(AdtFlags::IS_ARC)
+    }
+
+    /// Returns `true` if this is `Rc<T>`.
+    pub fn is_rc(&self) -> bool {
+        self.flags.intersects(AdtFlags::IS_RC)
+    }
+
     /// Returns true if this is Box<T>.
     #[inline]
     pub fn is_box(&self) -> bool {
index 6c40dd892391671f8fd7770b83dd5db68c8a37e6..4f80bf45ab1f9f9300e03adea3bcd4ab75b57efc 100644 (file)
@@ -1598,6 +1598,22 @@ pub fn is_unsafe_ptr(&self) -> bool {
         }
     }
 
+    /// Returns `true` if this type is an `Arc<T>`.
+    pub fn is_arc(&self) -> bool {
+        match self.sty {
+            Adt(def, _) => def.is_arc(),
+            _ => false,
+        }
+    }
+
+    /// Returns `true` if this type is an `Rc<T>`.
+    pub fn is_rc(&self) -> bool {
+        match self.sty {
+            Adt(def, _) => def.is_rc(),
+            _ => false,
+        }
+    }
+
     pub fn is_box(&self) -> bool {
         match self.sty {
             Adt(def, _) => def.is_box(),