Skip to content

Commit ef05336

Browse files
The Miri Cronjob Botgitbot
authored andcommitted
Merge from rustc
2 parents 672a79a + 59fb4ee commit ef05336

File tree

12 files changed

+221
-128
lines changed

12 files changed

+221
-128
lines changed

core/src/alloc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl fmt::Display for AllocError {
9494
/// - the memory block is deallocated, or
9595
/// - the allocator is dropped.
9696
///
97-
/// Copying, cloning, or moving the allocator must not invalidate memory blocks returned from it
97+
/// Copying, cloning, or moving the allocator must not invalidate memory blocks returned from it.
9898
/// A copied or cloned allocator must behave like the original allocator.
9999
///
100100
/// A memory block which is [*currently allocated*] may be passed to

core/src/any.rs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,101 @@ impl dyn Any + Send + Sync {
610610
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
611611
/// noting that the hashes and ordering will vary between Rust releases. Beware
612612
/// of relying on them inside of your code!
613+
///
614+
/// # Danger of Improper Variance
615+
///
616+
/// You might think that subtyping is impossible between two static types,
617+
/// but this is false; there exists a static type with a static subtype.
618+
/// To wit, `fn(&str)`, which is short for `for<'any> fn(&'any str)`, and
619+
/// `fn(&'static str)`, are two distinct, static types, and yet,
620+
/// `fn(&str)` is a subtype of `fn(&'static str)`, since any value of type
621+
/// `fn(&str)` can be used where a value of type `fn(&'static str)` is needed.
622+
///
623+
/// This means that abstractions around `TypeId`, despite its
624+
/// `'static` bound on arguments, still need to worry about unnecessary
625+
/// and improper variance: it is advisable to strive for invariance
626+
/// first. The usability impact will be negligible, while the reduction
627+
/// in the risk of unsoundness will be most welcome.
628+
///
629+
/// ## Examples
630+
///
631+
/// Suppose `SubType` is a subtype of `SuperType`, that is,
632+
/// a value of type `SubType` can be used wherever
633+
/// a value of type `SuperType` is expected.
634+
/// Suppose also that `CoVar<T>` is a generic type, which is covariant over `T`
635+
/// (like many other types, including `PhantomData<T>` and `Vec<T>`).
636+
///
637+
/// Then, by covariance, `CoVar<SubType>` is a subtype of `CoVar<SuperType>`,
638+
/// that is, a value of type `CoVar<SubType>` can be used wherever
639+
/// a value of type `CoVar<SuperType>` is expected.
640+
///
641+
/// Then if `CoVar<SuperType>` relies on `TypeId::of::<SuperType>()` to uphold any invariants,
642+
/// those invariants may be broken because a value of type `CoVar<SuperType>` can be created
643+
/// without going through any of its methods, like so:
644+
/// ```
645+
/// type SubType = fn(&());
646+
/// type SuperType = fn(&'static ());
647+
/// type CoVar<T> = Vec<T>; // imagine something more complicated
648+
///
649+
/// let sub: CoVar<SubType> = CoVar::new();
650+
/// // we have a `CoVar<SuperType>` instance without
651+
/// // *ever* having called `CoVar::<SuperType>::new()`!
652+
/// let fake_super: CoVar<SuperType> = sub;
653+
/// ```
654+
///
655+
/// The following is an example program that tries to use `TypeId::of` to
656+
/// implement a generic type `Unique<T>` that guarantees unique instances for each `Unique<T>`,
657+
/// that is, and for each type `T` there can be at most one value of type `Unique<T>` at any time.
658+
///
659+
/// ```
660+
/// mod unique {
661+
/// use std::any::TypeId;
662+
/// use std::collections::BTreeSet;
663+
/// use std::marker::PhantomData;
664+
/// use std::sync::Mutex;
665+
///
666+
/// static ID_SET: Mutex<BTreeSet<TypeId>> = Mutex::new(BTreeSet::new());
667+
///
668+
/// // TypeId has only covariant uses, which makes Unique covariant over TypeAsId 🚨
669+
/// #[derive(Debug, PartialEq)]
670+
/// pub struct Unique<TypeAsId: 'static>(
671+
/// // private field prevents creation without `new` outside this module
672+
/// PhantomData<TypeAsId>,
673+
/// );
674+
///
675+
/// impl<TypeAsId: 'static> Unique<TypeAsId> {
676+
/// pub fn new() -> Option<Self> {
677+
/// let mut set = ID_SET.lock().unwrap();
678+
/// (set.insert(TypeId::of::<TypeAsId>())).then(|| Self(PhantomData))
679+
/// }
680+
/// }
681+
///
682+
/// impl<TypeAsId: 'static> Drop for Unique<TypeAsId> {
683+
/// fn drop(&mut self) {
684+
/// let mut set = ID_SET.lock().unwrap();
685+
/// (!set.remove(&TypeId::of::<TypeAsId>())).then(|| panic!("duplicity detected"));
686+
/// }
687+
/// }
688+
/// }
689+
///
690+
/// use unique::Unique;
691+
///
692+
/// // `OtherRing` is a subtype of `TheOneRing`. Both are 'static, and thus have a TypeId.
693+
/// type TheOneRing = fn(&'static ());
694+
/// type OtherRing = fn(&());
695+
///
696+
/// fn main() {
697+
/// let the_one_ring: Unique<TheOneRing> = Unique::new().unwrap();
698+
/// assert_eq!(Unique::<TheOneRing>::new(), None);
699+
///
700+
/// let other_ring: Unique<OtherRing> = Unique::new().unwrap();
701+
/// // Use that `Unique<OtherRing>` is a subtype of `Unique<TheOneRing>` 🚨
702+
/// let fake_one_ring: Unique<TheOneRing> = other_ring;
703+
/// assert_eq!(fake_one_ring, the_one_ring);
704+
///
705+
/// std::mem::forget(fake_one_ring);
706+
/// }
707+
/// ```
613708
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
614709
#[stable(feature = "rust1", since = "1.0.0")]
615710
pub struct TypeId {
@@ -627,8 +722,7 @@ impl PartialEq for TypeId {
627722
}
628723

629724
impl TypeId {
630-
/// Returns the `TypeId` of the type this generic function has been
631-
/// instantiated with.
725+
/// Returns the `TypeId` of the generic type parameter.
632726
///
633727
/// # Examples
634728
///

core/src/hint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ pub const fn must_use<T>(value: T) -> T {
646646
/// ```
647647
///
648648
///
649-
#[unstable(feature = "likely_unlikely", issue = "26179")]
649+
#[unstable(feature = "likely_unlikely", issue = "136873")]
650650
#[inline(always)]
651651
pub const fn likely(b: bool) -> bool {
652652
crate::intrinsics::likely(b)
@@ -696,7 +696,7 @@ pub const fn likely(b: bool) -> bool {
696696
/// }
697697
/// }
698698
/// ```
699-
#[unstable(feature = "likely_unlikely", issue = "26179")]
699+
#[unstable(feature = "likely_unlikely", issue = "136873")]
700700
#[inline(always)]
701701
pub const fn unlikely(b: bool) -> bool {
702702
crate::intrinsics::unlikely(b)
@@ -729,7 +729,7 @@ pub const fn unlikely(b: bool) -> bool {
729729
/// }
730730
/// }
731731
/// ```
732-
#[unstable(feature = "cold_path", issue = "26179")]
732+
#[unstable(feature = "cold_path", issue = "136873")]
733733
#[inline(always)]
734734
pub const fn cold_path() {
735735
crate::intrinsics::cold_path()

core/src/marker.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,23 @@ impl Copy for ! {}
453453
#[stable(feature = "rust1", since = "1.0.0")]
454454
impl<T: ?Sized> Copy for &T {}
455455

456+
/// Marker trait for the types that are allowed in union fields, unsafe fields,
457+
/// and unsafe binder types.
458+
///
459+
/// Implemented for:
460+
/// * `&T`, `&mut T` for all `T`,
461+
/// * `ManuallyDrop<T>` for all `T`,
462+
/// * tuples and arrays whose elements implement `BikeshedGuaranteedNoDrop`,
463+
/// * or otherwise, all types that are `Copy`.
464+
///
465+
/// Notably, this doesn't include all trivially-destructible types for semver
466+
/// reasons.
467+
///
468+
/// Bikeshed name for now.
469+
#[unstable(feature = "bikeshed_guaranteed_no_drop", issue = "none")]
470+
#[cfg_attr(not(bootstrap), lang = "bikeshed_guaranteed_no_drop")]
471+
pub trait BikeshedGuaranteedNoDrop {}
472+
456473
/// Types for which it is safe to share references between threads.
457474
///
458475
/// This trait is automatically implemented when the compiler determines
@@ -1284,8 +1301,22 @@ pub trait FnPtr: Copy + Clone {
12841301
/// }
12851302
/// ```
12861303
#[rustc_builtin_macro(CoercePointee, attributes(pointee))]
1287-
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize)]
1304+
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize, coerce_pointee_validated)]
12881305
#[unstable(feature = "derive_coerce_pointee", issue = "123430")]
12891306
pub macro CoercePointee($item:item) {
12901307
/* compiler built-in */
12911308
}
1309+
1310+
/// A trait that is implemented for ADTs with `derive(CoercePointee)` so that
1311+
/// the compiler can enforce the derive impls are valid post-expansion, since
1312+
/// the derive has stricter requirements than if the impls were written by hand.
1313+
///
1314+
/// This trait is not intended to be implemented by users or used other than
1315+
/// validation, so it should never be stabilized.
1316+
#[cfg(not(bootstrap))]
1317+
#[lang = "coerce_pointee_validated"]
1318+
#[unstable(feature = "coerce_pointee_validated", issue = "none")]
1319+
#[doc(hidden)]
1320+
pub trait CoercePointeeValidated {
1321+
/* compiler built-in */
1322+
}

core/src/num/nonzero.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,6 @@ macro_rules! nonzero_integer {
612612
/// Basic usage:
613613
///
614614
/// ```
615-
/// #![feature(non_zero_count_ones)]
616-
///
617615
/// # use std::num::NonZero;
618616
/// #
619617
/// # fn main() { test().unwrap(); }
@@ -627,7 +625,8 @@ macro_rules! nonzero_integer {
627625
/// # }
628626
/// ```
629627
///
630-
#[unstable(feature = "non_zero_count_ones", issue = "120287")]
628+
#[stable(feature = "non_zero_count_ones", since = "CURRENT_RUSTC_VERSION")]
629+
#[rustc_const_stable(feature = "non_zero_count_ones", since = "CURRENT_RUSTC_VERSION")]
631630
#[doc(alias = "popcount")]
632631
#[doc(alias = "popcnt")]
633632
#[must_use = "this returns the result of the operation, \

core/src/ptr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
10701070
#[rustc_diagnostic_item = "ptr_swap_nonoverlapping"]
10711071
pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
10721072
ub_checks::assert_unsafe_precondition!(
1073-
check_language_ub,
1073+
check_library_ub,
10741074
"ptr::swap_nonoverlapping requires that both pointer arguments are aligned and non-null \
10751075
and the specified memory ranges do not overlap",
10761076
(

std/src/f128.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Constants for the `f128` double-precision floating point type.
1+
//! Constants for the `f128` quadruple-precision floating point type.
22
//!
33
//! *[See also the `f128` primitive type](primitive@f128).*
44
//!

std/src/f16.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Constants for the `f16` double-precision floating point type.
1+
//! Constants for the `f16` half-precision floating point type.
22
//!
33
//! *[See also the `f16` primitive type](primitive@f16).*
44
//!

std/src/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ impl File {
664664
/// use std::fs::File;
665665
///
666666
/// fn main() -> std::io::Result<()> {
667-
/// let f = File::open("foo.txt")?;
667+
/// let f = File::create("foo.txt")?;
668668
/// f.lock()?;
669669
/// Ok(())
670670
/// }
@@ -767,7 +767,7 @@ impl File {
767767
/// use std::fs::File;
768768
///
769769
/// fn main() -> std::io::Result<()> {
770-
/// let f = File::open("foo.txt")?;
770+
/// let f = File::create("foo.txt")?;
771771
/// f.try_lock()?;
772772
/// Ok(())
773773
/// }

std/src/keyword_docs.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -651,16 +651,24 @@ mod if_keyword {}
651651

652652
#[doc(keyword = "impl")]
653653
//
654-
/// Implement some functionality for a type.
654+
/// Implementations of functionality for a type, or a type implementing some functionality.
655+
///
656+
/// There are two uses of the keyword `impl`:
657+
/// * An `impl` block is an item that is used to implement some functionality for a type.
658+
/// * An `impl Trait` in a type-position can be used to designate a type that implements a trait called `Trait`.
659+
///
660+
/// # Implementing Functionality for a Type
655661
///
656662
/// The `impl` keyword is primarily used to define implementations on types. Inherent
657663
/// implementations are standalone, while trait implementations are used to implement traits for
658664
/// types, or other traits.
659665
///
660-
/// Functions and consts can both be defined in an implementation. A function defined in an
661-
/// `impl` block can be standalone, meaning it would be called like `Foo::bar()`. If the function
666+
/// An implementation consists of definitions of functions and consts. A function defined in an
667+
/// `impl` block can be standalone, meaning it would be called like `Vec::new()`. If the function
662668
/// takes `self`, `&self`, or `&mut self` as its first argument, it can also be called using
663-
/// method-call syntax, a familiar feature to any object oriented programmer, like `foo.bar()`.
669+
/// method-call syntax, a familiar feature to any object-oriented programmer, like `vec.len()`.
670+
///
671+
/// ## Inherent Implementations
664672
///
665673
/// ```rust
666674
/// struct Example {
@@ -680,6 +688,17 @@ mod if_keyword {}
680688
/// self.number
681689
/// }
682690
/// }
691+
/// ```
692+
///
693+
/// It matters little where an inherent implementation is defined;
694+
/// its functionality is in scope wherever its implementing type is.
695+
///
696+
/// ## Trait Implementations
697+
///
698+
/// ```rust
699+
/// struct Example {
700+
/// number: i32,
701+
/// }
683702
///
684703
/// trait Thingy {
685704
/// fn do_thingy(&self);
@@ -692,11 +711,19 @@ mod if_keyword {}
692711
/// }
693712
/// ```
694713
///
714+
/// It matters little where a trait implementation is defined;
715+
/// its functionality can be brought into scope by importing the trait it implements.
716+
///
695717
/// For more information on implementations, see the [Rust book][book1] or the [Reference].
696718
///
697-
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be seen as a shorthand
698-
/// for "a concrete type that implements this trait". Its primary use is working with closures,
699-
/// which have type definitions generated at compile time that can't be simply typed out.
719+
/// # Designating a Type that Implements Some Functionality
720+
///
721+
/// The other use of the `impl` keyword is in `impl Trait` syntax, which can be understood to mean
722+
/// "any (or some) concrete type that implements Trait".
723+
/// It can be used as the type of a variable declaration,
724+
/// in [argument position](https://rust-lang.github.io/rfcs/1951-expand-impl-trait.html)
725+
/// or in [return position](https://rust-lang.github.io/rfcs/3425-return-position-impl-trait-in-traits.html).
726+
/// One pertinent use case is in working with closures, which have unnameable types.
700727
///
701728
/// ```rust
702729
/// fn thing_returning_closure() -> impl Fn(i32) -> bool {

0 commit comments

Comments
 (0)