Add #[rustc_no_implicit_autorefs] and apply it to std methods

This commit is contained in:
Urgau
2024-03-30 16:00:28 +01:00
parent 191df20fca
commit 1632f624fb
7 changed files with 20 additions and 0 deletions

View File

@@ -931,6 +931,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
EncodeCrossCrate::Yes,
"#[rustc_never_returns_null_ptr] is used to mark functions returning non-null pointers."
),
rustc_attr!(
rustc_no_implicit_autorefs, AttributeType::Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes,
"`#[rustc_no_implicit_autorefs]` is used to mark functions for which an autoref to the dereference of a raw pointer should not be used as an argument."
),
rustc_attr!(
rustc_coherence_is_core, AttributeType::CrateLevel, template!(Word), ErrorFollowing, EncodeCrossCrate::No,
"#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."

View File

@@ -179,6 +179,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::rustc_as_ptr, ..] => {
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
}
[sym::rustc_no_implicit_autorefs, ..] => {
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
}
[sym::rustc_never_returns_null_ptr, ..] => {
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
}

View File

@@ -1830,6 +1830,7 @@ symbols! {
rustc_must_implement_one_of,
rustc_never_returns_null_ptr,
rustc_never_type_options,
rustc_no_implicit_autorefs,
rustc_no_mir_inline,
rustc_nonnull_optimization_guaranteed,
rustc_nounwind,

View File

@@ -1832,6 +1832,7 @@ impl String {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_vec_string_slice", since = "1.87.0")]
#[rustc_confusables("length", "size")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
pub const fn len(&self) -> usize {
self.vec.len()
}
@@ -1851,6 +1852,7 @@ impl String {
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_vec_string_slice", since = "1.87.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

View File

@@ -67,6 +67,7 @@ pub trait Index<Idx: ?Sized> {
///
/// May panic if the index is out of bounds.
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[track_caller]
fn index(&self, index: Idx) -> &Self::Output;
}
@@ -171,6 +172,7 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
///
/// May panic if the index is out of bounds.
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[track_caller]
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
}

View File

@@ -109,6 +109,7 @@ impl<T> [T] {
#[lang = "slice_len_fn"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_slice_len", since = "1.39.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[inline]
#[must_use]
pub const fn len(&self) -> usize {
@@ -128,6 +129,7 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_slice_is_empty", since = "1.39.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
@@ -562,6 +564,7 @@ impl<T> [T] {
/// assert_eq!(None, v.get(0..4));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[inline]
#[must_use]
pub fn get<I>(&self, index: I) -> Option<&I::Output>
@@ -587,6 +590,7 @@ impl<T> [T] {
/// assert_eq!(x, &[0, 42, 2]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[inline]
#[must_use]
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
@@ -624,6 +628,7 @@ impl<T> [T] {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[inline]
#[must_use]
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
@@ -666,6 +671,7 @@ impl<T> [T] {
/// assert_eq!(x, &[1, 13, 4]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[inline]
#[must_use]
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output

View File

@@ -134,6 +134,7 @@ impl str {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_str_len", since = "1.39.0")]
#[rustc_diagnostic_item = "str_len"]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[must_use]
#[inline]
pub const fn len(&self) -> usize {
@@ -153,6 +154,7 @@ impl str {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_str_is_empty", since = "1.39.0")]
#[cfg_attr(not(bootstrap), rustc_no_implicit_autorefs)]
#[must_use]
#[inline]
pub const fn is_empty(&self) -> bool {