diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 981dedd5b5c7e..447c31e2ad79a 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -759,7 +759,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } } - if projection.is_owned() { + if Cow::is_owned(&projection) { place.projection = self.tcx.mk_place_elems(&projection); } diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs index 17dad3277b95d..0ea365e95b119 100644 --- a/library/alloc/src/borrow.rs +++ b/library/alloc/src/borrow.rs @@ -212,6 +212,10 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> { impl<B: ?Sized + ToOwned> Cow<'_, B> { /// Returns true if the data is borrowed, i.e. if `to_mut` would require additional work. /// + /// Note: this is an associated function, which means that you have to call + /// it as `Cow::is_borrowed(&c)` instead of `c.is_borrowed()`. This is so + /// that there is no conflict with a method on the inner type. + /// /// # Examples /// /// ``` @@ -219,14 +223,14 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> { /// use std::borrow::Cow; /// /// let cow = Cow::Borrowed("moo"); - /// assert!(cow.is_borrowed()); + /// assert!(Cow::is_borrowed(&cow)); /// /// let bull: Cow<'_, str> = Cow::Owned("...moo?".to_string()); - /// assert!(!bull.is_borrowed()); + /// assert!(!Cow::is_borrowed(&bull)); /// ``` #[unstable(feature = "cow_is_borrowed", issue = "65143")] - pub const fn is_borrowed(&self) -> bool { - match *self { + pub const fn is_borrowed(c: &Self) -> bool { + match *c { Borrowed(_) => true, Owned(_) => false, } @@ -234,6 +238,10 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> { /// Returns true if the data is owned, i.e. if `to_mut` would be a no-op. /// + /// Note: this is an associated function, which means that you have to call + /// it as `Cow::is_owned(&c)` instead of `c.is_owned()`. This is so that + /// there is no conflict with a method on the inner type. + /// /// # Examples /// /// ``` @@ -241,14 +249,14 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> { /// use std::borrow::Cow; /// /// let cow: Cow<'_, str> = Cow::Owned("moo".to_string()); - /// assert!(cow.is_owned()); + /// assert!(Cow::is_owned(&cow)); /// /// let bull = Cow::Borrowed("...moo?"); - /// assert!(!bull.is_owned()); + /// assert!(!Cow::is_owned(&bull)); /// ``` #[unstable(feature = "cow_is_borrowed", issue = "65143")] - pub const fn is_owned(&self) -> bool { - !self.is_borrowed() + pub const fn is_owned(c: &Self) -> bool { + !Cow::is_borrowed(c) } /// Acquires a mutable reference to the owned form of the data. diff --git a/library/alloc/tests/borrow.rs b/library/alloc/tests/borrow.rs index af7efb7d78223..19695d424db2d 100644 --- a/library/alloc/tests/borrow.rs +++ b/library/alloc/tests/borrow.rs @@ -52,9 +52,9 @@ fn cow_const() { const COW: Cow<'_, str> = Cow::Borrowed("moo"); - const IS_BORROWED: bool = COW.is_borrowed(); + const IS_BORROWED: bool = Cow::is_borrowed(&COW); assert!(IS_BORROWED); - const IS_OWNED: bool = COW.is_owned(); + const IS_OWNED: bool = Cow::is_owned(&COW); assert!(!IS_OWNED); }