Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #86303

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,12 @@ pub enum UndefinedBehaviorInfo<'tcx> {
/// The value validity check found a problem.
/// Should only be thrown by `validity.rs` and always point out which part of the value
/// is the problem.
ValidationFailure(String),
ValidationFailure {
/// The "path" to the value in question, e.g. `.0[5].field` for a struct
/// field in the 6th element of an array that is the first element of a tuple.
path: Option<String>,
msg: String,
},
/// Using a non-boolean `u8` as bool.
InvalidBool(u8),
/// Using a non-character `u32` as character.
Expand Down Expand Up @@ -331,7 +336,10 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
),
WriteToReadOnly(a) => write!(f, "writing to {} which is read-only", a),
DerefFunctionPointer(a) => write!(f, "accessing {} which contains a function", a),
ValidationFailure(ref err) => write!(f, "type validation failed: {}", err),
ValidationFailure { path: None, msg } => write!(f, "type validation failed: {}", msg),
ValidationFailure { path: Some(path), msg } => {
write!(f, "type validation failed at {}: {}", path, msg)
}
InvalidBool(b) => {
write!(f, "interpreting an invalid 8-bit value as a bool: 0x{:02x}", b)
}
Expand Down Expand Up @@ -499,13 +507,13 @@ impl fmt::Debug for InterpError<'_> {
}

