Skip to content

Commit

Permalink
Auto merge of #72562 - RalfJung:rollup-2ngjgwi, r=RalfJung
Browse files Browse the repository at this point in the history
Rollup of 5 pull requests

Successful merges:

 - #71940 (Add `len` and `slice_from_raw_parts` to `NonNull<[T]>`)
 - #72525 (Miri casts: do not blindly rely on dest type)
 - #72537 (Fix InlineAsmOperand expresions being visited twice during liveness checking)
 - #72544 (librustc_middle: Rename upvars query to upvars_mentioned)
 - #72551 (First draft documenting Debug stability.)

Failed merges:

r? @ghost
  • Loading branch information
bors committed May 25, 2020
2 parents 997d953 + 7c9fdb3 commit ee6c0da
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 136 deletions.
7 changes: 7 additions & 0 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,13 @@ impl Display for Arguments<'_> {
/// `enum`s, it will use the name of the variant and, if applicable, `(`, then the
/// `Debug` values of the fields, then `)`.
///
/// # Stability
///
/// Derived `Debug` formats are not stable, and so may change with future Rust
/// versions. Additionally, `Debug` implementations of types provided by the
/// standard library (`libstd`, `libcore`, `liballoc`, etc.) are not stable, and
/// may also change with future Rust versions.
///
/// # Examples
///
/// Deriving an implementation:
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
#![feature(const_generics)]
#![feature(const_ptr_offset_from)]
#![feature(const_result)]
#![feature(const_slice_from_raw_parts)]
#![feature(const_slice_ptr_len)]
#![feature(const_type_name)]
#![feature(custom_inner_attributes)]
#![feature(decl_macro)]
Expand Down
59 changes: 59 additions & 0 deletions src/libcore/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,65 @@ impl<T: ?Sized> NonNull<T> {
}
}

impl<T> NonNull<[T]> {
/// Creates a non-null raw slice from a thin pointer and a length.
///
/// The `len` argument is the number of **elements**, not the number of bytes.
///
/// This function is safe, but dereferencing the return value is unsafe.
/// See the documentation of [`slice::from_raw_parts`] for slice safety requirements.
///
/// [`slice::from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
///
/// # Examples
///
/// ```rust
/// #![feature(nonnull_slice_from_raw_parts)]
///
/// use std::ptr::NonNull;
///
/// // create a slice pointer when starting out with a pointer to the first element
/// let mut x = [5, 6, 7];
/// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
/// let slice = NonNull::slice_from_raw_parts(nonnull_pointer, 3);
/// assert_eq!(unsafe { slice.as_ref()[2] }, 7);
/// ```
///
/// (Note that this example artifically demonstrates a use of this method,
/// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.)
#[unstable(feature = "nonnull_slice_from_raw_parts", issue = "71941")]
#[rustc_const_unstable(feature = "const_nonnull_slice_from_raw_parts", issue = "71941")]
#[inline]
pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self {
// SAFETY: `data` is a `NonNull` pointer which is necessarily non-null
unsafe { Self::new_unchecked(super::slice_from_raw_parts_mut(data.as_ptr(), len)) }
}

/// Returns the length of a non-null raw slice.
///
/// The returned value is the number of **elements**, not the number of bytes.
///
/// This function is safe, even when the non-null raw slice cannot be dereferenced to a slice
/// because the pointer does not have a valid address.
///
/// # Examples
///
/// ```rust
/// #![feature(slice_ptr_len, nonnull_slice_from_raw_parts)]
///
/// use std::ptr::NonNull;
///
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
/// assert_eq!(slice.len(), 3);
/// ```
#[unstable(feature = "slice_ptr_len", issue = "71146")]
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
#[inline]
pub const fn len(self) -> usize {
self.as_ptr().len()
}
}

#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: ?Sized> Clone for NonNull<T> {
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ macro_rules! arena_types {
[few] privacy_access_levels: rustc_middle::middle::privacy::AccessLevels,
[few] foreign_module: rustc_middle::middle::cstore::ForeignModule,
[few] foreign_modules: Vec<rustc_middle::middle::cstore::ForeignModule>,
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
[] upvars_mentioned: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<$tcx>,
[] attribute: rustc_ast::ast::Attribute,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
};
let mut struct_fmt = fmt.debug_struct(&name);

if let Some(upvars) = tcx.upvars(def_id) {
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
for (&var_id, place) in upvars.keys().zip(places) {
let var_name = tcx.hir().name(var_id);
struct_fmt.field(&var_name.as_str(), place);
Expand All @@ -2458,7 +2458,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
let mut struct_fmt = fmt.debug_struct(&name);

if let Some(upvars) = tcx.upvars(def_id) {
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
for (&var_id, place) in upvars.keys().zip(places) {
let var_name = tcx.hir().name(var_id);
struct_fmt.field(&var_name.as_str(), place);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ rustc_queries! {
desc { "generating a postorder list of CrateNums" }
}

query upvars(_: DefId) -> Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>> {
query upvars_mentioned(_: DefId) -> Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>> {
eval_always
}
query maybe_unused_trait_import(def_id: LocalDefId) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ pub trait PrettyPrinter<'tcx>:
let mut sep = " ";
for (&var_id, upvar_ty) in self
.tcx()
.upvars(did)
.upvars_mentioned(did)
.as_ref()
.iter()
.flat_map(|v| v.keys())
Expand Down Expand Up @@ -659,7 +659,7 @@ pub trait PrettyPrinter<'tcx>:
let mut sep = " ";
for (&var_id, upvar_ty) in self
.tcx()
.upvars(did)
.upvars_mentioned(did)
.as_ref()
.iter()
.flat_map(|v| v.keys())
Expand Down
13 changes: 9 additions & 4 deletions src/librustc_mir/borrow_check/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
self.describe_field_from_ty(&ty, field, variant_index)
}
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
// `tcx.upvars(def_id)` returns an `Option`, which is `None` in case
// `tcx.upvars_mentioned(def_id)` returns an `Option`, which is `None` in case
// the closure comes from another crate. But in that case we wouldn't
// be borrowck'ing it, so we can just unwrap:
let (&var_id, _) =
self.infcx.tcx.upvars(def_id).unwrap().get_index(field.index()).unwrap();
let (&var_id, _) = self
.infcx
.tcx
.upvars_mentioned(def_id)
.unwrap()
.get_index(field.index())
.unwrap();

self.infcx.tcx.hir().name(var_id).to_string()
}
Expand Down Expand Up @@ -809,7 +814,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind;
debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr {
for (upvar, place) in self.infcx.tcx.upvars(def_id)?.values().zip(places) {
for (upvar, place) in self.infcx.tcx.upvars_mentioned(def_id)?.values().zip(places) {
match place {
Operand::Copy(place) | Operand::Move(place)
if target_place == place.as_ref() =>
Expand Down
Loading

0 comments on commit ee6c0da

Please sign in to comment.