impl InterpError<'_> {
/// Some errors to string formatting even if the error is never printed.
/// Some errors do string formatting even if the error is never printed.
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
/// so this method lets us detect them and `bug!` on unexpected errors.
pub fn formatted_string(&self) -> bool {
match self {
InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_))
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure(_))
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure { .. })
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) => true,
_ => false,
}
Expand Down
27 changes: 14 additions & 13 deletions compiler/rustc_mir/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ use super::{

macro_rules! throw_validation_failure {
($where:expr, { $( $what_fmt:expr ),+ } $( expected { $( $expected_fmt:expr ),+ } )?) => {{
let msg = rustc_middle::ty::print::with_no_trimmed_paths(|| {
let mut msg = String::new();
msg.push_str("encountered ");
write!(&mut msg, $($what_fmt),+).unwrap();
let mut msg = String::new();
msg.push_str("encountered ");
write!(&mut msg, $($what_fmt),+).unwrap();
$(
msg.push_str(", but expected ");
write!(&mut msg, $($expected_fmt),+).unwrap();
)?
let path = rustc_middle::ty::print::with_no_trimmed_paths(|| {
let where_ = &$where;
if !where_.is_empty() {
msg.push_str(" at ");
write_path(&mut msg, where_);
let mut path = String::new();
write_path(&mut path, where_);
Some(path)
} else {
None
}
$(
msg.push_str(", but expected ");
write!(&mut msg, $($expected_fmt),+).unwrap();
)?

msg
});
throw_ub!(ValidationFailure(msg))
throw_ub!(ValidationFailure { path, msg })
}};
}

Expand Down
80 changes: 67 additions & 13 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,60 @@ impl<T> MaybeUninit<T> {
u
}

/// Sets the value of the `MaybeUninit<T>`. This overwrites any previous value
/// without dropping it, so be careful not to use this twice unless you want to
/// skip running the destructor. For your convenience, this also returns a mutable
/// reference to the (now safely initialized) contents of `self`.
/// Sets the value of the `MaybeUninit<T>`.
///
/// This overwrites any previous value without dropping it, so be careful
/// not to use this twice unless you want to skip running the destructor.
/// For your convenience, this also returns a mutable reference to the
/// (now safely initialized) contents of `self`.
///
/// As the content is stored inside a `MaybeUninit`, the destructor is not
/// ran for the inner data if the MaybeUninit leaves scope without a call to
/// [`assume_init`], [`assume_init_drop`], or similar. Code that receives
/// the mutable reference returned by this function needs to keep this in
/// mind. The safety model of Rust regards leaks as safe, but they are
/// usually still undesirable. This being said, the mutable reference
/// behaves like any other mutable reference would, so assigning a new value
/// to it will drop the old content.
///
/// [`assume_init`]: Self::assume_init
/// [`assume_init_drop`]: Self::assume_init_drop
///
/// # Examples
///
/// Correct usage of this method:
///
/// ```rust
/// #![feature(maybe_uninit_extra)]
/// use std::mem::MaybeUninit;
///
/// let mut x = MaybeUninit::<Vec<u8>>::uninit();
///
/// {
/// let hello = x.write((&b"Hello, world!").to_vec());
/// // Setting hello does not leak prior allocations, but drops them
/// *hello = (&b"Hello").to_vec();
/// hello[0] = 'h' as u8;
/// }
/// // x is initialized now:
/// let s = unsafe { x.assume_init() };
/// assert_eq!(b"hello", s.as_slice());
/// ```
///
/// This usage of the method causes a leak:
///
/// ```rust
/// #![feature(maybe_uninit_extra)]
/// use std::mem::MaybeUninit;
///
/// let mut x = MaybeUninit::<String>::uninit();
///
/// x.write("Hello".to_string());
/// // This leaks the contained string:
/// x.write("hello".to_string());
/// // x is initialized now:
/// let s = unsafe { x.assume_init() };
/// ```
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
#[rustc_const_unstable(feature = "maybe_uninit_extra", issue = "63567")]
#[inline(always)]
Expand Down Expand Up @@ -564,9 +614,11 @@ impl<T> MaybeUninit<T> {
/// behavior. The [type-level documentation][inv] contains more information about
/// this initialization invariant.
///
/// Moreover, this leaves a copy of the same data behind in the `MaybeUninit<T>`. When using
/// multiple copies of the data (by calling `assume_init_read` multiple times, or first
/// calling `assume_init_read` and then [`assume_init`]), it is your responsibility
/// Moreover, similar to the [`ptr::read`] function, this function creates a
/// bitwise copy of the contents, regardless whether the contained type
/// implements the [`Copy`] trait or not. When using multiple copies of the
/// data (by calling `assume_init_read` multiple times, or first calling
/// `assume_init_read` and then [`assume_init`]), it is your responsibility
/// to ensure that that data may indeed be duplicated.
///
/// [inv]: #initialization-invariant
Expand Down Expand Up @@ -622,7 +674,8 @@ impl<T> MaybeUninit<T> {

/// Drops the contained value in place.
///
/// If you have ownership of the `MaybeUninit`, you can use [`assume_init`] instead.
/// If you have ownership of the `MaybeUninit`, you can also use
/// [`assume_init`] as an alternative.
///
/// # Safety
///
Expand All @@ -632,11 +685,12 @@ impl<T> MaybeUninit<T> {
///
/// On top of that, all additional invariants of the type `T` must be
/// satisfied, as the `Drop` implementation of `T` (or its members) may
/// rely on this. For example, a `1`-initialized [`Vec<T>`] is considered
/// initialized (under the current implementation; this does not constitute
/// a stable guarantee) because the only requirement the compiler knows
/// about it is that the data pointer must be non-null. Dropping such a
/// `Vec<T>` however will cause undefined behaviour.
/// rely on this. For example, setting a [`Vec<T>`] to an invalid but
/// non-null address makes it initialized (under the current implementation;
/// this does not constitute a stable guarantee), because the only
/// requirement the compiler knows about it is that the data pointer must be
/// non-null. Dropping such a `Vec<T>` however will cause undefined
/// behaviour.
///
/// [`assume_init`]: MaybeUninit::assume_init
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
Expand Down
16 changes: 8 additions & 8 deletions library/core/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ pub mod v1;
/// The 2015 version of the core prelude.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2015", issue = "85684")]
#[stable(feature = "prelude_2015", since = "1.55.0")]
pub mod rust_2015 {
#[unstable(feature = "prelude_2015", issue = "85684")]
#[stable(feature = "prelude_2015", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;
}

/// The 2018 version of the core prelude.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2018", issue = "85684")]
#[stable(feature = "prelude_2018", since = "1.55.0")]
pub mod rust_2018 {
#[unstable(feature = "prelude_2018", issue = "85684")]
#[stable(feature = "prelude_2018", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;
}

/// The 2021 version of the core prelude.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2021", issue = "85684")]
#[stable(feature = "prelude_2021", since = "1.55.0")]
pub mod rust_2021 {
#[unstable(feature = "prelude_2021", issue = "85684")]
#[stable(feature = "prelude_2021", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;

#[unstable(feature = "prelude_2021", issue = "85684")]
#[stable(feature = "prelude_2021", since = "1.55.0")]
#[doc(no_inline)]
pub use crate::iter::FromIterator;

#[unstable(feature = "prelude_2021", issue = "85684")]
#[stable(feature = "prelude_2021", since = "1.55.0")]
#[doc(no_inline)]
pub use crate::convert::{TryFrom, TryInto};
}
1 change: 0 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@
#![feature(panic_internals)]
#![feature(panic_unwind)]
#![feature(pin_static_ref)]
#![feature(prelude_2021)]
#![feature(prelude_import)]
#![feature(ptr_internals)]
#![feature(raw)]
Expand Down
32 changes: 23 additions & 9 deletions library/std/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
//!
//! # Prelude contents
//!
//! The current version of the prelude (version 1) lives in
//! [`std::prelude::v1`], and re-exports the following:
//! The first version of the prelude is used in Rust 2015 and Rust 2018,
//! and lives in [`std::prelude::v1`].
//! [`std::prelude::rust_2015`] and [`std::prelude::rust_2018`] re-export this prelude.
//! It re-exports the following:
//!
//! * <code>[std::marker]::{[Copy], [Send], [Sized], [Sync], [Unpin]}</code>,
//! marker traits that indicate fundamental properties of types.
Expand Down Expand Up @@ -58,6 +60,12 @@
//! * <code>[std::string]::{[String], [ToString]}</code>, heap-allocated strings.
//! * <code>[std::vec]::[Vec]</code>, a growable, heap-allocated vector.
//!
//! The prelude used in Rust 2021, [`std::prelude::rust_2021`], includes all of the above,
//! and in addition re-exports:
//!
//! * <code>[std::convert]::{[TryFrom], [TryInto]}</code>,
//! * <code>[std::iter]::[FromIterator]</code>.
//!
//! [mem::drop]: crate::mem::drop
//! [std::borrow]: crate::borrow
//! [std::boxed]: crate::boxed
Expand All @@ -71,10 +79,16 @@
//! [std::ops]: crate::ops
//! [std::option]: crate::option
//! [`std::prelude::v1`]: v1
//! [`std::prelude::rust_2015`]: rust_2015
//! [`std::prelude::rust_2018`]: rust_2018
//! [`std::prelude::rust_2021`]: rust_2021
//! [std::result]: crate::result
//! [std::slice]: crate::slice
//! [std::string]: crate::string
//! [std::vec]: mod@crate::vec
//! [TryFrom]: crate::convert::TryFrom
//! [TryInto]: crate::convert::TryInto
//! [FromIterator]: crate::iter::FromIterator
//! [`to_owned`]: crate::borrow::ToOwned::to_owned
//! [book-closures]: ../../book/ch13-01-closures.html
//! [book-dtor]: ../../book/ch15-03-drop.html
Expand All @@ -88,33 +102,33 @@ pub mod v1;
/// The 2015 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2015", issue = "85684")]
#[stable(feature = "prelude_2015", since = "1.55.0")]
pub mod rust_2015 {
#[unstable(feature = "prelude_2015", issue = "85684")]
#[stable(feature = "prelude_2015", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;
}

/// The 2018 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2018", issue = "85684")]
#[stable(feature = "prelude_2018", since = "1.55.0")]
pub mod rust_2018 {
#[unstable(feature = "prelude_2018", issue = "85684")]
#[stable(feature = "prelude_2018", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;
}

/// The 2021 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2021", issue = "85684")]
#[stable(feature = "prelude_2021", since = "1.55.0")]
pub mod rust_2021 {
#[unstable(feature = "prelude_2021", issue = "85684")]
#[stable(feature = "prelude_2021", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;

#[unstable(feature = "prelude_2021", issue = "85684")]
#[stable(feature = "prelude_2021", since = "1.55.0")]
#[doc(no_inline)]
pub use core::prelude::rust_2021::*;
}
10 changes: 2 additions & 8 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,14 +965,8 @@ fn render_assoc_item(
}
}

const ALLOWED_ATTRIBUTES: &[Symbol] = &[
sym::export_name,
sym::link_section,
sym::must_use,
sym::no_mangle,
sym::repr,
sym::non_exhaustive,
];
const ALLOWED_ATTRIBUTES: &[Symbol] =
&[sym::export_name, sym::link_section, sym::no_mangle, sym::repr, sym::non_exhaustive];

fn attributes(it: &clean::Item) -> Vec<String> {
it.attrs
Expand Down
8 changes: 0 additions & 8 deletions src/test/rustdoc/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ pub extern "C" fn f() {}
#[export_name = "bar"]
pub extern "C" fn g() {}

// @matches foo/enum.Foo.html '//*[@class="rust enum"]' \
// '#\[repr\(i64\)\]\n#\[must_use\]'
#[repr(i64)]
#[must_use]
pub enum Foo {
Bar,
}

// @has foo/struct.Repr.html '//*[@class="docblock type-decl"]' '#[repr(C, align(8))]'
#[repr(C, align(8))]
pub struct Repr;
3 changes: 1 addition & 2 deletions src/test/rustdoc/cap-lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// therefore should not concern itself with the lints.
#[deny(warnings)]

// @has cap_lints/struct.Foo.html //pre '#[must_use]'
#[must_use]
// @has cap_lints/struct.Foo.html //* 'Struct Foo'
pub struct Foo {
field: i32,
}
11 changes: 0 additions & 11 deletions src/test/rustdoc/must-use.rs

This file was deleted.

Loading